Skip to content

Commit

Permalink
Return an error after processing all charts if any of them failed dur…
Browse files Browse the repository at this point in the history
…ing image discovery (#2038)

## Description

This changes the find image behavior to return an error if any charts
failed to template during `zarf prepare find-images`

## Related Issue

Fixes #1999 

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging

- [x] Test, docs, adr added or updated as needed
- [x] [Contributor Guide
Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow)
followed
  • Loading branch information
Racer159 authored Sep 30, 2023
1 parent 685334e commit 8f4d55f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cmd/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ var prepareFindImages = &cobra.Command{

// Find all the images the package might need
if _, err := pkgClient.FindImages(baseDir, repoHelmChartPath, kubeVersionOverride); err != nil {
message.Fatalf(err, lang.CmdPrepareFindImagesErr, baseDir)
message.Fatalf(err, lang.CmdPrepareFindImagesErr, baseDir, err.Error())
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ const (
CmdPrepareFindImagesShort = "Evaluates components in a zarf file to identify images specified in their helm charts and manifests"
CmdPrepareFindImagesLong = "Evaluates components in a zarf file to identify images specified in their helm charts and manifests.\n\n" +
"Components that have repos that host helm charts can be processed by providing the --repo-chart-path."
CmdPrepareFindImagesErr = "Unable to find images for the package definition %s"
CmdPrepareFindImagesErr = "Unable to find images for the package definition %s: %s"

CmdPrepareGenerateConfigShort = "Generates a config file for Zarf"
CmdPrepareGenerateConfigLong = "Generates a Zarf config file for controlling how the Zarf CLI operates. Optionally accepts a filename to write the config to.\n\n" +
Expand Down
7 changes: 7 additions & 0 deletions src/pkg/packager/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (p *Packager) FindImages(baseDir, repoHelmChartPath string, kubeVersionOver

var originalDir string
imagesMap := make(map[string][]string)
erroredCharts := []string{}

// Change the working directory if this run has an alternate base dir
if baseDir != "" {
Expand Down Expand Up @@ -154,6 +155,7 @@ func (p *Packager) FindImages(baseDir, repoHelmChartPath string, kubeVersionOver

if err != nil {
message.WarnErrf(err, "Problem rendering the helm template for %s: %s", chart.URL, err.Error())
erroredCharts = append(erroredCharts, chart.URL)
continue
}

Expand All @@ -171,6 +173,7 @@ func (p *Packager) FindImages(baseDir, repoHelmChartPath string, kubeVersionOver
annotatedImages, err := helm.FindAnnotatedImagesForChart(chartTarball, values)
if err != nil {
message.WarnErrf(err, "Problem looking for image annotations for %s: %s", chart.URL, err.Error())
erroredCharts = append(erroredCharts, chart.URL)
continue
}
for _, image := range annotatedImages {
Expand Down Expand Up @@ -267,6 +270,10 @@ func (p *Packager) FindImages(baseDir, repoHelmChartPath string, kubeVersionOver
_ = os.Chdir(originalDir)
}

if len(erroredCharts) > 0 {
return imagesMap, fmt.Errorf("the following charts had errors: %s", erroredCharts)
}

return imagesMap, nil
}

Expand Down
6 changes: 4 additions & 2 deletions src/test/e2e/00_use_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ func TestUseCLI(t *testing.T) {
t.Run("zarf prepare find-images --kube-version", func(t *testing.T) {
t.Parallel()
// Test `zarf prepare find-images` on a chart that has a `kubeVersion` declaration greater than the default (v1.20.0)
_, stdErr, _ := e2e.Zarf("prepare", "find-images", "src/test/packages/00-kube-version-override")
stdOut, stdErr, err := e2e.Zarf("prepare", "find-images", "src/test/packages/00-kube-version-override")
require.Error(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "Problem rendering the helm template for https://charts.jetstack.io/", "The kubeVersion declaration should prevent this from templating")
require.Contains(t, stdErr, "following charts had errors: [https://charts.jetstack.io/]", "Zarf should print an ending error message")

// Test `zarf prepare find-images` with `--kube-version` specified and greater than the declared minimum (v1.21.0)
stdOut, stdErr, err := e2e.Zarf("prepare", "find-images", "--kube-version=v1.22.0", "src/test/packages/00-kube-version-override")
stdOut, stdErr, err = e2e.Zarf("prepare", "find-images", "--kube-version=v1.22.0", "src/test/packages/00-kube-version-override")
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdOut, "quay.io/jetstack/cert-manager-controller:v1.11.1", "The chart image should be found by Zarf")
})
Expand Down

0 comments on commit 8f4d55f

Please sign in to comment.