forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_instancegroup.go
80 lines (65 loc) · 1.62 KB
/
delete_instancegroup.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
package main
import (
"fmt"
"github.com/spf13/cobra"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/upup/pkg/kutil"
)
type DeleteInstanceceGroupCmd struct {
}
var deleteInstanceceGroupCmd DeleteInstanceceGroupCmd
func init() {
cmd := &cobra.Command{
Use: "instancegroup",
Aliases: []string{"instancegroups", "ig"},
Short: "Delete instancegroup",
Long: `Delete an instancegroup configuration.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
exitWithError(fmt.Errorf("Specify name of instance group to delete"))
}
if len(args) != 1 {
exitWithError(fmt.Errorf("Can only edit one instance group at a time!"))
}
err := deleteInstanceceGroupCmd.Run(args[0])
if err != nil {
exitWithError(err)
}
},
}
deleteCmd.AddCommand(cmd)
}
func (c *DeleteInstanceceGroupCmd) Run(groupName string) error {
if groupName == "" {
return fmt.Errorf("name is required")
}
cluster, err := rootCommand.Cluster()
if err != nil {
return err
}
clientset, err := rootCommand.Clientset()
if err != nil {
return err
}
group, err := clientset.InstanceGroups(cluster.Name).Get(groupName)
if err != nil {
return fmt.Errorf("error reading InstanceGroup %q: %v", groupName, err)
}
if group == nil {
return fmt.Errorf("InstanceGroup %q not found", groupName)
}
cloud, err := cloudup.BuildCloud(cluster)
if err != nil {
return err
}
d := &kutil.DeleteInstanceGroup{}
d.Cluster = cluster
d.Cloud = cloud
d.Clientset = clientset
err = d.DeleteInstanceGroup(group)
if err != nil {
return err
}
fmt.Printf("InstanceGroup %q deleted\n", group.Name)
return nil
}