args.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package args
  14. import (
  15. "fmt"
  16. "path"
  17. "github.com/spf13/pflag"
  18. "k8s.io/gengo/args"
  19. "k8s.io/code-generator/cmd/client-gen/types"
  20. codegenutil "k8s.io/code-generator/pkg/util"
  21. )
  22. var DefaultInputDirs = []string{}
  23. // ClientGenArgs is a wrapper for arguments to client-gen.
  24. type CustomArgs struct {
  25. // A sorted list of group versions to generate. For each of them the package path is found
  26. // in GroupVersionToInputPath.
  27. Groups []types.GroupVersions
  28. // Overrides for which types should be included in the client.
  29. IncludedTypesOverrides map[types.GroupVersion][]string
  30. // ClientsetName is the name of the clientset to be generated. It's
  31. // populated from command-line arguments.
  32. ClientsetName string
  33. // ClientsetAPIPath is the default API HTTP path for generated clients.
  34. ClientsetAPIPath string
  35. // ClientsetOnly determines if we should generate the clients for groups and
  36. // types along with the clientset. It's populated from command-line
  37. // arguments.
  38. ClientsetOnly bool
  39. // FakeClient determines if client-gen generates the fake clients.
  40. FakeClient bool
  41. // PluralExceptions specify list of exceptions used when pluralizing certain types.
  42. // For example 'Endpoints:Endpoints', otherwise the pluralizer will generate 'Endpointes'.
  43. PluralExceptions []string
  44. // ApplyConfigurationPackage is the package of apply builders generated by typebuilder-gen.
  45. // If non-empty, Apply functions are generated for each type and reference the apply builders.
  46. // If empty (""), Apply functions are not generated.
  47. ApplyConfigurationPackage string
  48. }
  49. func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
  50. genericArgs := args.Default().WithoutDefaultFlagParsing()
  51. customArgs := &CustomArgs{
  52. ClientsetName: "internalclientset",
  53. ClientsetAPIPath: "/apis",
  54. ClientsetOnly: false,
  55. FakeClient: true,
  56. PluralExceptions: []string{"Endpoints:Endpoints"},
  57. ApplyConfigurationPackage: "",
  58. }
  59. genericArgs.CustomArgs = customArgs
  60. genericArgs.InputDirs = DefaultInputDirs
  61. if pkg := codegenutil.CurrentPackage(); len(pkg) != 0 {
  62. genericArgs.OutputPackagePath = path.Join(pkg, "pkg/client/clientset")
  63. }
  64. return genericArgs, customArgs
  65. }
  66. func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet, inputBase string) {
  67. gvsBuilder := NewGroupVersionsBuilder(&ca.Groups)
  68. pflag.Var(NewGVPackagesValue(gvsBuilder, nil), "input", "group/versions that client-gen will generate clients for. At most one version per group is allowed. Specified in the format \"group1/version1,group2/version2...\".")
  69. pflag.Var(NewGVTypesValue(&ca.IncludedTypesOverrides, []string{}), "included-types-overrides", "list of group/version/type for which client should be generated. By default, client is generated for all types which have genclient in types.go. This overrides that. For each groupVersion in this list, only the types mentioned here will be included. The default check of genclient will be used for other group versions.")
  70. pflag.Var(NewInputBasePathValue(gvsBuilder, inputBase), "input-base", "base path to look for the api group.")
  71. pflag.StringVarP(&ca.ClientsetName, "clientset-name", "n", ca.ClientsetName, "the name of the generated clientset package.")
  72. pflag.StringVarP(&ca.ClientsetAPIPath, "clientset-api-path", "", ca.ClientsetAPIPath, "the value of default API HTTP path, starting with / and without trailing /.")
  73. pflag.BoolVar(&ca.ClientsetOnly, "clientset-only", ca.ClientsetOnly, "when set, client-gen only generates the clientset shell, without generating the individual typed clients")
  74. pflag.BoolVar(&ca.FakeClient, "fake-clientset", ca.FakeClient, "when set, client-gen will generate the fake clientset that can be used in tests")
  75. fs.StringSliceVar(&ca.PluralExceptions, "plural-exceptions", ca.PluralExceptions, "list of comma separated plural exception definitions in Type:PluralizedType form")
  76. fs.StringVar(&ca.ApplyConfigurationPackage, "apply-configuration-package", ca.ApplyConfigurationPackage, "optional package of apply configurations, generated by applyconfiguration-gen, that are required to generate Apply functions for each type in the clientset. By default Apply functions are not generated.")
  77. // support old flags
  78. fs.SetNormalizeFunc(mapFlagName("clientset-path", "output-package", fs.GetNormalizeFunc()))
  79. }
  80. func Validate(genericArgs *args.GeneratorArgs) error {
  81. customArgs := genericArgs.CustomArgs.(*CustomArgs)
  82. if len(genericArgs.OutputPackagePath) == 0 {
  83. return fmt.Errorf("output package cannot be empty")
  84. }
  85. if len(customArgs.ClientsetName) == 0 {
  86. return fmt.Errorf("clientset name cannot be empty")
  87. }
  88. if len(customArgs.ClientsetAPIPath) == 0 {
  89. return fmt.Errorf("clientset API path cannot be empty")
  90. }
  91. return nil
  92. }
  93. // GroupVersionPackages returns a map from GroupVersion to the package with the types.go.
  94. func (ca *CustomArgs) GroupVersionPackages() map[types.GroupVersion]string {
  95. res := map[types.GroupVersion]string{}
  96. for _, pkg := range ca.Groups {
  97. for _, v := range pkg.Versions {
  98. res[types.GroupVersion{Group: pkg.Group, Version: v.Version}] = v.Package
  99. }
  100. }
  101. return res
  102. }
  103. func mapFlagName(from, to string, old func(fs *pflag.FlagSet, name string) pflag.NormalizedName) func(fs *pflag.FlagSet, name string) pflag.NormalizedName {
  104. return func(fs *pflag.FlagSet, name string) pflag.NormalizedName {
  105. if name == from {
  106. name = to
  107. }
  108. return old(fs, name)
  109. }
  110. }