Skip to content

Commit

Permalink
Implementing the shared blob case logic
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Vlérick <[email protected]>
  • Loading branch information
Pvlerick committed Jun 24, 2023
1 parent 784671b commit 121e1f3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
39 changes: 29 additions & 10 deletions oci/layout/oci_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,43 @@ func (ref ociReference) NewImageDestination(ctx context.Context, sys *types.Syst
func (ref ociReference) DeleteImage(ctx context.Context, sys *types.SystemContext) error {

// Get the manifest for the image
descriptor, err := ref.getManifestDescriptor()
manifestDescriptor, err := ref.getManifestDescriptor()
if err != nil {
return err
}

manifest, err := ref.getManifest(descriptor)
manifest, err := ref.getManifest(manifestDescriptor)
if err != nil {
return err
}

// Get all the layers used by all other images
index, err := ref.getIndex()
if err != nil {
return err
}
layersUsedByOtherImages := make(map[digest.Digest]bool, len(index.Manifests))
for _, v := range index.Manifests {
if v.Digest != manifestDescriptor.Digest {
otherImageManifest, err := ref.getManifest(v)
if err != nil {
return err
}
for _, layer := range otherImageManifest.Layers {
layersUsedByOtherImages[layer.Digest] = true
}
}
}

// Delete all blobs
for _, d := range append(manifest.Layers, manifest.Config, descriptor) {
blobPath, err := ref.blobPath(d.Digest, "")
blobsToDelete := make([]digest.Digest, 0, len(manifest.Layers))
for _, layer := range manifest.Layers {
if !layersUsedByOtherImages[layer.Digest] {
blobsToDelete = append(blobsToDelete, layer.Digest)
}
}
for _, digest := range append(blobsToDelete, manifest.Config.Digest, manifestDescriptor.Digest) {
blobPath, err := ref.blobPath(digest, "")
if err != nil {
return err
}
Expand All @@ -272,14 +296,9 @@ func (ref ociReference) DeleteImage(ctx context.Context, sys *types.SystemContex
}

// Update the index
index, err := ref.getIndex()
if err != nil {
return err
}

newManifests := make([]imgspecv1.Descriptor, 0, len(index.Manifests)-1)
for _, v := range index.Manifests {
if v.Digest != descriptor.Digest {
if v.Digest != manifestDescriptor.Digest {
newManifests = append(newManifests, v)
}
}
Expand Down
2 changes: 1 addition & 1 deletion oci/layout/oci_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ func TestReferenceDeleteImage_someLayersAreReferencedByOtherImages(t *testing.T)
blobDoesNotExist(t, blobsDir, image.layers[0])
blobDoesNotExist(t, blobsDir, image.layers[2])

// Check that the blob used by another image is still there
// Check that the blob used by another image was not deleted
commonBlobDigest, err := digest.Parse(commonLayer)
require.NoError(t, err)
_, err = os.Stat(filepath.Join(blobsDir, commonBlobDigest.Algorithm().String(), commonBlobDigest.Hex()))
Expand Down

0 comments on commit 121e1f3

Please sign in to comment.