diff --git a/cmd/dt/annotate.go b/cmd/dt/annotate.go index 1968621..183607e 100644 --- a/cmd/dt/annotate.go +++ b/cmd/dt/annotate.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "github.com/spf13/cobra" @@ -30,9 +31,14 @@ Use it cautiously. Very often the complete list of images cannot be guessed from chartutils.WithAnnotationsKey(getAnnotationsKey()), chartutils.WithLog(l), ) + }) if err != nil { + if errors.Is(err, chartutils.ErrNoImagesToAnnotate) { + l.Warnf("No container images found to be annotated") + return nil + } return l.Failf("failed to annotate Helm chart %q: %v", chartPath, err) } diff --git a/cmd/dt/annotate_test.go b/cmd/dt/annotate_test.go index cfac3b6..39de450 100644 --- a/cmd/dt/annotate_test.go +++ b/cmd/dt/annotate_test.go @@ -89,7 +89,7 @@ func (suite *CmdSuite) TestAnnotateCommand() { map[string]interface{}{"ServerURL": serverURL}, )) - dt("charts", "annotate", chartDir).AssertSuccess(t) + dt("charts", "annotate", chartDir).AssertSuccessMatch(t, regexp.MustCompile(`No container images found`)) tu.AssertChartAnnotations(t, chartDir, defaultAnnotationsKey, make([]tu.AnnotationEntry, 0)) diff --git a/pkg/chartutils/chartutils.go b/pkg/chartutils/chartutils.go index e2a2315..852107e 100644 --- a/pkg/chartutils/chartutils.go +++ b/pkg/chartutils/chartutils.go @@ -15,9 +15,14 @@ import ( "helm.sh/helm/v3/pkg/chart/loader" ) +// ErrNoImagesToAnnotate is returned when the chart can't be annotated because +// there are no container images +var ErrNoImagesToAnnotate = errors.New("no container images to annotate found") + // AnnotateChart parses the values.yaml file in the chart specified by chartPath and // annotates the Chart with the list of found images func AnnotateChart(chartPath string, opts ...Option) error { + var annotated bool cfg := NewConfiguration(opts...) chart, err := loader.Load(chartPath) if err != nil { @@ -36,19 +41,34 @@ func AnnotateChart(chartPath string, opts ...Option) error { // Make sure order is always the same sort.Sort(res) + if len(res) > 0 { + annotated = true + } chartFile := filepath.Join(chartRoot, "Chart.yaml") if err := writeAnnotationsToChart(res, chartFile, cfg); err != nil { return fmt.Errorf("failed to serialize annotations: %v", err) } + var allErrors error for _, dep := range chart.Dependencies() { subChart := filepath.Join(chartRoot, "charts", dep.Name()) if err := AnnotateChart(subChart, opts...); err != nil { - allErrors = errors.Join(allErrors, fmt.Errorf("failed to annotate sub-chart %q: %v", dep.ChartFullPath(), err)) + // Ignore the error if its ErrNoImagesToAnnotate + if !errors.Is(err, ErrNoImagesToAnnotate) { + allErrors = errors.Join(allErrors, fmt.Errorf("failed to annotate sub-chart %q: %v", dep.ChartFullPath(), err)) + } + } else { + // No error means the dependency was annotated + annotated = true } } + + if !annotated && allErrors == nil { + return ErrNoImagesToAnnotate + } + return allErrors }