-
Notifications
You must be signed in to change notification settings - Fork 787
/
upgrade_cluster.go
206 lines (172 loc) · 5.28 KB
/
upgrade_cluster.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package cmd
import (
"encoding/json"
"errors"
"io"
"strings"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
var (
upgradeClusterLong = templates.LongDesc(`
Upgrades the Jenkins X Kubernetes master to the specified version
`)
upgradeClusterExample = templates.Examples(`
# Upgrades the Jenkins X Cluster tools
jx upgrade cluster
`)
)
// UpgradeClusterOptions the options for the create spring command
type UpgradeClusterOptions struct {
UpgradeOptions
Version string
ClusterName string
}
// NewCmdUpgradeCluster defines the command
func NewCmdUpgradeCluster(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &UpgradeClusterOptions{
UpgradeOptions: UpgradeOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "cluster",
Short: "Upgrades the Kubernetes master to the specified version",
Long: upgradeClusterLong,
Example: upgradeClusterExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Version, "version", "v", "", "The specific version to upgrade to")
cmd.Flags().StringVarP(&options.ClusterName, "cluster-name", "c", "", "The specific cluster to upgrade")
return cmd
}
// Run implements the command
func (o *UpgradeClusterOptions) Run() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
confirm := false
prompt := &survey.Confirm{
Message: "Upgrading a GKE cluster is an experimental feature in jx. Would you like to continue?",
}
survey.AskOne(prompt, &confirm, nil, surveyOpts)
if !confirm {
// exit at this point
return nil
}
// check to see if gcloud is available
err := o.validateGCloudIsAvailable()
if err != nil {
return errors.New("Unable to locate gcloud command")
}
// if no cluster name is set, prompt.
selectedClusterName, err := o.getClusterName()
if err != nil {
return err
}
// if no version, prompt.
selectedVersion, err := o.getVersion()
if err != nil {
return err
}
log.Infof("Upgrading %s master to %s (this may take a few minutes)\n", selectedClusterName, selectedVersion)
err = o.runCommandVerbose("gcloud", "container", "clusters", "upgrade", selectedClusterName, "--cluster-version", selectedVersion, "--master", "--quiet")
if err != nil {
return err
}
log.Infof("Upgrading %s nodes (this may take a few minutes)\n", selectedClusterName)
return o.runCommandVerbose("gcloud", "container", "clusters", "upgrade", selectedClusterName, "--quiet")
}
func (o *UpgradeClusterOptions) getClusterName() (string, error) {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
selectedClusterName := o.ClusterName
if selectedClusterName != "" {
return selectedClusterName, nil
}
out, err := o.getCommandOutput("", "gcloud", "container", "clusters", "list")
if err != nil {
return "", err
}
lines := strings.Split(string(out), "\n")
var existingClusters []string
for _, l := range lines {
if strings.Contains(l, "MASTER_VERSION") {
continue
}
fields := strings.Fields(l)
existingClusters = append(existingClusters, fields[0])
}
if len(existingClusters) == 0 {
return "", errors.New("Could not find a cluster to upgrade, please manually create one and rerun the wizard")
} else if len(existingClusters) == 1 {
selectedClusterName = existingClusters[0]
log.Infof("Using the only GKE cluster %s\n", util.ColorInfo(selectedClusterName))
} else {
prompts := &survey.Select{
Message: "GKE Cluster:",
Options: existingClusters,
Help: "Select a GKE cluster to upgrade",
}
err := survey.AskOne(prompts, &selectedClusterName, nil, surveyOpts)
if err != nil {
return "", err
}
}
if selectedClusterName == "" {
return "", errors.New("no GKE cluster found, please manual create one and rerun this wizard")
}
return selectedClusterName, nil
}
func (o *UpgradeClusterOptions) getVersion() (string, error) {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
selectedVersion := o.Version
if selectedVersion != "" {
return selectedVersion, nil
}
out, err := o.getCommandOutput("", "gcloud", "container", "get-server-config", "--format=json")
if err != nil {
return "", err
}
startOfJSON := strings.Index(out, "{")
if startOfJSON == -1 {
return "", errors.New("does not appear to be a valid JSON response")
}
sc := &serverConfig{}
err = json.Unmarshal([]byte(out[startOfJSON:]), sc)
if err != nil {
return "", err
}
prompts := &survey.Select{
Message: "New Cluster Version:",
Options: sc.ValidMasterVersions,
Help: "Select a GKE cluster version to upgrade to",
}
err = survey.AskOne(prompts, &selectedVersion, nil, surveyOpts)
if err != nil {
return "", err
}
return selectedVersion, nil
}
func (o *UpgradeClusterOptions) validateGCloudIsAvailable() error {
_, err := o.getCommandOutput("", "gcloud", "version")
if err != nil {
return err
}
return nil
}
// internal type to help unmarshal Kubernetes upgrade versions
type serverConfig struct {
ValidMasterVersions []string `json:"validMasterVersions"`
}