Skip to content

Commit 565b835

Browse files
committed
added a commandline flag to skip RBAC validation.
1 parent 9025ebf commit 565b835

File tree

6 files changed

+57
-21
lines changed

6 files changed

+57
-21
lines changed

cmd/internal/codegen/parse/parser.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ func NewAPIs(context *generator.Context, arguments *args.GeneratorArgs) *APIs {
8181
// e.g. if there is an // +kubebuilder:informer annotation for Pods, then there
8282
// should also be a // +kubebuilder:rbac annotation for Pods
8383
func (b *APIs) verifyRBACAnnotations() {
84+
parseOption := b.arguments.CustomArgs.(*ParseOptions)
85+
if parseOption.SkipRBACValidation {
86+
log.Println("skipping RBAC validations")
87+
return
88+
}
8489
err := rbacMatchesInformers(b.Informers, b.Rules)
8590
if err != nil {
8691
log.Fatal(err)
@@ -141,7 +146,8 @@ func rbacMatchesInformers(informers map[v1.GroupVersionKind]bool, rbacRules []rb
141146
}
142147
if !found {
143148
return fmt.Errorf("Missing rbac rule for %s.%s. Add with // +kubebuilder:rbac:groups=%s,"+
144-
"resources=%s,verbs=get;list;watch", gvk.Group, gvk.Kind, gvk.Group,
149+
"resources=%s,verbs=get;list;watch comment on controller struct "+
150+
"or run the command with '--skip-rbac-validation' arg", gvk.Group, gvk.Kind, gvk.Group,
145151
inflect.NewDefaultRuleset().Pluralize(strings.ToLower(gvk.Kind)))
146152
}
147153
}

cmd/internal/codegen/parse/util.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ import (
2727
)
2828

2929
type ParseOptions struct {
30-
SkipMapValidation bool
30+
SkipMapValidation bool
31+
32+
// SkipRBACValidation flag determines whether to check RBAC annotations
33+
// for the controller or not at parse stage.
34+
SkipRBACValidation bool
3135
}
3236

3337
// IsAPIResource returns true if t has a +resource/+kubebuilder:resource comment tag

cmd/kubebuilder-gen/codegen/run/generator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
"github.com/golang/glog"
2121
"github.com/kubernetes-sigs/kubebuilder/cmd/internal/codegen/parse"
2222
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder-gen/codegen"
23+
"github.com/spf13/pflag"
2324
"k8s.io/gengo/args"
2425
"k8s.io/gengo/generator"
25-
"github.com/spf13/pflag"
2626
)
2727

2828
// CodeGenerator generates code for Kubernetes resources and controllers
@@ -54,6 +54,8 @@ func (g *CodeGenerator) Execute() error {
5454
customArgs := &parse.ParseOptions{}
5555
pflag.CommandLine.BoolVar(&customArgs.SkipMapValidation, "skip-map-validation", true,
5656
"if set to true, skip generating validation schema for map type in CRD.")
57+
pflag.CommandLine.BoolVar(&customArgs.SkipRBACValidation, "skip-rbac-validation", false,
58+
"if set to true, skip validation for RBAC annotations for the controller.")
5759
arguments.CustomArgs = customArgs
5860

5961
arguments.OutputFileBaseName = g.OutputFileBaseName

cmd/kubebuilder/create/controller/run.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ import (
2121
"log"
2222
"os"
2323

24+
"strings"
25+
2426
createutil "github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/create/util"
2527
generatecmd "github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/generate"
2628
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
2729
"github.com/markbates/inflect"
2830
"github.com/spf13/cobra"
29-
"strings"
3031
)
3132

3233
type ControllerArguments struct {
33-
nonNamespacedKind bool
34-
generate bool
35-
CoreType bool
34+
nonNamespacedKind bool
35+
generate bool
36+
CoreType bool
37+
SkipRBACValidation bool
3638
}
3739

3840
func AddCreateController(cmd *cobra.Command) {
@@ -59,6 +61,7 @@ kubebuilder create controller --group apps --version v1beta2 --kind Deployment -
5961
createControllerCmd.Flags().BoolVar(&c.nonNamespacedKind, "non-namespaced", false, "if set, the API kind will be non namespaced")
6062
createControllerCmd.Flags().BoolVar(&c.generate, "generate", true, "generate controller code")
6163
createControllerCmd.Flags().BoolVar(&c.CoreType, "core-type", false, "generate controller for core type")
64+
createControllerCmd.Flags().BoolVar(&c.SkipRBACValidation, "skip-rbac-validation", false, "if set to true, skip validation for RBAC annotations for the controller.")
6265
cmd.AddCommand(createControllerCmd)
6366
}
6467

@@ -77,6 +80,9 @@ func (c *ControllerArguments) RunCreateController(cmd *cobra.Command, args []str
7780
if c.generate {
7881
fmt.Printf("Generating code for new controller... " +
7982
"Regenerate after editing controller files by running `kubebuilder generate clean; kubebuilder generate`.\n")
83+
if c.SkipRBACValidation {
84+
args = append(args, "--skip-rbac-validation")
85+
}
8086
generatecmd.RunGenerate(cmd, args)
8187
}
8288
fmt.Printf("Next: Run the controller and create an instance with:\n" +

cmd/kubebuilder/create/resource/run.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ import (
3030
"github.com/spf13/cobra"
3131
)
3232

33-
var nonNamespacedKind bool
34-
var controller bool
35-
var generate bool
33+
var (
34+
nonNamespacedKind bool
35+
controller bool
36+
generate bool
37+
skipRBACValidation bool
38+
)
3639

3740
var createResourceCmd = &cobra.Command{
3841
Use: "resource",
@@ -63,6 +66,7 @@ func AddCreateResource(cmd *cobra.Command) {
6366
createResourceCmd.Flags().BoolVar(&controller, "controller", true, "if true, generate the controller code for the resource")
6467
createResourceCmd.Flags().BoolVar(&generate, "generate", true, "generate source code")
6568
createResourceCmd.Flags().BoolVar(&createutil.AllowPluralKind, "plural-kind", false, "allow the kind to be plural")
69+
createResourceCmd.Flags().BoolVar(&skipRBACValidation, "skip-rbac-validation", false, "if set to true, skip validation for RBAC annotations")
6670
cmd.AddCommand(createResourceCmd)
6771
}
6872

@@ -83,6 +87,9 @@ func RunCreateResource(cmd *cobra.Command, args []string) {
8387
if generate {
8488
fmt.Printf("Generating code for new resource... " +
8589
"Regenerate after editing resources files by running `kubebuilder build generated`.\n")
90+
if skipRBACValidation {
91+
args = append(args, "--skip-rbac-validation")
92+
}
8693
generatecmd.RunGenerate(cmd, args)
8794
}
8895
fmt.Printf("Next: Install the API, run the controller and create an instance with:\n" +
@@ -115,7 +122,10 @@ func createResource(boilerplate string) {
115122

116123
if controller {
117124
fmt.Printf("Creating controller ...\n")
118-
c := controllerct.ControllerArguments{CoreType: false}
125+
c := controllerct.ControllerArguments{
126+
CoreType: false,
127+
SkipRBACValidation: skipRBACValidation,
128+
}
119129
c.CreateController(boilerplate)
120130
}
121131

cmd/kubebuilder/generate/generate.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ import (
3131
"k8s.io/apimachinery/pkg/util/sets"
3232
)
3333

34-
var versionedAPIs []string
35-
var unversionedAPIs []string
36-
var Codegenerators []string
37-
var copyright string
38-
var generators = sets.String{}
39-
var vendorDir string
40-
var Docscopyright string
41-
var Docstitle string
34+
var (
35+
versionedAPIs []string
36+
unversionedAPIs []string
37+
Codegenerators []string
38+
copyright string
39+
generators = sets.String{}
40+
vendorDir string
41+
Docscopyright string
42+
Docstitle string
43+
skipRBACValidation bool
44+
)
4245

4346
var generateCmd = &cobra.Command{
4447
Use: "generate",
@@ -59,6 +62,7 @@ func AddGenerate(cmd *cobra.Command) {
5962
cmd.AddCommand(generateCmd)
6063
generateCmd.Flags().StringVar(&copyright, "copyright", filepath.Join("hack", "boilerplate.go.txt"), "Location of copyright boilerplate file.")
6164
generateCmd.Flags().StringVar(&vendorDir, "vendor-dir", "", "Location of directory containing vendor files.")
65+
generateCmd.Flags().BoolVar(&skipRBACValidation, "skip-rbac-validation", false, "if set to true, skip validation for RBAC annotations for the controller.")
6266
generateCmd.Flags().StringArrayVar(&versionedAPIs, "api-versions", []string{}, "API version to generate code for. Can be specified multiple times. e.g. --api-versions foo/v1beta1 --api-versions bar/v1 defaults to all versions found under directories pkg/apis/<group>/<version>")
6367
generateCmd.Flags().StringArrayVar(&Codegenerators, "generator", []string{}, "list of generators to run. e.g. --generator kubebuilder --generator conversion Valid values: [kubebuilder,client,openapi]")
6468
generateCmd.Flags().StringVar(&Docscopyright, "docs-copyright", "<a href=\"https://github.com/kubernetes/kubernetes\">Copyright 2018 The Kubernetes Authors.</a>", "html for the copyright text on the docs")
@@ -126,12 +130,16 @@ func RunGenerate(cmd *cobra.Command, args []string) {
126130
}
127131

128132
if doGen("kubebuilder-gen") {
129-
c := exec.Command(filepath.Join(root, "kubebuilder-gen"),
133+
genArgs := []string{
130134
"--go-header-file", copyright,
131135
"--input-dirs", filepath.Join(util.Repo, "pkg", "apis", "..."),
132136
"--input-dirs", filepath.Join(util.Repo, "pkg", "controller", "..."),
133137
"--input-dirs", filepath.Join(util.Repo, "pkg", "inject", "..."),
134-
)
138+
}
139+
if skipRBACValidation {
140+
genArgs = append(genArgs, "--skip-rbac-validation")
141+
}
142+
c := exec.Command(filepath.Join(root, "kubebuilder-gen"), genArgs...)
135143
glog.V(4).Infof("%s\n", strings.Join(c.Args, " "))
136144
out, err := c.CombinedOutput()
137145
if err != nil {

0 commit comments

Comments
 (0)