From 60db31af6ff3f10176ce32a9f3926d639cfdad76 Mon Sep 17 00:00:00 2001 From: Adrian Orive Date: Mon, 9 Nov 2020 12:48:16 +0100 Subject: [PATCH] Add --plural flag to kubebuilder create api and update the v3 flag for kubebuilder create webhook from --resource to --plural Signed-off-by: Adrian Orive --- pkg/model/config/config.go | 4 +++- pkg/model/resource/resource.go | 11 ++++++++++- pkg/plugin/v2/webhook.go | 2 +- pkg/plugin/v3/api.go | 1 + pkg/plugin/v3/webhook.go | 4 ++-- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/pkg/model/config/config.go b/pkg/model/config/config.go index 1a3ec594866..41ea7e0826e 100644 --- a/pkg/model/config/config.go +++ b/pkg/model/config/config.go @@ -148,6 +148,7 @@ type GVK struct { Group string `json:"group,omitempty"` Version string `json:"version,omitempty"` Kind string `json:"kind,omitempty"` + Plural string `json:"plural,omitempty"` // CRDVersion holds the CustomResourceDefinition API version used for the GVK. CRDVersion string `json:"crdVersion,omitempty"` @@ -159,7 +160,8 @@ type GVK struct { func (r GVK) isEqualTo(other GVK) bool { return r.Group == other.Group && r.Version == other.Version && - r.Kind == other.Kind + r.Kind == other.Kind && + r.Plural == other.Plural } // merge combines fields of two GVKs that have matching group, version, and kind, diff --git a/pkg/model/resource/resource.go b/pkg/model/resource/resource.go index 35a104757ee..93d6262908b 100644 --- a/pkg/model/resource/resource.go +++ b/pkg/model/resource/resource.go @@ -20,6 +20,8 @@ import ( "fmt" "strings" + "github.com/gobuffalo/flect" + "sigs.k8s.io/kubebuilder/v2/pkg/model/config" ) @@ -60,13 +62,20 @@ type Resource struct { // GVK returns the group-version-kind information to check against tracked resources in the configuration file func (r *Resource) GVK() config.GVK { - return config.GVK{ + gvk := config.GVK{ Group: r.Group, Version: r.Version, Kind: r.Kind, CRDVersion: r.CRDVersion, WebhookVersion: r.WebhookVersion, } + + // Only store plural if it is irregular + if r.Plural != flect.Pluralize(strings.ToLower(r.Kind)) { + gvk.Plural = r.Plural + } + + return gvk } func wrapKey(key string) string { diff --git a/pkg/plugin/v2/webhook.go b/pkg/plugin/v2/webhook.go index d688cb4b7ab..de34a90096b 100644 --- a/pkg/plugin/v2/webhook.go +++ b/pkg/plugin/v2/webhook.go @@ -67,7 +67,7 @@ func (p *createWebhookSubcommand) BindFlags(fs *pflag.FlagSet) { fs.StringVar(&p.resource.Group, "group", "", "resource Group") fs.StringVar(&p.resource.Version, "version", "", "resource Version") fs.StringVar(&p.resource.Kind, "kind", "", "resource Kind") - fs.StringVar(&p.resource.Plural, "resource", "", "resource Resource") + fs.StringVar(&p.resource.Plural, "resource", "", "resource kind irregular plural form") fs.BoolVar(&p.defaulting, "defaulting", false, "if set, scaffold the defaulting webhook") diff --git a/pkg/plugin/v3/api.go b/pkg/plugin/v3/api.go index da2c69180ce..a7c0befcf44 100644 --- a/pkg/plugin/v3/api.go +++ b/pkg/plugin/v3/api.go @@ -128,6 +128,7 @@ func (p *createAPISubcommand) BindFlags(fs *pflag.FlagSet) { fs.StringVar(&p.resource.Kind, "kind", "", "resource Kind") fs.StringVar(&p.resource.Group, "group", "", "resource Group") fs.StringVar(&p.resource.Version, "version", "", "resource Version") + fs.StringVar(&p.resource.Plural, "plural", "", "resource kind irregular plural form") fs.BoolVar(&p.resource.Namespaced, "namespaced", true, "resource is namespaced") fs.StringVar(&p.resource.CRDVersion, "crd-version", defaultCRDVersion, "version of CustomResourceDefinition to scaffold. Options: [v1, v1beta1]") diff --git a/pkg/plugin/v3/webhook.go b/pkg/plugin/v3/webhook.go index 4dc7717d6ef..44517f3c95c 100644 --- a/pkg/plugin/v3/webhook.go +++ b/pkg/plugin/v3/webhook.go @@ -67,10 +67,10 @@ validating and (or) conversion webhooks. func (p *createWebhookSubcommand) BindFlags(fs *pflag.FlagSet) { p.resource = &resource.Options{} + fs.StringVar(&p.resource.Kind, "kind", "", "resource Kind") fs.StringVar(&p.resource.Group, "group", "", "resource Group") fs.StringVar(&p.resource.Version, "version", "", "resource Version") - fs.StringVar(&p.resource.Kind, "kind", "", "resource Kind") - fs.StringVar(&p.resource.Plural, "resource", "", "resource Resource") + fs.StringVar(&p.resource.Plural, "plural", "", "resource kind irregular plural form") fs.StringVar(&p.resource.WebhookVersion, "webhook-version", defaultWebhookVersion, "version of {Mutating,Validating}WebhookConfigurations to scaffold. Options: [v1, v1beta1]")