Skip to content

Commit

Permalink
fix: fix index out of range panic when annotation is not in 'key=valu…
Browse files Browse the repository at this point in the history
…e' format
  • Loading branch information
lianghao208 committed Jan 19, 2023
1 parent 54a2e9f commit 9fc6970
Show file tree
Hide file tree
Showing 3 changed files with 554 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/crd/markers/crd.go
Expand Up @@ -369,6 +369,9 @@ func (s Metadata) ApplyToCRD(crd *apiext.CustomResourceDefinition, version strin
}
for _, str := range s.Annotations {
kv := strings.SplitN(str, "=", 2)
if len(kv) < 2 {
return fmt.Errorf("annotation %s is not in 'xxx=xxx' format", str)
}
crd.Annotations[kv[0]] = kv[1]
}
}
Expand Down
32 changes: 29 additions & 3 deletions pkg/crd/parser_integration_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo"
Expand All @@ -35,14 +36,17 @@ import (
"sigs.k8s.io/controller-tools/pkg/markers"
)

func packageErrors(pkg *loader.Package, filterKinds ...packages.ErrorKind) error {
func packageErrors(pkg *loader.Package, filterMsg string, filterKinds ...packages.ErrorKind) error {
toSkip := make(map[packages.ErrorKind]struct{})
for _, errKind := range filterKinds {
toSkip[errKind] = struct{}{}
}
var outErr error
packages.Visit([]*packages.Package{pkg.Package}, nil, func(pkgRaw *packages.Package) {
for _, err := range pkgRaw.Errors {
if skip := strings.Contains(err.Msg, filterMsg); filterMsg != "" && skip {
continue
}
if _, skip := toSkip[err.Kind]; skip {
continue
}
Expand Down Expand Up @@ -110,7 +114,7 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
crd.FixTopLevelMetadata(parser.CustomResourceDefinitions[groupKind])

By("checking that no errors occurred along the way (expect for type errors)")
ExpectWithOffset(1, packageErrors(pkg, packages.TypeError)).NotTo(HaveOccurred())
ExpectWithOffset(1, packageErrors(pkg, "", packages.TypeError)).NotTo(HaveOccurred())

By(fmt.Sprintf("checking that the %s CRD is present", kind))
ExpectWithOffset(1, parser.CustomResourceDefinitions).To(HaveKey(groupKind))
Expand All @@ -132,6 +136,18 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
ExpectWithOffset(1, parser.CustomResourceDefinitions[groupKind]).To(Equal(crd), "type not as expected, check pkg/crd/testdata/README.md for more details.\n\nDiff:\n\n%s", cmp.Diff(parser.CustomResourceDefinitions[groupKind], crd))
}

assertError := func(pkg *loader.Package, kind, errorMsg string) {
By(fmt.Sprintf("requesting that the %s CRD be generated", kind))
groupKind := schema.GroupKind{Kind: kind, Group: "testdata.kubebuilder.io"}
parser.NeedCRDFor(groupKind, nil)

By(fmt.Sprintf("fixing top level ObjectMeta on the %s CRD", kind))
crd.FixTopLevelMetadata(parser.CustomResourceDefinitions[groupKind])

By("checking that no errors occurred along the way (expect for type errors and specific error messages)")
ExpectWithOffset(1, packageErrors(pkg, errorMsg, packages.TypeError)).NotTo(HaveOccurred())
}

Context("CronJob API", func() {
BeforeEach(func() {
pkgPaths = []string{"./", "./unserved", "./deprecated"}
Expand Down Expand Up @@ -162,6 +178,16 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
assertCRD(pkgs[3], "Job", "testdata.kubebuilder.io_jobs.yaml")
})
})

Context("CronJob API with Wrong Annotation Format", func() {
BeforeEach(func() {
pkgPaths = []string{"./wrong_annotation_format"}
expPkgLen = 1
})
It("can not successfully generate the CronJob CRD", func() {
assertError(pkgs[0], "CronJob", "is not in 'xxx=xxx' format")
})
})
})

It("should generate plural words for Kind correctly", func() {
Expand Down Expand Up @@ -242,6 +268,6 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
Expect(parser.Types).To(BeEmpty())

By("checking that no errors occurred along the way (expect for type errors)")
Expect(packageErrors(cronJobPkg, packages.TypeError)).NotTo(HaveOccurred())
Expect(packageErrors(cronJobPkg, "", packages.TypeError)).NotTo(HaveOccurred())
})
})

0 comments on commit 9fc6970

Please sign in to comment.