Skip to content

Commit ea7327a

Browse files
authored
Merge pull request kubernetes#86212 from mfojtik/plural-exceptions
code-generator: expose pluralExceptions via flag
2 parents 7b792c3 + 15e9836 commit ea7327a

File tree

13 files changed

+84
-29
lines changed

13 files changed

+84
-29
lines changed

staging/src/k8s.io/code-generator/cmd/client-gen/args/args.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ type CustomArgs struct {
4949
ClientsetOnly bool
5050
// FakeClient determines if client-gen generates the fake clients.
5151
FakeClient bool
52+
// PluralExceptions specify list of exceptions used when pluralizing certain types.
53+
// For example 'Endpoints:Endpoints', otherwise the pluralizer will generate 'Endpointes'.
54+
PluralExceptions []string
5255
}
5356

5457
func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
@@ -58,6 +61,7 @@ func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
5861
ClientsetAPIPath: "/apis",
5962
ClientsetOnly: false,
6063
FakeClient: true,
64+
PluralExceptions: []string{"Endpoints:Endpoints"},
6165
}
6266
genericArgs.CustomArgs = customArgs
6367
genericArgs.InputDirs = DefaultInputDirs
@@ -79,6 +83,8 @@ func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet, inputBase string) {
7983
pflag.BoolVar(&ca.ClientsetOnly, "clientset-only", ca.ClientsetOnly, "when set, client-gen only generates the clientset shell, without generating the individual typed clients")
8084
pflag.BoolVar(&ca.FakeClient, "fake-clientset", ca.FakeClient, "when set, client-gen will generate the fake clientset that can be used in tests")
8185

86+
fs.StringArrayVar(&ca.PluralExceptions, "plural-exceptions", ca.PluralExceptions, "list of comma separated plural exception definitions in Type:PluralizedType form")
87+
8288
// support old flags
8389
fs.SetNormalizeFunc(mapFlagName("clientset-path", "output-package", fs.GetNormalizeFunc()))
8490
}

staging/src/k8s.io/code-generator/cmd/client-gen/generators/client_generator.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ import (
3737
)
3838

