From 0ecfd56fe223f1b41330261d1f855a26e3ba110a Mon Sep 17 00:00:00 2001 From: igorushi Date: Mon, 27 Mar 2023 18:35:56 +0300 Subject: [PATCH] added a skip-empty flag to upgrade command Signed-off-by: igorushi --- cmd/helm/status.go | 2 +- cmd/helm/testdata/output/upgrade-with-skip.txt | 1 + cmd/helm/upgrade.go | 7 ++++++- cmd/helm/upgrade_test.go | 6 ++++++ pkg/action/upgrade.go | 13 ++++++++++++- pkg/release/release.go | 2 ++ 6 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 cmd/helm/testdata/output/upgrade-with-skip.txt diff --git a/cmd/helm/status.go b/cmd/helm/status.go index aa22aa02a..27a0cfb96 100644 --- a/cmd/helm/status.go +++ b/cmd/helm/status.go @@ -123,7 +123,7 @@ func (s statusPrinter) WriteYAML(out io.Writer) error { } func (s statusPrinter) WriteTable(out io.Writer) error { - if s.release == nil { + if s.release == nil || s.release.Skipped { return nil } fmt.Fprintf(out, "NAME: %s\n", s.release.Name) diff --git a/cmd/helm/testdata/output/upgrade-with-skip.txt b/cmd/helm/testdata/output/upgrade-with-skip.txt new file mode 100644 index 000000000..4e4015ab2 --- /dev/null +++ b/cmd/helm/testdata/output/upgrade-with-skip.txt @@ -0,0 +1 @@ +Release "funny-bunny" has been skipped. Happy Helming! diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index 145d342b7..dbf496d33 100644 --- a/cmd/helm/upgrade.go +++ b/cmd/helm/upgrade.go @@ -210,7 +210,11 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { } if outfmt == output.Table { - fmt.Fprintf(out, "Release %q has been upgraded. Happy Helming!\n", args[0]) + if rel.Skipped { + fmt.Fprintf(out, "Release %q has been skipped. Happy Helming!\n", args[0]) + } else { + fmt.Fprintf(out, "Release %q has been upgraded. Happy Helming!\n", args[0]) + } } return outfmt.Write(out, &statusPrinter{rel, settings.Debug, false, false}) @@ -240,6 +244,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f.StringVar(&client.Description, "description", "", "add a custom description") f.BoolVar(&client.DependencyUpdate, "dependency-update", false, "update dependencies if they are missing before installing the chart") f.BoolVar(&client.EnableDNS, "enable-dns", false, "enable DNS lookups when rendering templates") + f.BoolVar(&client.SkipEmptyUpgrade, "skip-empty", false, "skip upgrade if there are no changes") addChartPathOptionsFlags(f, &client.ChartPathOptions) addValueOptionsFlags(f, valueOpts) bindOutputFlag(cmd, &outfmt) diff --git a/cmd/helm/upgrade_test.go b/cmd/helm/upgrade_test.go index e366f8d19..6de634db7 100644 --- a/cmd/helm/upgrade_test.go +++ b/cmd/helm/upgrade_test.go @@ -168,6 +168,12 @@ func TestUpgradeCmd(t *testing.T) { golden: "output/upgrade.txt", rels: []*release.Release{relWithStatusMock("funny-bunny", 2, ch, release.StatusFailed)}, }, + { + name: "upgrade skip empty release", + cmd: fmt.Sprintf("upgrade --skip-empty funny-bunny '%s' ", chartPath), + golden: "output/upgrade-with-skip.txt", + rels: []*release.Release{release.Mock(&release.MockReleaseOptions{Name: "funny-bunny", Version: 2, Chart: ch, Status: release.StatusDeployed})}, + }, { name: "upgrade a pending install release", cmd: fmt.Sprintf("upgrade funny-bunny '%s'", chartPath), diff --git a/pkg/action/upgrade.go b/pkg/action/upgrade.go index 829be51df..d7e0ba551 100644 --- a/pkg/action/upgrade.go +++ b/pkg/action/upgrade.go @@ -106,6 +106,8 @@ type Upgrade struct { Lock sync.Mutex // Enable DNS lookups when rendering templates EnableDNS bool + // Skip upgrade if there are no changes + SkipEmptyUpgrade bool } type resultMessage struct { @@ -161,7 +163,9 @@ func (u *Upgrade) RunWithContext(ctx context.Context, name string, chart *chart. return res, err } - if !u.DryRun { + if upgradedRelease.Skipped { + u.cfg.Log("upgrade release for %s was skipped", name) + } else if !u.DryRun { u.cfg.Log("updating status for upgraded release for %s", name) if err := u.cfg.Releases.Update(upgradedRelease); err != nil { return res, err @@ -317,6 +321,13 @@ func (u *Upgrade) performUpgrade(ctx context.Context, originalRelease, upgradedR return nil }) + if u.SkipEmptyUpgrade { + if len(toBeCreated) == 0 && len(toBeUpdated) == 0 { + upgradedRelease.Skipped = true + return upgradedRelease, nil + } + } + if u.DryRun { u.cfg.Log("dry run for %s", upgradedRelease.Name) if len(u.Description) > 0 { diff --git a/pkg/release/release.go b/pkg/release/release.go index b90612873..7d9a8058b 100644 --- a/pkg/release/release.go +++ b/pkg/release/release.go @@ -40,6 +40,8 @@ type Release struct { // Labels of the release. // Disabled encoding into Json cause labels are stored in storage driver metadata field. Labels map[string]string `json:"-"` + // Skipped indicates if release was skipped. + Skipped bool } // SetStatus is a helper for setting the status on a release.