Support linting generateName

Signed-off-by: Charlie Getzen <charliegetzenlc@gmail.com>
pull/9644/head
Charlie Getzen 4 years ago
parent 43853ea772
commit 9b4af3f906

@ -32,6 +32,7 @@ import (
apipath "k8s.io/apimachinery/pkg/api/validation/path"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/apiserver/pkg/storage/names"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
@ -211,11 +212,15 @@ func validateYamlContent(err error) error {
func validateMetadataName(obj *K8sYamlStruct) error {
fn := validateMetadataNameFunc(obj)
allErrs := field.ErrorList{}
for _, msg := range fn(obj.Metadata.Name, false) {
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata").Child("name"), obj.Metadata.Name, msg))
name := obj.Metadata.Name
if len(name) == 0 && len(obj.Metadata.GenerateName) != 0 {
name = names.SimpleNameGenerator.GenerateName(obj.Metadata.GenerateName)
}
for _, msg := range fn(name, false) {
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata").Child("name"), name, msg))
}
if len(allErrs) > 0 {
return errors.Wrapf(allErrs.ToAggregate(), "object name does not conform to Kubernetes naming requirements: %q", obj.Metadata.Name)
return errors.Wrapf(allErrs.ToAggregate(), "object name does not conform to Kubernetes naming requirements: %q", name)
}
return nil
}
@ -307,4 +312,5 @@ type K8sYamlStruct struct {
type k8sYamlMetadata struct {
Namespace string
Name string
GenerateName string
}

@ -120,6 +120,15 @@ func TestMultiTemplateFail(t *testing.T) {
}
}
func TestGenerateName(t *testing.T) {
linter := support.Linter{ChartDir: "./testdata/generate-name"}
Templates(&linter, values, namespace, strict)
res := linter.Messages
if len(res) != 0 {
t.Fatalf("Unexpected error: %s", res[0].Err)
}
}
func TestValidateMetadataName(t *testing.T) {
tests := []struct {
obj *K8sYamlStruct

@ -0,0 +1,21 @@
apiVersion: v2
name: generate-name
description: A Helm chart for Kubernetes
# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
version: 0.1.0
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application and it is recommended to use it with quotes.
appVersion: "1.16.0"

@ -0,0 +1,13 @@
apiVersion: batch/v1
kind: Job
metadata:
generateName: test-
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
Loading…
Cancel
Save