Deduplicate the kubeValues data in InitKube [#67]
Now that the InitKube initialization happens inside its own package, the private .values field can be populated at the same time, rather than having to wait for Prepare(). Also clarified the config/template filename fields (configFile vs. ConfigFile was particularly ambiguous).
This commit is contained in:
@@ -11,17 +11,11 @@ import (
|
||||
|
||||
// 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
|
||||
ConfigFile string
|
||||
|
||||
template *template.Template
|
||||
configFile io.WriteCloser
|
||||
values kubeValues
|
||||
templateFilename string
|
||||
configFilename string
|
||||
template *template.Template
|
||||
configFile io.WriteCloser
|
||||
values kubeValues
|
||||
}
|
||||
|
||||
type kubeValues struct {
|
||||
@@ -36,20 +30,23 @@ type kubeValues struct {
|
||||
// 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{
|
||||
SkipTLSVerify: cfg.SkipTLSVerify,
|
||||
Certificate: cfg.Certificate,
|
||||
APIServer: cfg.APIServer,
|
||||
ServiceAccount: cfg.ServiceAccount,
|
||||
Token: cfg.KubeToken,
|
||||
TemplateFile: templateFile,
|
||||
ConfigFile: configFile,
|
||||
values: kubeValues{
|
||||
SkipTLSVerify: cfg.SkipTLSVerify,
|
||||
Certificate: cfg.Certificate,
|
||||
APIServer: cfg.APIServer,
|
||||
Namespace: cfg.Namespace,
|
||||
ServiceAccount: cfg.ServiceAccount,
|
||||
Token: cfg.KubeToken,
|
||||
},
|
||||
templateFilename: templateFile,
|
||||
configFilename: configFile,
|
||||
}
|
||||
}
|
||||
|
||||
// Execute generates a kubernetes config file from drone-helm3's template.
|
||||
func (i *InitKube) Execute(cfg Config) error {
|
||||
if cfg.Debug {
|
||||
fmt.Fprintf(cfg.Stderr, "writing kubeconfig file to %s\n", i.ConfigFile)
|
||||
fmt.Fprintf(cfg.Stderr, "writing kubeconfig file to %s\n", i.configFilename)
|
||||
}
|
||||
defer i.configFile.Close()
|
||||
return i.template.Execute(i.configFile, i.values)
|
||||
@@ -59,45 +56,36 @@ func (i *InitKube) Execute(cfg Config) error {
|
||||
func (i *InitKube) Prepare(cfg Config) error {
|
||||
var err error
|
||||
|
||||
if i.APIServer == "" {
|
||||
if i.values.APIServer == "" {
|
||||
return errors.New("an API Server is needed to deploy")
|
||||
}
|
||||
if i.Token == "" {
|
||||
if i.values.Token == "" {
|
||||
return errors.New("token is needed to deploy")
|
||||
}
|
||||
|
||||
if i.ServiceAccount == "" {
|
||||
i.ServiceAccount = "helm"
|
||||
if i.values.ServiceAccount == "" {
|
||||
i.values.ServiceAccount = "helm"
|
||||
}
|
||||
|
||||
if cfg.Debug {
|
||||
fmt.Fprintf(cfg.Stderr, "loading kubeconfig template from %s\n", i.TemplateFile)
|
||||
fmt.Fprintf(cfg.Stderr, "loading kubeconfig template from %s\n", i.templateFilename)
|
||||
}
|
||||
i.template, err = template.ParseFiles(i.TemplateFile)
|
||||
i.template, err = template.ParseFiles(i.templateFilename)
|
||||
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 {
|
||||
if _, err := os.Stat(i.ConfigFile); err != nil {
|
||||
if _, err := os.Stat(i.configFilename); err != nil {
|
||||
// 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 ")
|
||||
}
|
||||
fmt.Fprintf(cfg.Stderr, "kubeconfig file at %s\n", i.ConfigFile)
|
||||
fmt.Fprintf(cfg.Stderr, "kubeconfig file at %s\n", i.configFilename)
|
||||
}
|
||||
|
||||
i.configFile, err = os.Create(i.ConfigFile)
|
||||
i.configFile, err = os.Create(i.configFilename)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not open kubeconfig file for writing: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user