Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: docker/buildx
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.21.2
Choose a base ref
...
head repository: docker/buildx
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.21.3
Choose a head ref
  • 3 commits
  • 6 files changed
  • 2 contributors

Commits on Mar 17, 2025

  1. otel: avoid tracing raw os arguments

    User might pass a value that they don't expect to
    be kept in trace storage. For example some cache backends
    allow passing authentication tokens with a flag.
    
    Instead use known primary config values as attributes
    of the root span.
    
    Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
    tonistiigi authored and crazy-max committed Mar 17, 2025

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    0982070 View commit details
  2. localstate: remove definition and inputs fields from group

    Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
    crazy-max committed Mar 17, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    05f75a5 View commit details
  3. Merge pull request #3067 from crazy-max/0.21_picks_0.21.3

    [v0.21] cherry-picks for v0.21.3
    crazy-max authored Mar 17, 2025
    Copy the full SHA
    7b5fecb View commit details
Showing with 105 additions and 33 deletions.
  1. +17 −15 commands/bake.go
  2. +5 −1 commands/build.go
  3. +22 −9 localstate/localstate.go
  4. +2 −4 localstate/localstate_test.go
  5. +56 −0 localstate/migrate.go
  6. +3 −4 util/tracing/trace.go
32 changes: 17 additions & 15 deletions commands/bake.go
Original file line number Diff line number Diff line change
@@ -66,7 +66,11 @@ type bakeOptions struct {
func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in bakeOptions, cFlags commonFlags) (err error) {
mp := dockerCli.MeterProvider()

ctx, end, err := tracing.TraceCurrentCommand(ctx, "bake")
ctx, end, err := tracing.TraceCurrentCommand(ctx, append([]string{"bake"}, targets...),
attribute.String("builder", in.builder),
attribute.StringSlice("targets", targets),
attribute.StringSlice("files", in.files),
)
if err != nil {
return err
}
@@ -283,7 +287,7 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
}
}

if err := saveLocalStateGroup(dockerCli, in, targets, bo, overrides, def); err != nil {
if err := saveLocalStateGroup(dockerCli, in, targets, bo); err != nil {
return err
}

@@ -488,7 +492,14 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
return cmd
}

