Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interface method, AddOrReuseLayerWithHistory #266

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading