forked from kubernetes/kops
/
edit_instancegroup.go
137 lines (114 loc) · 2.91 KB
/
edit_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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"k8s.io/kops/upup/pkg/api"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/editor"
)
type EditInstanceGroupCmd struct {
}
var editInstanceGroupCmd EditInstanceGroupCmd
func init() {
cmd := &cobra.Command{
Use: "instancegroup",
Aliases: []string{"instancegroups", "ig"},
Short: "Edit instancegroup",
Long: `Edit an instancegroup configuration.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
exitWithError(fmt.Errorf("Specify name of instance group to edit"))
}
if len(args) != 1 {
exitWithError(fmt.Errorf("Can only edit one instance group at a time!"))
}
err := editInstanceGroupCmd.Run(args[0])
if err != nil {
exitWithError(err)
}
},
}
editCmd.AddCommand(cmd)
}
func (c *EditInstanceGroupCmd) Run(groupName string) error {
cluster, err := rootCommand.Cluster()
if err != nil {
return err
}
channel, err := cloudup.ChannelForCluster(cluster)
if err != nil {
return err
}
clientset, err := rootCommand.Clientset()
if err != nil {
return err
}
if groupName == "" {
return fmt.Errorf("name is required")
}
oldGroup, err := clientset.InstanceGroups(cluster.Name).Get(groupName)
if err != nil {
return fmt.Errorf("error reading InstanceGroup %q: %v", groupName, err)
}
if oldGroup == nil {
return fmt.Errorf("InstanceGroup %q not found", groupName)
}
var (
edit = editor.NewDefaultEditor(editorEnvs)
)
ext := "yaml"
raw, err := api.ToYaml(oldGroup)
if err != nil {
return fmt.Errorf("error parsing InstanceGroup: %v", err)
}
// launch the editor
edited, file, err := edit.LaunchTempFile(fmt.Sprintf("%s-edit-", filepath.Base(os.Args[0])), ext, bytes.NewReader(raw))
defer func() {
if file != "" {
os.Remove(file)
}
}()
if err != nil {
return fmt.Errorf("error launching editor: %v", err)
}
if bytes.Equal(edited, raw) {
fmt.Fprintln(os.Stderr, "Edit cancelled, no changes made.")
return nil
}
newGroup := &api.InstanceGroup{}
err = api.ParseYaml(edited, newGroup)
if err != nil {
return fmt.Errorf("error parsing config: %v", err)
}
err = newGroup.Validate(false)
if err != nil {
return err
}
fullGroup, err := cloudup.PopulateInstanceGroupSpec(cluster, newGroup, channel)
if err != nil {
return err
}
// We need the full cluster spec to perform deep validation
// Note that we don't write it back though
err = cluster.PerformAssignments()
if err != nil {
return fmt.Errorf("error populating configuration: %v", err)
}
fullCluster, err := cloudup.PopulateClusterSpec(cluster)
if err != nil {
return err
}
err = fullGroup.CrossValidate(fullCluster, true)
if err != nil {
return err
}
// Note we perform as much validation as we can, before writing a bad config
_, err = clientset.InstanceGroups(cluster.Name).Update(fullGroup)
if err != nil {
return err
}
return nil
}