From f6b1189aa201231b67133df4c75db0709465c84f Mon Sep 17 00:00:00 2001 From: Lev Aminov Date: Mon, 12 Nov 2018 16:37:43 +0500 Subject: [PATCH] fix(helm): Non-zero exit code on failed chart repository update (#4348) * Fail on failed chart repository update * Fix failed test * Add repo update strict flag --- cmd/helm/repo_update.go | 25 +++++++++++++++++++------ cmd/helm/repo_update_test.go | 5 +++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/cmd/helm/repo_update.go b/cmd/helm/repo_update.go index 291b21b72..526300343 100644 --- a/cmd/helm/repo_update.go +++ b/cmd/helm/repo_update.go @@ -40,9 +40,10 @@ future releases. var errNoRepositories = errors.New("no repositories found. You must add one before updating") type repoUpdateCmd struct { - update func([]*repo.ChartRepository, io.Writer, helmpath.Home) + update func([]*repo.ChartRepository, io.Writer, helmpath.Home, bool) error home helmpath.Home out io.Writer + strict bool } func newRepoUpdateCmd(out io.Writer) *cobra.Command { @@ -60,6 +61,10 @@ func newRepoUpdateCmd(out io.Writer) *cobra.Command { return u.run() }, } + + f := cmd.Flags() + f.BoolVar(&u.strict, "strict", false, "fail on update warnings") + return cmd } @@ -80,14 +85,15 @@ func (u *repoUpdateCmd) run() error { } repos = append(repos, r) } - - u.update(repos, u.out, u.home) - return nil + return u.update(repos, u.out, u.home, u.strict) } -func updateCharts(repos []*repo.ChartRepository, out io.Writer, home helmpath.Home) { +func updateCharts(repos []*repo.ChartRepository, out io.Writer, home helmpath.Home, strict bool) error { fmt.Fprintln(out, "Hang tight while we grab the latest from your chart repositories...") - var wg sync.WaitGroup + var ( + errorCounter int + wg sync.WaitGroup + ) for _, re := range repos { wg.Add(1) go func(re *repo.ChartRepository) { @@ -98,6 +104,7 @@ func updateCharts(repos []*repo.ChartRepository, out io.Writer, home helmpath.Ho } err := re.DownloadIndexFile(home.Cache()) if err != nil { + errorCounter++ fmt.Fprintf(out, "...Unable to get an update from the %q chart repository (%s):\n\t%s\n", re.Config.Name, re.Config.URL, err) } else { fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", re.Config.Name) @@ -105,5 +112,11 @@ func updateCharts(repos []*repo.ChartRepository, out io.Writer, home helmpath.Ho }(re) } wg.Wait() + + if errorCounter != 0 && strict { + return errors.New("Update Failed. Check log for details") + } + fmt.Fprintln(out, "Update Complete. ⎈ Happy Helming!⎈ ") + return nil } diff --git a/cmd/helm/repo_update_test.go b/cmd/helm/repo_update_test.go index 71dc87966..86af437c5 100644 --- a/cmd/helm/repo_update_test.go +++ b/cmd/helm/repo_update_test.go @@ -46,10 +46,11 @@ func TestUpdateCmd(t *testing.T) { out := bytes.NewBuffer(nil) // Instead of using the HTTP updater, we provide our own for this test. // The TestUpdateCharts test verifies the HTTP behavior independently. - updater := func(repos []*repo.ChartRepository, out io.Writer, hh helmpath.Home) { + updater := func(repos []*repo.ChartRepository, out io.Writer, hh helmpath.Home, strict bool) error { for _, re := range repos { fmt.Fprintln(out, re.Config.Name) } + return nil } uc := &repoUpdateCmd{ update: updater, @@ -94,7 +95,7 @@ func TestUpdateCharts(t *testing.T) { } b := bytes.NewBuffer(nil) - updateCharts([]*repo.ChartRepository{r}, b, hh) + updateCharts([]*repo.ChartRepository{r}, b, hh, false) got := b.String() if strings.Contains(got, "Unable to get an update") {