3939
// NameSystems returns the name system used by the generators in this package.
40-
func NameSystems() namer.NameSystems {
41-
pluralExceptions := map[string]string{
42-
"Endpoints": "Endpoints",
43-
}
40+
func NameSystems(pluralExceptions map[string]string) namer.NameSystems {
4441
lowercaseNamer := namer.NewAllLowercasePluralNamer(pluralExceptions)
4542

4643
publicNamer := &ExceptionNamer{

staging/src/k8s.io/code-generator/cmd/client-gen/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func main() {
5757
}
5858

5959
if err := genericArgs.Execute(
60-
generators.NameSystems(),
60+
generators.NameSystems(util.PluralExceptionListToMapOrDie(customArgs.PluralExceptions)),
6161
generators.DefaultNameSystem(),
6262
generators.Packages,
6363
); err != nil {

staging/src/k8s.io/code-generator/cmd/informer-gen/args/args.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ type CustomArgs struct {
3131
InternalClientSetPackage string
3232
ListersPackage string
3333
SingleDirectory bool
34+
35+
// PluralExceptions define a list of pluralizer exceptions in Type:PluralType format.
36+
// The default list is "Endpoints:Endpoints"
37+
PluralExceptions []string
3438
}
3539

3640
// NewDefaults returns default arguments for the generator.
3741
func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
3842
genericArgs := args.Default().WithoutDefaultFlagParsing()
3943
customArgs := &CustomArgs{
40-
SingleDirectory: false,
44+
SingleDirectory: false,
45+
PluralExceptions: []string{"Endpoints:Endpoints"},
4146
}
4247
genericArgs.CustomArgs = customArgs
4348

@@ -57,6 +62,7 @@ func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {
5762
fs.StringVar(&ca.VersionedClientSetPackage, "versioned-clientset-package", ca.VersionedClientSetPackage, "the full package name for the versioned clientset to use")
5863
fs.StringVar(&ca.ListersPackage, "listers-package", ca.ListersPackage, "the full package name for the listers to use")
5964
fs.BoolVar(&ca.SingleDirectory, "single-directory", ca.SingleDirectory, "if true, omit the intermediate \"internalversion\" and \"externalversions\" subdirectories")
65+
fs.StringArrayVar(&ca.PluralExceptions, "plural-exceptions", ca.PluralExceptions, "list of comma separated plural exception definitions in Type:PluralizedType format")
6066
}
6167

6268
// Validate checks the given arguments.

staging/src/k8s.io/code-generator/cmd/informer-gen/generators/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ go_library(
2424
"//staging/src/k8s.io/code-generator/cmd/client-gen/types:go_default_library",
2525
"//staging/src/k8s.io/code-generator/cmd/informer-gen/args:go_default_library",
2626
"//staging/src/k8s.io/code-generator/pkg/namer:go_default_library",
27+
"//staging/src/k8s.io/code-generator/pkg/util:go_default_library",
2728
"//vendor/k8s.io/gengo/args:go_default_library",
2829
"//vendor/k8s.io/gengo/generator:go_default_library",
2930
"//vendor/k8s.io/gengo/namer:go_default_library",

staging/src/k8s.io/code-generator/cmd/informer-gen/generators/generic.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type genericGenerator struct {
3535
imports namer.ImportTracker
3636
groupVersions map[string]clientgentypes.GroupVersions
3737
groupGoNames map[string]string
38+
pluralExceptions map[string]string
3839
typesForGroupVersion map[clientgentypes.GroupVersion][]*types.Type
3940
filtered bool
4041
}
@@ -50,14 +51,11 @@ func (g *genericGenerator) Filter(c *generator.Context, t *types.Type) bool {
5051
}
5152

5253
func (g *genericGenerator) Namers(c *generator.Context) namer.NameSystems {
53-
pluralExceptions := map[string]string{
54-
"Endpoints": "Endpoints",
55-
}
5654
return namer.NameSystems{
5755
"raw": namer.NewRawNamer(g.outputPackage, g.imports),
58-
"allLowercasePlural": namer.NewAllLowercasePluralNamer(pluralExceptions),
59-
"publicPlural": namer.NewPublicPluralNamer(pluralExceptions),
60-
"resource": codegennamer.NewTagOverrideNamer("resourceName", namer.NewAllLowercasePluralNamer(pluralExceptions)),
56+
"allLowercasePlural": namer.NewAllLowercasePluralNamer(g.pluralExceptions),
57+
"publicPlural": namer.NewPublicPluralNamer(g.pluralExceptions),
58+
"resource": codegennamer.NewTagOverrideNamer("resourceName", namer.NewAllLowercasePluralNamer(g.pluralExceptions)),
6159
}
6260
}
6361

staging/src/k8s.io/code-generator/cmd/informer-gen/generators/packages.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ import (
3131
"k8s.io/code-generator/cmd/client-gen/generators/util"
3232
clientgentypes "k8s.io/code-generator/cmd/client-gen/types"
3333
informergenargs "k8s.io/code-generator/cmd/informer-gen/args"
34+
genutil "k8s.io/code-generator/pkg/util"
3435
)
3536

3637
// NameSystems returns the name system used by the generators in this package.
37-
func NameSystems() namer.NameSystems {
38-
pluralExceptions := map[string]string{
39-
"Endpoints": "Endpoints",
40-
}
38+
func NameSystems(pluralExceptions map[string]string) namer.NameSystems {
4139
return namer.NameSystems{
4240
"public": namer.NewPublicNamer(0),
4341
"private": namer.NewPrivateNamer(0),
@@ -208,15 +206,17 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
208206

209207
if len(externalGroupVersions) != 0 {
210208
packageList = append(packageList, factoryInterfacePackage(externalVersionPackagePath, boilerplate, customArgs.VersionedClientSetPackage))
211-
packageList = append(packageList, factoryPackage(externalVersionPackagePath, boilerplate, groupGoNames, externalGroupVersions, customArgs.VersionedClientSetPackage, typesForGroupVersion))
209+
packageList = append(packageList, factoryPackage(externalVersionPackagePath, boilerplate, groupGoNames, genutil.PluralExceptionListToMapOrDie(customArgs.PluralExceptions), externalGroupVersions,
210+
customArgs.VersionedClientSetPackage,
211+
typesForGroupVersion))
212212
for _, gvs := range externalGroupVersions {
213213
packageList = append(packageList, groupPackage(externalVersionPackagePath, gvs, boilerplate))
214214
}
215215
}
216216

217217
if len(internalGroupVersions) != 0 {
218218
packageList = append(packageList, factoryInterfacePackage(internalVersionPackagePath, boilerplate, customArgs.InternalClientSetPackage))
219-
packageList = append(packageList, factoryPackage(internalVersionPackagePath, boilerplate, groupGoNames, internalGroupVersions, customArgs.InternalClientSetPackage, typesForGroupVersion))
219+
packageList = append(packageList, factoryPackage(internalVersionPackagePath, boilerplate, groupGoNames, genutil.PluralExceptionListToMapOrDie(customArgs.PluralExceptions), internalGroupVersions, customArgs.InternalClientSetPackage, typesForGroupVersion))
220220
for _, gvs := range internalGroupVersions {
221221
packageList = append(packageList, groupPackage(internalVersionPackagePath, gvs, boilerplate))
222222
}
@@ -225,7 +225,8 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
225225
return packageList
226226
}
227227

228-
func factoryPackage(basePackage string, boilerplate []byte, groupGoNames map[string]string, groupVersions map[string]clientgentypes.GroupVersions, clientSetPackage string, typesForGroupVersion map[clientgentypes.GroupVersion][]*types.Type) generator.Package {
228+
func factoryPackage(basePackage string, boilerplate []byte, groupGoNames, pluralExceptions map[string]string, groupVersions map[string]clientgentypes.GroupVersions, clientSetPackage string,
229+
typesForGroupVersion map[clientgentypes.GroupVersion][]*types.Type) generator.Package {
229230
return &generator.DefaultPackage{
230231
PackageName: filepath.Base(basePackage),
231232
PackagePath: basePackage,
@@ -250,6 +251,7 @@ func factoryPackage(basePackage string, boilerplate []byte, groupGoNames map[str
250251
outputPackage: basePackage,
251252
imports: generator.NewImportTracker(),
252253
groupVersions: groupVersions,
254+
pluralExceptions: pluralExceptions,
253255
typesForGroupVersion: typesForGroupVersion,
254256
groupGoNames: groupGoNames,
255257
})

staging/src/k8s.io/code-generator/cmd/informer-gen/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353

5454
// Run it.
5555
if err := genericArgs.Execute(
56-
generators.NameSystems(),
56+
generators.NameSystems(util.PluralExceptionListToMapOrDie(customArgs.PluralExceptions)),
5757
generators.DefaultNameSystem(),
5858
generators.Packages,
5959
); err != nil {

staging/src/k8s.io/code-generator/cmd/lister-gen/args/args.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ import (
2626
)
2727

2828
// CustomArgs is used by the gengo framework to pass args specific to this generator.
29-
type CustomArgs struct{}
29+
type CustomArgs struct {
30+
// PluralExceptions specify list of exceptions used when pluralizing certain types.
31+
// For example 'Endpoints:Endpoints', otherwise the pluralizer will generate 'Endpointes'.
32+
PluralExceptions []string
33+
}
3034

3135
// NewDefaults returns default arguments for the generator.
3236
func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
3337
genericArgs := args.Default().WithoutDefaultFlagParsing()
34-
customArgs := &CustomArgs{}
38+
customArgs := &CustomArgs{
39+
PluralExceptions: []string{"Endpoints:Endpoints"},
40+
}
3541
genericArgs.CustomArgs = customArgs
3642

3743
if pkg := codegenutil.CurrentPackage(); len(pkg) != 0 {
@@ -42,7 +48,9 @@ func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
4248
}
4349

4450
// AddFlags add the generator flags to the flag set.
45-
func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {}
51+
func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {
52+
fs.StringArrayVar(&ca.PluralExceptions, "plural-exceptions", ca.PluralExceptions, "list of comma separated plural exception definitions in Type:PluralizedType format")
53+
}
4654

4755
// Validate checks the given arguments.
4856
func Validate(genericArgs *args.GeneratorArgs) error {

staging/src/k8s.io/code-generator/cmd/lister-gen/generators/lister.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ import (
3434
)
3535

3636
// NameSystems returns the name system used by the generators in this package.
37-
func NameSystems() namer.NameSystems {
38-
pluralExceptions := map[string]string{
39-
"Endpoints": "Endpoints",
40-
}
37+
func NameSystems(pluralExceptions map[string]string) namer.NameSystems {
4138
return namer.NameSystems{
4239
"public": namer.NewPublicNamer(0),
4340
"private": namer.NewPrivateNamer(0),

0 commit comments

Comments
 (0)