From 9a2d2f5529e2c457edbc668dc2e604e46dfecbbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Sat, 2 Jul 2022 00:26:05 +0200 Subject: [PATCH] Add imagedestination.impl.Properties.DesiredLayerCompression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Should not change behavior. Signed-off-by: Miloslav Trmač --- directory/directory_dest.go | 11 +++------- docker/archive/dest.go | 5 ----- docker/docker_image_dest.go | 5 +---- docker/internal/tarfile/dest.go | 1 + internal/imagedestination/impl/properties.go | 9 +++++++++ oci/layout/oci_dest.go | 21 +++++++++----------- ostree/ostree_dest.go | 6 +----- storage/storage_image.go | 11 ++++------ 8 files changed, 28 insertions(+), 41 deletions(-) diff --git a/directory/directory_dest.go b/directory/directory_dest.go index 0cde4f7175..70c8e784f1 100644 --- a/directory/directory_dest.go +++ b/directory/directory_dest.go @@ -31,8 +31,7 @@ type dirImageDestination struct { stubs.NoPutBlobPartialInitialize stubs.AlwaysSupportsSignatures - ref dirReference - desiredLayerCompression types.LayerCompression + ref dirReference } // newImageDestination returns an ImageDestination for writing to a directory. @@ -102,14 +101,14 @@ func newImageDestination(sys *types.SystemContext, ref dirReference) (types.Imag d := &dirImageDestination{ PropertyMethodsInitialize: impl.PropertyMethods(impl.Properties{ SupportedManifestMIMETypes: nil, + DesiredLayerCompression: desiredLayerCompression, MustMatchRuntimeOS: false, IgnoresEmbeddedDockerReference: false, // N/A, DockerReference() returns nil. HasThreadSafePutBlob: false, }), NoPutBlobPartialInitialize: stubs.NoPutBlobPartial(ref), - ref: ref, - desiredLayerCompression: desiredLayerCompression, + ref: ref, } d.Compat = impl.AddCompat(d) return d, nil @@ -126,10 +125,6 @@ func (d *dirImageDestination) Close() error { return nil } -func (d *dirImageDestination) DesiredLayerCompression() types.LayerCompression { - return d.desiredLayerCompression -} - // AcceptsForeignLayerURLs returns false iff foreign layers in manifest should be actually // uploaded to the image destination, true otherwise. func (d *dirImageDestination) AcceptsForeignLayerURLs() bool { diff --git a/docker/archive/dest.go b/docker/archive/dest.go index db73ae48d9..f8e42cf280 100644 --- a/docker/archive/dest.go +++ b/docker/archive/dest.go @@ -47,11 +47,6 @@ func newImageDestination(sys *types.SystemContext, ref archiveReference) (types. }, nil } -// DesiredLayerCompression indicates if layers must be compressed, decompressed or preserved -func (d *archiveImageDestination) DesiredLayerCompression() types.LayerCompression { - return types.Decompress -} - // Reference returns the reference used to set up this destination. Note that this should directly correspond to user's intent, // e.g. it should use the public hostname instead of the result of resolving CNAMEs or following redirects. func (d *archiveImageDestination) Reference() types.ImageReference { diff --git a/docker/docker_image_dest.go b/docker/docker_image_dest.go index 25c8f9ea96..886ba9e1dc 100644 --- a/docker/docker_image_dest.go +++ b/docker/docker_image_dest.go @@ -62,6 +62,7 @@ func newImageDestination(sys *types.SystemContext, ref dockerReference) (types.I dest := &dockerImageDestination{ PropertyMethodsInitialize: impl.PropertyMethods(impl.Properties{ SupportedManifestMIMETypes: mimeTypes, + DesiredLayerCompression: types.Compress, MustMatchRuntimeOS: false, IgnoresEmbeddedDockerReference: false, // We do want the manifest updated; older registry versions refuse manifests if the embedded reference does not match. HasThreadSafePutBlob: true, @@ -102,10 +103,6 @@ func (d *dockerImageDestination) SupportsSignatures(ctx context.Context) error { } } -func (d *dockerImageDestination) DesiredLayerCompression() types.LayerCompression { - return types.Compress -} - // AcceptsForeignLayerURLs returns false iff foreign layers in manifest should be actually // uploaded to the image destination, true otherwise. func (d *dockerImageDestination) AcceptsForeignLayerURLs() bool { diff --git a/docker/internal/tarfile/dest.go b/docker/internal/tarfile/dest.go index 0fda88d0b2..6ca8d95e8c 100644 --- a/docker/internal/tarfile/dest.go +++ b/docker/internal/tarfile/dest.go @@ -45,6 +45,7 @@ func NewDestination(sys *types.SystemContext, archive *Writer, transportName str SupportedManifestMIMETypes: []string{ manifest.DockerV2Schema2MediaType, // We rely on the types.Image.UpdatedImage schema conversion capabilities. }, + DesiredLayerCompression: types.Decompress, MustMatchRuntimeOS: false, IgnoresEmbeddedDockerReference: false, // N/A, we only accept schema2 images where EmbeddedDockerReferenceConflicts() is always false. // The code _is_ actually thread-safe, but apart from computing sizes/digests of layers where diff --git a/internal/imagedestination/impl/properties.go b/internal/imagedestination/impl/properties.go index 088234ee42..2843766b60 100644 --- a/internal/imagedestination/impl/properties.go +++ b/internal/imagedestination/impl/properties.go @@ -1,11 +1,15 @@ package impl +import "github.com/containers/image/v5/types" + // Properties collects properties of an ImageDestination that are constant throughout its lifetime // (but might differ across instances). type Properties struct { // SupportedManifestMIMETypes tells which manifest MIME types the destination supports. // A empty slice or nil means any MIME type can be tried to upload. SupportedManifestMIMETypes []string + // DesiredLayerCompression indicates the kind of compression to apply on layers + DesiredLayerCompression types.LayerCompression // MustMatchRuntimeOS is set to true if the destination can store only images targeted for the current runtime architecture and OS. MustMatchRuntimeOS bool // IgnoresEmbeddedDockerReference is set to true if the destination does not care about Image.EmbeddedDockerReferenceConflicts(), @@ -36,6 +40,11 @@ func (o PropertyMethodsInitialize) SupportedManifestMIMETypes() []string { return o.vals.SupportedManifestMIMETypes } +// DesiredLayerCompression indicates the kind of compression to apply on layers +func (o PropertyMethodsInitialize) DesiredLayerCompression() types.LayerCompression { + return o.vals.DesiredLayerCompression +} + // MustMatchRuntimeOS returns true iff the destination can store only images targeted for the current runtime architecture and OS. False otherwise. func (o PropertyMethodsInitialize) MustMatchRuntimeOS() bool { return o.vals.MustMatchRuntimeOS diff --git a/oci/layout/oci_dest.go b/oci/layout/oci_dest.go index c009e57668..2497d33e3b 100644 --- a/oci/layout/oci_dest.go +++ b/oci/layout/oci_dest.go @@ -27,10 +27,9 @@ type ociImageDestination struct { stubs.NoPutBlobPartialInitialize stubs.NoSignaturesInitialize - ref ociReference - index imgspecv1.Index - sharedBlobDir string - acceptUncompressedLayers bool + ref ociReference + index imgspecv1.Index + sharedBlobDir string } // newImageDestination returns an ImageDestination for writing to an existing directory. @@ -51,12 +50,18 @@ func newImageDestination(sys *types.SystemContext, ref ociReference) (types.Imag } } + desiredLayerCompression := types.Compress + if sys != nil && sys.OCIAcceptUncompressedLayers { + desiredLayerCompression = types.PreserveOriginal + } + d := &ociImageDestination{ PropertyMethodsInitialize: impl.PropertyMethods(impl.Properties{ SupportedManifestMIMETypes: []string{ imgspecv1.MediaTypeImageManifest, imgspecv1.MediaTypeImageIndex, }, + DesiredLayerCompression: desiredLayerCompression, MustMatchRuntimeOS: false, IgnoresEmbeddedDockerReference: false, // N/A, DockerReference() returns nil. HasThreadSafePutBlob: true, @@ -70,7 +75,6 @@ func newImageDestination(sys *types.SystemContext, ref ociReference) (types.Imag d.Compat = impl.AddCompat(d) if sys != nil { d.sharedBlobDir = sys.OCISharedBlobDirPath - d.acceptUncompressedLayers = sys.OCIAcceptUncompressedLayers } if err := ensureDirectoryExists(d.ref.dir); err != nil { @@ -96,13 +100,6 @@ func (d *ociImageDestination) Close() error { return nil } -func (d *ociImageDestination) DesiredLayerCompression() types.LayerCompression { - if d.acceptUncompressedLayers { - return types.PreserveOriginal - } - return types.Compress -} - // AcceptsForeignLayerURLs returns false iff foreign layers in manifest should be actually // uploaded to the image destination, true otherwise. func (d *ociImageDestination) AcceptsForeignLayerURLs() bool { diff --git a/ostree/ostree_dest.go b/ostree/ostree_dest.go index 446952ebfc..452c64ba65 100644 --- a/ostree/ostree_dest.go +++ b/ostree/ostree_dest.go @@ -93,6 +93,7 @@ func newImageDestination(ref ostreeReference, tmpDirPath string) (types.ImageDes d := &ostreeImageDestination{ PropertyMethodsInitialize: impl.PropertyMethods(impl.Properties{ SupportedManifestMIMETypes: []string{manifest.DockerV2Schema2MediaType}, + DesiredLayerCompression: types.PreserveOriginal, MustMatchRuntimeOS: true, IgnoresEmbeddedDockerReference: false, // N/A, DockerReference() returns nil. HasThreadSafePutBlob: false, @@ -126,11 +127,6 @@ func (d *ostreeImageDestination) Close() error { return os.RemoveAll(d.tmpDirPath) } -// ShouldCompressLayers returns true iff it is desirable to compress layer blobs written to this destination. -func (d *ostreeImageDestination) DesiredLayerCompression() types.LayerCompression { - return types.PreserveOriginal -} - // AcceptsForeignLayerURLs returns false iff foreign layers in manifest should be actually // uploaded to the image destination, true otherwise. func (d *ostreeImageDestination) AcceptsForeignLayerURLs() bool { diff --git a/storage/storage_image.go b/storage/storage_image.go index ec5f05c1d8..54f2d2fa4e 100644 --- a/storage/storage_image.go +++ b/storage/storage_image.go @@ -414,6 +414,10 @@ func newImageDestination(sys *types.SystemContext, imageRef storageReference) (* manifest.DockerV2Schema1SignedMediaType, manifest.DockerV2Schema1MediaType, }, + // We ultimately have to decompress layers to populate trees on disk + // and need to explicitly ask for it here, so that the layers' MIME + // types can be set accordingly. + DesiredLayerCompression: types.PreserveOriginal, MustMatchRuntimeOS: true, IgnoresEmbeddedDockerReference: true, // Yes, we want the unmodified manifest HasThreadSafePutBlob: true, @@ -455,13 +459,6 @@ func (s *storageImageDestination) Close() error { return os.RemoveAll(s.directory) } -func (s *storageImageDestination) DesiredLayerCompression() types.LayerCompression { - // We ultimately have to decompress layers to populate trees on disk - // and need to explicitly ask for it here, so that the layers' MIME - // types can be set accordingly. - return types.PreserveOriginal -} - func (s *storageImageDestination) computeNextBlobCacheFile() string { return filepath.Join(s.directory, fmt.Sprintf("%d", atomic.AddInt32(&s.nextTempFileID, 1))) }