woodpecker-helm3/internal/helm/plan.go

139 lines
3.1 KiB
Go
Raw Normal View History

2019-12-09 17:56:02 +00:00
package helm
import (
"errors"
"fmt"
"github.com/pelotech/drone-helm3/internal/env"
2019-12-09 17:56:02 +00:00
"github.com/pelotech/drone-helm3/internal/run"
"os"
2019-12-09 17:56:02 +00:00
)
const (
kubeConfigTemplate = "/root/.kube/config.tpl"
kubeConfigFile = "/root/.kube/config"
)
// A Step is one step in the plan.
2019-12-09 17:56:02 +00:00
type Step interface {
Prepare() error
Execute() error
2019-12-09 17:56:02 +00:00
}
// A Plan is a series of steps to perform.
2019-12-09 17:56:02 +00:00
type Plan struct {
steps []Step
cfg env.Config
2019-12-09 17:56:02 +00:00
}
// NewPlan makes a plan for running a helm operation.
func NewPlan(cfg env.Config) (*Plan, error) {
p := Plan{
cfg: cfg,
}
if cfg.UpdateDependencies && cfg.DependenciesAction != "" {
return nil, errors.New("update_dependencies is deprecated and cannot be provided together with dependencies_action")
}
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(); err != nil {
err = fmt.Errorf("while preparing %T step: %w", step, err)
2019-12-09 17:56:02 +00:00
return nil, err
}
}
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 env.Config) *func(env.Config) []Step {
switch cfg.Command {
case "upgrade":
return &upgrade
case "uninstall", "delete":
return &uninstall
2019-12-09 17:56:02 +00:00
case "lint":
return &lint
2019-12-09 17:56:02 +00:00
case "help":
return &help
2019-12-09 17:56:02 +00:00
default:
switch cfg.DroneEvent {
case "push", "tag", "deployment", "pull_request", "promote", "rollback":
return &upgrade
case "delete":
return &uninstall
2019-12-09 17:56:02 +00:00
default:
2019-12-26 18:47:42 +00:00
return &help
2019-12-09 17:56:02 +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 {
for i, step := range p.steps {
if p.cfg.Debug {
fmt.Fprintf(p.cfg.Stderr, "calling %T.Execute (step %d)\n", step, i)
}
if err := step.Execute(); err != nil {
return fmt.Errorf("while executing %T step: %w", step, err)
2019-12-09 17:56:02 +00:00
}
}
return nil
}
var upgrade = func(cfg env.Config) []Step {
var steps []Step
steps = append(steps, run.NewInitKube(cfg, kubeConfigTemplate, kubeConfigFile))
for _, repo := range cfg.AddRepos {
steps = append(steps, run.NewAddRepo(cfg, repo))
}
if cfg.DependenciesAction != "" {
steps = append(steps, run.NewDepAction(cfg))
}
if cfg.UpdateDependencies {
steps = append(steps, run.NewDepUpdate(cfg))
}
steps = append(steps, run.NewUpgrade(cfg))
2019-12-09 17:56:02 +00:00
return steps
2019-12-09 17:56:02 +00:00
}
var uninstall = func(cfg env.Config) []Step {
var steps []Step
steps = append(steps, run.NewInitKube(cfg, kubeConfigTemplate, kubeConfigFile))
if cfg.UpdateDependencies {
steps = append(steps, run.NewDepUpdate(cfg))
}
steps = append(steps, run.NewUninstall(cfg))
return steps
}
var lint = func(cfg env.Config) []Step {
var steps []Step
for _, repo := range cfg.AddRepos {
steps = append(steps, run.NewAddRepo(cfg, repo))
}
if cfg.UpdateDependencies {
steps = append(steps, run.NewDepUpdate(cfg))
}
steps = append(steps, run.NewLint(cfg))
return steps
}
var help = func(cfg env.Config) []Step {
return []Step{run.NewHelp(cfg)}
}