Skip to content

Commit

Permalink
Merge c781f35 into d98c4cb
Browse files Browse the repository at this point in the history
  • Loading branch information
Adirio committed Jan 20, 2020
2 parents d98c4cb + c781f35 commit fc5224c
Show file tree
Hide file tree
Showing 55 changed files with 891 additions and 749 deletions.
21 changes: 11 additions & 10 deletions cmd/api.go
Expand Up @@ -59,19 +59,20 @@ func (o *apiOptions) bindCmdFlags(cmd *cobra.Command) {
}
cmd.Flags().BoolVar(&o.apiScaffolder.Force, "force", false,
"attempt to create resource even if it already exists")
o.apiScaffolder.Resource = resourceForFlags(cmd.Flags())
o.apiScaffolder.ResourceOptions = resourceOptionsForFlags(cmd.Flags())
}

// resourceForFlags registers flags for Resource fields and returns the Resource
func resourceForFlags(f *flag.FlagSet) *resource.Resource {
r := &resource.Resource{}
f.StringVar(&r.Kind, "kind", "", "resource Kind")
f.StringVar(&r.Group, "group", "", "resource Group")
f.StringVar(&r.Version, "version", "", "resource Version")
f.BoolVar(&r.Namespaced, "namespaced", true, "resource is namespaced")
f.BoolVar(&r.CreateExampleReconcileBody, "example", true,
// resourceOptionsForFlags registers flags for Resource fields and returns the resource Options
func resourceOptionsForFlags(f *flag.FlagSet) *resource.Options {
opts := &resource.Options{}
f.BoolVar(&opts.Namespaced, "namespaced", true, "resource is namespaced")
f.StringVar(&opts.Group, "group", "", "resource Group")
f.StringVar(&opts.Version, "version", "", "resource Version")
f.StringVar(&opts.Kind, "kind", "", "resource Kind")
f.StringVar(&opts.Plural, "plural", "", "[optional] resource Kind plural form")
f.BoolVar(&opts.CreateExampleReconcileBody, "example", true,
"if true an example reconcile body should be written while scaffolding a resource.")
return r
return opts
}

// APICmd represents the resource command
Expand Down
31 changes: 16 additions & 15 deletions cmd/webhook_v1.go
Expand Up @@ -61,20 +61,21 @@ This command is only available for v1 scaffolding project.
os.Exit(1)
}

if err := o.res.Validate(); err != nil {
log.Fatal(err)
if err := o.resOpts.Validate(); err != nil {
log.Fatalf("invalid resource information provided: %v", err)
}
res := o.resOpts.NewV1Resource(projectInfo.Repo, projectInfo.Domain)

fmt.Println("Writing scaffold for you to edit...")
config := webhook.Config{Server: o.server, Type: o.webhookType, Operations: o.operations}
err = (&scaffold.Scaffold{}).Execute(
&model.Universe{},
input.Options{},
&manager.Webhook{},
&webhook.AdmissionHandler{Resource: o.res, Config: config},
&webhook.AdmissionWebhookBuilder{Resource: o.res, Config: config},
&webhook.AdmissionWebhooks{Resource: o.res, Config: config},
&webhook.AddAdmissionWebhookBuilderHandler{Resource: o.res, Config: config},
&webhook.AdmissionHandler{Resource: res, Config: config},
&webhook.AdmissionWebhookBuilder{Resource: res, Config: config},
&webhook.AdmissionWebhooks{Resource: res, Config: config},
&webhook.AddAdmissionWebhookBuilderHandler{Resource: res, Config: config},
&webhook.Server{Config: config},
&webhook.AddServer{Config: config},
)
Expand All @@ -101,25 +102,25 @@ This command is only available for v1 scaffolding project.
"the operations that the webhook will intercept, e.g. create, update, delete and connect")
cmd.Flags().BoolVar(&o.doMake, "make", true,
"if true, run make after generating files")
o.res = gvkForFlags(cmd.Flags())
o.resOpts = gvkForFlags(cmd.Flags())
return cmd
}

// webhookOptions represents commandline options for scaffolding a webhook.
type webhookOptions struct {
res *resource.Resource
resOpts *resource.Options
operations []string
server string
webhookType string
doMake bool
}

