Skip to content

Commit 1ccaeea

Browse files
author
Phillip Wittrock
authored
Merge pull request #66 from Liujingfang1/tagprefix
standardize kubebuilder codegeneration labels to have +kubebuilder: prefix
2 parents 72e1199 + c3292c4 commit 1ccaeea

File tree

14 files changed

+69
-33
lines changed

14 files changed

+69
-33
lines changed

cmd/internal/codegen/parse/apis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func parseType(t *types.Type) (*codegen.Struct, []*types.Type) {
267267

268268
func (b *APIs) genClient(c *types.Type) bool {
269269
comments := Comments(c.CommentLines)
270-
resource := comments.getTag("resource", ":")
270+
resource := comments.getTag("resource", ":") + comments.getTag("kubebuilder:resource", ":")
271271
return len(resource) > 0
272272
}
273273

cmd/internal/codegen/parse/controllers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ func (b *APIs) parseControllers() {
4646

4747
func (b *APIs) getControllerTag(c *types.Type) string {
4848
comments := Comments(c.CommentLines)
49-
resource := comments.getTag("controller", ":")
49+
resource := comments.getTag("controller", ":") + comments.getTag("kubebuilder:controller", ":")
5050
if len(resource) == 0 {
51-
panic(errors.Errorf("Must specify +controller comment for type %v", c.Name))
51+
panic(errors.Errorf("Must specify +kubebuilder:controller comment for type %v", c.Name))
5252
}
5353
return resource
5454
}
@@ -59,7 +59,7 @@ func parseControllerTag(tag string) controllerTags {
5959
for _, elem := range strings.Split(tag, ",") {
6060
kv := strings.Split(elem, "=")
6161
if len(kv) != 2 {
62-
log.Fatalf("// +controller: tags must be key value pairs. Expected "+
62+
log.Fatalf("// +kubebuilder:controller: tags must be key value pairs. Expected "+
6363
"keys [group=<group>,version=<version>,kind=<kind>,resource=<resource>] "+
6464
"Got string: [%s]", tag)
6565
}

cmd/internal/codegen/parse/index.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func parseResourceTag(tag string) resourceTags {
184184
for _, elem := range strings.Split(tag, ",") {
185185
kv := strings.Split(elem, "=")
186186
if len(kv) != 2 {
187-
log.Fatalf("// +resource: tags must be key value pairs. Expected "+
187+
log.Fatalf("// +kubebuilder:resource: tags must be key value pairs. Expected "+
188188
"keys [path=<subresourcepath>] "+
189189
"Got string: [%s]", tag)
190190
}
@@ -230,9 +230,9 @@ func parseResourceTag(tag string) resourceTags {
230230
// getResourceTag returns the value of the "+resource=" comment tag
231231
func (b *APIs) getResourceTag(c *types.Type) string {
232232
comments := Comments(c.CommentLines)
233-
resource := comments.getTag("resource", ":")
233+
resource := comments.getTag("resource", ":") + comments.getTag("kubebuilder:resource", ":")
234234
if len(resource) == 0 {
235-
panic(errors.Errorf("Must specify +resource comment for type %v", c.Name))
235+
panic(errors.Errorf("Must specify +kubebuilder:resource comment for type %v", c.Name))
236236
}
237237
return resource
238238
}

cmd/internal/codegen/parse/rbac.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ func (b *APIs) parseRBAC() {
3939
func (b *APIs) getRBACTag(c *types.Type) []string {
4040
comments := Comments(c.CommentLines)
4141
resource := comments.getTags("rbac", ":")
42+
resource = append(resource, comments.getTags("kubebuilder:rbac", ":")...)
4243
if len(resource) == 0 {
43-
panic(fmt.Errorf("Must specify +rbac comment for type %v", c.Name))
44+
panic(fmt.Errorf("Must specify +kubebuilder:rbac comment for type %v", c.Name))
4445
}
4546
return resource
4647
}
@@ -50,7 +51,7 @@ func parseRBACTag(tag string) rbacv1.PolicyRule {
5051
for _, elem := range strings.Split(tag, ",") {
5152
kv := strings.Split(elem, "=")
5253
if len(kv) != 2 {
53-
log.Fatalf("// +rbac: tags must be key value pairs. Expected "+
54+
log.Fatalf("// +kubebuilder:rbac: tags must be key value pairs. Expected "+
5455
"keys [groups=<group1;group2>,resources=<resource1;resource2>,verbs=<verb1;verb2>] "+
5556
"Got string: [%s]", tag)
5657
}
@@ -90,8 +91,9 @@ func (b *APIs) parseInformers() {
9091
func (b *APIs) getInformerTag(c *types.Type) []string {
9192
comments := Comments(c.CommentLines)
9293
resource := comments.getTags("informers", ":")
94+
resource = append(resource, comments.getTags("kubebuilder:informers", ":")...)
9395
if len(resource) == 0 {
94-
panic(fmt.Errorf("Must specify +informers comment for type %v", c.Name))
96+
panic(fmt.Errorf("Must specify +kubebuilder:informers comment for type %v", c.Name))
9597
}
9698
return resource
9799
}
@@ -101,7 +103,7 @@ func parseInformerTag(tag string) v1.GroupVersionKind {
101103
for _, elem := range strings.Split(tag, ",") {
102104
kv := strings.Split(elem, "=")
103105
if len(kv) != 2 {
104-
log.Fatalf("// +informers: tags must be key value pairs. Expected "+
106+
log.Fatalf("// +kubebuilder:informers: tags must be key value pairs. Expected "+
105107
"keys [group=core,version=v1,kind=Pod] "+
106108
"Got string: [%s]", tag)
107109
}

cmd/internal/codegen/parse/util.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626
"k8s.io/gengo/types"
2727
)
2828

29-
// IsAPIResource returns true if t has a +resource comment tag
29+
// IsAPIResource returns true if t has a +resource/+kubebuilder:resource comment tag
3030
func IsAPIResource(t *types.Type) bool {
3131
for _, c := range t.CommentLines {
32-
if strings.Contains(c, "+resource") {
32+
if strings.Contains(c, "+resource") || strings.Contains(c, "+kubebuilder:resource"){
3333
return true
3434
}
3535
}
@@ -59,7 +59,7 @@ func IsNonNamespaced(t *types.Type) bool {
5959

6060
func IsController(t *types.Type) bool {
6161
for _, c := range t.CommentLines {
62-
if strings.Contains(c, "+controller") {
62+
if strings.Contains(c, "+controller") || strings.Contains(c, "+kubebuilder:controller") {
6363
return true
6464
}
6565
}
@@ -68,7 +68,7 @@ func IsController(t *types.Type) bool {
6868

6969
func IsRBAC(t *types.Type) bool {
7070
for _, c := range t.CommentLines {
71-
if strings.Contains(c, "+rbac") {
71+
if strings.Contains(c, "+rbac") || strings.Contains(c, "+kubebuilder:rbac") {
7272
return true
7373
}
7474
}
@@ -77,7 +77,7 @@ func IsRBAC(t *types.Type) bool {
7777

7878
func IsInformer(t *types.Type) bool {
7979
for _, c := range t.CommentLines {
80-
if strings.Contains(c, "+informers") {
80+
if strings.Contains(c, "+informers") || strings.Contains(c, "+kubebuilder:informers") {
8181
return true
8282
}
8383
}

cmd/kubebuilder/create/resource/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (bc *{{.Kind}}Controller) Reconcile(k types.ReconcileKey) error {
5959
return nil
6060
}
6161
62-
// +controller:group={{ .Group }},version={{ .Version }},kind={{ .Kind}},resource={{ .Resource }}
62+
// +kubebuilder:controller:group={{ .Group }},version={{ .Version }},kind={{ .Kind}},resource={{ .Resource }}
6363
type {{.Kind}}Controller struct {
6464
// INSERT ADDITIONAL FIELDS HERE
6565
{{lower .Kind}}Lister {{.Group}}{{.Version}}lister.{{.Kind}}Lister

cmd/kubebuilder/create/resource/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type {{.Kind}}Status struct {
7777
7878
// {{.Kind}}
7979
// +k8s:openapi-gen=true
80-
// +resource:path={{.Resource}}
80+
// +kubebuilder:resource:path={{.Resource}}
8181
type {{.Kind}} struct {
8282
metav1.TypeMeta ` + "`" + `json:",inline"` + "`" + `
8383
metav1.ObjectMeta ` + "`" + `json:"metadata,omitempty"` + "`" + `

cmd/kubebuilder/initproject/init.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
"log"
2222
"os"
2323
"path/filepath"
24+
"runtime"
25+
"strconv"
26+
"strings"
2427

2528
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
2629
"github.com/spf13/cobra"
@@ -48,6 +51,11 @@ func AddInit(cmd *cobra.Command) {
4851
}
4952

5053
func runInitRepo(cmd *cobra.Command, args []string) {
54+
version := runtime.Version()
55+
if versionCmp(version, "go1.10") < 0 {
56+
log.Fatalf("The go version is %v, must be 1.10+", version)
57+
}
58+
5159
if len(domain) == 0 {
5260
log.Fatal("Must specify --domain")
5361
}
@@ -97,3 +105,29 @@ type templateArgs struct {
97105
BoilerPlate string
98106
Repo string
99107
}
108+
109+
func versionCmp(v1 string, v2 string) int {
110+
v1s := strings.Split(strings.Replace(v1, "go", "", 1), ".")
111+
v2s := strings.Split(strings.Replace(v2, "go", "", 1), ".")
112+
for i := 0; i < len(v1s) && i < len(v2s); i++ {
113+
mv1, err1 := strconv.Atoi(v1s[i])
114+
mv2, err2 := strconv.Atoi(v2s[i])
115+
if err1 == nil && err2 == nil {
116+
cmp := mv1 - mv2
117+
if cmp > 0 {
118+
return 1
119+
} else if cmp < 0 {
120+
return -1
121+
}
122+
} else {
123+
log.Fatalf("Unexpected error comparing %v with %v", v1, v2)
124+
}
125+
}
126+
if len(v1s) == len(v2s) {
127+
return 0
128+
} else if len(v1s) > len(v2s) {
129+
return 1
130+
} else {
131+
return -1
132+
}
133+
}

docs/adding_non_namespaced_resources.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Example:
3030
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
3131
// +nonNamespaced=true
3232

33-
// +resource:path=foos
33+
// +kubebuilder:resource:path=foos
3434
// +k8s:openapi-gen=true
3535
// Foo defines some thing
3636
type Foo struct {

docs/declaring_rbac_rules_for_controllers.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ Since your controller will likely be interacting with additional resources
1414
(e.g. core resources), it is possible to declare additional RBAC rules
1515
for the controller ServiceAccount to be installed.
1616

17-
To define additional rbac rules, add a `//+rbac` comment to the controller struct
17+
To define additional rbac rules, add a `//kubebuilder:+rbac` comment to the controller struct
1818
under `pkg/controller/<name>/controller.go`
1919

2020
```go
21-
// +rbac:groups=apps;extensions,resources=deployments,verbs=get;list;watch;create;update;delete
22-
// +rbac:groups=,resources=pods,verbs=get;list;watch;create;update;delete
23-
// +controller:group=foo,version=v1alpha1,kind=Bar,resource=bars
21+
// +kubebuilder:rbac:groups=apps;extensions,resources=deployments,verbs=get;list;watch;create;update;delete
22+
// +kubebuilder:rbac:groups=,resources=pods,verbs=get;list;watch;create;update;delete
23+
// +kubebuilder:controller:group=foo,version=v1alpha1,kind=Bar,resource=bars
2424
type BarControllerImpl struct {
2525
builders.DefaultControllerFns
2626

pkg/gen/apis/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The apis package describes the comment directives that may be applied to apis /
2020
package apis
2121

2222
// Resource annotates a type as a resource
23-
const Resource = "// +resource:path="
23+
const Resource = "// +kubebuilder:resource:path="
2424

2525
// Maximum annotates a go struct field for CRD validation
2626
const Maximum = "// +kubebuilder:validation:Maximum="

pkg/gen/apis/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type FooStatus struct{}
3737

3838
// Foo
3939
// +k8s:openapi-gen=true
40-
// +resource:path=foos
40+
// +kubebuilder:resource:path=foos
4141
type Foo struct {
4242
metav1.TypeMeta `json:",inline"`
4343
metav1.ObjectMeta `json:"metadata,omitempty"`

pkg/gen/controller/doc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ The controller package describes comment directives that may be applied to contr
2020
package controller
2121

2222
// Controller annotates a type as being a controller for a specific resource
23-
const Controller = "// +controller:group=,version=,kind=,resource="
23+
const Controller = "// +kubebuilder:controller:group=,version=,kind=,resource="
2424

2525
// RBAC annotates a controller struct as needing an RBAC rule to run
26-
const RBAC = "// +rbac:groups=<group1;group2>,resources=<resource1;resource2>,verbs=<verb1;verb2>"
26+
const RBAC = "// +kubebuilder:rbac:groups=<group1;group2>,resources=<resource1;resource2>,verbs=<verb1;verb2>"
2727

2828
// Informers indicates that an informer must be started for this controller
29-
const Informers = "// +informers:group=core,version=v1,kind=Pod"
29+
const Informers = "// +kubebuilder:informers:group=core,version=v1,kind=Pod"

pkg/gen/controller/example_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ package controller_test
1818

1919
func Example() {}
2020

21-
// +controller:group=foo,version=v1beta1,kind=Bar,resource=bars
22-
// +rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
23-
// +informers:group=apps,version=v1,kind=Deployment
24-
// +rbac:groups="",resources=pods,verbs=get;watch;list
25-
// +informers:group=core,version=v1,kind=Pod
21+
// +kubebuilder:controller:group=foo,version=v1beta1,kind=Bar,resource=bars
22+
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
23+
// +kubebuilder:informers:group=apps,version=v1,kind=Deployment
24+
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;watch;list
25+
// +kubebuilder:informers:group=core,version=v1,kind=Pod
2626
type FooController struct{}

0 commit comments

Comments
 (0)