fix(helm-lint): do not validate metadata.name for List resources

Closes #13192

Signed-off-by: Thiago Perrotta <thiago@perrotta.dev>
pull/31169/head
Thiago Perrotta 1 month ago
parent 30404b4173
commit d793d56151

@ -160,7 +160,11 @@ func TemplatesWithSkipSchemaValidation(linter *support.Linter, values map[string
if yamlStruct != nil {
// NOTE: set to warnings to allow users to support out-of-date kubernetes
// Refs https://github.com/helm/helm/issues/8596
// Skip metadata name validation for List resources as they don't require meaningful names
// Refs https://github.com/helm/helm/issues/13192
if !isListResource(yamlStruct) {
linter.RunLinterRule(support.WarningSev, fpath, validateMetadataName(yamlStruct))
}
linter.RunLinterRule(support.WarningSev, fpath, validateNoDeprecations(yamlStruct, kubeVersion))
linter.RunLinterRule(support.ErrorSev, fpath, validateMatchSelector(yamlStruct, renderedContent))
@ -235,6 +239,12 @@ func validateYamlContent(err error) error {
return nil
}
// isListResource returns true if the resource is a Kubernetes List type
// (e.g. ConfigMapList, SecretList).
func isListResource(obj *k8sYamlStruct) bool {
return strings.HasSuffix(obj.Kind, "List")
}
// validateMetadataName uses the correct validation function for the object
// Kind, or if not set, defaults to the standard definition of a subdomain in
// DNS (RFC 1123), used by most resources.

@ -438,3 +438,48 @@ items:
t.Fatalf("List objects keep annotations should pass. got: %s", err)
}
}
func TestIsListResource(t *testing.T) {
tests := []struct {
kind string
expected bool
}{
{"ConfigMap", false},
{"ConfigMapList", true},
{"Secret", false},
{"SecretList", true},
{"List", true},
{"", false},
{"SomethingListExtra", false},
}
for _, test := range tests {
t.Run(test.kind, func(t *testing.T) {
obj := &k8sYamlStruct{Kind: test.kind}
result := isListResource(obj)
if result != test.expected {
t.Errorf("isListResource(%q) = %v, expected %v", test.kind, result, test.expected)
}
})
}
}
func TestConfigMapListLinting(t *testing.T) {
linter := support.Linter{ChartDir: "./testdata/configmaplist-chart"}
Templates(&linter, values, namespace, strict)
// The ConfigMapList should not generate any lint warnings about metadata.name
for _, msg := range linter.Messages {
if strings.Contains(msg.Err.Error(), "object name does not conform to Kubernetes naming requirements") {
t.Errorf("ConfigMapList should not generate metadata name validation errors, but got: %v", msg.Err)
}
}
// Should have no errors or warnings
if len(linter.Messages) > 0 {
t.Errorf("Expected no lint messages for ConfigMapList, got %d messages:", len(linter.Messages))
for i, msg := range linter.Messages {
t.Logf("Message %d: %s", i, msg)
}
}
}

@ -0,0 +1,6 @@
apiVersion: v2
name: configmaplist-chart
description: Test chart with ConfigMapList
type: application
version: "0.1.0"
appVersion: "1.0"

@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-single-config
namespace: {{ .Release.Namespace }}
data:
setting: "value"

@ -0,0 +1,21 @@
apiVersion: v1
kind: ConfigMapList
metadata:
# ConfigMapList objects don't require meaningful names
# This should not trigger lint warnings
items:
- apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-database-config
namespace: {{ .Release.Namespace }}
data:
host: {{ .Values.configs.database.host }}
port: "{{ .Values.configs.database.port }}"
- apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-cache-config
namespace: {{ .Release.Namespace }}
data:
redis-url: {{ .Values.configs.cache.redis }}

@ -0,0 +1,7 @@
# Default values for configmaplist-chart
configs:
database:
host: localhost
port: 5432
cache:
redis: redis://localhost:6379
Loading…
Cancel
Save