Skip to content

Commit b1ff5ff

Browse files
authored
Merge pull request #1000 from twz123/optional-groupname
✨ Make the anonymous field of the groupName marker optional
2 parents 979e571 + 7c1db6b commit b1ff5ff

File tree

5 files changed

+102
-4
lines changed

5 files changed

+102
-4
lines changed

pkg/crd/markers/package.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
func init() {
2424
AllDefinitions = append(AllDefinitions,
25-
must(markers.MakeDefinition("groupName", markers.DescribesPackage, "")).
25+
mustOptional(markers.MakeDefinition("groupName", markers.DescribesPackage, "")).
2626
WithHelp(markers.SimpleHelp("CRD", "specifies the API group name for this package.")),
2727

2828
must(markers.MakeDefinition("versionName", markers.DescribesPackage, "")).

pkg/crd/markers/register.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package markers
1818

1919
import (
20+
"fmt"
2021
"reflect"
2122

2223
"sigs.k8s.io/controller-tools/pkg/markers"
@@ -56,6 +57,19 @@ func must(def *markers.Definition, err error) *definitionWithHelp {
5657
}
5758
}
5859

60+
func mustOptional(def *markers.Definition, err error) *definitionWithHelp {
61+
def = markers.Must(def, err)
62+
if !def.AnonymousField() {
63+
def = markers.Must(def, fmt.Errorf("not an anonymous field: %v", def))
64+
}
65+
field := def.Fields[""]
66+
field.Optional = true
67+
def.Fields[""] = field
68+
return &definitionWithHelp{
69+
Definition: def,
70+
}
71+
}
72+
5973
// AllDefinitions contains all marker definitions for this package.
6074
var AllDefinitions []*definitionWithHelp
6175

pkg/crd/parser_integration_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
100100
}
101101
})
102102

103-
assertCRD := func(pkg *loader.Package, kind, fileName string) {
104-
By(fmt.Sprintf("requesting that the %s CRD be generated", kind))
105-
groupKind := schema.GroupKind{Kind: kind, Group: "testdata.kubebuilder.io"}
103+
assertCRDForGroupKind := func(pkg *loader.Package, groupKind schema.GroupKind, fileName string) {
104+
kind := groupKind.Kind
105+
By(fmt.Sprintf("requesting that the %s CRD be generated in group %s", kind, groupKind.Group))
106106
parser.NeedCRDFor(groupKind, nil)
107107

108108
By(fmt.Sprintf("fixing top level ObjectMeta on the %s CRD", kind))
@@ -131,6 +131,10 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
131131
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))
132132
}
133133

134+
assertCRD := func(pkg *loader.Package, kind, fileName string) {
135+
assertCRDForGroupKind(pkg, schema.GroupKind{Group: "testdata.kubebuilder.io", Kind: kind}, fileName)
136+
}
137+
134138
assertError := func(pkg *loader.Package, kind, errorMsg string) {
135139
By(fmt.Sprintf("requesting that the %s CRD be generated", kind))
136140
groupKind := schema.GroupKind{Kind: kind, Group: "testdata.kubebuilder.io"}
@@ -183,6 +187,16 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
183187
assertError(pkgs[0], "CronJob", "is not in 'xxx=xxx' format")
184188
})
185189
})
190+
191+
Context("CronJob API without group", func() {
192+
BeforeEach(func() {
193+
pkgPaths = []string{"./nogroup"}
194+
expPkgLen = 1
195+
})
196+
It("should successfully generate the CronJob CRD", func() {
197+
assertCRDForGroupKind(pkgs[0], schema.GroupKind{Kind: "CronJob"}, "testdata._cronjobs.yaml")
198+
})
199+
})
186200
})
187201

188202
It("should generate plural words for Kind correctly", func() {

pkg/crd/testdata/nogroup/types.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
// +groupName=
17+
package nogroup
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// +kubebuilder:object:root=true
24+
25+
// CronJob is the Schema for the cronjobs API
26+
type CronJob struct {
27+
metav1.TypeMeta `json:",inline"`
28+
metav1.ObjectMeta `json:"metadata,omitempty"`
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: (devel)
7+
name: cronjobs.
8+
spec:
9+
group: ""
10+
names:
11+
kind: CronJob
12+
listKind: CronJobList
13+
plural: cronjobs
14+
singular: cronjob
15+
scope: Namespaced
16+
versions:
17+
- name: nogroup
18+
schema:
19+
openAPIV3Schema:
20+
description: CronJob is the Schema for the cronjobs API
21+
properties:
22+
apiVersion:
23+
description: |-
24+
APIVersion defines the versioned schema of this representation of an object.
25+
Servers should convert recognized schemas to the latest internal value, and
26+
may reject unrecognized values.
27+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
28+
type: string
29+
kind:
30+
description: |-
31+
Kind is a string value representing the REST resource this object represents.
32+
Servers may infer this from the endpoint the client submits requests to.
33+
Cannot be updated.
34+
In CamelCase.
35+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
36+
type: string
37+
metadata:
38+
type: object
39+
type: object
40+
served: true
41+
storage: true

0 commit comments

Comments
 (0)