Skip to content

Commit

Permalink
standardize kubebuilder codegeneration labels to have +kubebuilder: p…
Browse files Browse the repository at this point in the history
…refix
  • Loading branch information
Liujingfang1 committed Apr 9, 2018
1 parent 0ddf457 commit c3292c4
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/internal/codegen/parse/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func parseType(t *types.Type) (*codegen.Struct, []*types.Type) {

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

Expand Down
6 changes: 3 additions & 3 deletions cmd/internal/codegen/parse/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func (b *APIs) parseControllers() {

func (b *APIs) getControllerTag(c *types.Type) string {
comments := Comments(c.CommentLines)
resource := comments.getTag("controller", ":")
resource := comments.getTag("controller", ":") + comments.getTag("kubebuilder:controller", ":")
if len(resource) == 0 {
panic(errors.Errorf("Must specify +controller comment for type %v", c.Name))
panic(errors.Errorf("Must specify +kubebuilder:controller comment for type %v", c.Name))
}
return resource
}
Expand All @@ -59,7 +59,7 @@ func parseControllerTag(tag string) controllerTags {
for _, elem := range strings.Split(tag, ",") {
kv := strings.Split(elem, "=")
if len(kv) != 2 {
log.Fatalf("// +controller: tags must be key value pairs. Expected "+
log.Fatalf("// +kubebuilder:controller: tags must be key value pairs. Expected "+
"keys [group=<group>,version=<version>,kind=<kind>,resource=<resource>] "+
"Got string: [%s]", tag)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/internal/codegen/parse/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func parseResourceTag(tag string) resourceTags {
for _, elem := range strings.Split(tag, ",") {
kv := strings.Split(elem, "=")
if len(kv) != 2 {
log.Fatalf("// +resource: tags must be key value pairs. Expected "+
log.Fatalf("// +kubebuilder:resource: tags must be key value pairs. Expected "+
"keys [path=<subresourcepath>] "+
"Got string: [%s]", tag)
}
Expand Down Expand Up @@ -230,9 +230,9 @@ func parseResourceTag(tag string) resourceTags {
// getResourceTag returns the value of the "+resource=" comment tag
func (b *APIs) getResourceTag(c *types.Type) string {
comments := Comments(c.CommentLines)
resource := comments.getTag("resource", ":")
resource := comments.getTag("resource", ":") + comments.getTag("kubebuilder:resource", ":")
if len(resource) == 0 {
panic(errors.Errorf("Must specify +resource comment for type %v", c.Name))
panic(errors.Errorf("Must specify +kubebuilder:resource comment for type %v", c.Name))
}
return resource
}
10 changes: 6 additions & 4 deletions cmd/internal/codegen/parse/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func (b *APIs) parseRBAC() {
func (b *APIs) getRBACTag(c *types.Type) []string {
comments := Comments(c.CommentLines)
resource := comments.getTags("rbac", ":")
resource = append(resource, comments.getTags("kubebuilder:rbac", ":")...)
if len(resource) == 0 {
panic(fmt.Errorf("Must specify +rbac comment for type %v", c.Name))
panic(fmt.Errorf("Must specify +kubebuilder:rbac comment for type %v", c.Name))
}
return resource
}
Expand All @@ -50,7 +51,7 @@ func parseRBACTag(tag string) rbacv1.PolicyRule {
for _, elem := range strings.Split(tag, ",") {
kv := strings.Split(elem, "=")
if len(kv) != 2 {
log.Fatalf("// +rbac: tags must be key value pairs. Expected "+
log.Fatalf("// +kubebuilder:rbac: tags must be key value pairs. Expected "+
"keys [groups=<group1;group2>,resources=<resource1;resource2>,verbs=<verb1;verb2>] "+
"Got string: [%s]", tag)
}
Expand Down Expand Up @@ -90,8 +91,9 @@ func (b *APIs) parseInformers() {
func (b *APIs) getInformerTag(c *types.Type) []string {
comments := Comments(c.CommentLines)
resource := comments.getTags("informers", ":")
resource = append(resource, comments.getTags("kubebuilder:informers", ":")...)
if len(resource) == 0 {
panic(fmt.Errorf("Must specify +informers comment for type %v", c.Name))
panic(fmt.Errorf("Must specify +kubebuilder:informers comment for type %v", c.Name))
}
return resource
}
Expand All @@ -101,7 +103,7 @@ func parseInformerTag(tag string) v1.GroupVersionKind {
for _, elem := range strings.Split(tag, ",") {
kv := strings.Split(elem, "=")
if len(kv) != 2 {
log.Fatalf("// +informers: tags must be key value pairs. Expected "+
log.Fatalf("// +kubebuilder:informers: tags must be key value pairs. Expected "+
"keys [group=core,version=v1,kind=Pod] "+
"Got string: [%s]", tag)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/internal/codegen/parse/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (
"k8s.io/gengo/types"
)

// IsAPIResource returns true if t has a +resource comment tag
// IsAPIResource returns true if t has a +resource/+kubebuilder:resource comment tag
func IsAPIResource(t *types.Type) bool {
for _, c := range t.CommentLines {
if strings.Contains(c, "+resource") {
if strings.Contains(c, "+resource") || strings.Contains(c, "+kubebuilder:resource"){
return true
}
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func IsNonNamespaced(t *types.Type) bool {

func IsController(t *types.Type) bool {
for _, c := range t.CommentLines {
if strings.Contains(c, "+controller") {
if strings.Contains(c, "+controller") || strings.Contains(c, "+kubebuilder:controller") {
return true
}
}
Expand All @@ -68,7 +68,7 @@ func IsController(t *types.Type) bool {

func IsRBAC(t *types.Type) bool {
for _, c := range t.CommentLines {
if strings.Contains(c, "+rbac") {
if strings.Contains(c, "+rbac") || strings.Contains(c, "+kubebuilder:rbac") {
return true
}
}
Expand All @@ -77,7 +77,7 @@ func IsRBAC(t *types.Type) bool {

func IsInformer(t *types.Type) bool {
for _, c := range t.CommentLines {
if strings.Contains(c, "+informers") {
if strings.Contains(c, "+informers") || strings.Contains(c, "+kubebuilder:informers") {
return true
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubebuilder/create/resource/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (bc *{{.Kind}}Controller) Reconcile(k types.ReconcileKey) error {
return nil
}
// +controller:group={{ .Group }},version={{ .Version }},kind={{ .Kind}},resource={{ .Resource }}
// +kubebuilder:controller:group={{ .Group }},version={{ .Version }},kind={{ .Kind}},resource={{ .Resource }}
type {{.Kind}}Controller struct {
// INSERT ADDITIONAL FIELDS HERE
{{lower .Kind}}Lister {{.Group}}{{.Version}}lister.{{.Kind}}Lister
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubebuilder/create/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type {{.Kind}}Status struct {
// {{.Kind}}
// +k8s:openapi-gen=true
// +resource:path={{.Resource}}
// +kubebuilder:resource:path={{.Resource}}
type {{.Kind}} struct {
metav1.TypeMeta ` + "`" + `json:",inline"` + "`" + `
metav1.ObjectMeta ` + "`" + `json:"metadata,omitempty"` + "`" + `
Expand Down
34 changes: 34 additions & 0 deletions cmd/kubebuilder/initproject/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"

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

func runInitRepo(cmd *cobra.Command, args []string) {
version := runtime.Version()
if versionCmp(version, "go1.10") < 0 {
log.Fatalf("The go version is %v, must be 1.10+", version)
}

if len(domain) == 0 {
log.Fatal("Must specify --domain")
}
Expand Down Expand Up @@ -97,3 +105,29 @@ type templateArgs struct {
BoilerPlate string
Repo string
}

func versionCmp(v1 string, v2 string) int {
v1s := strings.Split(strings.Replace(v1, "go", "", 1), ".")
v2s := strings.Split(strings.Replace(v2, "go", "", 1), ".")
for i := 0; i < len(v1s) && i < len(v2s); i++ {
mv1, err1 := strconv.Atoi(v1s[i])
mv2, err2 := strconv.Atoi(v2s[i])
if err1 == nil && err2 == nil {
cmp := mv1 - mv2
if cmp > 0 {
return 1
} else if cmp < 0 {
return -1
}
} else {
log.Fatalf("Unexpected error comparing %v with %v", v1, v2)
}
}
if len(v1s) == len(v2s) {
return 0
} else if len(v1s) > len(v2s) {
return 1
} else {
return -1
}
}
2 changes: 1 addition & 1 deletion docs/adding_non_namespaced_resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Example:
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +nonNamespaced=true

// +resource:path=foos
// +kubebuilder:resource:path=foos
// +k8s:openapi-gen=true
// Foo defines some thing
type Foo struct {
Expand Down
8 changes: 4 additions & 4 deletions docs/declaring_rbac_rules_for_controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Since your controller will likely be interacting with additional resources
(e.g. core resources), it is possible to declare additional RBAC rules
for the controller ServiceAccount to be installed.

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

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

Expand Down
2 changes: 1 addition & 1 deletion pkg/gen/apis/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The apis package describes the comment directives that may be applied to apis /
package apis

// Resource annotates a type as a resource
const Resource = "// +resource:path="
const Resource = "// +kubebuilder:resource:path="

// Maximum annotates a go struct field for CRD validation
const Maximum = "// +kubebuilder:validation:Maximum="
Expand Down
2 changes: 1 addition & 1 deletion pkg/gen/apis/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type FooStatus struct{}

// Foo
// +k8s:openapi-gen=true
// +resource:path=foos
// +kubebuilder:resource:path=foos
type Foo struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions pkg/gen/controller/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ The controller package describes comment directives that may be applied to contr
package controller

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

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

// Informers indicates that an informer must be started for this controller
const Informers = "// +informers:group=core,version=v1,kind=Pod"
const Informers = "// +kubebuilder:informers:group=core,version=v1,kind=Pod"
10 changes: 5 additions & 5 deletions pkg/gen/controller/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package controller_test

func Example() {}

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

0 comments on commit c3292c4

Please sign in to comment.