Skip to content

Commit

Permalink
Show a warning when there are no images to annotate (#33)
Browse files Browse the repository at this point in the history
* Show a warning when there are no images to annotate

- When there are images:

```
➜ ./bin/dt charts annotate $CHARTS/wordpress
 🎉  Helm chart annotated successfully
```

- When there are no images:

```
➜ ./bin/dt charts annotate $CHARTS/common
 ⚠️  No container images found to be annotated
```

* Use a boolean instead
  • Loading branch information
alemorcuq authored Jan 12, 2024
1 parent 6c794a5 commit 597660d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cmd/dt/annotate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/dt/annotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
22 changes: 21 additions & 1 deletion pkg/chartutils/chartutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit 597660d

Please sign in to comment.