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

Allow overriding image creation time #137

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 18 additions & 2 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -789,12 +790,27 @@ func defaultPlatform(dockerClient client.CommonAPIClient) (imgutil.Platform, err
}, nil
}

func getCreationTimeFromEnv() v1.Time {
epoch := os.Getenv("SOURCE_DATE_EPOCH")
if epoch == "" {
return v1.Time{Time: imgutil.NormalizedDateTime}
}

seconds, err := strconv.ParseInt(epoch, 10, 64)
if err != nil {
return v1.Time{Time: imgutil.NormalizedDateTime}
}
return v1.Time{Time: time.Unix(seconds, 0)}
}

func v1Config(inspect types.ImageInspect) (v1.ConfigFile, error) {
creationTime := getCreationTimeFromEnv()

history := make([]v1.History, len(inspect.RootFS.Layers))
for i := range history {
// zero history
history[i] = v1.History{
Created: v1.Time{Time: imgutil.NormalizedDateTime},
Created: creationTime,
}
}
diffIDs := make([]v1.Hash, len(inspect.RootFS.Layers))
Expand Down Expand Up @@ -850,7 +866,7 @@ func v1Config(inspect types.ImageInspect) (v1.ConfigFile, error) {
}
return v1.ConfigFile{
Architecture: inspect.Architecture,
Created: v1.Time{Time: imgutil.NormalizedDateTime},
Created: creationTime,
History: history,
OS: inspect.Os,
OSVersion: inspect.OsVersion,
Expand Down
20 changes: 18 additions & 2 deletions remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -629,12 +631,26 @@ func findLayerWithSha(layers []v1.Layer, diffID string) (v1.Layer, error) {
return nil, fmt.Errorf("previous image did not have layer with diff id %q", diffID)
}

func getCreationTimeFromEnv() v1.Time {
epoch := os.Getenv("SOURCE_DATE_EPOCH")
if epoch == "" {
return v1.Time{Time: imgutil.NormalizedDateTime}
}

seconds, err := strconv.ParseInt(epoch, 10, 64)
if err != nil {
return v1.Time{Time: imgutil.NormalizedDateTime}
}
return v1.Time{Time: time.Unix(seconds, 0)}
}

func (i *Image) Save(additionalNames ...string) error {
var err error

allNames := append([]string{i.repoName}, additionalNames...)

i.image, err = mutate.CreatedAt(i.image, v1.Time{Time: imgutil.NormalizedDateTime})
creationTime := getCreationTimeFromEnv()
i.image, err = mutate.CreatedAt(i.image, creationTime)
if err != nil {
return errors.Wrap(err, "set creation time")
}
Expand All @@ -652,7 +668,7 @@ func (i *Image) Save(additionalNames ...string) error {
cfg.History = make([]v1.History, len(layers))
for i := range cfg.History {
cfg.History[i] = v1.History{
Created: v1.Time{Time: imgutil.NormalizedDateTime},
Created: creationTime,
imjasonh marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
24 changes: 24 additions & 0 deletions remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,30 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, item.Created.Unix(), imgutil.NormalizedDateTime.Unix())
}
})

it("can override creation time", func() {
img, err := remote.NewImage(repoName, authn.DefaultKeychain)
h.AssertNil(t, err)

tarPath, err := h.CreateSingleFileLayerTar("/new-layer.txt", "new-layer", "linux")
h.AssertNil(t, err)
defer os.Remove(tarPath)

h.AssertNil(t, img.AddLayer(tarPath))

epoch := time.Now().Unix()
os.Setenv("SOURCE_DATE_EPOCH", fmt.Sprintf("%d", epoch))
h.AssertNil(t, img.Save())

configFile := h.FetchManifestImageConfigFile(t, repoName)

h.AssertEq(t, configFile.Created.Time.Unix(), epoch)

h.AssertEq(t, len(configFile.History), len(configFile.RootFS.DiffIDs))
for _, item := range configFile.History {
h.AssertEq(t, item.Created.Unix(), epoch)
}
})
})

when("additional names are provided", func() {
Expand Down