Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix index out of range panic when annotation is not in 'key=value' format #764

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
22 changes: 22 additions & 0 deletions pkg/crd/parser_integration_test.go
Expand Up @@ -132,6 +132,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 specific errors occurred along the way")
Expect(packageErrors(pkg)).To(MatchError(ContainSubstring(errorMsg)))
}

Context("CronJob API", func() {
BeforeEach(func() {
pkgPaths = []string{"./", "./unserved", "./deprecated"}
Expand Down Expand Up @@ -162,6 +174,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