Skip to content

Commit 80d8bdf

Browse files
Adds cluster-name flag instead of implied (#325)
Updates cluster commands to require an explicit flag if using a cluster name instead of a cluster id. This could be a breaking change, as previously you could pass either the id or name
1 parent 2144a4c commit 80d8bdf

File tree

2 files changed

+214
-74
lines changed

2 files changed

+214
-74
lines changed

cmd/cluster.go

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/rancher/cli/cliclient"
11+
"github.com/rancher/norman/types"
1112
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
1213
"github.com/sirupsen/logrus"
1314
"github.com/urfave/cli"
@@ -98,18 +99,26 @@ func ClusterCommand() cli.Command {
9899
Name: "import",
99100
Usage: "Import an existing Kubernetes cluster into a Rancher cluster",
100101
Description: importDescription,
101-
ArgsUsage: "[CLUSTERID CLUSTERNAME]",
102+
ArgsUsage: "CLUSTERID|--cluster-name CLUSTERNAME",
102103
Action: clusterImport,
103104
Flags: []cli.Flag{
105+
cli.StringFlag{
106+
Name: "cluster-name, name, n",
107+
Usage: "Specify cluster by `CLUSTERNAME` instead of CLUSTERID",
108+
},
104109
quietFlag,
105110
},
106111
},
107112
{
108113
Name: "add-node",
109114
Usage: "Outputs the docker command needed to add a node to an existing Rancher custom cluster",
110-
ArgsUsage: "[CLUSTERID CLUSTERNAME]",
115+
ArgsUsage: "CLUSTERID|--cluster-name CLUSTERNAME",
111116
Action: clusterAddNode,
112117
Flags: []cli.Flag{
118+
cli.StringFlag{
119+
Name: "cluster-name, name, n",
120+
Usage: "Specify cluster by `CLUSTERNAME` instead of CLUSTERID",
121+
},
113122
cli.StringSliceFlag{
114123
Name: "label",
115124
Usage: "Label to apply to a node in the format [name]=[value]",
@@ -136,22 +145,40 @@ func ClusterCommand() cli.Command {
136145
{
137146
Name: "delete",
138147
Aliases: []string{"rm"},
139-
Usage: "Delete a cluster",
140-
ArgsUsage: "[CLUSTERID/CLUSTERNAME...]",
148+
Usage: "Delete one or more clusters",
149+
ArgsUsage: "CLUSTERID|--cluster-name CLUSTERNAME",
141150
Action: clusterDelete,
151+
Flags: []cli.Flag{
152+
cli.StringSliceFlag{
153+
Name: "cluster-name, name, n",
154+
Usage: "Specify cluster by `CLUSTERNAME` instead of CLUSTERID. Supply multiple times for multiple cluster names.",
155+
},
156+
},
142157
},
143158
{
144159
Name: "export",
145160
Usage: "Export a cluster",
146-
ArgsUsage: "[CLUSTERID/CLUSTERNAME...]",
161+
ArgsUsage: "CLUSTERID|--cluster-name CLUSTERNAME",
147162
Action: clusterExport,
163+
Flags: []cli.Flag{
164+
cli.StringFlag{
165+
Name: "cluster-name, name, n",
166+
Usage: "Specify cluster by `CLUSTERNAME` instead of CLUSTERID",
167+
},
168+
},
148169
},
149170
{
150171
Name: "kubeconfig",
151172
Aliases: []string{"kf"},
152-
Usage: "Return the kube config used to access the cluster",
153-
ArgsUsage: "[CLUSTERID CLUSTERNAME]",
173+
Usage: "Return the kubeconfig used to access the cluster",
174+
ArgsUsage: "CLUSTERID|--cluster-name CLUSTERNAME",
154175
Action: clusterKubeConfig,
176+
Flags: []cli.Flag{
177+
cli.StringFlag{
178+
Name: "cluster-name, name, n",
179+
Usage: "Specify cluster by `CLUSTERNAME` instead of CLUSTERID",
180+
},
181+
},
155182
},
156183
{
157184
Name: "add-member-role",
@@ -284,7 +311,7 @@ func clusterCreate(ctx *cli.Context) error {
284311
}
285312

286313
func clusterImport(ctx *cli.Context) error {
287-
if ctx.NArg() == 0 {
314+
if ctx.NArg() == 0 && ctx.String("cluster-name") == "" {
288315
return cli.ShowSubcommandHelp(ctx)
289316
}
290317

@@ -293,7 +320,12 @@ func clusterImport(ctx *cli.Context) error {
293320
return err
294321
}
295322

296-
resource, err := Lookup(c, ctx.Args().First(), "cluster")
323+
var resource *types.Resource
324+
if name := ctx.String("cluster-name"); name != "" {
325+
resource, err = LookupByName(c, name, "cluster")
326+
} else {
327+
resource, err = LookupByID(c, ctx.Args().First(), "cluster")
328+
}
297329
if err != nil {
298330
return err
299331
}
@@ -325,7 +357,7 @@ func clusterImport(ctx *cli.Context) error {
325357

326358
// clusterAddNode prints the command needed to add a node to a cluster
327359
func clusterAddNode(ctx *cli.Context) error {
328-
if ctx.NArg() == 0 {
360+
if ctx.NArg() == 0 && ctx.String("cluster-name") == "" {
329361
return cli.ShowSubcommandHelp(ctx)
330362
}
331363

@@ -334,7 +366,12 @@ func clusterAddNode(ctx *cli.Context) error {
334366
return err
335367
}
336368

337-
resource, err := Lookup(c, ctx.Args().First(), "cluster")
369+
var resource *types.Resource
370+
if name := ctx.String("cluster-name"); name != "" {
371+
resource, err = LookupByName(c, name, "cluster")
372+
} else {
373+
resource, err = LookupByID(c, ctx.Args().First(), "cluster")
374+
}
338375
if err != nil {
339376
return err
340377
}
@@ -400,18 +437,43 @@ func clusterAddNode(ctx *cli.Context) error {
400437
}
401438

402439
func clusterDelete(ctx *cli.Context) error {
403-
if ctx.NArg() == 0 {
440+
if ctx.NArg() == 0 && len(ctx.StringSlice("cluster-name")) == 0 {
404441
return cli.ShowSubcommandHelp(ctx)
405442
}
406443

444+
clusterNames := ctx.StringSlice("cluster-name")
445+
clusterIDs := ctx.Args()
446+
407447
c, err := GetClient(ctx)
408448
if err != nil {
409449
return err
410450
}
451+
schemaclient, err := lookupSchemaClient(c, "cluster")
452+
if err != nil {
453+
return err
454+
}
455+
456+
for _, clusterID := range clusterIDs {
457+
458+
resource, err := lookupByIDWithClient(schemaclient, clusterID, "cluster")
459+
if err != nil {
460+
return err
461+
}
462+
463+
cluster, err := getClusterByID(c, resource.ID)
464+
if err != nil {
465+
return err
466+
}
411467

412-
for _, cluster := range ctx.Args() {
468+
err = c.ManagementClient.Cluster.Delete(cluster)
469+
if err != nil {
470+
return err
471+
}
472+
fmt.Printf("Cluster with ID %s deleted\n", clusterID)
473+
}
413474

414-
resource, err := Lookup(c, cluster, "cluster")
475+
for _, clusterName := range clusterNames {
476+
resource, err := lookupByNameWithClient(schemaclient, clusterName, "cluster")
415477
if err != nil {
416478
return err
417479
}
@@ -425,13 +487,14 @@ func clusterDelete(ctx *cli.Context) error {
425487
if err != nil {
426488
return err
427489
}
490+
fmt.Printf("Cluster with name %s deleted\n", clusterName)
428491
}
429492

430493
return nil
431494
}
432495

433496
func clusterExport(ctx *cli.Context) error {
434-
if ctx.NArg() == 0 {
497+
if ctx.NArg() == 0 && ctx.String("cluster-name") == "" {
435498
return cli.ShowSubcommandHelp(ctx)
436499
}
437500

@@ -440,7 +503,12 @@ func clusterExport(ctx *cli.Context) error {
440503
return err
441504
}
442505

443-
resource, err := Lookup(c, ctx.Args().First(), "cluster")
506+
var resource *types.Resource
507+
if name := ctx.String("cluster-name"); name != "" {
508+
resource, err = LookupByName(c, name, "cluster")
509+
} else {
510+
resource, err = LookupByID(c, ctx.Args().First(), "cluster")
511+
}
444512
if err != nil {
445513
return err
446514
}
@@ -464,7 +532,7 @@ func clusterExport(ctx *cli.Context) error {
464532
}
465533

466534
func clusterKubeConfig(ctx *cli.Context) error {
467-
if ctx.NArg() == 0 {
535+
if ctx.NArg() == 0 && ctx.String("cluster-name") == "" {
468536
return cli.ShowSubcommandHelp(ctx)
469537
}
470538

@@ -473,7 +541,12 @@ func clusterKubeConfig(ctx *cli.Context) error {
473541
return err
474542
}
475543

476-
resource, err := Lookup(c, ctx.Args().First(), "cluster")
544+
var resource *types.Resource
545+
if name := ctx.String("cluster-name"); name != "" {
546+
resource, err = LookupByName(c, name, "cluster")
547+
} else {
548+
resource, err = LookupByID(c, ctx.Args().First(), "cluster")
549+
}
477550
if err != nil {
478551
return err
479552
}

0 commit comments

Comments
 (0)