func saveLocalStateGroup(dockerCli command.Cli, in bakeOptions, targets []string, bo map[string]build.Options, overrides []string, def any) error {
func saveLocalStateGroup(dockerCli command.Cli, in bakeOptions, targets []string, bo map[string]build.Options) error {
l, err := localstate.New(confutil.NewConfig(dockerCli))
if err != nil {
return err
}

defer l.MigrateIfNeeded()

prm := confutil.MetadataProvenance()
if len(in.metadataFile) == 0 {
prm = confutil.MetadataProvenanceModeDisabled
@@ -508,19 +519,10 @@ func saveLocalStateGroup(dockerCli command.Cli, in bakeOptions, targets []string
if len(refs) == 0 {
return nil
}
l, err := localstate.New(confutil.NewConfig(dockerCli))
if err != nil {
return err
}
dtdef, err := json.MarshalIndent(def, "", " ")
if err != nil {
return err
}

return l.SaveGroup(groupRef, localstate.StateGroup{
Definition: dtdef,
Targets: targets,
Inputs: overrides,
Refs: refs,
Refs: refs,
Targets: targets,
})
}

6 changes: 5 additions & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
@@ -285,7 +285,11 @@ func (o *buildOptionsHash) String() string {
func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions) (err error) {
mp := dockerCli.MeterProvider()

ctx, end, err := tracing.TraceCurrentCommand(ctx, "build")
ctx, end, err := tracing.TraceCurrentCommand(ctx, []string{"build", options.contextPath},
attribute.String("builder", options.builder),
attribute.String("context", options.contextPath),
attribute.String("dockerfile", options.dockerfileName),
)
if err != nil {
return err
}
31 changes: 22 additions & 9 deletions localstate/localstate.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"sync"

"github.com/docker/buildx/util/confutil"
@@ -14,6 +15,7 @@ import (
)

const (
version = 2
refsDir = "refs"
groupDir = "__group__"
)
@@ -31,12 +33,8 @@ type State struct {
}

type StateGroup struct {
// Definition is the raw representation of the group (bake definition)
Definition []byte
// Targets are the targets invoked
Targets []string `json:",omitempty"`
// Inputs are the user inputs (bake overrides)
Inputs []string `json:",omitempty"`
// Refs are used to track all the refs that belong to the same group
Refs []string
}
@@ -52,9 +50,7 @@ func New(cfg *confutil.Config) (*LocalState, error) {
if err := cfg.MkdirAll(refsDir, 0700); err != nil {
return nil, err
}
return &LocalState{
cfg: cfg,
}, nil
return &LocalState{cfg: cfg}, nil
}

func (ls *LocalState) ReadRef(builderName, nodeName, id string) (*State, error) {
@@ -87,8 +83,12 @@ func (ls *LocalState) SaveRef(builderName, nodeName, id string, st State) error
return ls.cfg.AtomicWriteFile(filepath.Join(refDir, id), dt, 0644)
}

func (ls *LocalState) GroupDir() string {
return filepath.Join(ls.cfg.Dir(), refsDir, groupDir)
}

func (ls *LocalState) ReadGroup(id string) (*StateGroup, error) {
dt, err := os.ReadFile(filepath.Join(ls.cfg.Dir(), refsDir, groupDir, id))
dt, err := os.ReadFile(filepath.Join(ls.GroupDir(), id))
if err != nil {
return nil, err
}
@@ -208,7 +208,7 @@ func (ls *LocalState) removeGroup(id string) error {
if id == "" {
return errors.Errorf("group ref empty")
}
f := filepath.Join(ls.cfg.Dir(), refsDir, groupDir, id)
f := filepath.Join(ls.GroupDir(), id)
if _, err := os.Lstat(f); err != nil {
if !os.IsNotExist(err) {
return err
@@ -230,3 +230,16 @@ func (ls *LocalState) validate(builderName, nodeName, id string) error {
}
return nil
}

func (ls *LocalState) readVersion() int {
if vdt, err := os.ReadFile(filepath.Join(ls.cfg.Dir(), refsDir, "version")); err == nil {
if v, err := strconv.Atoi(string(vdt)); err == nil {
return v
}
}
return 1
}

func (ls *LocalState) writeVersion(version int) error {
return ls.cfg.AtomicWriteFile(filepath.Join(refsDir, "version"), []byte(strconv.Itoa(version)), 0600)
}
6 changes: 2 additions & 4 deletions localstate/localstate_test.go
Original file line number Diff line number Diff line change
@@ -68,10 +68,8 @@ var (

testStateGroupID = "kvqs0sgly2rmitz84r25u9qd0"
testStateGroup = StateGroup{
Definition: []byte(`{"group":{"default":{"targets":["pre-checkin"]},"pre-checkin":{"targets":["vendor-update","format","build"]}},"target":{"build":{"context":".","dockerfile":"dev.Dockerfile","target":"build-update","platforms":["linux/amd64"],"output":["."]},"format":{"context":".","dockerfile":"dev.Dockerfile","target":"format-update","platforms":["linux/amd64"],"output":["."]},"vendor-update":{"context":".","dockerfile":"dev.Dockerfile","target":"vendor-update","platforms":["linux/amd64"],"output":["."]}}}`),
Targets: []string{"pre-checkin"},
Inputs: []string{"*.platform=linux/amd64"},
Refs: []string{"builder/builder0/hx2qf1w11qvz1x3k471c5i8xw", "builder/builder0/968zj0g03jmlx0s8qslnvh6rl", "builder/builder0/naf44f9i1710lf7y12lv5hb1z"},
Targets: []string{"pre-checkin"},
Refs: []string{"builder/builder0/hx2qf1w11qvz1x3k471c5i8xw", "builder/builder0/968zj0g03jmlx0s8qslnvh6rl", "builder/builder0/naf44f9i1710lf7y12lv5hb1z"},
}

testStateGroupRef1ID = "hx2qf1w11qvz1x3k471c5i8xw"
56 changes: 56 additions & 0 deletions localstate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package localstate

import (
"encoding/json"
"os"
"path/filepath"

"github.com/pkg/errors"
)

func (ls *LocalState) MigrateIfNeeded() error {
currentVersion := ls.readVersion()
if currentVersion == version {
return nil
}
migrations := map[int]func(*LocalState) error{
2: (*LocalState).migration2,
}
for v := currentVersion + 1; v <= version; v++ {
migration, found := migrations[v]
if !found {
return errors.Errorf("localstate migration v%d not found", v)
}
if err := migration(ls); err != nil {
return errors.Wrapf(err, "localstate migration v%d failed", v)
}
}
return ls.writeVersion(version)
}

func (ls *LocalState) migration2() error {
return filepath.Walk(ls.GroupDir(), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
dt, err := os.ReadFile(path)
if err != nil {
return err
}
var stg StateGroup
if err := json.Unmarshal(dt, &stg); err != nil {
return err
}
mdt, err := json.Marshal(stg)
if err != nil {
return err
}
if err := os.WriteFile(path, mdt, 0600); err != nil {
return err
}
return nil
})
}
7 changes: 3 additions & 4 deletions util/tracing/trace.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ package tracing

import (
"context"
"os"
"strings"

"github.com/moby/buildkit/util/tracing/delegated"
@@ -13,7 +12,7 @@ import (
"go.opentelemetry.io/otel/trace"
)

func TraceCurrentCommand(ctx context.Context, name string) (context.Context, func(error), error) {
func TraceCurrentCommand(ctx context.Context, args []string, attrs ...attribute.KeyValue) (context.Context, func(error), error) {
opts := []sdktrace.TracerProviderOption{
sdktrace.WithResource(detect.Resource()),
sdktrace.WithBatcher(delegated.DefaultExporter),
@@ -25,8 +24,8 @@ func TraceCurrentCommand(ctx context.Context, name string) (context.Context, fun
}

tp := sdktrace.NewTracerProvider(opts...)
ctx, span := tp.Tracer("").Start(ctx, name, trace.WithAttributes(
attribute.String("command", strings.Join(os.Args, " ")),
ctx, span := tp.Tracer("").Start(ctx, strings.Join(args, " "), trace.WithAttributes(
attrs...,
))

return ctx, func(err error) {