Skip to content

Commit

Permalink
Set created at when we save, not when we initialize
Browse files Browse the repository at this point in the history
This will allow us to preserve the digest when no modifying options are provided

Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano committed Jan 30, 2024
1 parent 37c0fdc commit 8ca3b4a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 47 deletions.
39 changes: 38 additions & 1 deletion cnb_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ func (i *CNBImageCore) AddLayerWithDiffID(path, _ string) error {
}

func (i *CNBImageCore) AddLayerWithDiffIDAndHistory(path, _ string, history v1.History) 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
}

layer, err := tarball.LayerFromFile(path)
if err != nil {
return err
Expand Down Expand Up @@ -437,7 +444,9 @@ func (i *CNBImageCore) ReuseLayerWithHistory(diffID string, history v1.History)
if err != nil {
return err
}
if !i.preserveHistory {
if i.preserveHistory {
history.Created = v1.Time{Time: i.createdAt}
} else {
history = emptyHistory
}
i.Image, err = mutate.Append(
Expand All @@ -464,6 +473,34 @@ func (i *CNBImageCore) MutateConfigFile(withFunc func(c *v1.ConfigFile)) error {
return err
}

func (i *CNBImageCore) SetCreatedAtAndHistory() error {
var err error
// set created at
if err = i.MutateConfigFile(func(c *v1.ConfigFile) {
c.Created = v1.Time{Time: i.createdAt}
c.Container = ""
}); err != nil {
return err
}
// set history
if i.preserveHistory {
// set created at for each history
err = i.MutateConfigFile(func(c *v1.ConfigFile) {
for j := range c.History {
c.History[j].Created = v1.Time{Time: i.createdAt}
}
})
} else {
// zero history
err = i.MutateConfigFile(func(c *v1.ConfigFile) {
for j := range c.History {
c.History[j] = v1.History{Created: v1.Time{Time: i.createdAt}}
}
})
}
return err
}

func getConfigFile(image v1.Image) (*v1.ConfigFile, error) {
configFile, err := image.ConfigFile()
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions layout/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,7 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
})
})

when.Pend("#CreatedAt", func() {
// Previously, we only zeroed CreatedAt at the point of save.
// Now, we zero CreatedAt at the point of instantiation.
// If this behavior change is acceptable, we can remove this test.
when("#CreatedAt", func() {
it.Before(func() {
imagePath = filepath.Join(tmpDir, "new-created-at-image")
})
Expand Down
3 changes: 3 additions & 0 deletions layout/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ func (i *Image) Save(additionalNames ...string) error {

// SaveAs ignores the image `Name()` method and saves the image according to name & additional names provided to this method
func (i *Image) SaveAs(name string, additionalNames ...string) error {
if err := i.SetCreatedAtAndHistory(); err != nil {
return err
}
refName, err := i.GetAnnotateRefName()
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions layout/sparse/sparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
})

when("#Digest", func() {
it.Pend("returns the original image digest when there are no modifications", func() {
// TODO: created at
it("returns the original image digest when there are no modifications", func() {
image, err := sparse.NewImage(imagePath, testImage)
h.AssertNil(t, err)

Expand Down
10 changes: 8 additions & 2 deletions locallayout/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,19 @@ func (i *Image) Rebase(baseTopLayerDiffID string, withNewBase imgutil.Image) err
}

func (i *Image) Save(additionalNames ...string) error {
var err error
err := i.SetCreatedAtAndHistory()
if err != nil {
return err
}
i.lastIdentifier, err = i.store.Save(i, i.Name(), additionalNames...)
return err
}

func (i *Image) SaveAs(name string, additionalNames ...string) error {
var err error
err := i.SetCreatedAtAndHistory()
if err != nil {
return err
}
i.lastIdentifier, err = i.store.Save(i, name, additionalNames...)
return err
}
Expand Down
5 changes: 1 addition & 4 deletions locallayout/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,7 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
})
})

when.Pend("#CreatedAt", func() {
// Previously, we only zeroed CreatedAt at the point of save.
// Now, we zero CreatedAt at the point of instantiation.
// If this behavior change is acceptable, we can remove this test.
when("#CreatedAt", func() {
it("returns the containers created at time", func() {
img, err := local.NewImage(newTestImageName(), dockerClient, local.FromBaseImage(runnableBaseImageName))
h.AssertNil(t, err)
Expand Down
34 changes: 0 additions & 34 deletions new.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ func NewCNBImage(options ImageOptions) (*CNBImageCore, error) {
return nil, err
}

// ensure existing history
if err = image.MutateConfigFile(func(c *v1.ConfigFile) {
c.History = NormalizedHistory(c.History, len(c.RootFS.DiffIDs))
}); err != nil {
return nil, err
}

// set config if requested
if options.Config != nil {
if err = image.MutateConfigFile(func(c *v1.ConfigFile) {
Expand All @@ -57,33 +50,6 @@ func NewCNBImage(options ImageOptions) (*CNBImageCore, error) {
}
}

// set created at
if err = image.MutateConfigFile(func(c *v1.ConfigFile) {
c.Created = v1.Time{Time: image.createdAt}
c.Container = ""
}); err != nil {
return nil, err
}
// set history
if options.PreserveHistory {
// set created at for each history
err = image.MutateConfigFile(func(c *v1.ConfigFile) {
for j := range c.History {
c.History[j].Created = v1.Time{Time: image.createdAt}
}
})
} else {
// zero history
err = image.MutateConfigFile(func(c *v1.ConfigFile) {
for j := range c.History {
c.History[j] = v1.History{Created: v1.Time{Time: image.createdAt}}
}
})
}
if err != nil {
return nil, err
}

return image, nil
}

Expand Down

0 comments on commit 8ca3b4a

Please sign in to comment.