-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplugin.go
435 lines (397 loc) · 14.3 KB
/
plugin.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"os/exec"
"strings"
"github.com/target/impeller/constants"
"github.com/target/impeller/types"
"github.com/target/impeller/utils"
"github.com/target/impeller/utils/commandbuilder"
"github.com/target/impeller/utils/report"
)
const (
kubectlBin = "kubectl"
)
var (
kubeConfig = os.Getenv("HOME") + "/.kube/config"
)
type Plugin struct {
ClusterConfig types.ClusterConfig
ClusterConfigPath string
ClustersList report.Clusters
ValueFiles []string
KubeConfig string
KubeConfigFile string
KubeConfigBase64 bool
KubeContext string
Dryrun bool
Diffrun bool
Audit bool
AuditFile string
}
func (p *Plugin) Exec() error {
if !p.Audit {
if !p.ClusterConfig.Helm.SkipSetupKubeConfig {
// Init Kubernetes config
if err := p.setupKubeconfig(); err != nil {
return fmt.Errorf("error initializing Kubernetes config: %v", err)
}
} else {
log.Println("Skipping setting up kubeconfig...")
}
// Add configured repos
if !p.ClusterConfig.Helm.SkipSetupHelmRepo {
for _, repo := range p.ClusterConfig.Helm.Repos {
if err := p.addHelmRepo(repo); err != nil {
return fmt.Errorf("error adding Helm repo: %v", err)
}
}
if err := p.updateHelmRepos(); err != nil {
return fmt.Errorf("error updating Helm repos: %v", err)
}
} else {
log.Println("Skipping setting up Helm repos...")
}
if !p.ClusterConfig.Helm.SkipSetupKubeConfig {
// Install addons
for _, addon := range p.ClusterConfig.Releases {
if err := p.installAddon(&addon); err != nil {
return fmt.Errorf("error installing addon \"%s\": %v", addon.Name, err)
}
}
}
} else {
log.Println("Generating Audit report:")
rpt := report.NewReport()
for cluster := range p.ClustersList.ClusterList {
clusterConfig, err := utils.ReadClusterConfig(p.ClusterConfigPath + "/" + cluster)
if err != nil {
return fmt.Errorf("error reading cluster config: %v", err)
}
for _, addon := range clusterConfig.Releases {
rpt.Add(report.ReportKey{
Name: addon.Name,
Cluster: cluster,
Namespace: addon.Namespace,
}, report.ReportDetail{
Version: addon.Version,
Overrides: "",
ChartPath: addon.ChartPath,
ChartsSource: addon.ChartsSource,
ValueFiles: utils.GetValueFiles(&addon.ValueFiles),
})
}
}
// write report to output file
err := rpt.Write(p.AuditFile)
if err != nil {
return err
}
}
return nil
}
func (p *Plugin) addHelmRepo(repo types.HelmRepo) error {
log.Println("Adding Helm repo:", repo.Name)
cb := commandbuilder.CommandBuilder{Name: constants.HelmBin}
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "repo"})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "add"})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: repo.Name})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: repo.URL})
if repo.Username != nil {
username, err := repo.Username.GetValue()
if err != nil {
return fmt.Errorf("could not get username for repo: %v", err)
}
cb.Add(commandbuilder.Arg{
Type: commandbuilder.ArgTypeLongParam,
Name: "username",
Value: username,
ValueSecret: true,
})
}
if repo.Password != nil {
password, err := repo.Password.GetValue()
if err != nil {
return fmt.Errorf("could not get password for repo: %v", err)
}
cb.Add(commandbuilder.Arg{
Type: commandbuilder.ArgTypeLongParam,
Name: "password",
Value: password,
ValueSecret: true,
})
}
if err := cb.Run(); err != nil {
return fmt.Errorf("could not add repo \"%s\": %v", repo.Name, err)
}
return nil
}
func (p *Plugin) updateHelmRepos() error {
log.Println("Updating Helm repos")
cmd := exec.Command(constants.HelmBin, "repo", "update")
if err := utils.Run(cmd, true); err != nil {
return fmt.Errorf("error updating helm repos: %v", err)
}
return nil
}
func (p *Plugin) installAddon(release *types.Release) error {
log.Println("Installing addon:", release.Name, "@", release.Version)
switch release.DeploymentMethod {
case "kubectl":
return p.installAddonViaKubectl(release)
case "helm":
fallthrough
default:
return p.installAddonViaHelm(release)
}
}
// installAddonViaHelm installs addons via helm upgrade --install RELEASE CHART
func (p *Plugin) installAddonViaHelm(release *types.Release) error {
cb := commandbuilder.CommandBuilder{Name: constants.HelmBin}
if p.Diffrun {
log.Println("Running Diff plugin:", release.Name)
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "diff"})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "upgrade"})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "--allow-unreleased"})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "--suppress-secrets"})
} else {
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "upgrade"})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "--install"})
if release.History > 0 {
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "history-max", Value: fmt.Sprint(release.History)})
} else if p.ClusterConfig.Helm.DefaultHistory > 0 {
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "history-max", Value: fmt.Sprint(p.ClusterConfig.Helm.DefaultHistory)})
}
}
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: release.Name})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: release.ChartPath})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "version", Value: release.Version})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "kube-context", Value: p.KubeContext})
if p.ClusterConfig.Helm.Debug {
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "--debug"})
}
// Add namespaces to command
if release.Namespace != "" {
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "namespace", Value: release.Namespace})
}
if p.ClusterConfig.Helm.LogLevel != 0 {
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "v", Value: fmt.Sprint(p.ClusterConfig.Helm.LogLevel)})
}
if release.ChartsSource != "" {
log.Println("Charts Source defined for:", release.Name)
_, err := p.downloadCharts(release)
if err != nil {
return fmt.Errorf("error downloading charts: %s", err)
}
}
// Add Overrides
for _, override := range p.overrides(release) {
cb.Add(override)
}
// Dry Run
if p.Dryrun {
log.Println("Running Dry run:", release.Name)
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "--dry-run"})
}
// Execute helm upgrade
if err := cb.Run(); err != nil {
return fmt.Errorf("error running helm: %v", err)
}
return nil
}
// installAddonViaKubectl installs addons via:
// helm fetch --version release.Version --untar release.ChartPath
// helm template $CHART | kubectl create -f -
func (p *Plugin) installAddonViaKubectl(release *types.Release) error {
renderedManifests, err := p.templateChart(release)
if err != nil {
return fmt.Errorf("error rendering chart for kubectl apply: %s", err)
}
cb := commandbuilder.CommandBuilder{Name: constants.KubectlBin}
// kubectl apply -f -
if release.Namespace != "" {
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "namespace", Value: release.Namespace})
}
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "context", Value: p.KubeContext})
// Diff Run
if p.Diffrun {
log.Println("Running Diff run:", release.Name)
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "diff"})
} else {
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "apply"})
}
// Dry Run
if p.Dryrun {
log.Println("Running Dry run:", release.Name)
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "--dry-run=server"})
}
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "filename", Value: "-"})
// Grab raw commandbuilder command so we can set stdin
kubectlApplyCmd := cb.Command()
kubectlApplyCmd.Stdin = strings.NewReader(renderedManifests)
// We may need to run kubectl apply -f - twice if the helm chart
// has dependant kubernetes resources. The first run will install
// independent components and the second run will install the ones
// that failed previously. If this command fails twice then the chart
// is just broken
if err := utils.Run(kubectlApplyCmd, false); err != nil && !(p.Diffrun && release.DeploymentMethod == "kubectl") {
kubectlApplyCmd := cb.Command()
kubectlApplyCmd.Stdin = strings.NewReader(renderedManifests)
return utils.Run(kubectlApplyCmd, false)
}
return nil
}
func (p *Plugin) fetchChart(release *types.Release) error {
cb := commandbuilder.CommandBuilder{Name: constants.HelmBin}
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "fetch"})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "version", Value: release.Version})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "--untar"})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: release.ChartPath})
return cb.Run()
}
func (p *Plugin) downloadCharts(release *types.Release) (string, error) {
if _, err := os.Stat("./downloads"); os.IsNotExist(err) {
err := os.Mkdir("./downloads", 0755)
if err != nil {
return "", fmt.Errorf("error creting ./downloads folder: %s", err)
}
}
myUrl, err := url.Parse(release.ChartsSource)
if err != nil {
return "", fmt.Errorf("error parsing charts myUrl: %s", err)
}
splits := strings.Split(myUrl.Path, "/")
tarFilePath := "./downloads/" + splits[len(splits)-1]
if _, err := os.Stat(tarFilePath); err != nil || os.IsExist(err) {
log.Println("Downloading:", tarFilePath)
cmd := exec.Command(constants.WgetBin, "-P", "./downloads", release.ChartsSource)
if err := utils.Run(cmd, true); err != nil {
return "", fmt.Errorf("error extracting Charts archive: %v", err)
}
err = p.extractCharts(tarFilePath)
if err != nil {
return tarFilePath, err
}
} else {
log.Println("File exist, skipping download:", tarFilePath)
}
return tarFilePath, nil
}
func (p *Plugin) extractCharts(archiveName string) error {
cmd := exec.Command(constants.TarBin, "-xzf", archiveName, "-C", "./downloads")
if err := utils.Run(cmd, true); err != nil {
return fmt.Errorf("error extracting Charts archive: %v", err)
}
return nil
}
func (p *Plugin) templateChart(release *types.Release) (string, error) {
cb := commandbuilder.CommandBuilder{Name: constants.HelmBin}
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "template"})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: release.Name})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: release.ChartPath})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "version", Value: release.Version})
// Add Overrides
for _, override := range p.overrides(release) {
cb.Add(override)
}
cmd := cb.Command()
templateBytes, err := cmd.Output()
if err != nil {
return "", err
}
return string(templateBytes), nil
}
func (p *Plugin) setupKubeconfig() error {
var err error
byteData := []byte{}
if p.KubeConfig != "" {
p.KubeConfigFile = kubeConfig + "-" + p.KubeContext
log.Println("Creating Kubernetes configfile" + p.KubeConfigFile)
if p.KubeConfigBase64 {
log.Println("configfile is base64 encoded")
byteData, err = base64.StdEncoding.DecodeString(p.KubeConfig)
if err != nil {
log.Fatalf("err %v", err)
}
} else {
log.Println("configfile is not encoded")
byteData = []byte(p.KubeConfig)
}
if err := ioutil.WriteFile(p.KubeConfigFile, byteData, 0600); err != nil {
return fmt.Errorf("error creating kube config file: %v", err)
}
log.Println("setting KUBECONFIG environment variable to: " + p.KubeConfigFile)
err := os.Setenv("KUBECONFIG", p.KubeConfigFile)
if err != nil {
log.Fatalf("err %v", err)
}
}
// Providing a Kubernetes config context is mostly used for Drone support.
// If not provided, the current context from Kubernetes config is used.
if p.KubeContext != "" {
log.Println("Setting Kubernetes context")
cb := commandbuilder.CommandBuilder{Name: kubectlBin}
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "config"})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "use-context"})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: p.KubeContext})
cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "kubeconfig", Value: p.KubeConfigFile})
cmd := cb.Command()
if err := utils.Run(cmd, false); err != nil {
return fmt.Errorf("error setting Kubernetes context: %v", err)
}
}
return nil
}
func (p *Plugin) overrides(release *types.Release) (args []commandbuilder.Arg) {
// Add override files
for _, fileName := range p.ValueFiles {
log.Println("Adding override file:", fileName)
args = append(args, commandbuilder.Arg{
Type: commandbuilder.ArgTypeShortParam,
Name: "f",
Value: strings.TrimSpace(fileName)})
}
path := fmt.Sprintf("values/%s/default.yaml", release.Name)
if _, err := os.Stat(path); err == nil {
log.Println("Adding override file:", path)
args = append(args, commandbuilder.Arg{
Type: commandbuilder.ArgTypeShortParam,
Name: "f",
Value: path})
}
for _, path := range release.ValueFiles {
if _, err := os.Stat(path); err != nil {
log.Println("WARN: Value file does not exist:", path)
continue
}
log.Println("Adding override file:", path)
args = append(args, commandbuilder.Arg{
Type: commandbuilder.ArgTypeShortParam,
Name: "f",
Value: path})
}
path = fmt.Sprintf("values/%s/%s.yaml", release.Name, p.ClusterConfig.Name)
if _, err := os.Stat(path); p.ClusterConfig.Name != "" && err == nil {
log.Println("Adding override file:", path)
args = append(args, commandbuilder.Arg{
Type: commandbuilder.ArgTypeShortParam,
Name: "f",
Value: path})
}
// Handle individual value overrides
for _, override := range release.Overrides {
log.Println("Overriding value for:", override.Target)
arg, err := override.BuildArg()
if err != nil {
log.Println("WARNING: Could not get override value. Skipping override:", err)
continue
}
args = append(args, *arg)
}
return args
}