forked from nytimes/drone-gke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
318 lines (259 loc) · 8.19 KB
/
main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/drone/drone-plugin-go/plugin"
)
type GKE struct {
DryRun bool `json:"dry_run"`
Verbose bool `json:"verbose"`
Token string `json:"token"`
GCloudCmd string `json:"gcloud_cmd"`
KubectlCmd string `json:"kubectl_cmd"`
Project string `json:"project"`
Zone string `json:"zone"`
Cluster string `json:"cluster"`
Namespace string `json:"namespace"`
Template string `json:"template"`
SecretTemplate string `json:"secret_template"`
Vars map[string]interface{} `json:"vars"`
Secrets map[string]string `json:"secrets"`
// SecretsBase64 holds secret values which are already base64 encoded and
// thus don't need to be re-encoded as they would be if they were in
// the Secrets field.
SecretsBase64 map[string]string `json:"secrets_base64"`
}
var (
rev string
)
var nsTemplate = `
---
apiVersion: v1
kind: Namespace
metadata:
name: %s
`
func main() {
err := wrapMain()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func wrapMain() error {
if rev == "" {
rev = "[unknown]"
}
fmt.Printf("Drone GKE Plugin built from %s\n", rev)
// https://godoc.org/github.com/drone/drone-plugin-go/plugin
workspace := plugin.Workspace{}
repo := plugin.Repo{}
build := plugin.Build{}
system := plugin.System{}
vargs := GKE{}
plugin.Param("workspace", &workspace)
plugin.Param("repo", &repo)
plugin.Param("build", &build)
plugin.Param("system", &system)
plugin.Param("vargs", &vargs)
plugin.MustParse()
// Check required params.
if vargs.Token == "" {
return fmt.Errorf("Missing required param: token")
}
if vargs.Project == "" {
vargs.Project = getProjectFromToken(vargs.Token)
}
if vargs.Project == "" {
return fmt.Errorf("Missing required param: project")
}
if vargs.Zone == "" {
return fmt.Errorf("Missing required param: zone")
}
sdkPath := "/google-cloud-sdk"
keyPath := "/tmp/gcloud.json"
// Defaults.
if vargs.GCloudCmd == "" {
vargs.GCloudCmd = fmt.Sprintf("%s/bin/gcloud", sdkPath)
}
if vargs.KubectlCmd == "" {
vargs.KubectlCmd = fmt.Sprintf("%s/bin/kubectl", sdkPath)
}
if vargs.Template == "" {
vargs.Template = ".kube.yml"
}
if vargs.SecretTemplate == "" {
vargs.SecretTemplate = ".kube.sec.yml"
}
// Trim whitespace, to forgive the vagaries of YAML parsing.
vargs.Token = strings.TrimSpace(vargs.Token)
// Write credentials to tmp file to be picked up by the 'gcloud' command.
// This is inside the ephemeral plugin container, not on the host.
err := ioutil.WriteFile(keyPath, []byte(vargs.Token), 0600)
if err != nil {
return fmt.Errorf("Error writing token file: %s\n", err)
}
// Warn if the keyfile can't be deleted, but don't abort.
// We're almost certainly running inside an ephemeral container, so the file will be discarded when we're finished anyway.
defer func() {
err := os.Remove(keyPath)
if err != nil {
fmt.Printf("Warning: error removing token file: %s\n", err)
}
}()
e := os.Environ()
e = append(e, fmt.Sprintf("GOOGLE_APPLICATION_CREDENTIALS=%s", keyPath))
runner := NewEnviron(workspace.Path, e, os.Stdout, os.Stderr)
err = runner.Run(vargs.GCloudCmd, "auth", "activate-service-account", "--key-file", keyPath)
if err != nil {
return fmt.Errorf("Error: %s\n", err)
}
err = runner.Run(vargs.GCloudCmd, "container", "clusters", "get-credentials", vargs.Cluster, "--project", vargs.Project, "--zone", vargs.Zone)
if err != nil {
return fmt.Errorf("Error: %s\n", err)
}
data := map[string]interface{}{
// http://readme.drone.io/usage/variables/#string-interpolation:2b8b8ac4006be88c769f5e3fd99b009a
"BUILD_NUMBER": build.Number,
"COMMIT": build.Commit,
"BRANCH": build.Branch,
"TAG": "", // How?
// https://godoc.org/github.com/drone/drone-plugin-go/plugin#Workspace
"workspace": workspace,
"repo": repo,
"build": build,
"system": system,
// Misc useful stuff.
// Note that we don't include all of the vargs, since that includes the GCP token.
"project": vargs.Project,
"zone": vargs.Zone,
"cluster": vargs.Cluster,
"namespace": vargs.Namespace,
}
for k, v := range vargs.Vars {
// Don't allow vars to be overridden.
// We do this to ensure that the built-in template vars (above) can be relied upon.
if _, ok := data[k]; ok {
return fmt.Errorf("Error: var %q shadows existing var\n", k)
}
data[k] = v
}
if vargs.Verbose {
dump := data
delete(dump, "workspace")
dumpData(os.Stdout, "DATA (Workspace Values Omitted)", dump)
}
secrets := map[string]interface{}{}
for k, v := range vargs.Secrets {
if v == "" {
return fmt.Errorf("Error: secret var %q is an empty string\n", k)
}
// Base64 encode secret strings.
secrets[k] = base64.StdEncoding.EncodeToString([]byte(v))
}
for k, v := range vargs.SecretsBase64 {
if _, ok := secrets[k]; ok {
return fmt.Errorf("Error: secret var %q is already set in Secrets\n", k)
}
if v == "" {
return fmt.Errorf("Error: secret var %q is an empty string\n", k)
}
// Don't base64 encode these secrets, they already are.
secrets[k] = v
}
mapping := map[string]map[string]interface{}{
vargs.Template: data,
vargs.SecretTemplate: secrets,
}
outPaths := make(map[string]string)
pathArg := []string{}
for t, content := range mapping {
if t == "" {
continue
}
inPath := filepath.Join(workspace.Path, t)
bn := filepath.Base(inPath)
// Ensure the required template file exists.
_, err := os.Stat(inPath)
if os.IsNotExist(err) {
if t == vargs.Template {
return fmt.Errorf("Error finding template: %s\n", err)
} else {
fmt.Printf("Warning: skipping optional template %s, it was not found\n", t)
continue
}
}
// Generate the file.
blob, err := ioutil.ReadFile(inPath)
if err != nil {
return fmt.Errorf("Error reading template: %s\n", err)
}
tmpl, err := template.New(bn).Option("missingkey=error").Parse(string(blob))
if err != nil {
return fmt.Errorf("Error parsing template: %s\n", err)
}
outPaths[t] = fmt.Sprintf("/tmp/%s", bn)
f, err := os.Create(outPaths[t])
if err != nil {
return fmt.Errorf("Error creating deployment file: %s\n", err)
}
err = tmpl.Execute(f, content)
if err != nil {
return fmt.Errorf("Error executing deployment template: %s\n", err)
}
f.Close()
pathArg = append(pathArg, outPaths[t])
}
if vargs.Verbose {
dumpFile(os.Stdout, "DEPLOYMENT (Secret Template Omitted)", outPaths[vargs.Template])
}
if vargs.DryRun {
fmt.Println("Skipping kubectl apply, because dry_run: true")
return nil
}
// Set the execution namespace.
if len(vargs.Namespace) > 0 {
fmt.Printf("Configuring kubectl to the %s namespace\n", vargs.Namespace)
context := strings.Join([]string{"gke", vargs.Project, vargs.Zone, vargs.Cluster}, "_")
err = runner.Run(vargs.KubectlCmd, "config", "set-context", context, "--namespace", vargs.Namespace)
if err != nil {
return fmt.Errorf("Error: %s\n", err)
}
resource := fmt.Sprintf(nsTemplate, vargs.Namespace)
nsPath := "/tmp/namespace.json"
// Write namespace resource file to tmp file to be picked up by the 'kubectl' command.
// This is inside the ephemeral plugin container, not on the host.
err := ioutil.WriteFile(nsPath, []byte(resource), 0600)
if err != nil {
return fmt.Errorf("Error writing namespace resource file: %s\n", err)
}
// Ensure the namespace exists, without errors (unlike `kubectl create namespace`).
err = runner.Run(vargs.KubectlCmd, "apply", "--filename", nsPath)
if err != nil {
return fmt.Errorf("Error: %s\n", err)
}
}
// Apply Kubernetes configuration files.
err = runner.Run(vargs.KubectlCmd, "apply", "--filename", strings.Join(pathArg, ","))
if err != nil {
return fmt.Errorf("Error: %s\n", err)
}
return nil
}
type token struct {
ProjectID string `json:"project_id"`
}
func getProjectFromToken(j string) string {
t := token{}
err := json.Unmarshal([]byte(j), &t)
if err != nil {
return ""
}
return t.ProjectID
}