Skip to content

Commit acaa066

Browse files
feat(gc): simplify gc logic to increase coverage
Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
1 parent 479eaf1 commit acaa066

4 files changed

Lines changed: 23 additions & 28 deletions

File tree

pkg/api/routes.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,10 @@ func (rh *RouteHandler) GetReferrers(response http.ResponseWriter, request *http
514514
return
515515
}
516516

517-
response.Header().Set("OCI-Filters-Applied", strings.Join(artifactTypes, ","))
517+
if len(artifactTypes) > 0 {
518+
response.Header().Set("OCI-Filters-Applied", strings.Join(artifactTypes, ","))
519+
}
520+
518521
WriteData(response, http.StatusOK, ispec.MediaTypeImageIndex, out)
519522
}
520523

pkg/extensions/sync/sync_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ func TestSyncWithNonDistributableBlob(t *testing.T) {
869869
nonDistributableLayer := make([]byte, 10)
870870
nonDistributableDigest := godigest.FromBytes(nonDistributableLayer)
871871
nonDistributableLayerDesc := ispec.Descriptor{
872-
MediaType: ispec.MediaTypeImageLayerNonDistributableGzip,
872+
MediaType: ispec.MediaTypeImageLayerNonDistributableGzip, //nolint:staticcheck
873873
Digest: nonDistributableDigest,
874874
Size: int64(len(nonDistributableLayer)),
875875
URLs: []string{

pkg/meta/repodb/storage_parsing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func GetReferredSubject(descriptorBlob []byte, referrerDigest, mediaType string,
325325
err := json.Unmarshal(descriptorBlob, &manifestContent)
326326
if err != nil {
327327
return "", referrerInfo, false,
328-
fmt.Errorf("repodb: can't unmarhsal manifest for digest %s: %w", referrerDigest, err)
328+
fmt.Errorf("repodb: can't unmarshal manifest for digest %s: %w", referrerDigest, err)
329329
}
330330

331331
referrerSubject = manifestContent.Subject

pkg/storage/local/local.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,12 @@ func ensureDir(dir string, log zerolog.Logger) error {
14511451
return nil
14521452
}
14531453

1454+
type extendedManifest struct {
1455+
ispec.Manifest
1456+
1457+
Digest godigest.Digest
1458+
}
1459+
14541460
func (is *ImageStoreLocal) garbageCollect(dir string, repo string) error {
14551461
oci, err := umoci.OpenLayout(dir)
14561462
if err := test.Error(err); err != nil {
@@ -1466,7 +1472,7 @@ func (is *ImageStoreLocal) garbageCollect(dir string, repo string) error {
14661472

14671473
referencedByImageIndex := []string{}
14681474
cosignDescriptors := []ispec.Descriptor{}
1469-
notationDescriptors := []ispec.Descriptor{}
1475+
notationManifests := []extendedManifest{}
14701476

14711477
/* gather manifests references by multiarch images (to skip gc)
14721478
gather cosign and notation signatures descriptors */
@@ -1504,7 +1510,10 @@ func (is *ImageStoreLocal) garbageCollect(dir string, repo string) error {
15041510
}
15051511

15061512
if zcommon.GetManifestArtifactType(manifestContent) == notreg.ArtifactTypeNotation {
1507-
notationDescriptors = append(notationDescriptors, desc)
1513+
notationManifests = append(notationManifests, extendedManifest{
1514+
Digest: desc.Digest,
1515+
Manifest: manifestContent,
1516+
})
15081517

15091518
continue
15101519
}
@@ -1525,7 +1534,7 @@ func (is *ImageStoreLocal) garbageCollect(dir string, repo string) error {
15251534

15261535
is.log.Info().Msg("gc: notation signatures")
15271536

1528-
if err := gcNotationSignatures(is, oci, &index, repo, notationDescriptors); err != nil {
1537+
if err := gcNotationSignatures(is, oci, &index, repo, notationManifests); err != nil {
15291538
return err
15301539
}
15311540

@@ -1641,41 +1650,24 @@ func gcCosignSignatures(imgStore *ImageStoreLocal, oci casext.Engine, index *isp
16411650
}
16421651

16431652
func gcNotationSignatures(imgStore *ImageStoreLocal, oci casext.Engine, index *ispec.Index, repo string,
1644-
notationDescriptors []ispec.Descriptor,
1653+
notationManifests []extendedManifest,
16451654
) error {
1646-
for _, notationDesc := range notationDescriptors {
1655+
for _, notationManifest := range notationManifests {
16471656
foundSubject := false
1648-
// check if we can find the manifest which the signature points to
1649-
var artManifest ispec.Manifest
1650-
1651-
buf, err := imgStore.GetBlobContent(repo, notationDesc.Digest)
1652-
if err != nil {
1653-
imgStore.log.Error().Err(err).Str("repository", repo).Str("digest", notationDesc.Digest.String()).
1654-
Msg("gc: failed to get notation manifest")
1655-
1656-
return err
1657-
}
1658-
1659-
if err := json.Unmarshal(buf, &artManifest); err != nil {
1660-
imgStore.log.Error().Err(err).Str("repository", repo).Str("digest", notationDesc.Digest.String()).
1661-
Msg("gc: failed to get notation manifest")
1662-
1663-
return err
1664-
}
16651657

16661658
for _, desc := range index.Manifests {
1667-
if desc.Digest == artManifest.Subject.Digest {
1659+
if desc.Digest == notationManifest.Subject.Digest {
16681660
foundSubject = true
16691661
}
16701662
}
16711663

16721664
if !foundSubject {
16731665
// remove manifest
1674-
imgStore.log.Info().Str("repository", repo).Str("digest", notationDesc.Digest.String()).
1666+
imgStore.log.Info().Str("repository", repo).Str("digest", notationManifest.Digest.String()).
16751667
Msg("gc: removing notation signature without subject")
16761668

16771669
// no need to check for manifest conflict, if one doesn't have a subject, then none with same digest will have
1678-
_, _ = storage.RemoveManifestDescByReference(index, notationDesc.Digest.String(), false)
1670+
_, _ = storage.RemoveManifestDescByReference(index, notationManifest.Digest.String(), false)
16791671

16801672
err := oci.PutIndex(context.Background(), *index)
16811673
if err != nil {

0 commit comments

Comments
 (0)