From 78ae826d747faa3b4c4c268c9713d5441f8b1af1 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sun, 15 May 2022 08:30:31 +0200 Subject: [PATCH 1/2] ls: dedup instances from store and context Signed-off-by: CrazyMax --- commands/ls.go | 26 +++++++++++++++++--------- commands/util.go | 9 +++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/commands/ls.go b/commands/ls.go index 15bc6f30..6ca5cd5b 100644 --- a/commands/ls.go +++ b/commands/ls.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "sort" "strings" "text/tabwriter" "time" @@ -45,23 +46,30 @@ func runLs(dockerCli command.Cli, in lsOptions) error { builders[i] = &nginfo{ng: ng} } - list, err := dockerCli.ContextStore().List() + contexts, err := dockerCli.ContextStore().List() if err != nil { return err } - ctxbuilders := make([]*nginfo, len(list)) - for i, l := range list { - ctxbuilders[i] = &nginfo{ng: &store.NodeGroup{ - Name: l.Name, + sort.Slice(contexts, func(i, j int) bool { + return contexts[i].Name < contexts[j].Name + }) + for _, c := range contexts { + ngi := &nginfo{ng: &store.NodeGroup{ + Name: c.Name, Nodes: []store.Node{{ - Name: l.Name, - Endpoint: l.Name, + Name: c.Name, + Endpoint: c.Name, }}, }} + // if a context has the same name as an instance from the store, do not + // add it to the builders list. An instance from the store takes + // precedence over context builders. + if hasNodeGroup(builders, ngi) { + continue + } + builders = append(builders, ngi) } - builders = append(builders, ctxbuilders...) - eg, _ := errgroup.WithContext(ctx) for _, b := range builders { diff --git a/commands/util.go b/commands/util.go index a75d3420..9d90098e 100644 --- a/commands/util.go +++ b/commands/util.go @@ -390,6 +390,15 @@ func loadNodeGroupData(ctx context.Context, dockerCli command.Cli, ngi *nginfo) return nil } +func hasNodeGroup(list []*nginfo, ngi *nginfo) bool { + for _, l := range list { + if ngi.ng.Name == l.ng.Name { + return true + } + } + return false +} + func dockerAPI(dockerCli command.Cli) *api { return &api{dockerCli: dockerCli} } From 69d95cc847e937b447eba6ea91f4aa8bcecab7f9 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 19 May 2022 17:17:28 +0200 Subject: [PATCH 2/2] create: warn if instance name already exists as context builder Signed-off-by: CrazyMax --- commands/create.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/commands/create.go b/commands/create.go index f946b903..2a9c1296 100644 --- a/commands/create.go +++ b/commands/create.go @@ -101,6 +101,19 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error { } } + if !in.actionLeave && !in.actionAppend { + contexts, err := dockerCli.ContextStore().List() + if err != nil { + return err + } + for _, c := range contexts { + if c.Name == name { + logrus.Warnf("instance name %q already exists as context builder", name) + break + } + } + } + ng, err := txn.NodeGroupByName(name) if err != nil { if os.IsNotExist(errors.Cause(err)) {