Skip to content

Commit

Permalink
update based on feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Joey Brown <[email protected]>
  • Loading branch information
joeybrown-sf committed Jul 9, 2024
1 parent 80d089f commit 6a2a590
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 54 deletions.
27 changes: 1 addition & 26 deletions cache/image_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (c *ImageCache) RetrieveLayer(diffID string) (io.ReadCloser, error) {
if IsLayerNotFound(err) {
return nil, NewReadErr(fmt.Sprintf("failed to find cache layer with SHA '%s'", diffID))
}
return nil, NewReadErr(fmt.Sprintf("unknown error retrieving layer with SHA '%s'", diffID))
return nil, fmt.Errorf("failed to get cache layer with SHA '%s'", diffID)
}
return closer, nil
}
Expand All @@ -143,28 +143,3 @@ func (c *ImageCache) Commit() error {

return nil
}

// LayerExists checks if a layer exists in the cache
func (c *ImageCache) LayerExists(diffID string) (bool, error) {
layers, err := c.origImage.UnderlyingImage().Layers()
if err != nil {
return false, errors.Wrap(err, "getting image layers")
}

for _, layer := range layers {
d, err := layer.DiffID()
if err != nil {
return false, errors.Wrap(err, "getting layer diffID")
}

if d.String() == diffID {
return true, nil
}
}
return false, nil
}

// Destroy deletes the cache image
func (c *ImageCache) Destroy() {
c.imageDeleter.DeleteImage(c.origImage)
}
4 changes: 2 additions & 2 deletions cache/image_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func testImageCache(t *testing.T, when spec.G, it spec.S) {
when("layer does not exist", func() {
it("returns an error", func() {
_, err := subject.RetrieveLayer("some_nonexistent_sha")
h.AssertError(t, err, "unknown error retrieving layer with SHA 'some_nonexistent_sha'")
h.AssertError(t, err, "failed to get cache layer with SHA 'some_nonexistent_sha'")
})
})
})
Expand Down Expand Up @@ -236,7 +236,7 @@ func testImageCache(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, subject.AddLayerFile(testLayerTarPath, testLayerSHA))

_, err := subject.RetrieveLayer(testLayerSHA)
h.AssertError(t, err, fmt.Sprintf("unknown error retrieving layer with SHA '%s'", testLayerSHA))
h.AssertError(t, err, fmt.Sprintf("failed to get cache layer with SHA '%s'", testLayerSHA))
})
})
})
Expand Down
21 changes: 1 addition & 20 deletions cache/volume_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (c *VolumeCache) RetrieveLayer(diffID string) (io.ReadCloser, error) {
if os.IsPermission(err) {
return nil, NewReadErr(fmt.Sprintf("failed to read cache layer with SHA '%s' due to insufficient permissions", diffID))
}
return nil, NewReadErr(fmt.Sprintf("unknown error retrieving layer with SHA '%s'", diffID))
return nil, fmt.Errorf("failed to get cache layer with SHA '%s'", diffID)
}
return file, nil
}
Expand Down Expand Up @@ -228,22 +228,3 @@ func (c *VolumeCache) setupStagingDir() error {
}
return os.MkdirAll(c.stagingDir, 0777)
}

// LayerExists returns true if the layer with the given diffID exists in the cache
func (c *VolumeCache) LayerExists(diffID string) (bool, error) {
path := diffIDPath(c.committedDir, diffID)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, errors.Wrapf(err, "retrieving layer with SHA '%s'", diffID)
}
return true, nil
}

// Destroy removes the cache directory and all its contents
func (c *VolumeCache) Destroy() {
if err := os.RemoveAll(c.dir); err != nil {
c.logger.Warnf("Unable to delete cache directory: %v", err.Error())
}
}
8 changes: 4 additions & 4 deletions cache/volume_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func testVolumeCache(t *testing.T, when spec.G, it spec.S) {
when("layer does not exist", func() {
it("returns an error", func() {
_, err := subject.RetrieveLayer("some_nonexistent_sha")
h.AssertError(t, err, "Layer with SHA 'some_nonexistent_sha' not found")
h.AssertError(t, err, "failed to find cache layer with SHA 'some_nonexistent_sha'")
})
})
})
Expand All @@ -235,7 +235,7 @@ func testVolumeCache(t *testing.T, when spec.G, it spec.S) {
when("layer does not exist", func() {
it("returns an error", func() {
_, err := subject.RetrieveLayerFile("some_nonexistent_sha")
h.AssertError(t, err, "Layer with SHA 'some_nonexistent_sha' not found")
h.AssertError(t, err, "failed to find cache layer with SHA 'some_nonexistent_sha'")
})
})
})
Expand Down Expand Up @@ -345,7 +345,7 @@ func testVolumeCache(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, subject.AddLayerFile(tarPath, "some_sha"))

_, err := subject.RetrieveLayer("some_sha")
h.AssertError(t, err, "Layer with SHA 'some_sha' not found")
h.AssertError(t, err, "failed to find cache layer with SHA 'some_sha'")
})
})

Expand Down Expand Up @@ -420,7 +420,7 @@ func testVolumeCache(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, subject.AddLayer(layerReader, layerSha))

_, err := subject.RetrieveLayer(layerSha)
h.AssertError(t, err, fmt.Sprintf("Layer with SHA '%s' not found", layerSha))
h.AssertError(t, err, fmt.Sprintf("failed to find cache layer with SHA '%s'", layerSha))
})
})

Expand Down
2 changes: 0 additions & 2 deletions phase/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ type Cache interface {
AddLayerFile(tarPath string, sha string) error
ReuseLayer(sha string) error
RetrieveLayer(sha string) (io.ReadCloser, error)
LayerExists(sha string) (bool, error)
Commit() error
Destroy()
}

type Exporter struct {
Expand Down

0 comments on commit 6a2a590

Please sign in to comment.