2019-12-12 18:20:11 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InitKube is a step in a helm Plan that initializes the kubernetes config file.
|
|
|
|
type InitKube struct {
|
|
|
|
SkipTLSVerify bool
|
|
|
|
Certificate string
|
|
|
|
APIServer string
|
|
|
|
ServiceAccount string
|
|
|
|
Token string
|
|
|
|
TemplateFile string
|
2019-12-23 20:43:17 +00:00
|
|
|
ConfigFile string
|
2019-12-12 18:20:11 +00:00
|
|
|
|
|
|
|
template *template.Template
|
|
|
|
configFile io.WriteCloser
|
|
|
|
values kubeValues
|
|
|
|
}
|
|
|
|
|
|
|
|
type kubeValues struct {
|
|
|
|
SkipTLSVerify bool
|
|
|
|
Certificate string
|
|
|
|
APIServer string
|
|
|
|
Namespace string
|
|
|
|
ServiceAccount string
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute generates a kubernetes config file from drone-helm3's template.
|
|
|
|
func (i *InitKube) Execute(cfg Config) error {
|
|
|
|
if cfg.Debug {
|
2019-12-23 20:43:17 +00:00
|
|
|
fmt.Fprintf(cfg.Stderr, "writing kubeconfig file to %s\n", i.ConfigFile)
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
defer i.configFile.Close()
|
|
|
|
return i.template.Execute(i.configFile, i.values)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare ensures all required configuration is present and that the config file is writable.
|
|
|
|
func (i *InitKube) Prepare(cfg Config) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if i.APIServer == "" {
|
|
|
|
return errors.New("an API Server is needed to deploy")
|
|
|
|
}
|
|
|
|
if i.Token == "" {
|
|
|
|
return errors.New("token is needed to deploy")
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ServiceAccount == "" {
|
|
|
|
i.ServiceAccount = "helm"
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Debug {
|
|
|
|
fmt.Fprintf(cfg.Stderr, "loading kubeconfig template from %s\n", i.TemplateFile)
|
|
|
|
}
|
|
|
|
i.template, err = template.ParseFiles(i.TemplateFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not load kubeconfig template: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.values = kubeValues{
|
|
|
|
SkipTLSVerify: i.SkipTLSVerify,
|
|
|
|
Certificate: i.Certificate,
|
|
|
|
APIServer: i.APIServer,
|
|
|
|
ServiceAccount: i.ServiceAccount,
|
|
|
|
Token: i.Token,
|
|
|
|
Namespace: cfg.Namespace,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Debug {
|
2019-12-23 20:43:17 +00:00
|
|
|
if _, err := os.Stat(i.ConfigFile); err != nil {
|
2019-12-12 18:20:11 +00:00
|
|
|
// non-nil err here isn't an actual error state; the kubeconfig just doesn't exist
|
|
|
|
fmt.Fprint(cfg.Stderr, "creating ")
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(cfg.Stderr, "truncating ")
|
|
|
|
}
|
2019-12-23 20:43:17 +00:00
|
|
|
fmt.Fprintf(cfg.Stderr, "kubeconfig file at %s\n", i.ConfigFile)
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 20:43:17 +00:00
|
|
|
i.configFile, err = os.Create(i.ConfigFile)
|
2019-12-12 18:20:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open kubeconfig file for writing: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|