Skip to content

Commit

Permalink
Merge branch 'main' into dev-image-index
Browse files Browse the repository at this point in the history
  • Loading branch information
jjbustamante committed Apr 12, 2024
2 parents 50d54c8 + cdd1ed7 commit ebde93a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
20 changes: 20 additions & 0 deletions cnb_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ func (i *CNBImageCore) GetAnnotateRefName() (string, error) {
}

func (i *CNBImageCore) GetLayer(diffID string) (io.ReadCloser, error) {
layerHash, err := v1.NewHash(diffID)
if err != nil {
return nil, err
}
configFile, err := i.ConfigFile()
if err != nil {
return nil, err
}
if !contains(configFile.RootFS.DiffIDs, layerHash) {
return nil, ErrLayerNotFound{DiffID: layerHash.String()}
}
hash, err := v1.NewHash(diffID)
if err != nil {
return nil, err
Expand All @@ -95,6 +106,15 @@ func (i *CNBImageCore) GetLayer(diffID string) (io.ReadCloser, error) {
return layer.Uncompressed()
}

func contains(diffIDs []v1.Hash, hash v1.Hash) bool {
for _, diffID := range diffIDs {
if diffID.String() == hash.String() {
return true
}
}
return false
}

// TBD Deprecated: History
func (i *CNBImageCore) History() ([]v1.History, error) {
configFile, err := getConfigFile(i.Image)
Expand Down
8 changes: 8 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,11 @@ func (e SaveError) Error() string {
}
return fmt.Sprintf("failed to write image to the following tags: %s", strings.Join(errors, ","))
}

type ErrLayerNotFound struct {
DiffID string
}

func (e ErrLayerNotFound) Error() string {
return fmt.Sprintf("failed to find layer with diff ID %q", e.DiffID)
}
14 changes: 7 additions & 7 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ func (i *Image) GetLayer(diffID string) (io.ReadCloser, error) {
if err != nil {
return nil, err
}
configFile, err := i.ConfigFile()
if err != nil {
return nil, err
}
if !contains(configFile.RootFS.DiffIDs, layerHash) {
return nil, imgutil.ErrLayerNotFound{DiffID: layerHash.String()}
}
layer, err := i.LayerByDiffID(layerHash)
if err == nil {
// this avoids downloading ALL the image layers from the daemon
Expand All @@ -64,13 +71,6 @@ func (i *Image) GetLayer(diffID string) (io.ReadCloser, error) {
return layer.Uncompressed()
}
}
configFile, err := i.ConfigFile()
if err != nil {
return nil, err
}
if !contains(configFile.RootFS.DiffIDs, layerHash) {
return nil, fmt.Errorf("image %q does not contain layer with diff ID %q", i.Name(), layerHash.String())
}
if err = i.ensureLayers(); err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1590,11 +1590,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertNil(t, err)
_, err = img.GetLayer(someSHA)
h.AssertError(
t,
err,
fmt.Sprintf(`image %q does not contain layer with diff ID "%s"`, repoName, someSHA),
)
h.AssertError(t, err, fmt.Sprintf("failed to find layer with diff ID %q", someSHA))
_, ok := err.(imgutil.ErrLayerNotFound)
h.AssertEq(t, ok, true)
})
})
})
Expand All @@ -1606,7 +1604,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {

readCloser, err := image.GetLayer(someSHA)
h.AssertNil(t, readCloser)
h.AssertError(t, err, fmt.Sprintf("image %q does not contain layer with diff ID %q", "not-exist", someSHA))
h.AssertError(t, err, fmt.Sprintf("failed to find layer with diff ID %q", someSHA))
_, ok := err.(imgutil.ErrLayerNotFound)
h.AssertEq(t, ok, true)
})
})
})
Expand Down

0 comments on commit ebde93a

Please sign in to comment.