2019-12-09 17:56:02 +00:00
|
|
|
package helm
|
|
|
|
|
|
|
|
import (
|
2019-12-10 00:25:47 +00:00
|
|
|
"fmt"
|
2019-12-09 17:56:02 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/run"
|
2019-12-10 00:25:47 +00:00
|
|
|
"os"
|
2019-12-09 17:56:02 +00:00
|
|
|
)
|
|
|
|
|
2019-12-23 20:43:17 +00:00
|
|
|
const (
|
|
|
|
kubeConfigTemplate = "/root/.kube/config.tpl"
|
|
|
|
kubeConfigFile = "/root/.kube/config"
|
|
|
|
)
|
2019-12-12 18:20:11 +00:00
|
|
|
|
2019-12-09 18:52:41 +00:00
|
|
|
// A Step is one step in the plan.
|
2019-12-09 17:56:02 +00:00
|
|
|
type Step interface {
|
2019-12-10 00:25:47 +00:00
|
|
|
Prepare(run.Config) error
|
2019-12-12 18:20:11 +00:00
|
|
|
Execute(run.Config) error
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 18:52:41 +00:00
|
|
|
// A Plan is a series of steps to perform.
|
2019-12-09 17:56:02 +00:00
|
|
|
type Plan struct {
|
2019-12-12 18:20:11 +00:00
|
|
|
steps []Step
|
|
|
|
cfg Config
|
|
|
|
runCfg run.Config
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 18:52:41 +00:00
|
|
|
// NewPlan makes a plan for running a helm operation.
|
2019-12-09 17:56:02 +00:00
|
|
|
func NewPlan(cfg Config) (*Plan, error) {
|
2019-12-12 18:20:11 +00:00
|
|
|
p := Plan{
|
|
|
|
cfg: cfg,
|
|
|
|
runCfg: run.Config{
|
|
|
|
Debug: cfg.Debug,
|
|
|
|
Values: cfg.Values,
|
|
|
|
StringValues: cfg.StringValues,
|
|
|
|
ValuesFiles: cfg.ValuesFiles,
|
|
|
|
Namespace: cfg.Namespace,
|
2019-12-24 19:08:09 +00:00
|
|
|
Stdout: cfg.Stdout,
|
|
|
|
Stderr: cfg.Stderr,
|
2019-12-12 18:20:11 +00:00
|
|
|
},
|
2019-12-10 00:25:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
p.steps = (*determineSteps(cfg))(cfg)
|
|
|
|
|
|
|
|
for i, step := range p.steps {
|
|
|
|
if cfg.Debug {
|
|
|
|
fmt.Fprintf(os.Stderr, "calling %T.Prepare (step %d)\n", step, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := step.Prepare(p.runCfg); err != nil {
|
|
|
|
err = fmt.Errorf("while preparing %T step: %w", step, err)
|
2019-12-09 17:56:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// determineSteps is primarily for the tests' convenience: it allows testing the "which stuff should
|
|
|
|
// we do" logic without building a config that meets all the steps' requirements.
|
|
|
|
func determineSteps(cfg Config) *func(Config) []Step {
|
|
|
|
switch cfg.Command {
|
|
|
|
case "upgrade":
|
|
|
|
return &upgrade
|
2019-12-19 23:02:49 +00:00
|
|
|
case "uninstall", "delete":
|
|
|
|
return &uninstall
|
2019-12-09 17:56:02 +00:00
|
|
|
case "lint":
|
2019-12-18 01:14:39 +00:00
|
|
|
return &lint
|
2019-12-09 17:56:02 +00:00
|
|
|
case "help":
|
2019-12-12 18:20:11 +00:00
|
|
|
return &help
|
2019-12-09 17:56:02 +00:00
|
|
|
default:
|
|
|
|
switch cfg.DroneEvent {
|
|
|
|
case "push", "tag", "deployment", "pull_request", "promote", "rollback":
|
2019-12-12 18:20:11 +00:00
|
|
|
return &upgrade
|
2019-12-19 00:58:31 +00:00
|
|
|
case "delete":
|
2019-12-19 23:02:49 +00:00
|
|
|
return &uninstall
|
2019-12-09 17:56:02 +00:00
|
|
|
default:
|
2019-12-12 18:20:11 +00:00
|
|
|
panic("not implemented")
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 18:52:41 +00:00
|
|
|
// Execute runs each step in the plan, aborting and reporting on error
|
2019-12-09 17:56:02 +00:00
|
|
|
func (p *Plan) Execute() error {
|
2019-12-12 18:20:11 +00:00
|
|
|
for i, step := range p.steps {
|
|
|
|
if p.cfg.Debug {
|
2019-12-24 19:08:09 +00:00
|
|
|
fmt.Fprintf(p.cfg.Stderr, "calling %T.Execute (step %d)\n", step, i)
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := step.Execute(p.runCfg); err != nil {
|
2019-12-24 23:49:47 +00:00
|
|
|
return fmt.Errorf("while executing %T step: %w", step, err)
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
var upgrade = func(cfg Config) []Step {
|
2019-12-19 01:13:17 +00:00
|
|
|
steps := initKube(cfg)
|
2019-12-12 18:20:11 +00:00
|
|
|
|
|
|
|
steps = append(steps, &run.Upgrade{
|
2019-12-10 00:25:47 +00:00
|
|
|
Chart: cfg.Chart,
|
|
|
|
Release: cfg.Release,
|
|
|
|
ChartVersion: cfg.ChartVersion,
|
2019-12-17 17:23:44 +00:00
|
|
|
DryRun: cfg.DryRun,
|
2019-12-10 00:25:47 +00:00
|
|
|
Wait: cfg.Wait,
|
|
|
|
ReuseValues: cfg.ReuseValues,
|
|
|
|
Timeout: cfg.Timeout,
|
|
|
|
Force: cfg.Force,
|
2019-12-12 18:20:11 +00:00
|
|
|
})
|
2019-12-09 17:56:02 +00:00
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
return steps
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
2019-12-10 00:25:47 +00:00
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
var uninstall = func(cfg Config) []Step {
|
2019-12-19 01:13:17 +00:00
|
|
|
steps := initKube(cfg)
|
2019-12-19 23:02:49 +00:00
|
|
|
steps = append(steps, &run.Uninstall{
|
2019-12-19 01:13:17 +00:00
|
|
|
Release: cfg.Release,
|
|
|
|
DryRun: cfg.DryRun,
|
|
|
|
})
|
|
|
|
|
|
|
|
return steps
|
|
|
|
}
|
|
|
|
|
2019-12-18 01:14:39 +00:00
|
|
|
var lint = func(cfg Config) []Step {
|
|
|
|
lint := &run.Lint{
|
|
|
|
Chart: cfg.Chart,
|
|
|
|
}
|
|
|
|
|
|
|
|
return []Step{lint}
|
|
|
|
}
|
|
|
|
|
2019-12-19 01:13:17 +00:00
|
|
|
var help = func(cfg Config) []Step {
|
|
|
|
help := &run.Help{}
|
|
|
|
return []Step{help}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initKube(cfg Config) []Step {
|
2019-12-19 00:58:31 +00:00
|
|
|
return []Step{
|
|
|
|
&run.InitKube{
|
|
|
|
SkipTLSVerify: cfg.SkipTLSVerify,
|
|
|
|
Certificate: cfg.Certificate,
|
|
|
|
APIServer: cfg.APIServer,
|
|
|
|
ServiceAccount: cfg.ServiceAccount,
|
|
|
|
Token: cfg.KubeToken,
|
|
|
|
TemplateFile: kubeConfigTemplate,
|
2019-12-23 20:43:17 +00:00
|
|
|
ConfigFile: kubeConfigFile,
|
2019-12-19 00:58:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|