From 9928e698265648f9b86698a5e6ac1a3fc3614193 Mon Sep 17 00:00:00 2001 From: Joel Speed Date: Fri, 16 Sep 2022 18:26:17 +0100 Subject: [PATCH] Ensure schemapatcher does not error when non CRD yaml files are present --- pkg/schemapatcher/gen.go | 10 +++++++--- pkg/schemapatcher/testdata/valid/example.cr.yaml | 3 +++ pkg/schemapatcher/testdata/valid/random.yaml | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 pkg/schemapatcher/testdata/valid/example.cr.yaml create mode 100644 pkg/schemapatcher/testdata/valid/random.yaml diff --git a/pkg/schemapatcher/gen.go b/pkg/schemapatcher/gen.go index 583ba1be0..e33ec11ff 100644 --- a/pkg/schemapatcher/gen.go +++ b/pkg/schemapatcher/gen.go @@ -358,12 +358,16 @@ func crdsFromDirectory(ctx *genall.GenerationContext, dir string) (map[schema.Gr if err := kyaml.Unmarshal(rawContent, &typeMeta); err != nil { continue } + + if typeMeta.APIVersion == "" || typeMeta.Kind != "CustomResourceDefinition" { + // If there's no API version this file probably isn't a CRD. + // Likewise we don't need to care if the Kind isn't CustomResourceDefinition. + continue + } + if !isSupportedAPIExtGroupVer(typeMeta.APIVersion) { return nil, fmt.Errorf("load %q: apiVersion %q not supported", filepath.Join(dir, fileInfo.Name()), typeMeta.APIVersion) } - if typeMeta.Kind != "CustomResourceDefinition" { - continue - } // collect the group-kind and versions from the actual structured form var actualCRD crdIsh diff --git a/pkg/schemapatcher/testdata/valid/example.cr.yaml b/pkg/schemapatcher/testdata/valid/example.cr.yaml new file mode 100644 index 000000000..71e46e007 --- /dev/null +++ b/pkg/schemapatcher/testdata/valid/example.cr.yaml @@ -0,0 +1,3 @@ +# This is an example of the resource defined in this package. +apiVersion: kubebuilder.schemapatcher.controller-tools.sigs.k8s.io/v1 +kind: Example diff --git a/pkg/schemapatcher/testdata/valid/random.yaml b/pkg/schemapatcher/testdata/valid/random.yaml new file mode 100644 index 000000000..542419b90 --- /dev/null +++ b/pkg/schemapatcher/testdata/valid/random.yaml @@ -0,0 +1 @@ +name: This is a random YAML file to ensure the generator doesn't error with non-kube YAML files.