From f38e7c28d0737e21804e00c8ba5241d7e4695dee Mon Sep 17 00:00:00 2001 From: Viktor Kuznietsov Date: Wed, 17 Aug 2022 19:55:19 +0000 Subject: [PATCH] Remove null blobs from Soci Index Signed-off-by: Viktor Kuznietsov --- fs/artifact_fetcher.go | 5 +---- fs/fs.go | 6 ++---- integration/create_test.go | 20 ++------------------ soci/soci_index.go | 11 +++++++++-- 4 files changed, 14 insertions(+), 28 deletions(-) diff --git a/fs/artifact_fetcher.go b/fs/artifact_fetcher.go index 7f7a445df..c39c58b9d 100644 --- a/fs/artifact_fetcher.go +++ b/fs/artifact_fetcher.go @@ -217,10 +217,7 @@ func FetchSociArtifacts(ctx context.Context, imageRef, indexDigest string, store eg, ctx := errgroup.WithContext(ctx) for _, blob := range index.Blobs { - if blob == nil { - continue - } - blob := *blob + blob := blob eg.Go(func() error { rc, local, err := fetcher.Fetch(ctx, blob) if err != nil { diff --git a/fs/fs.go b/fs/fs.go index 48e9fac24..f01426709 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -209,10 +209,8 @@ func (fs *filesystem) fetchSociArtifacts(ctx context.Context, imageRef, indexDig func (fs *filesystem) populateImageLayerToSociMapping(sociIndex *soci.SociIndex) { for _, desc := range sociIndex.Blobs { - if desc != nil { - ociDigest := desc.Annotations[soci.IndexAnnotationImageLayerDigest] - fs.imageLayerToSociDesc[ociDigest] = *desc - } + ociDigest := desc.Annotations[soci.IndexAnnotationImageLayerDigest] + fs.imageLayerToSociDesc[ociDigest] = desc } } diff --git a/integration/create_test.go b/integration/create_test.go index b3c4530bc..34448ddd8 100644 --- a/integration/create_test.go +++ b/integration/create_test.go @@ -106,22 +106,11 @@ func TestSociCreateSparseIndex(t *testing.T) { } blobs := sociIndex.Blobs - notNilBlobCount := 0 - for _, b := range blobs { - if b != nil { - notNilBlobCount++ - } - } - if notNilBlobCount != len(includedLayers) { - t.Fatalf("unexpected blob count; expected=%v, got=%v", len(includedLayers), notNilBlobCount) + if len(blobs) != len(includedLayers) { + t.Fatalf("unexpected blob count; expected=%v, got=%v", len(includedLayers), len(blobs)) } for i, blob := range blobs { - if blob == nil { - continue - } - blob := *blob - blobContent := fetchContentFromPath(sh, blobStorePath+"/"+trimSha256Prefix(blob.Digest.String())) blobSize := int64(len(blobContent)) blobDigest := digest.FromBytes(blobContent) @@ -205,11 +194,6 @@ func TestSociCreate(t *testing.T) { blobs := sociIndex.Blobs for _, blob := range blobs { - if blob == nil { - continue - } - blob := *blob - blobContent := fetchContentFromPath(sh, blobStorePath+"/"+trimSha256Prefix(blob.Digest.String())) blobSize := int64(len(blobContent)) blobDigest := digest.FromBytes(blobContent) diff --git a/soci/soci_index.go b/soci/soci_index.go index 7c0678948..25211b567 100644 --- a/soci/soci_index.go +++ b/soci/soci_index.go @@ -64,7 +64,7 @@ type SociIndex struct { MediaType string `json:"mediaType"` ArtifactType string `json:"artifactType"` // descriptors of ztocs - Blobs []*ocispec.Descriptor `json:"blobs,omitempty"` + Blobs []ocispec.Descriptor `json:"blobs,omitempty"` // descriptor of image manifest Subject ocispec.Descriptor `json:"subject,omitempty"` @@ -172,6 +172,13 @@ func BuildSociIndex(ctx context.Context, cs content.Store, img images.Image, spa return nil, err } + ztocsDesc := make([]ocispec.Descriptor, 0, len(manifest.Layers)) + for _, desc := range sociLayersDesc { + if desc != nil { + ztocsDesc = append(ztocsDesc, *desc) + } + } + annotations := map[string]string{ IndexAnnotationBuildToolIdentifier: config.buildToolIdentifier, IndexAnnotationBuildToolVersion: config.buildToolVersion, @@ -180,7 +187,7 @@ func BuildSociIndex(ctx context.Context, cs content.Store, img images.Image, spa sociIndex := SociIndex{ MediaType: sociIndexMediaType, ArtifactType: SociIndexArtifactType, - Blobs: sociLayersDesc, + Blobs: ztocsDesc, Subject: ocispec.Descriptor{ MediaType: imgManifestDesc.MediaType, Digest: imgManifestDesc.Digest,