From eaae99f7b2e1948cad60b065eec66b47b9e2a536 Mon Sep 17 00:00:00 2001 From: Janit Sriganeshaelankovan Date: Sun, 26 Oct 2025 17:28:39 -0400 Subject: [PATCH] update for v3 and update tests Signed-off-by: Janit Sriganeshaelankovan --- internal/chart/v3/lint/rules/chartfile.go | 17 ++++++++++++----- internal/chart/v3/lint/rules/chartfile_test.go | 2 +- internal/chart/v3/util/create.go | 4 ++-- internal/chart/v3/util/create_test.go | 6 ++++-- pkg/chart/v2/lint/rules/chartfile_test.go | 2 +- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/internal/chart/v3/lint/rules/chartfile.go b/internal/chart/v3/lint/rules/chartfile.go index fc246ba80..b9a560d92 100644 --- a/internal/chart/v3/lint/rules/chartfile.go +++ b/internal/chart/v3/lint/rules/chartfile.go @@ -21,6 +21,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "github.com/Masterminds/semver/v3" "github.com/asaskevich/govalidator" @@ -31,6 +32,13 @@ import ( chartutil "helm.sh/helm/v4/internal/chart/v3/util" ) +// chartName is a regular expression for testing the supplied name of a chart. +var chartName = regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`) + +// maxChartNameLength is lower than the limits we know of with certain file systems, +// and with certain Kubernetes fields. +const maxChartNameLength = 250 + // Chartfile runs a set of linter rules related to Chart.yaml file func Chartfile(linter *support.Linter) { chartFileName := "Chart.yaml" @@ -113,12 +121,11 @@ func validateChartYamlStrictFormat(chartFileError error) error { } func validateChartName(cf *chart.Metadata) error { - if cf.Name == "" { - return errors.New("name is required") + if cf.Name == "" || len(cf.Name) > maxChartNameLength { + return fmt.Errorf("chart name must be between 1 and %d characters", maxChartNameLength) } - name := filepath.Base(cf.Name) - if name != cf.Name { - return fmt.Errorf("chart name %q is invalid", cf.Name) + if !chartName.MatchString(cf.Name) { + return fmt.Errorf("chart name must use only lowercase letters or digits optionally with dash separators") } return nil } diff --git a/internal/chart/v3/lint/rules/chartfile_test.go b/internal/chart/v3/lint/rules/chartfile_test.go index 53efda4aa..f652b9ed2 100644 --- a/internal/chart/v3/lint/rules/chartfile_test.go +++ b/internal/chart/v3/lint/rules/chartfile_test.go @@ -272,7 +272,7 @@ func TestV3Chartfile(t *testing.T) { return } - if !strings.Contains(msgs[0].Err.Error(), "name is required") { + if !strings.Contains(msgs[0].Err.Error(), "chart name must be between 1 and 250 characters") { t.Errorf("Unexpected message 0: %s", msgs[0].Err) } diff --git a/internal/chart/v3/util/create.go b/internal/chart/v3/util/create.go index c5e728721..88afaf1a3 100644 --- a/internal/chart/v3/util/create.go +++ b/internal/chart/v3/util/create.go @@ -35,7 +35,7 @@ import ( // This regular expression is probably stricter than it needs to be. We can relax it // somewhat. Newline characters, as well as $, quotes, +, parens, and % are known to be // problematic. -var chartName = regexp.MustCompile("^[a-zA-Z0-9._-]+$") +var chartName = regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`) const ( // ChartfileName is the default Chart file name. @@ -828,7 +828,7 @@ func validateChartName(name string) error { return fmt.Errorf("chart name must be between 1 and %d characters", maxChartNameLength) } if !chartName.MatchString(name) { - return fmt.Errorf("chart name must match the regular expression %q", chartName.String()) + return fmt.Errorf("chart name must use only lowercase letters or digits optionally with dash separators") } return nil } diff --git a/internal/chart/v3/util/create_test.go b/internal/chart/v3/util/create_test.go index b3b58cc5a..36b2fbd5e 100644 --- a/internal/chart/v3/util/create_test.go +++ b/internal/chart/v3/util/create_test.go @@ -147,8 +147,10 @@ func TestCreate_Overwrite(t *testing.T) { func TestValidateChartName(t *testing.T) { for name, shouldPass := range map[string]bool{ "": false, - "abcdefghijklmnopqrstuvwxyz-_.": true, - "ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.": true, + "abcdefghijklmnopqrstuvwxyz-_.": false, + "abcdefghijklmnopqrstuvwxyz": true, + "abcdefghijklm-nopqrstuvwxyz": true, + "ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.": false, "$hello": false, "HellĂ´": false, "he%%o": false, diff --git a/pkg/chart/v2/lint/rules/chartfile_test.go b/pkg/chart/v2/lint/rules/chartfile_test.go index fb49ebc1b..0e99be16d 100644 --- a/pkg/chart/v2/lint/rules/chartfile_test.go +++ b/pkg/chart/v2/lint/rules/chartfile_test.go @@ -299,7 +299,7 @@ func TestChartfile(t *testing.T) { return } - if !strings.Contains(msgs[0].Err.Error(), "name is required") { + if !strings.Contains(msgs[0].Err.Error(), "chart name must be between 1 and 250 characters") { t.Errorf("Unexpected message 0: %s", msgs[0].Err) }