Skip to content

Commit 9cd6df2

Browse files
authored
Merge pull request #346 from replicatedhq/joshd/sc-89457/cli-make-cluster-rm-accept-name
Joshd/sc 89457/cli make cluster rm accept name
2 parents 02cc272 + a469767 commit 9cd6df2

4 files changed

Lines changed: 105 additions & 31 deletions

File tree

cli/cmd/cluster.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package cmd
22

33
import (
4+
"strings"
5+
46
"github.com/pkg/errors"
7+
"github.com/replicatedhq/replicated/pkg/kotsclient"
58
"github.com/spf13/cobra"
69
)
710

@@ -11,11 +14,27 @@ var (
1114

1215
func (r *runners) InitClusterCommand(parent *cobra.Command) *cobra.Command {
1316
cmd := &cobra.Command{
14-
Use: "cluster",
15-
Short: "Manage test clusters",
16-
Long: ``,
17+
Use: "cluster",
18+
Short: "Manage test clusters",
19+
Long: ``,
1720
}
1821
parent.AddCommand(cmd)
1922

2023
return cmd
2124
}
25+
26+
func parseTags(tags []string) ([]kotsclient.ClusterTag, error) {
27+
clusterTags := []kotsclient.ClusterTag{}
28+
for _, tag := range tags {
29+
tagParts := strings.SplitN(tag, "=", 2)
30+
if len(tagParts) != 2 {
31+
return nil, errors.Errorf("invalid tag format: %s", tag)
32+
}
33+
34+
clusterTags = append(clusterTags, kotsclient.ClusterTag{
35+
Key: tagParts[0],
36+
Value: tagParts[1],
37+
})
38+
}
39+
return clusterTags, nil
40+
}

cli/cmd/cluster_create.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"strings"
65
"time"
76

87
"github.com/moby/moby/pkg/namesgenerator"
@@ -51,17 +50,9 @@ func (r *runners) createCluster(_ *cobra.Command, args []string) error {
5150
r.args.createClusterName = generateClusterName()
5251
}
5352

54-
tags := []kotsclient.ClusterTag{}
55-
for _, tag := range r.args.createClusterTags {
56-
tagParts := strings.SplitN(tag, "=", 2)
57-
if len(tagParts) != 2 {
58-
return errors.Errorf("invalid tag format: %s", tag)
59-
}
60-
61-
tags = append(tags, kotsclient.ClusterTag{
62-
Key: tagParts[0],
63-
Value: tagParts[1],
64-
})
53+
tags, err := parseTags(r.args.createClusterTags)
54+
if err != nil {
55+
return errors.Wrap(err, "parse tags")
6556
}
6657

6758
opts := kotsclient.CreateClusterOpts{

cli/cmd/cluster_rm.go

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,105 @@ You can specify the --all flag to terminate all clusters.`,
1919
}
2020
parent.AddCommand(cmd)
2121

22+
cmd.Flags().StringArrayVar(&r.args.removeClusterNames, "name", []string{}, "Name of the cluster to remove (can be specified multiple times)")
23+
cmd.Flags().StringArrayVar(&r.args.removeClusterTags, "tag", []string{}, "Tag of the cluster to remove (key=value format, can be specified multiple times)")
24+
2225
cmd.Flags().BoolVar(&r.args.removeClusterAll, "all", false, "remove all clusters")
2326

27+
cmd.Flags().BoolVar(&r.args.removeClusterDryRun, "dry-run", false, "Dry run")
28+
2429
return cmd
2530
}
2631

2732
func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
28-
if len(args) == 0 && !r.args.removeClusterAll {
29-
return errors.New("ID or --all flag required")
30-
} else if len(args) > 0 && r.args.removeClusterAll {
31-
return errors.New("cannot specify ID and --all flag")
33+
if len(args) == 0 && !r.args.removeClusterAll && len(r.args.removeClusterNames) == 0 && len(r.args.removeClusterTags) == 0 {
34+
return errors.New("One of ID, --all, --name or --tag flag required")
35+
} else if len(args) > 0 && (r.args.removeClusterAll || len(r.args.removeClusterNames) > 0 || len(r.args.removeClusterTags) > 0) {
36+
return errors.New("cannot specify ID and --all, --name or --tag flag")
37+
} else if len(args) == 0 && r.args.removeClusterAll && (len(r.args.removeClusterNames) > 0 || len(r.args.removeClusterTags) > 0) {
38+
return errors.New("cannot specify --all and --name or --tag flag")
39+
} else if len(args) == 0 && !r.args.removeClusterAll && len(r.args.removeClusterNames) > 0 && len(r.args.removeClusterTags) > 0 {
40+
return errors.New("cannot specify --name and --tag flag")
3241
}
3342

34-
if r.args.removeClusterAll {
43+
if len(r.args.removeClusterNames) > 0 {
3544
clusters, err := r.kotsAPI.ListClusters(false, nil, nil)
3645
if err != nil {
3746
return errors.Wrap(err, "list clusters")
3847
}
48+
for _, cluster := range clusters {
49+
for _, name := range r.args.removeClusterNames {
50+
if cluster.Name == name {
51+
err := remove(r, cluster.ID)
52+
if err != nil {
53+
return errors.Wrap(err, "remove cluster")
54+
}
55+
}
56+
}
57+
}
58+
}
59+
60+
if len(r.args.removeClusterTags) > 0 {
61+
clusters, err := r.kotsAPI.ListClusters(false, nil, nil)
62+
if err != nil {
63+
return errors.Wrap(err, "list clusters")
64+
}
65+
tags, err := parseTags(r.args.removeClusterTags)
66+
if err != nil {
67+
return errors.Wrap(err, "parse tags")
68+
}
69+
70+
for _, cluster := range clusters {
71+
if cluster.Tags != nil && len(cluster.Tags) > 0 {
72+
for _, tag := range tags {
73+
for _, clusterTag := range cluster.Tags {
74+
if clusterTag.Key == tag.Key && clusterTag.Value == tag.Value {
75+
err := remove(r, cluster.ID)
76+
if err != nil {
77+
return errors.Wrap(err, "remove cluster")
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}
3985

86+
if r.args.removeClusterAll {
87+
clusters, err := r.kotsAPI.ListClusters(false, nil, nil)
88+
if err != nil {
89+
return errors.Wrap(err, "list clusters")
90+
}
4091
for _, cluster := range clusters {
41-
err := r.kotsAPI.RemoveCluster(cluster.ID)
42-
if errors.Cause(err) == platformclient.ErrForbidden {
43-
return ErrCompatibilityMatrixTermsNotAccepted
44-
} else if err != nil {
92+
err := remove(r, cluster.ID)
93+
if err != nil {
4594
return errors.Wrap(err, "remove cluster")
46-
} else {
47-
fmt.Printf("removed cluster %s\n", cluster.ID)
4895
}
4996
}
5097
}
5198

5299
for _, arg := range args {
53-
err := r.kotsAPI.RemoveCluster(arg)
54-
if errors.Cause(err) == platformclient.ErrForbidden {
55-
return ErrCompatibilityMatrixTermsNotAccepted
56-
} else if err != nil {
100+
err := remove(r, arg)
101+
if err != nil {
57102
return errors.Wrap(err, "remove cluster")
58103
}
59104
}
60105

61106
return nil
62107
}
108+
109+
func remove(r *runners, clusterID string) error {
110+
if r.args.removeClusterDryRun {
111+
fmt.Printf("would remove cluster %s\n", clusterID)
112+
return nil
113+
}
114+
err := r.kotsAPI.RemoveCluster(clusterID)
115+
if errors.Cause(err) == platformclient.ErrForbidden {
116+
return ErrCompatibilityMatrixTermsNotAccepted
117+
} else if err != nil {
118+
return errors.Wrap(err, "remove cluster")
119+
} else {
120+
fmt.Printf("removed cluster %s\n", clusterID)
121+
}
122+
return nil
123+
}

cli/cmd/runner.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ type runnerArgs struct {
199199
prepareClusterKotsSharedPassword string
200200
prepareClusterAppReadyTimeout time.Duration
201201

202-
removeClusterAll bool
202+
removeClusterAll bool
203+
removeClusterTags []string
204+
removeClusterNames []string
205+
removeClusterDryRun bool
203206

204207
lsAppVersion string
205208
lsVersionsClusterKubernetesDistribution string

0 commit comments

Comments
 (0)