From 96f4878355081507cd73ff49333d026c8ef445c6 Mon Sep 17 00:00:00 2001 From: Qi Wang Date: Wed, 15 Jun 2022 14:56:10 +0800 Subject: [PATCH] nydusify: add fs-version annotation Add an annotation to the image manifest for detecting the bootstrap fs version without reading the magic number from bootstrap. fix #496 Signed-off-by: Qi Wang --- contrib/nydusify/pkg/cache/cache.go | 20 +++++++++++++++++--- contrib/nydusify/pkg/cache/cache_test.go | 2 ++ contrib/nydusify/pkg/converter/converter.go | 1 + contrib/nydusify/pkg/converter/layer.go | 5 +++-- contrib/nydusify/pkg/converter/manifest.go | 1 + contrib/nydusify/pkg/utils/constant.go | 1 + 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/contrib/nydusify/pkg/cache/cache.go b/contrib/nydusify/pkg/cache/cache.go index a8b7a7ddd40..bf1fd05fb53 100644 --- a/contrib/nydusify/pkg/cache/cache.go +++ b/contrib/nydusify/pkg/cache/cache.go @@ -29,7 +29,10 @@ import ( type Opt struct { // Maximum records(bootstrap layer + blob layer) in cache image. MaxRecords uint - Version string + // Version of cache image + Version string + // Bootstrap's RAFS version of cache image + FsVersion string // Make cache image manifest compatible with the docker v2 media // type defined in github.com/containerd/containerd/images. DockerV2Format bool @@ -129,6 +132,7 @@ func (cache *Cache) recordToLayer(record *Record) (*ocispec.Descriptor, *ocispec Size: record.NydusBootstrapDesc.Size, Annotations: map[string]string{ utils.LayerAnnotationNydusBootstrap: "true", + utils.LayerAnnotationNydusFsVersion: cache.opt.FsVersion, utils.LayerAnnotationNydusSourceChainID: record.SourceChainID.String(), // Use the annotation to record bootstrap layer DiffID. utils.LayerAnnotationUncompressed: record.NydusBootstrapDiffID.String(), @@ -230,6 +234,7 @@ func (cache *Cache) layerToRecord(layer *ocispec.Descriptor) *Record { Size: layer.Size, Annotations: map[string]string{ utils.LayerAnnotationNydusBootstrap: "true", + utils.LayerAnnotationNydusFsVersion: cache.opt.FsVersion, utils.LayerAnnotationUncompressed: uncompressedDigestStr, }, } @@ -402,7 +407,8 @@ func (cache *Cache) Export(ctx context.Context) error { Config: *configDesc, Layers: layers, Annotations: map[string]string{ - utils.ManifestNydusCache: cache.opt.Version, + utils.ManifestNydusCache: cache.opt.Version, + utils.LayerAnnotationNydusFsVersion: cache.opt.FsVersion, }, }, } @@ -443,7 +449,7 @@ func (cache *Cache) Import(ctx context.Context) error { return errors.Wrap(err, "Unmarshal cache manifest") } - // Discard the cache mismatched version + // Discard the cache if mismatched version if manifest.Annotations[utils.ManifestNydusCache] != cache.opt.Version { return fmt.Errorf( "unmatched cache image version %s, required to be %s", @@ -451,6 +457,14 @@ func (cache *Cache) Import(ctx context.Context) error { ) } + // Discard the cache if mismatched RAFS FsVersion + if manifest.Annotations[utils.LayerAnnotationNydusFsVersion] != cache.opt.FsVersion { + return fmt.Errorf( + "unmatched cache image version %s, required to be %s", + manifest.Annotations[utils.LayerAnnotationNydusFsVersion], cache.opt.FsVersion, + ) + } + cache.importRecordsFromLayers(manifest.Layers) return nil diff --git a/contrib/nydusify/pkg/cache/cache_test.go b/contrib/nydusify/pkg/cache/cache_test.go index 8d5a7f0249e..a45eb6a263a 100644 --- a/contrib/nydusify/pkg/cache/cache_test.go +++ b/contrib/nydusify/pkg/cache/cache_test.go @@ -47,6 +47,7 @@ func makeBootstrapLayer(id int64, hasBlob bool) ocispec.Descriptor { Size: id, Annotations: map[string]string{ utils.LayerAnnotationNydusBootstrap: "true", + utils.LayerAnnotationNydusFsVersion: "6", utils.LayerAnnotationNydusSourceChainID: digest.FromString("chain-" + idStr).String(), utils.LayerAnnotationUncompressed: digest.FromString("bootstrap-uncompressed-" + idStr).String(), }, @@ -76,6 +77,7 @@ func testWithBackend(t *testing.T, _backend backend.Backend) { MaxRecords: 3, DockerV2Format: false, Backend: _backend, + FsVersion: "6", }) assert.Nil(t, err) diff --git a/contrib/nydusify/pkg/converter/converter.go b/contrib/nydusify/pkg/converter/converter.go index 5b77a46bf31..223f2035ff1 100644 --- a/contrib/nydusify/pkg/converter/converter.go +++ b/contrib/nydusify/pkg/converter/converter.go @@ -254,6 +254,7 @@ func (cvt *Converter) convert(ctx context.Context) (retErr error) { forcePush: cvt.BackendForcePush, alignedChunk: cvt.BackendAlignedChunk, referenceBlobs: blobLayers, + fsVersion: cvt.FsVersion, } parentBuildLayer = buildLayer buildLayers = append(buildLayers, buildLayer) diff --git a/contrib/nydusify/pkg/converter/layer.go b/contrib/nydusify/pkg/converter/layer.go index 447606671e2..40f18793652 100644 --- a/contrib/nydusify/pkg/converter/layer.go +++ b/contrib/nydusify/pkg/converter/layer.go @@ -56,8 +56,8 @@ type buildLayer struct { backend backend.Backend forcePush bool alignedChunk bool - - referenceBlobs []ocispec.Descriptor + fsVersion string + referenceBlobs []ocispec.Descriptor } // parseSourceMount parses mounts object returned by the Mount method in @@ -159,6 +159,7 @@ func (layer *buildLayer) pushBootstrap(ctx context.Context) (*ocispec.Descriptor // DiffID of layer defined in OCI spec utils.LayerAnnotationUncompressed: uncompressedDigest.String(), utils.LayerAnnotationNydusBootstrap: "true", + utils.LayerAnnotationNydusFsVersion: layer.fsVersion, }, } if len(layer.referenceBlobs) > 0 { diff --git a/contrib/nydusify/pkg/converter/manifest.go b/contrib/nydusify/pkg/converter/manifest.go index 1a1f09f0935..b652af0932c 100644 --- a/contrib/nydusify/pkg/converter/manifest.go +++ b/contrib/nydusify/pkg/converter/manifest.go @@ -253,6 +253,7 @@ func (mm *manifestManager) Push(ctx context.Context, buildLayers []*buildLayer) utils.LayerAnnotationNydusBlobIDs: true, utils.LayerAnnotationNydusReferenceBlobIDs: true, utils.LayerAnnotationNydusBootstrap: true, + utils.LayerAnnotationNydusFsVersion: true, } for idx, desc := range layers { layerDiffID := digest.Digest(desc.Annotations[utils.LayerAnnotationUncompressed]) diff --git a/contrib/nydusify/pkg/utils/constant.go b/contrib/nydusify/pkg/utils/constant.go index dbcde40044d..bafdf59d8bf 100644 --- a/contrib/nydusify/pkg/utils/constant.go +++ b/contrib/nydusify/pkg/utils/constant.go @@ -16,6 +16,7 @@ const ( LayerAnnotationNydusBlobSize = "containerd.io/snapshot/nydus-blob-size" LayerAnnotationNydusBlobIDs = "containerd.io/snapshot/nydus-blob-ids" LayerAnnotationNydusBootstrap = "containerd.io/snapshot/nydus-bootstrap" + LayerAnnotationNydusFsVersion = "containerd.io/snapshot/nydus-fs-version" LayerAnnotationNydusSourceChainID = "containerd.io/snapshot/nydus-source-chainid" LayerAnnotationNydusReferenceBlobIDs = "containerd.io/snapshot/nydus-reference-blob-ids"