Skip to content

Commit

Permalink
containerimage: support SOURCE_DATE_EPOCH for CreatedAt
Browse files Browse the repository at this point in the history
Fix issue 3167

Signed-off-by: Akihiro Suda <[email protected]>
  • Loading branch information
AkihiroSuda committed Nov 9, 2022
1 parent a5263dd commit c6a5ba9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
55 changes: 55 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func TestIntegration(t *testing.T) {
testSourceDateEpochReset,
testSourceDateEpochLocalExporter,
testSourceDateEpochTarExporter,
testSourceDateEpochImageExporter,
testAttestationBundle,
testSBOMScan,
testSBOMScanSingleRef,
Expand Down Expand Up @@ -2683,6 +2684,60 @@ func testSourceDateEpochTarExporter(t *testing.T, sb integration.Sandbox) {
checkAllReleasable(t, c, sb, true)
}

func testSourceDateEpochImageExporter(t *testing.T, sb integration.Sandbox) {
integration.SkipIfDockerd(t, sb, "image exporter")

requiresLinux(t)
c, err := New(sb.Context(), sb.Address())
require.NoError(t, err)

busybox := llb.Image("busybox:latest")
st := llb.Scratch()

run := func(cmd string) {
st = busybox.Run(llb.Shlex(cmd), llb.Dir("/wd")).AddMount("/wd", st)
}

run(`sh -c "echo -n first > foo"`)
run(`sh -c "echo -n second > bar"`)

def, err := st.Marshal(sb.Context())
require.NoError(t, err)

name := strings.ToLower(path.Base(t.Name()))
tm := time.Date(2015, time.October, 21, 7, 28, 0, 0, time.UTC)

_, err = c.Solve(sb.Context(), def, SolveOpt{
FrontendAttrs: map[string]string{
"build-arg:SOURCE_DATE_EPOCH": fmt.Sprintf("%d", tm.Unix()),
},
Exports: []ExportEntry{
{
Type: ExporterImage,
Attrs: map[string]string{
"name": name,
},
},
},
}, nil)
require.NoError(t, err)

if cdAddress := sb.ContainerdAddress(); cdAddress != "" {
ctx := namespaces.WithNamespace(sb.Context(), "buildkit")
client, err := newContainerd(cdAddress)
require.NoError(t, err)
defer client.Close()

img, err := client.GetImage(ctx, name)
require.NoError(t, err)
require.Equal(t, tm, img.Metadata().CreatedAt)

// TODO: mount and validate the snapshots
}

checkAllReleasable(t, c, sb, true)
}

func testFrontendMetadataReturn(t *testing.T, sb integration.Sandbox) {
requiresLinux(t)
c, err := New(sb.Context(), sb.Address())
Expand Down
6 changes: 5 additions & 1 deletion exporter/containerimage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,13 @@ func (e *imageExporterInstance) Export(ctx context.Context, src *exporter.Source
for _, targetName := range targetNames {
if e.opt.Images != nil && e.store {
tagDone := progress.OneOff(ctx, "naming to "+targetName)
tm := time.Now()
if e.opts.Epoch != nil {
tm = *e.opts.Epoch
}
img := images.Image{
Target: *desc,
CreatedAt: time.Now(),
CreatedAt: tm,
}
sfx := []string{""}
if nameCanonical {
Expand Down

0 comments on commit c6a5ba9

Please sign in to comment.