forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_cluster_gke_terraform.go
191 lines (147 loc) · 4.44 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
package cmd
import (
"io"
"fmt"
os_user "os/user"
"github.com/jenkins-x/jx/pkg/jx/cmd/log"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"os"
"path/filepath"
)
// CreateClusterOptions the flags for running create cluster
type UpdateClusterGKETerraformOptions struct {
UpdateClusterOptions
Flags UpdateClusterGKETerraformFlags
}
type UpdateClusterGKETerraformFlags struct {
ClusterName 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 cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := createUpdateClusterGKETerraformOptions(f, 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()
cmdutil.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Flags.ClusterName, optionClusterName, "n", "", "The name of this cluster")
return cmd
}
func createUpdateClusterGKETerraformOptions(f cmdutil.Factory, out io.Writer, errOut io.Writer, cloudProvider string) UpdateClusterGKETerraformOptions {
commonOptions := CommonOptions{
Factory: f,
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)
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 {
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)
if !confirm {
// exit at this point
return nil
}
var err error
if o.Flags.ClusterName == "" {
log.Info("No cluster name provided\n")
return nil
}
serviceAccount := fmt.Sprintf("jx-%s", o.Flags.ClusterName)
user, err := os_user.Current()
if err != nil {
return err
}
jxHome := filepath.Join(user.HomeDir, ".jx")
clustersHome := filepath.Join(jxHome, "clusters")
clusterHome := filepath.Join(clustersHome, o.Flags.ClusterName)
os.MkdirAll(clusterHome, os.ModePerm)
keyPath := filepath.Join(clusterHome, fmt.Sprintf("%s.key.json", 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
}
confirm = false
prompt = &survey.Confirm{
Message: "Would you like to apply this plan",
}
survey.AskOne(prompt, &confirm, nil)
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
}