Skip to content

Commit

Permalink
Merge pull request #157 from pwittrock/pural
Browse files Browse the repository at this point in the history
Allow plural resource names
  • Loading branch information
Phillip Wittrock committed May 14, 2018
2 parents 391d011 + 1909c53 commit 019fa3c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/kubebuilder/create/resource/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func AddCreateResource(cmd *cobra.Command) {
createResourceCmd.Flags().BoolVar(&nonNamespacedKind, "non-namespaced", false, "if set, the API kind will be non namespaced")
createResourceCmd.Flags().BoolVar(&controller, "controller", true, "if true, generate the controller code for the resource")
createResourceCmd.Flags().BoolVar(&generate, "generate", true, "generate source code")
createResourceCmd.Flags().BoolVar(&createutil.AllowPluralKind, "plural-kind", false, "allow the kind to be plural")
cmd.AddCommand(createResourceCmd)
}

Expand Down
10 changes: 7 additions & 3 deletions cmd/kubebuilder/create/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

var (
GroupName, KindName, VersionName, ResourceName, Copyright string
AllowPluralKind bool
)

func ValidateResourceFlags() {
Expand All @@ -43,11 +44,14 @@ func ValidateResourceFlags() {
if len(KindName) == 0 {
log.Fatal("Must specify --kind")
}

rs := inflect.NewDefaultRuleset()
if len(ResourceName) == 0 {
if inflect.NewDefaultRuleset().Pluralize(KindName) == KindName {
log.Fatal("Client code generation expects singular --kind.")
if !AllowPluralKind && rs.Pluralize(KindName) == KindName && rs.Singularize(KindName) != KindName {
log.Fatalf("Client code generation expects singular --kind (e.g. %s)."+
"Or to be run with --pural-kind=true.", rs.Singularize(KindName))
}
ResourceName = inflect.NewDefaultRuleset().Pluralize(strings.ToLower(KindName))
ResourceName = rs.Pluralize(strings.ToLower(KindName))
}

groupMatch := regexp.MustCompile("^[a-z]+$")
Expand Down
9 changes: 9 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,13 @@ function generate_resource_with_coretype_controller {
update_controller_test
}

function test_plural_resource {
header_text "generating CRD for plural resource"

kubebuilder create resource --plural-kind=true --group testing --version v1beta1 --kind Metadata
kubebuilder create resource --group testing --version v1beta1 --kind Postgress
}

prepare_staging_dir
fetch_tools
build_kb
Expand All @@ -559,4 +566,6 @@ prepare_testdir_under_gopath
generate_coretype_controller
test_generated_controller

test_plural_resource

exit $rc

0 comments on commit 019fa3c

Please sign in to comment.