Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clientgen: allow to pass custom apiPath when generating client sets #32769

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/libs/go2idl/client-gen/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Args struct {
// ClientsetOutputPath is the path the clientset will be generated at. It's
// populated from command-line arguments.
ClientsetOutputPath string
// ClientsetAPIPath is the default API path for generated clients.
ClientsetAPIPath string
// ClientsetOnly determines if we should generate the clients for groups and
// types along with the clientset. It's populated from command-line
// arguments.
Expand Down
5 changes: 3 additions & 2 deletions cmd/libs/go2idl/client-gen/generators/client_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func generatedBy(customArgs clientgenargs.Args) string {
return fmt.Sprintf("\n// This package is generated by client-gen with the default arguments.\n\n")
}

func packageForGroup(gv unversioned.GroupVersion, typeList []*types.Type, packageBasePath string, srcTreePath string, inputPath string, boilerplate []byte, generatedBy string) generator.Package {
func packageForGroup(gv unversioned.GroupVersion, typeList []*types.Type, packageBasePath string, apiPath string, srcTreePath string, inputPath string, boilerplate []byte, generatedBy string) generator.Package {
outputPackagePath := filepath.Join(packageBasePath, gv.Group, gv.Version)
return &generator.DefaultPackage{
PackageName: gv.Version,
Expand Down Expand Up @@ -102,6 +102,7 @@ func packageForGroup(gv unversioned.GroupVersion, typeList []*types.Type, packag
inputPacakge: inputPath,
group: gv.Group,
version: gv.Version,
apiPath: apiPath,
types: typeList,
imports: generator.NewImportTracker(),
})
Expand Down Expand Up @@ -217,7 +218,7 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
for _, gv := range customArgs.GroupVersions {
types := gvToTypes[gv]
inputPath := customArgs.GroupVersionToInputPath[gv]
packageList = append(packageList, packageForGroup(normalization.GroupVersion(gv), orderer.OrderTypes(types), typedClientBasePath, arguments.OutputBase, inputPath, boilerplate, generatedBy))
packageList = append(packageList, packageForGroup(normalization.GroupVersion(gv), orderer.OrderTypes(types), typedClientBasePath, customArgs.ClientsetAPIPath, arguments.OutputBase, inputPath, boilerplate, generatedBy))
if customArgs.FakeClient {
packageList = append(packageList, fake.PackageForGroup(normalization.GroupVersion(gv), orderer.OrderTypes(types), typedClientBasePath, arguments.OutputBase, inputPath, boilerplate, generatedBy))
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/libs/go2idl/client-gen/generators/generator_for_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type genGroup struct {
outputPackage string
group string
version string
apiPath string
// types in this group
types []*types.Type
imports namer.ImportTracker
Expand Down Expand Up @@ -62,6 +63,9 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer
const pkgAPI = "k8s.io/kubernetes/pkg/api"
const pkgSerializer = "k8s.io/kubernetes/pkg/runtime/serializer"
apiPath := func(group string) string {
if len(g.apiPath) > 0 {
return `"` + g.apiPath + `"`
}
if group == "core" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any need to make that parametrized as well?

return `"/api"`
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/libs/go2idl/client-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var (
includedTypesOverrides = flag.StringSlice("included-types-overrides", []string{}, "list of group/version/type for which client should be generated. By default, client is generated for all types which have genclient=true 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=true will be used for other group versions.")
basePath = flag.String("input-base", "k8s.io/kubernetes/pkg/apis", "base path to look for the api group. Default to \"k8s.io/kubernetes/pkg/apis\"")
clientsetName = flag.StringP("clientset-name", "n", "internalclientset", "the name of the generated clientset package.")
clientsetAPIPath = flag.StringP("clientset-api-path", "", "", "the value of default API path.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having /apis as default would be cleaner I assume.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think defaulting to "" is better. We need to distinguish if "/apis" is user specified or if it's the default. It matters because we need to treat the "core" group specially in the latter case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, I guess with the ugly "core" special case we have to support there is no clean solution. I am fine with keeping "" as default.

clientsetPath = flag.String("clientset-path", "k8s.io/kubernetes/pkg/client/clientset_generated/", "the generated clientset will be output to <clientset-path>/<clientset-name>. Default to \"k8s.io/kubernetes/pkg/client/clientset_generated/\"")
clientsetOnly = flag.Bool("clientset-only", false, "when set, client-gen only generates the clientset shell, without generating the individual typed clients")
fakeClient = flag.Bool("fake-clientset", true, "when set, client-gen will generate the fake clientset that can be used in tests")
Expand Down Expand Up @@ -177,21 +178,22 @@ func main() {
if err != nil {
glog.Fatalf("Unexpected error: %v", err)
}
glog.Infof("going to generate clientset from these input paths: %v", inputPath)
glog.V(3).Infof("going to generate clientset from these input paths: %v", inputPath)
arguments.InputDirs = append(inputPath, dependencies...)

arguments.CustomArgs = clientgenargs.Args{
GroupVersions: groupVersions,
GroupVersionToInputPath: gvToPath,
ClientsetName: *clientsetName,
ClientsetAPIPath: *clientsetAPIPath,
ClientsetOutputPath: *clientsetPath,
ClientsetOnly: *clientsetOnly,
FakeClient: *fakeClient,
CmdArgs: cmdArgs,
IncludedTypesOverrides: includedTypesOverrides,
}

glog.Infof("==arguments: %v\n", arguments)
glog.V(3).Infof("==arguments: %v\n", arguments)
}

if err := arguments.Execute(
Expand Down
1 change: 1 addition & 0 deletions hack/verify-flags/known-flags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ cleanup-iptables
client-ca-file
client-certificate
client-key
clientset-api-path
clientset-name
clientset-only
clientset-path
Expand Down