2019-12-05 22:35:25 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
2019-12-10 00:25:47 +00:00
|
|
|
"fmt"
|
2021-05-19 17:16:43 +00:00
|
|
|
|
2020-01-16 21:50:04 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/env"
|
2019-12-05 22:35:25 +00:00
|
|
|
)
|
|
|
|
|
2019-12-10 00:25:47 +00:00
|
|
|
// Upgrade is an execution step that calls `helm upgrade` when executed.
|
2019-12-09 17:56:02 +00:00
|
|
|
type Upgrade struct {
|
2020-01-17 18:13:53 +00:00
|
|
|
*config
|
|
|
|
chart string
|
|
|
|
release string
|
2019-12-10 00:25:47 +00:00
|
|
|
|
2020-08-19 03:11:11 +00:00
|
|
|
chartVersion string
|
|
|
|
dryRun bool
|
|
|
|
wait bool
|
|
|
|
values string
|
|
|
|
stringValues string
|
|
|
|
valuesFiles []string
|
|
|
|
reuseValues bool
|
|
|
|
timeout string
|
|
|
|
force bool
|
|
|
|
atomic bool
|
|
|
|
cleanupOnFail bool
|
2021-05-19 17:16:43 +00:00
|
|
|
historyMax int
|
2020-08-19 03:11:11 +00:00
|
|
|
certs *repoCerts
|
|
|
|
createNamespace bool
|
2021-07-20 18:21:44 +00:00
|
|
|
skipCrds bool
|
2019-12-10 00:25:47 +00:00
|
|
|
|
|
|
|
cmd cmd
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 21:50:04 +00:00
|
|
|
// NewUpgrade creates an Upgrade using fields from the given Config. No validation is performed at this time.
|
|
|
|
func NewUpgrade(cfg env.Config) *Upgrade {
|
|
|
|
return &Upgrade{
|
2020-08-19 03:11:11 +00:00
|
|
|
config: newConfig(cfg),
|
|
|
|
chart: cfg.Chart,
|
|
|
|
release: cfg.Release,
|
|
|
|
chartVersion: cfg.ChartVersion,
|
|
|
|
dryRun: cfg.DryRun,
|
|
|
|
wait: cfg.Wait,
|
|
|
|
values: cfg.Values,
|
|
|
|
stringValues: cfg.StringValues,
|
|
|
|
valuesFiles: cfg.ValuesFiles,
|
|
|
|
reuseValues: cfg.ReuseValues,
|
|
|
|
timeout: cfg.Timeout,
|
|
|
|
force: cfg.Force,
|
|
|
|
atomic: cfg.AtomicUpgrade,
|
|
|
|
cleanupOnFail: cfg.CleanupOnFail,
|
2021-05-19 17:16:43 +00:00
|
|
|
historyMax: cfg.HistoryMax,
|
2020-08-19 03:11:11 +00:00
|
|
|
certs: newRepoCerts(cfg),
|
|
|
|
createNamespace: cfg.CreateNamespace,
|
2021-07-20 18:21:44 +00:00
|
|
|
skipCrds: cfg.SkipCrds,
|
2020-01-16 21:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:25:47 +00:00
|
|
|
// Execute executes the `helm upgrade` command.
|
2020-01-16 23:30:21 +00:00
|
|
|
func (u *Upgrade) Execute() error {
|
2019-12-09 17:56:02 +00:00
|
|
|
return u.cmd.Run()
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:25:47 +00:00
|
|
|
// Prepare gets the Upgrade ready to execute.
|
2020-01-17 18:13:53 +00:00
|
|
|
func (u *Upgrade) Prepare() error {
|
|
|
|
if u.chart == "" {
|
2019-12-17 00:55:54 +00:00
|
|
|
return fmt.Errorf("chart is required")
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.release == "" {
|
2019-12-17 00:55:54 +00:00
|
|
|
return fmt.Errorf("release is required")
|
|
|
|
}
|
|
|
|
|
2020-01-17 19:12:53 +00:00
|
|
|
args := u.globalFlags()
|
2019-12-17 00:55:05 +00:00
|
|
|
args = append(args, "upgrade", "--install")
|
2019-12-10 00:25:47 +00:00
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.chartVersion != "" {
|
|
|
|
args = append(args, "--version", u.chartVersion)
|
2019-12-17 00:55:05 +00:00
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.dryRun {
|
2019-12-17 00:55:05 +00:00
|
|
|
args = append(args, "--dry-run")
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.wait {
|
2019-12-17 00:55:05 +00:00
|
|
|
args = append(args, "--wait")
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.reuseValues {
|
2019-12-17 00:55:05 +00:00
|
|
|
args = append(args, "--reuse-values")
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.timeout != "" {
|
|
|
|
args = append(args, "--timeout", u.timeout)
|
2019-12-17 00:55:05 +00:00
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.force {
|
2019-12-17 00:55:05 +00:00
|
|
|
args = append(args, "--force")
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.atomic {
|
2020-01-07 20:17:54 +00:00
|
|
|
args = append(args, "--atomic")
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.cleanupOnFail {
|
2020-01-07 20:53:55 +00:00
|
|
|
args = append(args, "--cleanup-on-fail")
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.values != "" {
|
|
|
|
args = append(args, "--set", u.values)
|
2019-12-17 00:55:05 +00:00
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.stringValues != "" {
|
|
|
|
args = append(args, "--set-string", u.stringValues)
|
2019-12-17 00:55:05 +00:00
|
|
|
}
|
2020-08-19 03:11:11 +00:00
|
|
|
if u.createNamespace {
|
|
|
|
args = append(args, "--create-namespace")
|
|
|
|
}
|
2021-07-20 18:21:44 +00:00
|
|
|
if u.skipCrds {
|
|
|
|
args = append(args, "--skip-crds")
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
for _, vFile := range u.valuesFiles {
|
2019-12-17 00:55:05 +00:00
|
|
|
args = append(args, "--values", vFile)
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
2020-01-20 23:40:36 +00:00
|
|
|
args = append(args, u.certs.flags()...)
|
2019-12-05 22:35:25 +00:00
|
|
|
|
2021-05-19 17:16:43 +00:00
|
|
|
// always set --history-max since it defaults to non-zero value
|
|
|
|
args = append(args, fmt.Sprintf("--history-max=%d", u.historyMax))
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
args = append(args, u.release, u.chart)
|
2019-12-10 00:25:47 +00:00
|
|
|
u.cmd = command(helmBin, args...)
|
2020-01-17 18:13:53 +00:00
|
|
|
u.cmd.Stdout(u.stdout)
|
|
|
|
u.cmd.Stderr(u.stderr)
|
2019-12-10 00:25:47 +00:00
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
if u.debug {
|
|
|
|
fmt.Fprintf(u.stderr, "Generated command: '%s'\n", u.cmd.String())
|
2019-12-10 00:25:47 +00:00
|
|
|
}
|
2019-12-05 22:35:25 +00:00
|
|
|
|
2019-12-10 00:25:47 +00:00
|
|
|
return nil
|
2019-12-05 22:35:25 +00:00
|
|
|
}
|