From c939c10c035e73fa9cf6d1aeab6af49e7d42cad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Vl=C3=A9rick?= Date: Thu, 10 Aug 2023 10:48:21 +0200 Subject: [PATCH] Small changes after code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Philippe Vlérick --- go.sum | 1 - oci/layout/oci_transport.go | 26 +++++++++++++------------- oci/layout/oci_transport_test.go | 10 +++------- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/go.sum b/go.sum index f8bd1e454c..c880ea02a1 100644 --- a/go.sum +++ b/go.sum @@ -282,7 +282,6 @@ github.com/ostreedev/ostree-go v0.0.0-20210805093236-719684c64e4f/go.mod h1:J6OG github.com/otiai10/copy v1.12.0 h1:cLMgSQnXBs1eehF0Wy/FAGsgDTDmAqFR7rQylBb1nDY= github.com/otiai10/copy v1.12.0/go.mod h1:rSaLseMUsZFFbsFGc7wCJnnkTAvdc5L6VWxPE4308Ww= github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks= -github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/oci/layout/oci_transport.go b/oci/layout/oci_transport.go index 33cd0bbae3..f8bc107ab1 100644 --- a/oci/layout/oci_transport.go +++ b/oci/layout/oci_transport.go @@ -167,7 +167,7 @@ func (ref ociReference) getIndex() (*imgspecv1.Index, error) { return parseIndex(ref.indexPath()) } -func parseJson[T any](path string) (*T, error) { +func parseJSON[T any](path string) (*T, error) { content, err := os.Open(path) if err != nil { return nil, err @@ -182,7 +182,7 @@ func parseJson[T any](path string) (*T, error) { } func parseIndex(path string) (*imgspecv1.Index, error) { - return parseJson[imgspecv1.Index](path) + return parseJSON[imgspecv1.Index](path) } func (ref ociReference) getManifestDescriptor() (imgspecv1.Descriptor, error) { @@ -269,7 +269,7 @@ func (ref ociReference) getManifest(descriptor *imgspecv1.Descriptor) (*imgspecv return nil, err } - manifest, err := parseJson[imgspecv1.Manifest](manifestPath) + manifest, err := parseJSON[imgspecv1.Manifest](manifestPath) if err != nil { return nil, err } @@ -351,15 +351,15 @@ func (ref ociReference) DeleteImage(ctx context.Context, sys *types.SystemContex } // Delete all blobs used by this image only - blobsToDelete := make([]digest.Digest, 0, len(manifest.Layers)+2) + blobsToDelete := set.New[digest.Digest]() for _, descriptor := range append(manifest.Layers, manifest.Config, *imageDescriptorWrapper.descriptor) { if !blobsUsedByOtherImages.Contains(descriptor.Digest) { - blobsToDelete = append(blobsToDelete, descriptor.Digest) + blobsToDelete.Add(descriptor.Digest) } else { logrus.Debug("Blob ", descriptor.Digest.Hex(), " is used by another image, leaving it") } } - for _, digest := range blobsToDelete { + for _, digest := range blobsToDelete.Values() { //TODO Check if there is shared blob path? blobPath, err := ref.blobPath(digest, "") if err != nil { @@ -367,7 +367,7 @@ func (ref ociReference) DeleteImage(ctx context.Context, sys *types.SystemContex } logrus.Debug("Deleting blob ", digest.Hex()) err = os.Remove(blobPath) - if err != nil { + if err != nil && !os.IsNotExist(err) { return err } } @@ -402,7 +402,7 @@ func (ref ociReference) DeleteImage(ctx context.Context, sys *types.SystemContex // New index is ready, it has to be saved to disk now // ... if it is the root index, it's easy, just overwrite it if indexPath == ref.indexPath() { - return saveJson(ref.indexPath(), index) + return saveJSON(ref.indexPath(), index) } else { indexDigest, err := digest.Parse("sha256:" + filepath.Base(indexPath)) if err != nil { @@ -418,12 +418,12 @@ func (ref ociReference) DeleteImage(ctx context.Context, sys *types.SystemContex if err != nil { return err } - indexNewDigest := digest.SHA256.FromBytes(buffer.Bytes()) + indexNewDigest := digest.Canonical.FromBytes(buffer.Bytes()) indexNewPath, err := ref.blobPath(indexNewDigest, "") if err != nil { return err } - err = saveJson(indexNewPath, index) + err = saveJSON(indexNewPath, index) if err != nil { return err } @@ -440,10 +440,10 @@ func (ref ociReference) DeleteImage(ctx context.Context, sys *types.SystemContex return nil } -func saveJson(path string, content any) error { +func saveJSON(path string, content any) error { // If the file already exists, get its mode to preserve it var mode fs.FileMode - existinfFileInfo, err := os.Stat(path) + existingfi, err := os.Stat(path) if err != nil { if !os.IsNotExist(err) { return err @@ -451,7 +451,7 @@ func saveJson(path string, content any) error { mode = 0644 } } else { - mode = existinfFileInfo.Mode() + mode = existingfi.Mode() } // Then write the file diff --git a/oci/layout/oci_transport_test.go b/oci/layout/oci_transport_test.go index d20faad95a..9d724bd617 100644 --- a/oci/layout/oci_transport_test.go +++ b/oci/layout/oci_transport_test.go @@ -3,7 +3,6 @@ package layout import ( "context" "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -341,7 +340,7 @@ func TestReferenceDeleteImage(t *testing.T) { // Check that all blobs were deleted blobsDir := filepath.Join(tmpDir, "blobs") - files, err := ioutil.ReadDir(filepath.Join(blobsDir, "sha256")) + files, err := os.ReadDir(filepath.Join(blobsDir, "sha256")) require.NoError(t, err) require.Empty(t, files) @@ -364,7 +363,7 @@ func TestReferenceDeleteImage_emptyImageName(t *testing.T) { // Check that all blobs were deleted blobsDir := filepath.Join(tmpDir, "blobs") - files, err := ioutil.ReadDir(filepath.Join(blobsDir, "sha256")) + files, err := os.ReadDir(filepath.Join(blobsDir, "sha256")) require.NoError(t, err) require.Empty(t, files) @@ -463,10 +462,7 @@ func TestReferenceDeleteImage_someBlobsAreUsedByOtherImages(t *testing.T) { otherImagesStillPresent = append(otherImagesStillPresent, true) } } - require.Equal(t, 2, len(otherImagesStillPresent)) - for _, v := range otherImagesStillPresent { - require.True(t, v) - } + assert.Equal(t, []bool{true, true}, otherImagesStillPresent) } func TestReferenceDeleteImage_inNestedIndex(t *testing.T) {