Skip to content

Commit 895eccc

Browse files
authored
🐛 Generate plural words for Kind correctly (kubernetes-sigs#522)
* 🐛 Generate plural words for Kind correctly * add a test case
1 parent ede1d01 commit 895eccc

File tree

6 files changed

+123
-2
lines changed

6 files changed

+123
-2
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.13
44

55
require (
66
github.com/fatih/color v1.7.0
7-
github.com/gobuffalo/flect v0.2.0
7+
github.com/gobuffalo/flect v0.2.2
88
github.com/google/go-cmp v0.3.0
99
github.com/mattn/go-colorable v0.1.2 // indirect
1010
github.com/onsi/ginkgo v1.11.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ github.com/go-openapi/validate v0.19.5/go.mod h1:8DJv2CVJQ6kGNpFW6eV9N3JviE1C85n
144144
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
145145
github.com/gobuffalo/flect v0.2.0 h1:EWCvMGGxOjsgwlWaP+f4+Hh6yrrte7JeFL2S6b+0hdM=
146146
github.com/gobuffalo/flect v0.2.0/go.mod h1:W3K3X9ksuZfir8f/LrfVtWmCDQFfayuylOJ7sz/Fj80=
147+
github.com/gobuffalo/flect v0.2.2 h1:PAVD7sp0KOdfswjAw9BpLCU9hXo7wFSzgpQ+zNeks/A=
148+
github.com/gobuffalo/flect v0.2.2/go.mod h1:vmkQwuZYhN5Pc4ljYQZzP+1sq+NEkK+lh20jmEmX3jc=
147149
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
148150
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
149151
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=

pkg/crd/parser_integration_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,49 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
104104
Expect(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))
105105
})
106106

107+
It("should generate plural words for Kind correctly", func() {
108+
By("switching into testdata to appease go modules")
109+
cwd, err := os.Getwd()
110+
Expect(err).NotTo(HaveOccurred())
111+
Expect(os.Chdir("./testdata/plural")).To(Succeed())
112+
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
113+
114+
By("loading the roots")
115+
pkgs, err := loader.LoadRoots(".")
116+
Expect(err).NotTo(HaveOccurred())
117+
Expect(pkgs).To(HaveLen(1))
118+
pkg := pkgs[0]
119+
120+
By("setting up the parser")
121+
reg := &markers.Registry{}
122+
Expect(crdmarkers.Register(reg)).To(Succeed())
123+
parser := &crd.Parser{
124+
Collector: &markers.Collector{Registry: reg},
125+
Checker: &loader.TypeChecker{},
126+
}
127+
crd.AddKnownTypes(parser)
128+
129+
By("requesting that the package be parsed")
130+
parser.NeedPackage(pkg)
131+
132+
By("requesting that the CRD be generated")
133+
groupKind := schema.GroupKind{Kind: "TestQuota", Group: "plural.example.com"}
134+
parser.NeedCRDFor(groupKind, nil)
135+
136+
By("loading the desired YAML")
137+
expectedFile, err := ioutil.ReadFile("plural.example.com_testquotas.yaml")
138+
Expect(err).NotTo(HaveOccurred())
139+
140+
By("parsing the desired YAML")
141+
var crd apiext.CustomResourceDefinition
142+
Expect(yaml.Unmarshal(expectedFile, &crd)).To(Succeed())
143+
// clear the annotations -- we don't care about the attribution annotation
144+
crd.Annotations = nil
145+
146+
By("comparing the two")
147+
Expect(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))
148+
})
149+
107150
It("should skip api internal package", func() {
108151
By("switching into testdata to appease go modules")
109152
cwd, err := os.Getwd()

pkg/crd/spec.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (p *Parser) NeedCRDFor(groupKind schema.GroupKind, maxDescLen *int) {
5555
packages = append(packages, pkg)
5656
}
5757

58-
defaultPlural := flect.Pluralize(strings.ToLower(groupKind.Kind))
58+
defaultPlural := strings.ToLower(flect.Pluralize(groupKind.Kind))
5959
crd := apiext.CustomResourceDefinition{
6060
TypeMeta: metav1.TypeMeta{
6161
APIVersion: apiext.SchemeGroupVersion.String(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
---
3+
apiVersion: apiextensions.k8s.io/v1
4+
kind: CustomResourceDefinition
5+
metadata:
6+
annotations:
7+
controller-gen.kubebuilder.io/version: (devel)
8+
creationTimestamp: null
9+
name: testquotas.plural.example.com
10+
spec:
11+
group: plural.example.com
12+
names:
13+
kind: TestQuota
14+
listKind: TestQuotaList
15+
plural: testquotas
16+
singular: testquota
17+
scope: Namespaced
18+
versions:
19+
- name: v1
20+
schema:
21+
openAPIV3Schema:
22+
properties:
23+
apiVersion:
24+
description: 'APIVersion defines the versioned schema of this representation
25+
of an object. Servers should convert recognized schemas to the latest
26+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
27+
type: string
28+
kind:
29+
description: 'Kind is a string value representing the REST resource this
30+
object represents. Servers may infer this from the endpoint the client
31+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
32+
type: string
33+
metadata:
34+
type: object
35+
test:
36+
type: string
37+
type: object
38+
served: true
39+
storage: true
40+
status:
41+
acceptedNames:
42+
kind: ""
43+
plural: ""
44+
conditions: []
45+
storedVersions: []

pkg/crd/testdata/plural/types.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
//go:generate ../../../../.run-controller-gen.sh crd:crdVersions=v1 paths=. output:dir=.
17+
// +groupName=plural.example.com
18+
// +versionName=v1
19+
20+
package plural
21+
22+
import (
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
)
25+
26+
type TestQuota struct {
27+
metav1.TypeMeta `json:",inline"`
28+
metav1.ObjectMeta `json:"metadata,omitempty"`
29+
30+
Test string `json:"test,omitempty"`
31+
}

0 commit comments

Comments
 (0)