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

use containerd's epoch package for handling SOURCE_DATE_EPOCH #1908

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"encoding/json"
"fmt"
"os"
"strconv"

"github.com/containerd/console"
"github.com/containerd/containerd/pkg/epoch"
"github.com/containerd/containerd/platforms"
"github.com/docker/buildx/bake"
"github.com/docker/buildx/build"
Expand Down Expand Up @@ -162,16 +164,18 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
return err
}

if v := os.Getenv("SOURCE_DATE_EPOCH"); v != "" {
// TODO: extract env var parsing to a method easily usable by library consumers
if v, err := epoch.SourceDateEpoch(); err != nil {
return err
} else if v != nil {
esd := strconv.FormatInt(v.Unix(), 10)
for _, t := range tgts {
if _, ok := t.Args["SOURCE_DATE_EPOCH"]; ok {
if _, ok := t.Args[epoch.SourceDateEpochEnv]; ok {
continue
}
if t.Args == nil {
t.Args = map[string]*string{}
}
t.Args["SOURCE_DATE_EPOCH"] = &v
t.Args[epoch.SourceDateEpochEnv] = &esd
}
}

Expand Down
12 changes: 8 additions & 4 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"

"github.com/containerd/console"
"github.com/containerd/containerd/pkg/epoch"
"github.com/docker/buildx/build"
"github.com/docker/buildx/builder"
"github.com/docker/buildx/controller"
Expand Down Expand Up @@ -120,10 +121,13 @@ func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error
ExportLoad: o.exportLoad,
}

// TODO: extract env var parsing to a method easily usable by library consumers
if v := os.Getenv("SOURCE_DATE_EPOCH"); v != "" {
if _, ok := opts.BuildArgs["SOURCE_DATE_EPOCH"]; !ok {
opts.BuildArgs["SOURCE_DATE_EPOCH"] = v
if _, ok := opts.BuildArgs[epoch.SourceDateEpochEnv]; !ok {
v, err := epoch.SourceDateEpoch()
if err != nil {
return nil, err
}
if v != nil {
opts.BuildArgs[epoch.SourceDateEpochEnv] = strconv.FormatInt(v.Unix(), 10)
Comment on lines +125 to +130
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure parsing string to time and back to string for this purpose is ideal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's not; I started working on a branch to add a utility for parsing in containerd (although we have one in BuildKit as well).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could still use the parsing, and just get the env-var separately; let me check for a bit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm.. no no utility on its own, so if could be;

opts.BuildArgs[epoch.SourceDateEpochEnv] = os.Getenv(epoch.SourceDateEpochEnv)

which also looks a bit odd, as we're setting a value that's not the v != nil that we check 😞

}
}

Expand Down
41 changes: 41 additions & 0 deletions vendor/github.com/containerd/containerd/pkg/epoch/context.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions vendor/github.com/containerd/containerd/pkg/epoch/epoch.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ github.com/containerd/containerd/leases
github.com/containerd/containerd/log
github.com/containerd/containerd/namespaces
github.com/containerd/containerd/pkg/dialer
github.com/containerd/containerd/pkg/epoch
github.com/containerd/containerd/pkg/randutil
github.com/containerd/containerd/pkg/seed
github.com/containerd/containerd/pkg/userns
Expand Down