Skip to content

Commit

Permalink
Ensure we always normalize history before saving
Browse files Browse the repository at this point in the history
Previously, we normalized history whenever layers were added
(either via AddLayer or ReuseLayer),
but when rebasing, if the new base image has empty layer history
the information about the empty/non-empty aspect of the history will be lost
if "preserve history" isn't set on the image.

Signed-off-by: Natalie Arellano <narellano@vmware.com>
  • Loading branch information
natalieparellano committed Apr 22, 2024
1 parent bef4977 commit abe3e07
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cnb_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,15 @@ func (i *CNBImageCore) SetCreatedAtAndHistory() error {
if i.preserveHistory {
// set created at for each history
err = i.MutateConfigFile(func(c *v1.ConfigFile) {
c.History = NormalizedHistory(c.History, len(c.RootFS.DiffIDs))
for j := range c.History {
c.History[j].Created = v1.Time{Time: i.createdAt}
}
})
} else {
// zero history
err = i.MutateConfigFile(func(c *v1.ConfigFile) {
c.History = NormalizedHistory(c.History, len(c.RootFS.DiffIDs))
for j := range c.History {
c.History[j] = v1.History{Created: v1.Time{Time: i.createdAt}}
}
Expand Down
9 changes: 9 additions & 0 deletions remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,14 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
newBaseImg, err := remote.NewImage(newBase, authn.DefaultKeychain, remote.FromBaseImage(newBase))
h.AssertNil(t, err)

h.AssertNil(t, newBaseImg.MutateConfigFile(func(c *v1.ConfigFile) {
c.History = []v1.History{
{CreatedBy: "/new-base.txt"},
{CreatedBy: "FOOBAR", EmptyLayer: true}, // add empty layer history
{CreatedBy: "/otherfile.txt"},
}
})) // don't save the image, as that will strip the empty layer history

err = img.Rebase(oldTopLayerDiffID, newBaseImg)
h.AssertNil(t, err)
h.AssertNil(t, img.Save())
Expand All @@ -1288,6 +1296,7 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, rebasedImgConfig.OS, newBaseConfig.OS)
h.AssertEq(t, rebasedImgConfig.OSVersion, newBaseConfig.OSVersion)
h.AssertEq(t, rebasedImgConfig.Architecture, newBaseConfig.Architecture)
h.AssertEq(t, len(rebasedImgConfig.History), len(rebasedImgConfig.RootFS.DiffIDs))
})
})
})
Expand Down

0 comments on commit abe3e07

Please sign in to comment.