From 4f7517115c8fc41c75b2657d4e632efd1e5ff8e2 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Wed, 21 Oct 2020 23:19:28 -0700 Subject: [PATCH] allow builder flag to switch to default instance Signed-off-by: Tonis Tiigi --- commands/rm.go | 2 +- commands/util.go | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/commands/rm.go b/commands/rm.go index f5916373..37c0fc3a 100644 --- a/commands/rm.go +++ b/commands/rm.go @@ -93,7 +93,7 @@ func stop(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, rm bo } func stopCurrent(ctx context.Context, dockerCli command.Cli, rm bool) error { - dis, err := getDefaultDrivers(ctx, dockerCli, "") + dis, err := getDefaultDrivers(ctx, dockerCli, false, "") if err != nil { return err } diff --git a/commands/util.go b/commands/util.go index ad9e2206..75f3d6d6 100644 --- a/commands/util.go +++ b/commands/util.go @@ -281,10 +281,29 @@ func clientForEndpoint(dockerCli command.Cli, name string) (dockerclient.APIClie } func getInstanceOrDefault(ctx context.Context, dockerCli command.Cli, instance, contextPathHash string) ([]build.DriverInfo, error) { + var defaultOnly bool + + if instance == "default" && instance != dockerCli.CurrentContext() { + return nil, errors.Errorf("use `docker --context=default buildx` to switch to default context") + } + if instance == "default" || instance == dockerCli.CurrentContext() { + instance = "" + defaultOnly = true + } + list, err := dockerCli.ContextStore().List() + if err != nil { + return nil, err + } + for _, l := range list { + if l.Name == instance { + return nil, errors.Errorf("use `docker --context=%s buildx` to switch to context %s", instance, instance) + } + } + if instance != "" { return getInstanceByName(ctx, dockerCli, instance, contextPathHash) } - return getDefaultDrivers(ctx, dockerCli, contextPathHash) + return getDefaultDrivers(ctx, dockerCli, defaultOnly, contextPathHash) } func getInstanceByName(ctx context.Context, dockerCli command.Cli, instance, contextPathHash string) ([]build.DriverInfo, error) { @@ -302,20 +321,22 @@ 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, contextPathHash string) ([]build.DriverInfo, error) { +func getDefaultDrivers(ctx context.Context, dockerCli command.Cli, defaultOnly bool, contextPathHash string) ([]build.DriverInfo, error) { txn, release, err := getStore(dockerCli) if err != nil { return nil, err } defer release() - ng, err := getCurrentInstance(txn, dockerCli) - if err != nil { - return nil, err - } + if !defaultOnly { + ng, err := getCurrentInstance(txn, dockerCli) + if err != nil { + return nil, err + } - if ng != nil { - return driversForNodeGroup(ctx, dockerCli, ng, contextPathHash) + if ng != nil { + return driversForNodeGroup(ctx, dockerCli, ng, contextPathHash) + } } d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), nil, nil, "", nil, contextPathHash)