forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_cluster_gke_terraform.go
208 lines (166 loc) · 5.16 KB
/
update_cluster_gke_terraform.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
207
208
package cmd
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/jenkins-x/jx/pkg/cloud/gke"
"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"
)
// CreateClusterOptions the flags for running create cluster
type UpdateClusterGKETerraformOptions struct {
UpdateClusterOptions
Flags UpdateClusterGKETerraformFlags
}
type UpdateClusterGKETerraformFlags struct {
ClusterName string
SkipLogin bool
ServiceAccount string
}
var (
updateClusterGKETerraformLong = templates.LongDesc(`
Command re-applies the Terraform plan in ~/.jx/clusters/<cluster>/terraform against the specified cluster
`)
updateClusterGKETerraformExample = templates.Examples(`
jx update cluster gke terraform
`)
)
// NewCmdGet creates a command object for the generic "init" action, which
// installs the dependencies required to run the jenkins-x platform on a Kubernetes cluster.
func NewCmdUpdateClusterGKETerraform(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := createUpdateClusterGKETerraformOptions(f, in, out, errOut, GKE)
cmd := &cobra.Command{
Use: "terraform",
Short: "Updates an existing Kubernetes cluster on GKE using Terraform: Runs on Google Cloud",
Long: updateClusterGKETerraformLong,
Example: updateClusterGKETerraformExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCommonFlags(cmd)
cmd.Flags().StringVarP(&options.Flags.ClusterName, optionClusterName, "n", "", "The name of this cluster")
cmd.Flags().BoolVarP(&options.Flags.SkipLogin, "skip-login", "", false, "Skip Google auth if already logged in via gcloud auth")
cmd.Flags().StringVarP(&options.ServiceAccount, "service-account", "", "", "Use a service account to login to GCE")
return cmd
}
func createUpdateClusterGKETerraformOptions(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer, cloudProvider string) UpdateClusterGKETerraformOptions {
commonOptions := CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
}
options := UpdateClusterGKETerraformOptions{
UpdateClusterOptions: UpdateClusterOptions{
UpdateOptions: UpdateOptions{
CommonOptions: commonOptions,
},
Provider: cloudProvider,
},
}
return options
}
func (o *UpdateClusterGKETerraformOptions) Run() error {
err := o.installRequirements(GKE, "terraform", o.InstallOptions.InitOptions.HelmBinary())
if err != nil {
return err
}
err = o.updateClusterGKETerraform()
if err != nil {
log.Errorf("error creating cluster %v", err)
return err
}
return nil
}
func (o *UpdateClusterGKETerraformOptions) updateClusterGKETerraform() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
if !o.BatchMode {
confirm := false
prompt := &survey.Confirm{
Message: "Updating a GKE cluster with Terraform 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
}
}
err := gke.Login(o.ServiceAccount, o.Flags.SkipLogin)
if err != nil {
return err
}
if o.Flags.ClusterName == "" {
log.Info("No cluster name provided\n")
return nil
}
serviceAccount := fmt.Sprintf("jx-%s", o.Flags.ClusterName)
jxHome, err := util.ConfigDir()
if err != nil {
return err
}
clustersHome := filepath.Join(jxHome, "clusters")
clusterHome := filepath.Join(clustersHome, o.Flags.ClusterName)
os.MkdirAll(clusterHome, os.ModePerm)
var keyPath string
if o.ServiceAccount == "" {
keyPath = filepath.Join(clusterHome, fmt.Sprintf("%s.key.json", serviceAccount))
} else {
keyPath = o.ServiceAccount
}
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
log.Infof("Unable to find service account key %s\n", keyPath)
return nil
}
terraformDir := filepath.Join(clusterHome, "terraform")
if _, err := os.Stat(terraformDir); os.IsNotExist(err) {
log.Infof("Unable to find Terraform plan dir %s\n", terraformDir)
return nil
}
// create .tfvars file in .jx folder
terraformVars := filepath.Join(terraformDir, "terraform.tfvars")
args := []string{"init", terraformDir}
err = o.RunCommand("terraform", args...)
if err != nil {
return err
}
terraformState := filepath.Join(terraformDir, "terraform.tfstate")
args = []string{"plan",
fmt.Sprintf("-state=%s", terraformState),
fmt.Sprintf("-var-file=%s", terraformVars),
terraformDir}
err = o.runCommandVerbose("terraform", args...)
if err != nil {
return err
}
if !o.BatchMode {
confirm := false
prompt := &survey.Confirm{
Message: "Would you like to apply this plan",
}
survey.AskOne(prompt, &confirm, nil, surveyOpts)
if !confirm {
// exit at this point
return nil
}
}
log.Info("Applying plan...\n")
args = []string{"apply",
"-auto-approve",
fmt.Sprintf("-state=%s", terraformState),
fmt.Sprintf("-var-file=%s", terraformVars),
terraformDir}
err = o.runCommandVerbose("terraform", args...)
if err != nil {
return err
}
return nil
}