2019-12-12 18:20:11 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-01-16 21:50:04 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/env"
|
2019-12-12 18:20:11 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InitKube is a step in a helm Plan that initializes the kubernetes config file.
|
|
|
|
type InitKube struct {
|
2020-01-17 18:13:53 +00:00
|
|
|
*config
|
2020-01-16 23:11:42 +00:00
|
|
|
templateFilename string
|
|
|
|
configFilename string
|
|
|
|
template *template.Template
|
|
|
|
configFile io.WriteCloser
|
|
|
|
values kubeValues
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type kubeValues struct {
|
|
|
|
SkipTLSVerify bool
|
|
|
|
Certificate string
|
|
|
|
APIServer string
|
|
|
|
Namespace string
|
|
|
|
ServiceAccount string
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
2020-01-16 21:50:04 +00:00
|
|
|
// NewInitKube creates a InitKube using the given Config and filepaths. No validation is performed at this time.
|
|
|
|
func NewInitKube(cfg env.Config, templateFile, configFile string) *InitKube {
|
|
|
|
return &InitKube{
|
2020-01-17 18:13:53 +00:00
|
|
|
config: newConfig(cfg),
|
2020-01-16 23:11:42 +00:00
|
|
|
values: kubeValues{
|
|
|
|
SkipTLSVerify: cfg.SkipTLSVerify,
|
|
|
|
Certificate: cfg.Certificate,
|
|
|
|
APIServer: cfg.APIServer,
|
|
|
|
Namespace: cfg.Namespace,
|
|
|
|
ServiceAccount: cfg.ServiceAccount,
|
|
|
|
Token: cfg.KubeToken,
|
|
|
|
},
|
|
|
|
templateFilename: templateFile,
|
|
|
|
configFilename: configFile,
|
2020-01-16 21:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
// Execute generates a kubernetes config file from drone-helm3's template.
|
2020-01-16 23:30:21 +00:00
|
|
|
func (i *InitKube) Execute() error {
|
|
|
|
if i.debug {
|
|
|
|
fmt.Fprintf(i.stderr, "writing kubeconfig file to %s\n", i.configFilename)
|
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.
|
2020-01-17 18:13:53 +00:00
|
|
|
func (i *InitKube) Prepare() error {
|
2019-12-12 18:20:11 +00:00
|
|
|
var err error
|
|
|
|
|
2020-01-16 23:11:42 +00:00
|
|
|
if i.values.APIServer == "" {
|
2019-12-12 18:20:11 +00:00
|
|
|
return errors.New("an API Server is needed to deploy")
|
|
|
|
}
|
2020-01-16 23:11:42 +00:00
|
|
|
if i.values.Token == "" {
|
2019-12-12 18:20:11 +00:00
|
|
|
return errors.New("token is needed to deploy")
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:11:42 +00:00
|
|
|
if i.values.ServiceAccount == "" {
|
|
|
|
i.values.ServiceAccount = "helm"
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 23:30:21 +00:00
|
|
|
if i.debug {
|
|
|
|
fmt.Fprintf(i.stderr, "loading kubeconfig template from %s\n", i.templateFilename)
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
2020-01-16 23:11:42 +00:00
|
|
|
i.template, err = template.ParseFiles(i.templateFilename)
|
2019-12-12 18:20:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not load kubeconfig template: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:30:21 +00:00
|
|
|
if i.debug {
|
2020-01-16 23:11:42 +00:00
|
|
|
if _, err := os.Stat(i.configFilename); 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
|
2020-01-16 23:30:21 +00:00
|
|
|
fmt.Fprint(i.stderr, "creating ")
|
2019-12-12 18:20:11 +00:00
|
|
|
} else {
|
2020-01-16 23:30:21 +00:00
|
|
|
fmt.Fprint(i.stderr, "truncating ")
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
2020-01-16 23:30:21 +00:00
|
|
|
fmt.Fprintf(i.stderr, "kubeconfig file at %s\n", i.configFilename)
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 23:11:42 +00:00
|
|
|
i.configFile, err = os.Create(i.configFilename)
|
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
|
|
|
|
}
|