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 21, 2023
1 parent 54a2e9f commit aaf1431
Show file tree
Hide file tree
Showing 3 changed files with 549 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
27 changes: 24 additions & 3 deletions pkg/crd/parser_integration_test.go
Expand Up @@ -18,15 +18,14 @@ package crd_test

import (
"fmt"
"io/ioutil"
"os"

"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"golang.org/x/tools/go/packages"
"io/ioutil"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"os"
"sigs.k8s.io/yaml"

"sigs.k8s.io/controller-tools/pkg/crd"
Expand Down Expand Up @@ -132,6 +131,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 +173,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

0 comments on commit aaf1431

Please sign in to comment.