Fix linting `kind: List` metadata

Helm v2 didn't validate that resource names were valid. Helm v3
introduces this check, failing lint checks that were previously
passing.

This PR adds a check to validate metadata for a list of resources if
present, otherwise falling back to previous behavior of validating the
metadata.

Fixes https://github.com/helm/helm/issues/8615

Signed-off-by: Chris Bui <christopher.d.bui@gmail.com>
pull/8643/head
Chris Bui 5 years ago
parent 4bb8483a28
commit a4e61f7b41
No known key found for this signature in database
GPG Key ID: 898563FACD62DB0B

@ -126,11 +126,17 @@ func Templates(linter *support.Linter, values map[string]interface{}, namespace
// Even though K8sYamlStruct only defines a few fields, an error in any other
// key will be raised as well
err := yaml.Unmarshal([]byte(renderedContent), &yamlStruct)
// If YAML linting fails, we sill progress. So we don't capture the returned state
// on this linter run.
linter.RunLinterRule(support.ErrorSev, fpath, validateYamlContent(err))
linter.RunLinterRule(support.ErrorSev, fpath, validateMetadataName(&yamlStruct))
if isListOfResources(&yamlStruct) {
for i, item := range yamlStruct.Items {
err := errors.Wrapf(validateMetadataName(&item), "at item index %d", i)
linter.RunLinterRule(support.ErrorSev, fpath, err)
}
} else {
linter.RunLinterRule(support.ErrorSev, fpath, validateMetadataName(&yamlStruct))
}
linter.RunLinterRule(support.ErrorSev, fpath, validateNoDeprecations(&yamlStruct))
linter.RunLinterRule(support.ErrorSev, fpath, validateMatchSelector(&yamlStruct, renderedContent))
}
@ -164,13 +170,16 @@ func validateYamlContent(err error) error {
return errors.Wrap(err, "unable to parse YAML")
}
func validateMetadataName(obj *K8sYamlStruct) error {
// This will return an error if the characters do not abide by the standard OR if the
// name is left empty.
if validName.MatchString(obj.Metadata.Name) {
func isListOfResources(yamlStruct *K8sYamlStruct) bool {
return len(yamlStruct.Items) > 0
}
func validateMetadataName(yamlStruct *K8sYamlStruct) error {
if validName.MatchString(yamlStruct.Metadata.Name) {
return nil
}
return fmt.Errorf("object name does not conform to Kubernetes naming requirements: %q", obj.Metadata.Name)
return fmt.Errorf("object name does not conform to Kubernetes naming requirements: %q", yamlStruct.Metadata.Name)
}
func validateNoCRDHooks(manifest []byte) error {
@ -208,6 +217,7 @@ type K8sYamlStruct struct {
APIVersion string `json:"apiVersion"`
Kind string
Metadata k8sYamlMetadata
Items []K8sYamlStruct
}
type k8sYamlMetadata struct {

@ -134,6 +134,23 @@ func TestValidateMetadataName(t *testing.T) {
}
}
func TestListOfResources(t *testing.T) {
testData := map[string]int{
"./testdata/good_list_of_resources": 0,
"./testdata/bad_list_of_resources": 2,
}
for testDir, expectedErrs := range testData {
linter := support.Linter{ChartDir: testDir}
Templates(&linter, values, namespace, strict)
res := linter.Messages
if len(res) != expectedErrs {
t.Fatalf("Expected %d error(s) in %s, got %d, %v", expectedErrs, testDir, len(res), res)
}
}
}
func TestDeprecatedAPIFails(t *testing.T) {
mychart := chart.Chart{
Metadata: &chart.Metadata{

@ -0,0 +1,5 @@
apiVersion: v1
name: bad list of resources
description: list of resources testing chart
version: 199.44.12345-Alpha.1+cafe009
icon: http://riverrun.io

@ -0,0 +1,8 @@
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: ConfigMap
metadata:
- apiVersion: v1
kind: ConfigMap

@ -0,0 +1 @@
name: "bad list of resources"

@ -0,0 +1,5 @@
apiVersion: v1
name: good list of resources
description: list of resources testing chart
version: 199.44.12345-Alpha.1+cafe009
icon: http://riverrun.io

@ -0,0 +1,11 @@
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: ConfigMap
metadata:
name: config1
- apiVersion: v1
kind: ConfigMap
metadata:
name: config2

@ -0,0 +1 @@
name: "good list of resources"
Loading…
Cancel
Save