|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/base64" |
4 | 5 | "fmt" |
5 | 6 | "io/ioutil" |
6 | 7 | "log" |
@@ -30,6 +31,8 @@ type Plugin struct { |
30 | 31 | ClustersList report.Clusters |
31 | 32 | ValueFiles []string |
32 | 33 | KubeConfig string |
| 34 | + KubeConfigFile string |
| 35 | + KubeConfigBase64 bool |
33 | 36 | KubeContext string |
34 | 37 | Dryrun bool |
35 | 38 | Diffrun bool |
@@ -336,22 +339,45 @@ func (p *Plugin) templateChart(release *types.Release) (string, error) { |
336 | 339 | } |
337 | 340 |
|
338 | 341 | func (p *Plugin) setupKubeconfig() error { |
339 | | - // Providing a Kubernetes config is mostly used for Drone support. |
340 | | - // If not provided, the default `kubectl` search path is used. |
341 | | - // WARNING: this may overwrite your config if it already exists. |
| 342 | + var err error |
| 343 | + byteData := []byte{} |
| 344 | + |
342 | 345 | if p.KubeConfig != "" { |
343 | | - log.Println("Creating Kubernetes config") |
344 | | - if err := ioutil.WriteFile(kubeConfig, []byte(p.KubeConfig), 0600); err != nil { |
| 346 | + p.KubeConfigFile = kubeConfig + "-" + p.KubeContext |
| 347 | + log.Println("Creating Kubernetes configfile" + p.KubeConfigFile) |
| 348 | + if p.KubeConfigBase64 { |
| 349 | + log.Println("configfile is base64 encoded") |
| 350 | + byteData, err = base64.StdEncoding.DecodeString(p.KubeConfig) |
| 351 | + if err != nil { |
| 352 | + log.Fatalf("err %v", err) |
| 353 | + } |
| 354 | + |
| 355 | + } else { |
| 356 | + log.Println("configfile is not encoded") |
| 357 | + byteData = []byte(p.KubeConfig) |
| 358 | + } |
| 359 | + |
| 360 | + if err := ioutil.WriteFile(p.KubeConfigFile, byteData, 0600); err != nil { |
345 | 361 | return fmt.Errorf("error creating kube config file: %v", err) |
346 | 362 | } |
| 363 | + log.Println("setting KUBECONFIG environment variable to: " + p.KubeConfigFile) |
| 364 | + err := os.Setenv("KUBECONFIG", p.KubeConfigFile) |
| 365 | + if err != nil { |
| 366 | + log.Fatalf("err %v", err) |
| 367 | + } |
347 | 368 | } |
348 | 369 |
|
349 | 370 | // Providing a Kubernetes config context is mostly used for Drone support. |
350 | 371 | // If not provided, the current context from Kubernetes config is used. |
351 | 372 | if p.KubeContext != "" { |
352 | 373 | log.Println("Setting Kubernetes context") |
353 | | - cmd := exec.Command(kubectlBin, "config", "use-context", p.KubeContext) |
354 | | - if err := utils.Run(cmd, true); err != nil { |
| 374 | + cb := commandbuilder.CommandBuilder{Name: kubectlBin} |
| 375 | + cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "config"}) |
| 376 | + cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: "use-context"}) |
| 377 | + cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeRaw, Value: p.KubeContext}) |
| 378 | + cb.Add(commandbuilder.Arg{Type: commandbuilder.ArgTypeLongParam, Name: "kubeconfig", Value: p.KubeConfigFile}) |
| 379 | + cmd := cb.Command() |
| 380 | + if err := utils.Run(cmd, false); err != nil { |
355 | 381 | return fmt.Errorf("error setting Kubernetes context: %v", err) |
356 | 382 | } |
357 | 383 | } |
|
0 commit comments