diff --git a/build/build.go b/build/build.go index 34e095e5..047ac9be 100644 --- a/build/build.go +++ b/build/build.go @@ -21,7 +21,6 @@ import ( "github.com/docker/buildx/driver" "github.com/docker/buildx/util/imagetools" "github.com/docker/buildx/util/progress" - clitypes "github.com/docker/cli/cli/config/types" "github.com/docker/cli/opts" "github.com/docker/distribution/reference" "github.com/docker/docker/api/types" @@ -86,10 +85,7 @@ type DriverInfo struct { Name string Platform []specs.Platform Err error -} - -type Auth interface { - GetAuthConfig(registryHostname string) (clitypes.AuthConfig, error) + ImageOpt imagetools.Opt } type DockerAPI interface { @@ -189,8 +185,8 @@ func splitToDriverPairs(availablePlatforms map[string]int, opt map[string]Option return m } -func resolveDrivers(ctx context.Context, drivers []DriverInfo, auth Auth, opt map[string]Options, pw progress.Writer) (map[string][]driverPair, []*client.Client, error) { - dps, clients, err := resolveDriversBase(ctx, drivers, auth, opt, pw) +func resolveDrivers(ctx context.Context, drivers []DriverInfo, opt map[string]Options, pw progress.Writer) (map[string][]driverPair, []*client.Client, error) { + dps, clients, err := resolveDriversBase(ctx, drivers, opt, pw) if err != nil { return nil, nil, err } @@ -230,7 +226,7 @@ func resolveDrivers(ctx context.Context, drivers []DriverInfo, auth Auth, opt ma return dps, clients, nil } -func resolveDriversBase(ctx context.Context, drivers []DriverInfo, auth Auth, opt map[string]Options, pw progress.Writer) (map[string][]driverPair, []*client.Client, error) { +func resolveDriversBase(ctx context.Context, drivers []DriverInfo, opt map[string]Options, pw progress.Writer) (map[string][]driverPair, []*client.Client, error) { availablePlatforms := map[string]int{} for i, d := range drivers { for _, p := range d.Platform { @@ -583,7 +579,7 @@ func toSolveOpt(ctx context.Context, d driver.Driver, multiDriver bool, opt Opti return &so, releaseF, nil } -func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, docker DockerAPI, auth Auth, configDir string, w progress.Writer) (resp map[string]*client.SolveResponse, err error) { +func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, docker DockerAPI, configDir string, w progress.Writer) (resp map[string]*client.SolveResponse, err error) { if len(drivers) == 0 { return nil, errors.Errorf("driver required for build") } @@ -610,7 +606,7 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do } } - m, clients, err := resolveDrivers(ctx, drivers, auth, opt, w) + m, clients, err := resolveDrivers(ctx, drivers, opt, w) if err != nil { return nil, err } @@ -731,9 +727,12 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do } } if len(descs) > 0 { - itpull := imagetools.New(imagetools.Opt{ - Auth: auth, - }) + var imageopt imagetools.Opt + for _, dp := range dps { + imageopt = drivers[dp.driverIndex].ImageOpt + break + } + itpull := imagetools.New(imageopt) names := strings.Split(pushNames, ",") dt, desc, err := itpull.Combine(ctx, names[0], descs) @@ -746,9 +745,7 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do } } - itpush := imagetools.New(imagetools.Opt{ - Auth: auth, - }) + itpush := imagetools.New(imageopt) for _, n := range names { nn, err := reference.ParseNormalizedNamed(n) diff --git a/commands/bake.go b/commands/bake.go index 6f801b8c..c93e5cef 100644 --- a/commands/bake.go +++ b/commands/bake.go @@ -156,7 +156,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error return nil } - resp, err := build.Build(ctx, dis, bo, dockerAPI(dockerCli), dockerCli.ConfigFile(), confutil.ConfigDir(dockerCli), printer) + resp, err := build.Build(ctx, dis, bo, dockerAPI(dockerCli), confutil.ConfigDir(dockerCli), printer) if err != nil { return err } diff --git a/commands/build.go b/commands/build.go index 1499e3d6..bfefd707 100644 --- a/commands/build.go +++ b/commands/build.go @@ -225,7 +225,7 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu printer := progress.NewPrinter(ctx2, os.Stderr, progressMode) - resp, err := build.Build(ctx, dis, opts, dockerAPI(dockerCli), dockerCli.ConfigFile(), confutil.ConfigDir(dockerCli), printer) + resp, err := build.Build(ctx, dis, opts, dockerAPI(dockerCli), confutil.ConfigDir(dockerCli), printer) err1 := printer.Wait() if err == nil { err = err1 diff --git a/commands/create.go b/commands/create.go index e400a8cc..4bd29c58 100644 --- a/commands/create.go +++ b/commands/create.go @@ -12,6 +12,7 @@ import ( "github.com/docker/buildx/driver" "github.com/docker/buildx/store" + "github.com/docker/buildx/store/storeutil" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/google/shlex" @@ -74,7 +75,7 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error { return errors.Errorf("failed to find driver %q", in.driver) } - txn, release, err := getStore(dockerCli) + txn, release, err := storeutil.GetStore(dockerCli) if err != nil { return err } @@ -142,7 +143,7 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error { return errors.Errorf("could not create a builder instance with TLS data loaded from environment. Please use `docker context create ` to create a context for current environment and then create a builder instance with `docker buildx create `") } - ep, err = getCurrentEndpoint(dockerCli) + ep, err = storeutil.GetCurrentEndpoint(dockerCli) if err != nil { return err } @@ -174,7 +175,7 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error { } if in.use && ep != "" { - current, err := getCurrentEndpoint(dockerCli) + current, err := storeutil.GetCurrentEndpoint(dockerCli) if err != nil { return err } diff --git a/commands/imagetools/create.go b/commands/imagetools/create.go index a2078078..cbcb84b1 100644 --- a/commands/imagetools/create.go +++ b/commands/imagetools/create.go @@ -6,6 +6,8 @@ import ( "io/ioutil" "strings" + "github.com/docker/buildx/store" + "github.com/docker/buildx/store/storeutil" "github.com/docker/buildx/util/imagetools" "github.com/docker/cli/cli/command" "github.com/docker/distribution/reference" @@ -18,6 +20,7 @@ import ( ) type createOptions struct { + builder string files []string tags []string dryrun bool @@ -101,9 +104,32 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error { ctx := appcontext.Context() - r := imagetools.New(imagetools.Opt{ - Auth: dockerCli.ConfigFile(), - }) + txn, release, err := storeutil.GetStore(dockerCli) + if err != nil { + return err + } + defer release() + + var ng *store.NodeGroup + + if in.builder != "" { + ng, err = storeutil.GetNodeGroup(txn, dockerCli, in.builder) + if err != nil { + return err + } + } else { + ng, err = storeutil.GetCurrentInstance(txn, dockerCli) + if err != nil { + return err + } + } + + imageopt, err := storeutil.GetImageConfig(dockerCli, ng) + if err != nil { + return err + } + + r := imagetools.New(imageopt) if sourceRefs { eg, ctx2 := errgroup.WithContext(ctx) @@ -152,9 +178,7 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error { } // new resolver cause need new auth - r = imagetools.New(imagetools.Opt{ - Auth: dockerCli.ConfigFile(), - }) + r = imagetools.New(imageopt) for _, t := range tags { if err := r.Push(ctx, t, desc, dt); err != nil { @@ -224,13 +248,14 @@ func parseSource(in string) (*src, error) { return &s, nil } -func createCmd(dockerCli command.Cli) *cobra.Command { +func createCmd(dockerCli command.Cli, opts RootOptions) *cobra.Command { var options createOptions cmd := &cobra.Command{ Use: "create [OPTIONS] [SOURCE] [SOURCE...]", Short: "Create a new image based on source images", RunE: func(cmd *cobra.Command, args []string) error { + options.builder = opts.Builder return runCreate(dockerCli, options, args) }, } @@ -242,8 +267,6 @@ func createCmd(dockerCli command.Cli) *cobra.Command { flags.BoolVar(&options.dryrun, "dry-run", false, "Show final image instead of pushing") flags.BoolVar(&options.actionAppend, "append", false, "Append to existing manifest") - _ = flags - return cmd } diff --git a/commands/imagetools/inspect.go b/commands/imagetools/inspect.go index d852d82a..9f06793a 100644 --- a/commands/imagetools/inspect.go +++ b/commands/imagetools/inspect.go @@ -5,6 +5,8 @@ import ( "os" "github.com/containerd/containerd/images" + "github.com/docker/buildx/store" + "github.com/docker/buildx/store/storeutil" "github.com/docker/buildx/util/imagetools" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" @@ -14,15 +16,38 @@ import ( ) type inspectOptions struct { - raw bool + raw bool + builder string } func runInspect(dockerCli command.Cli, in inspectOptions, name string) error { ctx := appcontext.Context() - r := imagetools.New(imagetools.Opt{ - Auth: dockerCli.ConfigFile(), - }) + txn, release, err := storeutil.GetStore(dockerCli) + if err != nil { + return err + } + defer release() + + var ng *store.NodeGroup + + if in.builder != "" { + ng, err = storeutil.GetNodeGroup(txn, dockerCli, in.builder) + if err != nil { + return err + } + } else { + ng, err = storeutil.GetCurrentInstance(txn, dockerCli) + if err != nil { + return err + } + } + + imageopt, err := storeutil.GetImageConfig(dockerCli, ng) + if err != nil { + return err + } + r := imagetools.New(imageopt) dt, desc, err := r.Get(ctx, name) if err != nil { @@ -46,7 +71,7 @@ func runInspect(dockerCli command.Cli, in inspectOptions, name string) error { return nil } -func inspectCmd(dockerCli command.Cli) *cobra.Command { +func inspectCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command { var options inspectOptions cmd := &cobra.Command{ @@ -54,6 +79,7 @@ func inspectCmd(dockerCli command.Cli) *cobra.Command { Short: "Show details of image in the registry", Args: cli.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + options.builder = rootOpts.Builder return runInspect(dockerCli, options, args[0]) }, } @@ -62,7 +88,5 @@ func inspectCmd(dockerCli command.Cli) *cobra.Command { flags.BoolVar(&options.raw, "raw", false, "Show original JSON manifest") - _ = flags - return cmd } diff --git a/commands/imagetools/root.go b/commands/imagetools/root.go index ad1aa563..df4a87e2 100644 --- a/commands/imagetools/root.go +++ b/commands/imagetools/root.go @@ -5,15 +5,19 @@ import ( "github.com/spf13/cobra" ) -func RootCmd(dockerCli command.Cli) *cobra.Command { +type RootOptions struct { + Builder string +} + +func RootCmd(dockerCli command.Cli, opts RootOptions) *cobra.Command { cmd := &cobra.Command{ Use: "imagetools", Short: "Commands to work on images in registry", } cmd.AddCommand( - inspectCmd(dockerCli), - createCmd(dockerCli), + inspectCmd(dockerCli, opts), + createCmd(dockerCli, opts), ) return cmd diff --git a/commands/inspect.go b/commands/inspect.go index 902750a6..4e9e826e 100644 --- a/commands/inspect.go +++ b/commands/inspect.go @@ -9,6 +9,7 @@ import ( "time" "github.com/docker/buildx/store" + "github.com/docker/buildx/store/storeutil" "github.com/docker/buildx/util/platformutil" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" @@ -24,7 +25,7 @@ type inspectOptions struct { func runInspect(dockerCli command.Cli, in inspectOptions) error { ctx := appcontext.Context() - txn, release, err := getStore(dockerCli) + txn, release, err := storeutil.GetStore(dockerCli) if err != nil { return err } @@ -33,12 +34,12 @@ func runInspect(dockerCli command.Cli, in inspectOptions) error { var ng *store.NodeGroup if in.builder != "" { - ng, err = getNodeGroup(txn, dockerCli, in.builder) + ng, err = storeutil.GetNodeGroup(txn, dockerCli, in.builder) if err != nil { return err } } else { - ng, err = getCurrentInstance(txn, dockerCli) + ng, err = storeutil.GetCurrentInstance(txn, dockerCli) if err != nil { return err } diff --git a/commands/ls.go b/commands/ls.go index 9aeae340..efc20d5d 100644 --- a/commands/ls.go +++ b/commands/ls.go @@ -10,6 +10,7 @@ import ( "time" "github.com/docker/buildx/store" + "github.com/docker/buildx/store/storeutil" "github.com/docker/buildx/util/platformutil" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" @@ -24,7 +25,7 @@ type lsOptions struct { func runLs(dockerCli command.Cli, in lsOptions) error { ctx := appcontext.Context() - txn, release, err := getStore(dockerCli) + txn, release, err := storeutil.GetStore(dockerCli) if err != nil { return err } @@ -79,7 +80,7 @@ func runLs(dockerCli command.Cli, in lsOptions) error { } currentName := "default" - current, err := getCurrentInstance(txn, dockerCli) + current, err := storeutil.GetCurrentInstance(txn, dockerCli) if err != nil { return err } diff --git a/commands/rm.go b/commands/rm.go index b09b99e3..1acb1ab7 100644 --- a/commands/rm.go +++ b/commands/rm.go @@ -4,6 +4,7 @@ import ( "context" "github.com/docker/buildx/store" + "github.com/docker/buildx/store/storeutil" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/moby/buildkit/util/appcontext" @@ -18,14 +19,14 @@ type rmOptions struct { func runRm(dockerCli command.Cli, in rmOptions) error { ctx := appcontext.Context() - txn, release, err := getStore(dockerCli) + txn, release, err := storeutil.GetStore(dockerCli) if err != nil { return err } defer release() if in.builder != "" { - ng, err := getNodeGroup(txn, dockerCli, in.builder) + ng, err := storeutil.GetNodeGroup(txn, dockerCli, in.builder) if err != nil { return err } @@ -36,7 +37,7 @@ func runRm(dockerCli command.Cli, in rmOptions) error { return err1 } - ng, err := getCurrentInstance(txn, dockerCli) + ng, err := storeutil.GetCurrentInstance(txn, dockerCli) if err != nil { return err } diff --git a/commands/root.go b/commands/root.go index 7191722d..10993d9b 100644 --- a/commands/root.go +++ b/commands/root.go @@ -48,7 +48,7 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) { versionCmd(dockerCli), pruneCmd(dockerCli, opts), duCmd(dockerCli, opts), - imagetoolscmd.RootCmd(dockerCli), + imagetoolscmd.RootCmd(dockerCli, imagetoolscmd.RootOptions{Builder: opts.builder}), ) } diff --git a/commands/stop.go b/commands/stop.go index 2f1efc67..43b6c570 100644 --- a/commands/stop.go +++ b/commands/stop.go @@ -4,6 +4,7 @@ import ( "context" "github.com/docker/buildx/store" + "github.com/docker/buildx/store/storeutil" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/moby/buildkit/util/appcontext" @@ -17,14 +18,14 @@ type stopOptions struct { func runStop(dockerCli command.Cli, in stopOptions) error { ctx := appcontext.Context() - txn, release, err := getStore(dockerCli) + txn, release, err := storeutil.GetStore(dockerCli) if err != nil { return err } defer release() if in.builder != "" { - ng, err := getNodeGroup(txn, dockerCli, in.builder) + ng, err := storeutil.GetNodeGroup(txn, dockerCli, in.builder) if err != nil { return err } @@ -34,7 +35,7 @@ func runStop(dockerCli command.Cli, in stopOptions) error { return nil } - ng, err := getCurrentInstance(txn, dockerCli) + ng, err := storeutil.GetCurrentInstance(txn, dockerCli) if err != nil { return err } diff --git a/commands/use.go b/commands/use.go index 530d7552..425857a1 100644 --- a/commands/use.go +++ b/commands/use.go @@ -3,6 +3,7 @@ package commands import ( "os" + "github.com/docker/buildx/store/storeutil" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/pkg/errors" @@ -16,7 +17,7 @@ type useOptions struct { } func runUse(dockerCli command.Cli, in useOptions) error { - txn, release, err := getStore(dockerCli) + txn, release, err := storeutil.GetStore(dockerCli) if err != nil { return err } @@ -28,7 +29,7 @@ func runUse(dockerCli command.Cli, in useOptions) error { return errors.Errorf("run `docker context use default` to switch to default context") } if in.builder == "default" || in.builder == dockerCli.CurrentContext() { - ep, err := getCurrentEndpoint(dockerCli) + ep, err := storeutil.GetCurrentEndpoint(dockerCli) if err != nil { return err } @@ -51,7 +52,7 @@ func runUse(dockerCli command.Cli, in useOptions) error { return errors.Wrapf(err, "failed to find instance %q", in.builder) } - ep, err := getCurrentEndpoint(dockerCli) + ep, err := storeutil.GetCurrentEndpoint(dockerCli) if err != nil { return err } diff --git a/commands/util.go b/commands/util.go index 4b94d45c..0e19e9b2 100644 --- a/commands/util.go +++ b/commands/util.go @@ -9,7 +9,7 @@ import ( "github.com/docker/buildx/build" "github.com/docker/buildx/driver" "github.com/docker/buildx/store" - "github.com/docker/buildx/util/confutil" + "github.com/docker/buildx/store/storeutil" "github.com/docker/buildx/util/platformutil" "github.com/docker/buildx/util/progress" "github.com/docker/cli/cli/command" @@ -25,53 +25,9 @@ import ( "k8s.io/client-go/tools/clientcmd" ) -// getStore returns current builder instance store -func getStore(dockerCli command.Cli) (*store.Txn, func(), error) { - s, err := store.New(confutil.ConfigDir(dockerCli)) - if err != nil { - return nil, nil, err - } - return s.Txn() -} - -// getCurrentEndpoint returns the current default endpoint value -func getCurrentEndpoint(dockerCli command.Cli) (string, error) { - name := dockerCli.CurrentContext() - if name != "default" { - return name, nil - } - de, err := getDockerEndpoint(dockerCli, name) - if err != nil { - return "", errors.Errorf("docker endpoint for %q not found", name) - } - return de, nil -} - -// getDockerEndpoint returns docker endpoint string for given context -func getDockerEndpoint(dockerCli command.Cli, name string) (string, error) { - list, err := dockerCli.ContextStore().List() - if err != nil { - return "", err - } - for _, l := range list { - if l.Name == name { - ep, ok := l.Endpoints["docker"] - if !ok { - return "", errors.Errorf("context %q does not have a Docker endpoint", name) - } - typed, ok := ep.(docker.EndpointMeta) - if !ok { - return "", errors.Errorf("endpoint %q is not of type EndpointMeta, %T", ep, ep) - } - return typed.Host, nil - } - } - return "", nil -} - // validateEndpoint validates that endpoint is either a context or a docker host func validateEndpoint(dockerCli command.Cli, ep string) (string, error) { - de, err := getDockerEndpoint(dockerCli, ep) + de, err := storeutil.GetDockerEndpoint(dockerCli, ep) if err == nil && de != "" { if ep == "default" { return de, nil @@ -85,60 +41,6 @@ func validateEndpoint(dockerCli command.Cli, ep string) (string, error) { return h, nil } -// getCurrentInstance finds the current builder instance -func getCurrentInstance(txn *store.Txn, dockerCli command.Cli) (*store.NodeGroup, error) { - ep, err := getCurrentEndpoint(dockerCli) - if err != nil { - return nil, err - } - ng, err := txn.Current(ep) - if err != nil { - return nil, err - } - if ng == nil { - ng, _ = getNodeGroup(txn, dockerCli, dockerCli.CurrentContext()) - } - - return ng, nil -} - -// getNodeGroup returns nodegroup based on the name -func getNodeGroup(txn *store.Txn, dockerCli command.Cli, name string) (*store.NodeGroup, error) { - ng, err := txn.NodeGroupByName(name) - if err != nil { - if !os.IsNotExist(errors.Cause(err)) { - return nil, err - } - } - if ng != nil { - return ng, nil - } - - if name == "default" { - name = dockerCli.CurrentContext() - } - - list, err := dockerCli.ContextStore().List() - if err != nil { - return nil, err - } - for _, l := range list { - if l.Name == name { - return &store.NodeGroup{ - Name: "default", - Nodes: []store.Node{ - { - Name: "default", - Endpoint: name, - }, - }, - }, nil - } - } - - return nil, errors.Errorf("no builder %q found", name) -} - // driversForNodeGroup returns drivers for a nodegroup instance func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, contextPathHash string) ([]build.DriverInfo, error) { eg, _ := errgroup.WithContext(ctx) @@ -162,6 +64,10 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N } ng.Driver = f.Name() } + imageopt, err := storeutil.GetImageConfig(dockerCli, ng) + if err != nil { + return nil, err + } for i, n := range ng.Nodes { func(i int, n store.Node) { @@ -211,12 +117,13 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N } } - d, err := driver.GetDriver(ctx, "buildx_buildkit_"+n.Name, f, dockerapi, dockerCli.ConfigFile(), kcc, n.Flags, n.Files, n.DriverOpts, n.Platforms, contextPathHash) + d, err := driver.GetDriver(ctx, "buildx_buildkit_"+n.Name, f, dockerapi, imageopt.Auth, kcc, n.Flags, n.Files, n.DriverOpts, n.Platforms, contextPathHash) if err != nil { di.Err = err return nil } di.Driver = d + di.ImageOpt = imageopt return nil }) }(i, n) @@ -314,7 +221,7 @@ func getInstanceOrDefault(ctx context.Context, dockerCli command.Cli, instance, } func getInstanceByName(ctx context.Context, dockerCli command.Cli, instance, contextPathHash string) ([]build.DriverInfo, error) { - txn, release, err := getStore(dockerCli) + txn, release, err := storeutil.GetStore(dockerCli) if err != nil { return nil, err } @@ -329,14 +236,14 @@ func getInstanceByName(ctx context.Context, dockerCli command.Cli, instance, con // getDefaultDrivers returns drivers based on current cli config func getDefaultDrivers(ctx context.Context, dockerCli command.Cli, defaultOnly bool, contextPathHash string) ([]build.DriverInfo, error) { - txn, release, err := getStore(dockerCli) + txn, release, err := storeutil.GetStore(dockerCli) if err != nil { return nil, err } defer release() if !defaultOnly { - ng, err := getCurrentInstance(txn, dockerCli) + ng, err := storeutil.GetCurrentInstance(txn, dockerCli) if err != nil { return nil, err } @@ -346,14 +253,20 @@ func getDefaultDrivers(ctx context.Context, dockerCli command.Cli, defaultOnly b } } - d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), dockerCli.ConfigFile(), nil, nil, nil, nil, nil, contextPathHash) + imageopt, err := storeutil.GetImageConfig(dockerCli, nil) + if err != nil { + return nil, err + } + + d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), imageopt.Auth, nil, nil, nil, nil, nil, contextPathHash) if err != nil { return nil, err } return []build.DriverInfo{ { - Name: "default", - Driver: d, + Name: "default", + Driver: d, + ImageOpt: imageopt, }, }, nil } diff --git a/store/storeutil/storeutil.go b/store/storeutil/storeutil.go new file mode 100644 index 00000000..ea97cb99 --- /dev/null +++ b/store/storeutil/storeutil.go @@ -0,0 +1,116 @@ +package storeutil + +import ( + "os" + + "github.com/docker/buildx/store" + "github.com/docker/buildx/util/confutil" + "github.com/docker/buildx/util/imagetools" + "github.com/docker/cli/cli/command" + "github.com/docker/cli/cli/context/docker" + "github.com/pkg/errors" +) + +// GetStore returns current builder instance store +func GetStore(dockerCli command.Cli) (*store.Txn, func(), error) { + s, err := store.New(confutil.ConfigDir(dockerCli)) + if err != nil { + return nil, nil, err + } + return s.Txn() +} + +// GetCurrentEndpoint returns the current default endpoint value +func GetCurrentEndpoint(dockerCli command.Cli) (string, error) { + name := dockerCli.CurrentContext() + if name != "default" { + return name, nil + } + de, err := GetDockerEndpoint(dockerCli, name) + if err != nil { + return "", errors.Errorf("docker endpoint for %q not found", name) + } + return de, nil +} + +// GetDockerEndpoint returns docker endpoint string for given context +func GetDockerEndpoint(dockerCli command.Cli, name string) (string, error) { + list, err := dockerCli.ContextStore().List() + if err != nil { + return "", err + } + for _, l := range list { + if l.Name == name { + ep, ok := l.Endpoints["docker"] + if !ok { + return "", errors.Errorf("context %q does not have a Docker endpoint", name) + } + typed, ok := ep.(docker.EndpointMeta) + if !ok { + return "", errors.Errorf("endpoint %q is not of type EndpointMeta, %T", ep, ep) + } + return typed.Host, nil + } + } + return "", nil +} + +// GetCurrentInstance finds the current builder instance +func GetCurrentInstance(txn *store.Txn, dockerCli command.Cli) (*store.NodeGroup, error) { + ep, err := GetCurrentEndpoint(dockerCli) + if err != nil { + return nil, err + } + ng, err := txn.Current(ep) + if err != nil { + return nil, err + } + if ng == nil { + ng, _ = GetNodeGroup(txn, dockerCli, dockerCli.CurrentContext()) + } + + return ng, nil +} + +// GetNodeGroup returns nodegroup based on the name +func GetNodeGroup(txn *store.Txn, dockerCli command.Cli, name string) (*store.NodeGroup, error) { + ng, err := txn.NodeGroupByName(name) + if err != nil { + if !os.IsNotExist(errors.Cause(err)) { + return nil, err + } + } + if ng != nil { + return ng, nil + } + + if name == "default" { + name = dockerCli.CurrentContext() + } + + list, err := dockerCli.ContextStore().List() + if err != nil { + return nil, err + } + for _, l := range list { + if l.Name == name { + return &store.NodeGroup{ + Name: "default", + Nodes: []store.Node{ + { + Name: "default", + Endpoint: name, + }, + }, + }, nil + } + } + + return nil, errors.Errorf("no builder %q found", name) +} + +func GetImageConfig(dockerCli command.Cli, ng *store.NodeGroup) (opt imagetools.Opt, err error) { + opt.Auth = dockerCli.ConfigFile() + + return opt, nil +} diff --git a/util/imagetools/inspect.go b/util/imagetools/inspect.go index 55da1184..d8ae52f7 100644 --- a/util/imagetools/inspect.go +++ b/util/imagetools/inspect.go @@ -12,6 +12,7 @@ import ( "github.com/containerd/containerd/remotes/docker" clitypes "github.com/docker/cli/cli/config/types" "github.com/docker/distribution/reference" + registryconfig "github.com/moby/buildkit/util/resolver/config" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) @@ -20,7 +21,8 @@ type Auth interface { } type Opt struct { - Auth Auth + Auth Auth + RegistryConfig map[string]registryconfig.RegistryConfig } type Resolver struct { diff --git a/vendor/github.com/moby/buildkit/util/resolver/config/config.go b/vendor/github.com/moby/buildkit/util/resolver/config/config.go new file mode 100644 index 00000000..12525503 --- /dev/null +++ b/vendor/github.com/moby/buildkit/util/resolver/config/config.go @@ -0,0 +1,15 @@ +package config + +type RegistryConfig struct { + Mirrors []string `toml:"mirrors"` + PlainHTTP *bool `toml:"http"` + Insecure *bool `toml:"insecure"` + RootCAs []string `toml:"ca"` + KeyPairs []TLSKeyPair `toml:"keypair"` + TLSConfigDir []string `toml:"tlsconfigdir"` +} + +type TLSKeyPair struct { + Key string `toml:"key"` + Certificate string `toml:"cert"` +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 0e7057b0..26161a92 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -325,6 +325,7 @@ github.com/moby/buildkit/util/grpcerrors github.com/moby/buildkit/util/progress github.com/moby/buildkit/util/progress/progressui github.com/moby/buildkit/util/progress/progresswriter +github.com/moby/buildkit/util/resolver/config github.com/moby/buildkit/util/sshutil github.com/moby/buildkit/util/stack github.com/moby/buildkit/util/system