-
Notifications
You must be signed in to change notification settings - Fork 23
/
cluster_rm.go
123 lines (107 loc) · 3.68 KB
/
cluster_rm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package cmd
import (
"fmt"
"github.com/pkg/errors"
"github.com/replicatedhq/replicated/pkg/platformclient"
"github.com/spf13/cobra"
)
func (r *runners) InitClusterRemove(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "rm ID [ID …]",
Short: "Remove test clusters",
Long: `Removes a cluster immediately.
You can specify the --all flag to terminate all clusters.`,
RunE: r.removeCluster,
}
parent.AddCommand(cmd)
cmd.Flags().StringArrayVar(&r.args.removeClusterNames, "name", []string{}, "Name of the cluster to remove (can be specified multiple times)")
cmd.Flags().StringArrayVar(&r.args.removeClusterTags, "tag", []string{}, "Tag of the cluster to remove (key=value format, can be specified multiple times)")
cmd.Flags().BoolVar(&r.args.removeClusterAll, "all", false, "remove all clusters")
cmd.Flags().BoolVar(&r.args.removeClusterDryRun, "dry-run", false, "Dry run")
return cmd
}
func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
if len(args) == 0 && !r.args.removeClusterAll && len(r.args.removeClusterNames) == 0 && len(r.args.removeClusterTags) == 0 {
return errors.New("One of ID, --all, --name or --tag flag required")
} else if len(args) > 0 && (r.args.removeClusterAll || len(r.args.removeClusterNames) > 0 || len(r.args.removeClusterTags) > 0) {
return errors.New("cannot specify ID and --all, --name or --tag flag")
} else if len(args) == 0 && r.args.removeClusterAll && (len(r.args.removeClusterNames) > 0 || len(r.args.removeClusterTags) > 0) {
return errors.New("cannot specify --all and --name or --tag flag")
} else if len(args) == 0 && !r.args.removeClusterAll && len(r.args.removeClusterNames) > 0 && len(r.args.removeClusterTags) > 0 {
return errors.New("cannot specify --name and --tag flag")
}
if len(r.args.removeClusterNames) > 0 {
clusters, err := r.kotsAPI.ListClusters(false, nil, nil)
if err != nil {
return errors.Wrap(err, "list clusters")
}
for _, cluster := range clusters {
for _, name := range r.args.removeClusterNames {
if cluster.Name == name {
err := remove(r, cluster.ID)
if err != nil {
return errors.Wrap(err, "remove cluster")
}
}
}
}
}
if len(r.args.removeClusterTags) > 0 {
clusters, err := r.kotsAPI.ListClusters(false, nil, nil)
if err != nil {
return errors.Wrap(err, "list clusters")
}
tags, err := parseTags(r.args.removeClusterTags)
if err != nil {
return errors.Wrap(err, "parse tags")
}
for _, cluster := range clusters {
if cluster.Tags != nil && len(cluster.Tags) > 0 {
for _, tag := range tags {
for _, clusterTag := range cluster.Tags {
if clusterTag.Key == tag.Key && clusterTag.Value == tag.Value {
err := remove(r, cluster.ID)
if err != nil {
return errors.Wrap(err, "remove cluster")
}
}
}
}
}
}
}
if r.args.removeClusterAll {
clusters, err := r.kotsAPI.ListClusters(false, nil, nil)
if err != nil {
return errors.Wrap(err, "list clusters")
}
for _, cluster := range clusters {
err := remove(r, cluster.ID)
if err != nil {
return errors.Wrap(err, "remove cluster")
}
}
}
for _, arg := range args {
err := remove(r, arg)
if err != nil {
return errors.Wrap(err, "remove cluster")
}
}
return nil
}
func remove(r *runners, clusterID string) error {
if r.args.removeClusterDryRun {
fmt.Printf("would remove cluster %s\n", clusterID)
return nil
}
err := r.kotsAPI.RemoveCluster(clusterID)
if errors.Cause(err) == platformclient.ErrForbidden {
return ErrCompatibilityMatrixTermsNotAccepted
} else if err != nil {
return errors.Wrap(err, "remove cluster")
} else {
fmt.Printf("removed cluster %s\n", clusterID)
}
return nil
}