-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable to specify compression type of all layers of the finally expor…
…ted image Signed-off-by: ktock <[email protected]>
- Loading branch information
Showing
11 changed files
with
968 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package containerimage | ||
|
||
import ( | ||
"compress/gzip" | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/containerd/containerd/content" | ||
"github.com/containerd/containerd/errdefs" | ||
"github.com/containerd/containerd/images" | ||
"github.com/containerd/containerd/images/converter/uncompress" | ||
"github.com/containerd/containerd/labels" | ||
"github.com/opencontainers/go-digest" | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
func gzipLayerConvertFunc(ctx context.Context, cs content.Store, desc ocispec.Descriptor) (*ocispec.Descriptor, error) { | ||
if !images.IsLayerType(desc.MediaType) || isGzipCompressedType(desc.MediaType) { | ||
// No conversion. No need to return an error here. | ||
return nil, nil | ||
} | ||
|
||
// prepare the source and destination | ||
info, err := cs.Info(ctx, desc.Digest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
labelz := info.Labels | ||
if labelz == nil { | ||
labelz = make(map[string]string) | ||
} | ||
ra, err := cs.ReaderAt(ctx, desc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer ra.Close() | ||
ref := fmt.Sprintf("convert-gzip-from-%s", desc.Digest) | ||
w, err := cs.Writer(ctx, content.WithRef(ref)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer w.Close() | ||
if err := w.Truncate(0); err != nil { // Old written data possibly remains | ||
return nil, err | ||
} | ||
zw := gzip.NewWriter(w) | ||
defer zw.Close() | ||
|
||
// convert this layer | ||
diffID := digest.Canonical.Digester() | ||
if _, err := io.Copy(zw, io.TeeReader(io.NewSectionReader(ra, 0, ra.Size()), diffID.Hash())); err != nil { | ||
return nil, err | ||
} | ||
if err := zw.Close(); err != nil { // Flush the writer | ||
return nil, err | ||
} | ||
labelz[labels.LabelUncompressed] = diffID.Digest().String() // update diffID label | ||
if err = w.Commit(ctx, 0, "", content.WithLabels(labelz)); err != nil && !errdefs.IsAlreadyExists(err) { | ||
return nil, err | ||
} | ||
if err := w.Close(); err != nil { | ||
return nil, err | ||
} | ||
info, err = cs.Info(ctx, w.Digest()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
newDesc := desc | ||
if uncompress.IsUncompressedType(newDesc.MediaType) { | ||
if images.IsDockerType(newDesc.MediaType) { | ||
newDesc.MediaType += ".gzip" | ||
} else { | ||
newDesc.MediaType += "+gzip" | ||
} | ||
} | ||
newDesc.Digest = info.Digest | ||
newDesc.Size = info.Size | ||
return &newDesc, nil | ||
} | ||
|
||
func isGzipCompressedType(mt string) bool { | ||
switch mt { | ||
case | ||
images.MediaTypeDockerSchema2LayerGzip, | ||
images.MediaTypeDockerSchema2LayerForeignGzip, | ||
ocispec.MediaTypeImageLayerGzip, | ||
ocispec.MediaTypeImageLayerNonDistributableGzip: | ||
return true | ||
default: | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.