Skip to content

Commit 2dd7772

Browse files
authored
Merge pull request #227 from fluxcd/chart-name-validation
Validate provided name for charts from HelmRepos
2 parents 86712ff + 5f3c014 commit 2dd7772

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

controllers/helmchart_controller.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/url"
2424
"os"
2525
"path"
26+
"regexp"
2627
"strings"
2728
"time"
2829

@@ -187,7 +188,8 @@ func (r *HelmChartReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
187188
}
188189

189190
// Emit an event if we did not have an artifact before, or the revision has changed
190-
if chart.Status.Artifact == nil || reconciledChart.Status.Artifact.Revision != chart.Status.Artifact.Revision {
191+
if (chart.Status.Artifact == nil && reconciledChart.Status.Artifact != nil) ||
192+
reconciledChart.Status.Artifact.Revision != chart.Status.Artifact.Revision {
191193
r.event(reconciledChart, events.EventSeverityInfo, sourcev1.HelmChartReadyMessage(reconciledChart))
192194
}
193195
r.recordReadiness(reconciledChart)
@@ -275,6 +277,12 @@ func (r *HelmChartReconciler) getSource(ctx context.Context, chart sourcev1.Helm
275277

276278
func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
277279
repository sourcev1.HelmRepository, chart sourcev1.HelmChart, force bool) (sourcev1.HelmChart, error) {
280+
// TODO: move this to a validation webhook once the discussion around
281+
// certificates has settled: https://github.com/fluxcd/image-reflector-controller/issues/69
282+
if err := validHelmChartName(chart.Spec.Chart); err != nil {
283+
return sourcev1.HelmChartNotReady(chart, sourcev1.ChartPullFailedReason, err.Error()), nil
284+
}
285+
278286
// Configure ChartRepository getter options
279287
var clientOpts []getter.Option
280288
if secret, err := r.getHelmRepositorySecret(ctx, &repository); err != nil {
@@ -877,3 +885,15 @@ func (r *HelmChartReconciler) requestsForBucketChange(obj handler.MapObject) []r
877885
}
878886
return reqs
879887
}
888+
889+
// validHelmChartName returns an error if the given string is not a
890+
// valid Helm chart name; a valid name must be lower case letters
891+
// and numbers, words may be separated with dashes (-).
892+
// Ref: https://helm.sh/docs/chart_best_practices/conventions/#chart-names
893+
func validHelmChartName(s string) error {
894+
chartFmt := regexp.MustCompile("^([-a-z0-9]*)$")
895+
if !chartFmt.MatchString(s) {
896+
return fmt.Errorf("invalid chart name %q, a valid name must be lower case letters and numbers and MAY be seperated with dashes (-)", s)
897+
}
898+
return nil
899+
}

controllers/helmchart_controller_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"path"
2727
"path/filepath"
2828
"strings"
29+
"testing"
2930
"time"
3031

3132
"github.com/fluxcd/pkg/apis/meta"
@@ -938,3 +939,26 @@ var _ = Describe("HelmChartReconciler", func() {
938939
})
939940
})
940941
})
942+
943+
func Test_validHelmChartName(t *testing.T) {
944+
tests := []struct {
945+
name string
946+
chart string
947+
expectErr bool
948+
}{
949+
{"valid", "drupal", false},
950+
{"valid dash", "nginx-lego", false},
951+
{"valid dashes", "aws-cluster-autoscaler", false},
952+
{"valid alphanum", "ng1nx-leg0", false},
953+
{"invalid slash", "artifactory/invalid", true},
954+
{"invalid dot", "in.valid", true},
955+
{"invalid uppercase", "inValid", true},
956+
}
957+
for _, tt := range tests {
958+
t.Run(tt.name, func(t *testing.T) {
959+
if err := validHelmChartName(tt.chart); (err != nil) != tt.expectErr {
960+
t.Errorf("validHelmChartName() error = %v, expectErr %v", err, tt.expectErr)
961+
}
962+
})
963+
}
964+
}

0 commit comments

Comments
 (0)