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 5cd3d3e + 7ea6c92 commit d038320
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 @@ -443,6 +443,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 @@ -543,6 +569,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 @@ -19,6 +19,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 @@ -279,6 +281,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 @@ -85,6 +85,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 @@ -168,6 +168,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 d038320

Please sign in to comment.