Skip to content

Commit d781c83

Browse files
committed
commands: micro optimization for source date epoch
Avoids the call to `os.Getenv` when it is unnecessary because it would be overwritten anyway. Removes the comments about moving environment variable parsing to a method for use by library consumers. That method exists in containerd and this set of code doesn't actually perform any parsing since the parsing of this time is done within buildkit and not on the client. Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
1 parent 6bde7f2 commit d781c83

6 files changed

Lines changed: 127 additions & 12 deletions

File tree

commands/bake.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"text/tabwriter"
1818

1919
"github.com/containerd/console"
20+
"github.com/containerd/containerd/v2/pkg/epoch"
2021
"github.com/containerd/platforms"
2122
"github.com/docker/buildx/bake"
2223
"github.com/docker/buildx/bake/hclparser"
@@ -241,16 +242,20 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
241242
return err
242243
}
243244

244-
if v := os.Getenv("SOURCE_DATE_EPOCH"); v != "" {
245-
// TODO: extract env var parsing to a method easily usable by library consumers
246-
for _, t := range tgts {
247-
if _, ok := t.Args["SOURCE_DATE_EPOCH"]; ok {
248-
continue
249-
}
245+
var sourceDateEpoch *string
246+
for _, t := range tgts {
247+
if _, ok := t.Args[epoch.SourceDateEpochEnv]; ok {
248+
continue
249+
}
250+
251+
v := os.Getenv(epoch.SourceDateEpochEnv)
252+
sourceDateEpoch = &v
253+
254+
if *sourceDateEpoch != "" {
250255
if t.Args == nil {
251256
t.Args = map[string]*string{}
252257
}
253-
t.Args["SOURCE_DATE_EPOCH"] = &v
258+
t.Args[epoch.SourceDateEpochEnv] = sourceDateEpoch
254259
}
255260
}
256261

commands/build.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"github.com/containerd/console"
21+
"github.com/containerd/containerd/v2/pkg/epoch"
2122
"github.com/docker/buildx/build"
2223
"github.com/docker/buildx/builder"
2324
"github.com/docker/buildx/store"
@@ -139,10 +140,9 @@ func (o *buildOptions) toOptions() (*BuildOptions, error) {
139140
ExportLoad: o.exportLoad,
140141
}
141142

142-
// TODO: extract env var parsing to a method easily usable by library consumers
143-
if v := os.Getenv("SOURCE_DATE_EPOCH"); v != "" {
144-
if _, ok := opts.BuildArgs["SOURCE_DATE_EPOCH"]; !ok {
145-
opts.BuildArgs["SOURCE_DATE_EPOCH"] = v
143+
if _, ok := opts.BuildArgs[epoch.SourceDateEpochEnv]; !ok {
144+
if v := os.Getenv(epoch.SourceDateEpochEnv); v != "" {
145+
opts.BuildArgs[epoch.SourceDateEpochEnv] = v
146146
}
147147
}
148148

commands/history/inspect.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/containerd/containerd/v2/core/content"
2020
"github.com/containerd/containerd/v2/core/content/proxy"
2121
"github.com/containerd/containerd/v2/core/images"
22+
"github.com/containerd/containerd/v2/pkg/epoch"
2223
"github.com/containerd/platforms"
2324
"github.com/docker/buildx/localstate"
2425
"github.com/docker/buildx/util/cobrautil/completion"
@@ -392,7 +393,7 @@ workers0:
392393
readAttr(attrs, "ulimit", &out.Config.Ulimit, nil)
393394
readAttr(attrs, "build-arg:BUILDKIT_CACHE_MOUNT_NS", &out.Config.CacheMountNS, nil)
394395
readAttr(attrs, "build-arg:BUILDKIT_DOCKERFILE_CHECK", &out.Config.DockerfileCheckConfig, nil)
395-
readAttr(attrs, "build-arg:SOURCE_DATE_EPOCH", &out.Config.SourceDateEpoch, nil)
396+
readAttr(attrs, "build-arg:"+epoch.SourceDateEpochEnv, &out.Config.SourceDateEpoch, nil)
396397
readAttr(attrs, "build-arg:SANDBOX_HOSTNAME", &out.Config.SandboxHostname, nil)
397398

398399
var unusedAttrs []keyValueOutput

vendor/github.com/containerd/containerd/v2/pkg/epoch/context.go

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/containerd/v2/pkg/epoch/epoch.go

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ github.com/containerd/containerd/v2/internal/fsverity
222222
github.com/containerd/containerd/v2/internal/lazyregexp
223223
github.com/containerd/containerd/v2/internal/randutil
224224
github.com/containerd/containerd/v2/pkg/archive/compression
225+
github.com/containerd/containerd/v2/pkg/epoch
225226
github.com/containerd/containerd/v2/pkg/filters
226227
github.com/containerd/containerd/v2/pkg/identifiers
227228
github.com/containerd/containerd/v2/pkg/kernelversion

0 commit comments

Comments
 (0)