diff --git a/pkg/lint/lint_test.go b/pkg/lint/lint_test.go index 45e24f533..2b591f516 100644 --- a/pkg/lint/lint_test.go +++ b/pkg/lint/lint_test.go @@ -21,6 +21,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + chartutil "helm.sh/helm/v4/pkg/chart/v2/util" "helm.sh/helm/v4/pkg/lint/support" ) @@ -114,12 +116,9 @@ func TestBadValues(t *testing.T) { func TestBadCrdFile(t *testing.T) { m := RunAll(badCrdFileDir, values, namespace).Messages - if len(m) < 1 { - t.Fatalf("All didn't fail with expected errors, got %#v", m) - } - if !strings.Contains(m[0].Err.Error(), "object kind is not 'CustomResourceDefinition'") { - t.Errorf("All didn't have the error for invalid CRD: %s", m[0].Err) - } + assert.Lenf(t, m, 2, "All didn't fail with expected errors, got %#v", m) + assert.ErrorContains(t, m[0].Err, "apiVersion is not in 'apiextensions.k8s.io'") + assert.ErrorContains(t, m[1].Err, "object kind is not 'CustomResourceDefinition'") } func TestGoodChart(t *testing.T) { diff --git a/pkg/lint/rules/crds.go b/pkg/lint/rules/crds.go index 4740157b3..dd3e145fc 100644 --- a/pkg/lint/rules/crds.go +++ b/pkg/lint/rules/crds.go @@ -24,6 +24,7 @@ import ( "io/fs" "os" "path/filepath" + "strings" "k8s.io/apimachinery/pkg/util/yaml" @@ -41,7 +42,10 @@ func Crds(linter *support.Linter) { return } - linter.RunLinterRule(support.WarningSev, fpath, validateCrdsDir(crdsPath)) + crdsDirValid := linter.RunLinterRule(support.WarningSev, fpath, validateCrdsDir(crdsPath)) + if !crdsDirValid { + return + } // Load chart and parse CRDs chart, err := loader.Load(linter.ChartDir) @@ -53,8 +57,9 @@ func Crds(linter *support.Linter) { } /* Iterate over all the CRDs to check: - - It is a YAML file and not a template - - The kind is CustomResourceDefinition + 1. It is a YAML file and not a template + 2. The API version is apiextensions.k8s.io + 3. The kind is CustomResourceDefinition */ for _, crd := range chart.CRDObjects() { fileName := crd.Name @@ -75,6 +80,7 @@ func Crds(linter *support.Linter) { return } + linter.RunLinterRule(support.ErrorSev, fpath, validateCrdApiVersion(yamlStruct)) linter.RunLinterRule(support.ErrorSev, fpath, validateCrdKind(yamlStruct)) } } @@ -92,6 +98,13 @@ func validateCrdsDir(crdsPath string) error { return nil } +func validateCrdApiVersion(obj *K8sYamlStruct) error { + if !strings.HasPrefix(obj.APIVersion, "apiextensions.k8s.io") { + return fmt.Errorf("apiVersion is not in 'apiextensions.k8s.io'") + } + return nil +} + func validateCrdKind(obj *K8sYamlStruct) error { if obj.Kind != "CustomResourceDefinition" { return fmt.Errorf("object kind is not 'CustomResourceDefinition'") diff --git a/pkg/lint/rules/crds_test.go b/pkg/lint/rules/crds_test.go index a84b62a50..66da06121 100644 --- a/pkg/lint/rules/crds_test.go +++ b/pkg/lint/rules/crds_test.go @@ -17,13 +17,14 @@ limitations under the License. package rules import ( - "strings" "testing" + "github.com/stretchr/testify/assert" + "helm.sh/helm/v4/pkg/lint/support" ) -const crdsTestBaseDir = "./testdata/withcrd" +const crdsTestBaseDir = "./testdata/goodone" const invalidCrdsDir = "./testdata/invalidcrdsdir" func TestCrdsDir(t *testing.T) { @@ -31,9 +32,7 @@ func TestCrdsDir(t *testing.T) { Crds(&linter) res := linter.Messages - if len(res) > 0 { - t.Fatalf("Expected no errors, got %d, %v", len(res), res) - } + assert.Emptyf(t, res, "Expected no errors, got %v", res) } func TestInvalidCrdsDir(t *testing.T) { @@ -41,11 +40,6 @@ func TestInvalidCrdsDir(t *testing.T) { Crds(&linter) res := linter.Messages - if len(res) != 1 { - t.Fatalf("Expected one error, got %d, %v", len(res), res) - } - - if !strings.Contains(res[0].Err.Error(), "not a directory") { - t.Errorf("Unexpected error: %s", res[0]) - } + assert.Lenf(t, res, 1, "Expected one error, got %d, %v", len(res), res) + assert.ErrorContains(t, res[0].Err, "not a directory") } diff --git a/pkg/lint/rules/testdata/badcrdfile/crds/bad-apiversion.yaml b/pkg/lint/rules/testdata/badcrdfile/crds/bad-apiversion.yaml new file mode 100644 index 000000000..468916053 --- /dev/null +++ b/pkg/lint/rules/testdata/badcrdfile/crds/bad-apiversion.yaml @@ -0,0 +1,2 @@ +apiVersion: bad.k8s.io/v1beta1 +kind: CustomResourceDefinition diff --git a/pkg/lint/rules/testdata/withcrd/crds/test-crd.yaml b/pkg/lint/rules/testdata/goodone/crds/test-crd.yaml similarity index 100% rename from pkg/lint/rules/testdata/withcrd/crds/test-crd.yaml rename to pkg/lint/rules/testdata/goodone/crds/test-crd.yaml diff --git a/pkg/lint/rules/testdata/withcrd/Chart.yaml b/pkg/lint/rules/testdata/withcrd/Chart.yaml deleted file mode 100644 index 58e3a0c27..000000000 --- a/pkg/lint/rules/testdata/withcrd/Chart.yaml +++ /dev/null @@ -1,5 +0,0 @@ -apiVersion: v1 -name: withcrd -description: testing chart with a CRD -version: 199.44.12345-Alpha.1+cafe009 -icon: http://riverrun.io