Skip to content

Commit

Permalink
Merge pull request #266 from buildpacks/add/or-reuse-layer
Browse files Browse the repository at this point in the history
Add interface method, AddOrReuseLayerWithHistory
  • Loading branch information
natalieparellano authored Apr 12, 2024
2 parents cdd1ed7 + 801e572 commit 7ea6c92
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cnb_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,32 @@ func (i *CNBImageCore) AddLayerWithHistory(layer v1.Layer, history v1.History) e
return err
}

func (i *CNBImageCore) AddOrReuseLayerWithHistory(path string, diffID string, history v1.History) error {
prevLayerExists, err := i.PreviousImageHasLayer(diffID)
if err != nil {
return err
}
if !prevLayerExists {
return i.AddLayerWithDiffIDAndHistory(path, diffID, history)
}
return i.ReuseLayerWithHistory(diffID, history)
}

func (i *CNBImageCore) PreviousImageHasLayer(diffID string) (bool, error) {
if i.previousImage == nil {
return false, nil
}
layerHash, err := v1.NewHash(diffID)
if err != nil {
return false, fmt.Errorf("failed to get layer hash: %w", err)
}
prevConfigFile, err := getConfigFile(i.previousImage)
if err != nil {
return false, fmt.Errorf("failed to get previous image config: %w", err)
}
return contains(prevConfigFile.RootFS.DiffIDs, layerHash), nil
}

func (i *CNBImageCore) Rebase(baseTopLayerDiffID string, withNewBase Image) error {
newBase := withNewBase.UnderlyingImage() // FIXME: when all imgutil.Images are v1.Images, we can remove this part
var err error
Expand Down Expand Up @@ -457,6 +483,14 @@ func getHistory(forIndex int, fromImage v1.Image) (v1.History, error) {
}

func (i *CNBImageCore) ReuseLayerWithHistory(diffID string, history v1.History) error {
var err error
// ensure existing history
if err = i.MutateConfigFile(func(c *v1.ConfigFile) {
c.History = NormalizedHistory(c.History, len(c.RootFS.DiffIDs))
}); err != nil {
return err
}

layerHash, err := v1.NewHash(diffID)
if err != nil {
return fmt.Errorf("failed to get layer hash: %w", err)
Expand Down
6 changes: 6 additions & 0 deletions fakes/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/buildpacks/imgutil"
)

var _ imgutil.Image = &Image{}

func NewImage(name, topLayerSha string, identifier imgutil.Identifier) *Image {
return &Image{
labels: nil,
Expand Down Expand Up @@ -238,6 +240,10 @@ func shaForFile(path string) (string, error) {
return hex.EncodeToString(hasher.Sum(make([]byte, 0, hasher.Size()))), nil
}

func (i *Image) AddOrReuseLayerWithHistory(_, _ string, _ v1.History) error {
panic("implement me")
}

func (i *Image) GetLayer(sha string) (io.ReadCloser, error) {
path, ok := i.layersMap[sha]
if !ok {
Expand Down
1 change: 1 addition & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Image interface {
AddLayer(path string) error
AddLayerWithDiffID(path, diffID string) error
AddLayerWithDiffIDAndHistory(path, diffID string, history v1.History) error
AddOrReuseLayerWithHistory(path, diffID string, history v1.History) error
Delete() error
Rebase(string, Image) error
RemoveLabel(string) error
Expand Down
11 changes: 11 additions & 0 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ func (i *Image) addLayerToStore(fromPath, withDiffID string) (v1.Layer, error) {
return layer, nil
}

func (i *Image) AddOrReuseLayerWithHistory(path string, diffID string, history v1.History) error {
prevLayerExists, err := i.PreviousImageHasLayer(diffID)
if err != nil {
return err
}
if !prevLayerExists {
return i.AddLayerWithDiffIDAndHistory(path, diffID, history)
}
return i.ReuseLayerWithHistory(diffID, history)
}

func (i *Image) Rebase(baseTopLayerDiffID string, withNewBase imgutil.Image) error {
if err := i.ensureLayers(); err != nil {
return err
Expand Down

0 comments on commit 7ea6c92

Please sign in to comment.