// gvkForFlags registers flags for Resource fields and returns the Resource
func gvkForFlags(f *flag.FlagSet) *resource.Resource {
r := &resource.Resource{}
f.StringVar(&r.Group, "group", "", "resource Group")
f.StringVar(&r.Version, "version", "", "resource Version")
f.StringVar(&r.Kind, "kind", "", "resource Kind")
f.StringVar(&r.Resource, "resource", "", "resource Resource")
return r
func gvkForFlags(f *flag.FlagSet) *resource.Options {
options := &resource.Options{}
f.StringVar(&options.Group, "group", "", "resource Group")
f.StringVar(&options.Version, "version", "", "resource Version")
f.StringVar(&options.Kind, "kind", "", "resource Kind")
f.StringVar(&options.Plural, "resource", "", "resource Resource")
return options
}
25 changes: 13 additions & 12 deletions cmd/webhook_v2.go
Expand Up @@ -62,32 +62,33 @@ func newWebhookV2Cmd() *cobra.Command {
os.Exit(1)
}

if err := o.resOpts.Validate(); err != nil {
log.Fatalf("non-valid resource provided: %v", err)
}
res := o.resOpts.NewResource(projectInfo.Repo, projectInfo.Domain, projectInfo.MultiGroup)

if !o.defaulting && !o.validation && !o.conversion {
fmt.Printf("kubebuilder webhook requires at least one of" +
" --defaulting, --programmatic-validation and --conversion to be true")
os.Exit(1)
}

if err := o.res.Validate(); err != nil {
log.Fatal(err)
}

fmt.Println("Writing scaffold for you to edit...")

if projectInfo.MultiGroup {
fmt.Println(filepath.Join("apis", o.res.Group, o.res.Version,
fmt.Sprintf("%s_webhook.go", strings.ToLower(o.res.Kind))))
fmt.Println(filepath.Join("apis", res.Group, res.Version,
fmt.Sprintf("%s_webhook.go", strings.ToLower(res.Kind))))
} else {
fmt.Println(filepath.Join("api", o.res.Version,
fmt.Sprintf("%s_webhook.go", strings.ToLower(o.res.Kind))))
fmt.Println(filepath.Join("api", res.Version,
fmt.Sprintf("%s_webhook.go", strings.ToLower(res.Kind))))
}

if o.conversion {
fmt.Println(`Webhook server has been set up for you.
You need to implement the conversion.Hub and conversion.Convertible interfaces for your CRD types.`)
}
webhookScaffolder := &webhook.Webhook{
Resource: o.res,
Resource: res,
Defaulting: o.defaulting,
Validating: o.validation,
}
Expand All @@ -107,7 +108,7 @@ You need to implement the conversion.Hub and conversion.Convertible interfaces f
WireResource: false,
WireController: false,
WireWebhook: true,
Resource: o.res,
Resource: res,
})
if err != nil {
fmt.Printf("error updating main.go: %v", err)
Expand All @@ -116,7 +117,7 @@ You need to implement the conversion.Hub and conversion.Convertible interfaces f

},
}
o.res = gvkForFlags(cmd.Flags())
o.resOpts = gvkForFlags(cmd.Flags())
cmd.Flags().BoolVar(&o.defaulting, "defaulting", false,
"if set, scaffold the defaulting webhook")
cmd.Flags().BoolVar(&o.validation, "programmatic-validation", false,
Expand All @@ -129,7 +130,7 @@ You need to implement the conversion.Hub and conversion.Convertible interfaces f

// webhookOptions represents commandline options for scaffolding a webhook.
type webhookV2Options struct {
res *resource.Resource
resOpts *resource.Options
defaulting bool
validation bool
conversion bool
Expand Down
17 changes: 14 additions & 3 deletions pkg/model/types.go
Expand Up @@ -18,6 +18,7 @@ package model

import (
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

// Universe describes the entire state of file generation
Expand Down Expand Up @@ -50,16 +51,26 @@ type Resource struct {
// Plural is the plural lowercase of Kind.
Plural string `json:"plural,omitempty"`

// Resource is the API Resource.
Resource string `json:"resource,omitempty"`

// ResourcePackage is the go package of the Resource
GoPackage string `json:"goPackage,omitempty"`

// GroupDomain is the Group + "." + Domain for the Resource
GroupDomain string `json:"groupDomain,omitempty"`
}

// NewResource transforms a resource.Resource to a model.Resource
func NewResource(r *resource.Resource) *Resource {
return &Resource{
Namespaced: r.Namespaced,
Group: r.Group,
Version: r.Version,
Kind: r.Kind,
Plural: r.Plural,
GoPackage: r.Package,
GroupDomain: r.Domain,
}
}

// File describes a file that will be written
type File struct {
// Path is the file to write
Expand Down

0 comments on commit fc5224c

Please sign in to comment.