Fix informer-gen wrt gengo/v2

This commit is contained in:
Tim Hockin 2023-12-20 16:10:08 -08:00
parent 46665fa76a
commit 0aa98ee8e3
No known key found for this signature in database
3 changed files with 91 additions and 64 deletions

View File

@ -18,18 +18,17 @@ package args
import ( import (
"fmt" "fmt"
"path"
"github.com/spf13/pflag" "github.com/spf13/pflag"
codegenutil "k8s.io/code-generator/pkg/util"
"k8s.io/gengo/v2/args" "k8s.io/gengo/v2/args"
) )
// CustomArgs is used by the gengo framework to pass args specific to this generator. // CustomArgs is used by the gengo framework to pass args specific to this generator.
type CustomArgs struct { type CustomArgs struct {
VersionedClientSetPackage string OutputPackage string // must be a Go import-path
InternalClientSetPackage string VersionedClientSetPackage string // must be a Go import-path
ListersPackage string InternalClientSetPackage string // must be a Go import-path
ListersPackage string // must be a Go import-path
SingleDirectory bool SingleDirectory bool
// PluralExceptions define a list of pluralizer exceptions in Type:PluralType format. // PluralExceptions define a list of pluralizer exceptions in Type:PluralType format.
@ -46,37 +45,35 @@ func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
} }
genericArgs.CustomArgs = customArgs genericArgs.CustomArgs = customArgs
if pkg := codegenutil.CurrentPackage(); len(pkg) != 0 {
genericArgs.OutputPackagePath = path.Join(pkg, "pkg/client/informers")
customArgs.VersionedClientSetPackage = path.Join(pkg, "pkg/client/clientset/versioned")
customArgs.InternalClientSetPackage = path.Join(pkg, "pkg/client/clientset/internalversion")
customArgs.ListersPackage = path.Join(pkg, "pkg/client/listers")
}
return genericArgs, customArgs return genericArgs, customArgs
} }
// AddFlags add the generator flags to the flag set. // AddFlags add the generator flags to the flag set.
func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) { func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&ca.InternalClientSetPackage, "internal-clientset-package", ca.InternalClientSetPackage, "the full package name for the internal clientset to use") fs.StringVar(&ca.OutputPackage, "output-package", ca.OutputPackage, "the Go import-path of the generated results")
fs.StringVar(&ca.VersionedClientSetPackage, "versioned-clientset-package", ca.VersionedClientSetPackage, "the full package name for the versioned clientset to use") fs.StringVar(&ca.InternalClientSetPackage, "internal-clientset-package", ca.InternalClientSetPackage, "the Go import-path of the internal clientset to use")
fs.StringVar(&ca.ListersPackage, "listers-package", ca.ListersPackage, "the full package name for the listers to use") fs.StringVar(&ca.VersionedClientSetPackage, "versioned-clientset-package", ca.VersionedClientSetPackage, "the Go import-path of the versioned clientset to use")
fs.StringVar(&ca.ListersPackage, "listers-package", ca.ListersPackage, "the Go import-path of the listers to use")
fs.BoolVar(&ca.SingleDirectory, "single-directory", ca.SingleDirectory, "if true, omit the intermediate \"internalversion\" and \"externalversions\" subdirectories") fs.BoolVar(&ca.SingleDirectory, "single-directory", ca.SingleDirectory, "if true, omit the intermediate \"internalversion\" and \"externalversions\" subdirectories")
fs.StringSliceVar(&ca.PluralExceptions, "plural-exceptions", ca.PluralExceptions, "list of comma separated plural exception definitions in Type:PluralizedType format") fs.StringSliceVar(&ca.PluralExceptions, "plural-exceptions", ca.PluralExceptions, "list of comma separated plural exception definitions in Type:PluralizedType format")
} }
// Validate checks the given arguments. // Validate checks the given arguments.
func Validate(genericArgs *args.GeneratorArgs) error { func Validate(genericArgs *args.GeneratorArgs) error {
if len(genericArgs.OutputBase) == 0 {
return fmt.Errorf("--output-base must be specified")
}
customArgs := genericArgs.CustomArgs.(*CustomArgs) customArgs := genericArgs.CustomArgs.(*CustomArgs)
if len(genericArgs.OutputPackagePath) == 0 { if len(customArgs.OutputPackage) == 0 {
return fmt.Errorf("output package cannot be empty") return fmt.Errorf("--output-package must be specified")
} }
if len(customArgs.VersionedClientSetPackage) == 0 { if len(customArgs.VersionedClientSetPackage) == 0 {
return fmt.Errorf("versioned clientset package cannot be empty") return fmt.Errorf("--versioned-clientset-package must be specified")
} }
if len(customArgs.ListersPackage) == 0 { if len(customArgs.ListersPackage) == 0 {
return fmt.Errorf("listers package cannot be empty") return fmt.Errorf("--listers-package must be specified")
} }
return nil return nil

View File

@ -85,7 +85,10 @@ func isInternal(m types.Member) bool {
return !strings.Contains(m.Tags, "json") return !strings.Contains(m.Tags, "json")
} }
func packageForInternalInterfaces(base string) string { // subdirForInternalInterfaces returns a string which is a subdir of the
// provided base, which can be a Go import-path or a directory path, depending
// on what the caller needs.
func subdirForInternalInterfaces(base string) string {
return filepath.Join(base, "internalinterfaces") return filepath.Join(base, "internalinterfaces")
} }
@ -101,11 +104,15 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
klog.Fatalf("Wrong CustomArgs type: %T", arguments.CustomArgs) klog.Fatalf("Wrong CustomArgs type: %T", arguments.CustomArgs)
} }
internalVersionPackagePath := filepath.Join(arguments.OutputPackagePath) internalVersionOutputDir := arguments.OutputBase
externalVersionPackagePath := filepath.Join(arguments.OutputPackagePath) internalVersionOutputPkg := customArgs.OutputPackage
externalVersionOutputDir := arguments.OutputBase
externalVersionOutputPkg := customArgs.OutputPackage
if !customArgs.SingleDirectory { if !customArgs.SingleDirectory {
internalVersionPackagePath = filepath.Join(arguments.OutputPackagePath, "internalversion") internalVersionOutputDir = filepath.Join(internalVersionOutputDir, "internalversion")
externalVersionPackagePath = filepath.Join(arguments.OutputPackagePath, "externalversions") internalVersionOutputPkg = filepath.Join(internalVersionOutputPkg, "internalversion")
externalVersionOutputDir = filepath.Join(externalVersionOutputDir, "externalversions")
externalVersionOutputPkg = filepath.Join(externalVersionOutputPkg, "externalversions")
} }
var packageList generator.Packages var packageList generator.Packages
@ -191,49 +198,72 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
typesToGenerate = orderer.OrderTypes(typesToGenerate) typesToGenerate = orderer.OrderTypes(typesToGenerate)
if internal { if internal {
packageList = append(packageList, versionPackage(internalVersionPackagePath, groupPackageName, gv, groupGoNames[groupPackageName], boilerplate, typesToGenerate, customArgs.InternalClientSetPackage, customArgs.ListersPackage)) packageList = append(packageList,
versionPackage(
internalVersionOutputDir, internalVersionOutputPkg,
groupPackageName, gv, groupGoNames[groupPackageName],
boilerplate, typesToGenerate,
customArgs.InternalClientSetPackage, customArgs.ListersPackage))
} else { } else {
packageList = append(packageList, versionPackage(externalVersionPackagePath, groupPackageName, gv, groupGoNames[groupPackageName], boilerplate, typesToGenerate, customArgs.VersionedClientSetPackage, customArgs.ListersPackage)) packageList = append(packageList,
versionPackage(
externalVersionOutputDir, externalVersionOutputPkg,
groupPackageName, gv, groupGoNames[groupPackageName],
boilerplate, typesToGenerate,
customArgs.VersionedClientSetPackage, customArgs.ListersPackage))
} }
} }
if len(externalGroupVersions) != 0 { if len(externalGroupVersions) != 0 {
packageList = append(packageList, factoryInterfacePackage(externalVersionPackagePath, boilerplate, customArgs.VersionedClientSetPackage)) packageList = append(packageList,
packageList = append(packageList, factoryPackage(externalVersionPackagePath, boilerplate, groupGoNames, genutil.PluralExceptionListToMapOrDie(customArgs.PluralExceptions), externalGroupVersions, factoryInterfacePackage(
customArgs.VersionedClientSetPackage, externalVersionOutputDir, externalVersionOutputPkg,
typesForGroupVersion)) boilerplate, customArgs.VersionedClientSetPackage))
packageList = append(packageList,
factoryPackage(
externalVersionOutputDir, externalVersionOutputPkg,
boilerplate, groupGoNames, genutil.PluralExceptionListToMapOrDie(customArgs.PluralExceptions),
externalGroupVersions, customArgs.VersionedClientSetPackage, typesForGroupVersion))
for _, gvs := range externalGroupVersions { for _, gvs := range externalGroupVersions {
packageList = append(packageList, groupPackage(externalVersionPackagePath, gvs, boilerplate)) packageList = append(packageList,
groupPackage(externalVersionOutputDir, externalVersionOutputPkg, gvs, boilerplate))
} }
} }
if len(internalGroupVersions) != 0 { if len(internalGroupVersions) != 0 {
packageList = append(packageList, factoryInterfacePackage(internalVersionPackagePath, boilerplate, customArgs.InternalClientSetPackage)) packageList = append(packageList,
packageList = append(packageList, factoryPackage(internalVersionPackagePath, boilerplate, groupGoNames, genutil.PluralExceptionListToMapOrDie(customArgs.PluralExceptions), internalGroupVersions, customArgs.InternalClientSetPackage, typesForGroupVersion)) factoryInterfacePackage(internalVersionOutputDir, internalVersionOutputPkg, boilerplate, customArgs.InternalClientSetPackage))
packageList = append(packageList,
factoryPackage(
internalVersionOutputDir, internalVersionOutputPkg,
boilerplate, groupGoNames, genutil.PluralExceptionListToMapOrDie(customArgs.PluralExceptions),
internalGroupVersions, customArgs.InternalClientSetPackage, typesForGroupVersion))
for _, gvs := range internalGroupVersions { for _, gvs := range internalGroupVersions {
packageList = append(packageList, groupPackage(internalVersionPackagePath, gvs, boilerplate)) packageList = append(packageList,
groupPackage(internalVersionOutputDir, internalVersionOutputPkg, gvs, boilerplate))
} }
} }
return packageList return packageList
} }
func factoryPackage(basePackage string, boilerplate []byte, groupGoNames, pluralExceptions map[string]string, groupVersions map[string]clientgentypes.GroupVersions, clientSetPackage string, func factoryPackage(outputDirBase, outputPkgBase string, boilerplate []byte, groupGoNames, pluralExceptions map[string]string, groupVersions map[string]clientgentypes.GroupVersions, clientSetPackage string,
typesForGroupVersion map[clientgentypes.GroupVersion][]*types.Type) generator.Package { typesForGroupVersion map[clientgentypes.GroupVersion][]*types.Type) generator.Package {
return &generator.DefaultPackage{ return &generator.DefaultPackage{
PackageName: filepath.Base(basePackage), PackageName: filepath.Base(outputDirBase),
PackagePath: basePackage, PackagePath: outputPkgBase,
Source: outputDirBase,
HeaderText: boilerplate, HeaderText: boilerplate,
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) { GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
generators = append(generators, &factoryGenerator{ generators = append(generators, &factoryGenerator{
DefaultGen: generator.DefaultGen{ DefaultGen: generator.DefaultGen{
OptionalName: "factory", OptionalName: "factory",
}, },
outputPackage: basePackage, outputPackage: outputPkgBase,
imports: generator.NewImportTracker(), imports: generator.NewImportTracker(),
groupVersions: groupVersions, groupVersions: groupVersions,
clientSetPackage: clientSetPackage, clientSetPackage: clientSetPackage,
internalInterfacesPackage: packageForInternalInterfaces(basePackage), internalInterfacesPackage: subdirForInternalInterfaces(outputPkgBase),
gvGoNames: groupGoNames, gvGoNames: groupGoNames,
}) })
@ -241,7 +271,7 @@ func factoryPackage(basePackage string, boilerplate []byte, groupGoNames, plural
DefaultGen: generator.DefaultGen{ DefaultGen: generator.DefaultGen{
OptionalName: "generic", OptionalName: "generic",
}, },
outputPackage: basePackage, outputPackage: outputPkgBase,
imports: generator.NewImportTracker(), imports: generator.NewImportTracker(),
groupVersions: groupVersions, groupVersions: groupVersions,
pluralExceptions: pluralExceptions, pluralExceptions: pluralExceptions,
@ -254,19 +284,21 @@ func factoryPackage(basePackage string, boilerplate []byte, groupGoNames, plural
} }
} }
func factoryInterfacePackage(basePackage string, boilerplate []byte, clientSetPackage string) generator.Package { func factoryInterfacePackage(outputDirBase, outputPkgBase string, boilerplate []byte, clientSetPackage string) generator.Package {
packagePath := packageForInternalInterfaces(basePackage) outputDir := subdirForInternalInterfaces(outputDirBase)
outputPkg := subdirForInternalInterfaces(outputPkgBase)
return &generator.DefaultPackage{ return &generator.DefaultPackage{
PackageName: filepath.Base(packagePath), PackageName: filepath.Base(outputDir),
PackagePath: packagePath, PackagePath: outputPkg,
Source: outputDir,
HeaderText: boilerplate, HeaderText: boilerplate,
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) { GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
generators = append(generators, &factoryInterfaceGenerator{ generators = append(generators, &factoryInterfaceGenerator{
DefaultGen: generator.DefaultGen{ DefaultGen: generator.DefaultGen{
OptionalName: "factory_interfaces", OptionalName: "factory_interfaces",
}, },
outputPackage: packagePath, outputPackage: outputPkg,
imports: generator.NewImportTracker(), imports: generator.NewImportTracker(),
clientSetPackage: clientSetPackage, clientSetPackage: clientSetPackage,
}) })
@ -276,23 +308,25 @@ func factoryInterfacePackage(basePackage string, boilerplate []byte, clientSetPa
} }
} }
func groupPackage(basePackage string, groupVersions clientgentypes.GroupVersions, boilerplate []byte) generator.Package { func groupPackage(outputDirBase, outputPackageBase string, groupVersions clientgentypes.GroupVersions, boilerplate []byte) generator.Package {
packagePath := filepath.Join(basePackage, groupVersions.PackageName) outputDir := filepath.Join(outputDirBase, groupVersions.PackageName)
outputPkg := filepath.Join(outputPackageBase, groupVersions.PackageName)
groupPkgName := strings.Split(string(groupVersions.PackageName), ".")[0] groupPkgName := strings.Split(string(groupVersions.PackageName), ".")[0]
return &generator.DefaultPackage{ return &generator.DefaultPackage{
PackageName: groupPkgName, PackageName: groupPkgName,
PackagePath: packagePath, PackagePath: outputPkg,
Source: outputDir,
HeaderText: boilerplate, HeaderText: boilerplate,
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) { GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
generators = append(generators, &groupInterfaceGenerator{ generators = append(generators, &groupInterfaceGenerator{
DefaultGen: generator.DefaultGen{ DefaultGen: generator.DefaultGen{
OptionalName: "interface", OptionalName: "interface",
}, },
outputPackage: packagePath, outputPackage: outputPkg,
groupVersions: groupVersions, groupVersions: groupVersions,
imports: generator.NewImportTracker(), imports: generator.NewImportTracker(),
internalInterfacesPackage: packageForInternalInterfaces(basePackage), internalInterfacesPackage: subdirForInternalInterfaces(outputPackageBase),
}) })
return generators return generators
}, },
@ -303,22 +337,25 @@ func groupPackage(basePackage string, groupVersions clientgentypes.GroupVersions
} }
} }
func versionPackage(basePackage string, groupPkgName string, gv clientgentypes.GroupVersion, groupGoName string, boilerplate []byte, typesToGenerate []*types.Type, clientSetPackage, listersPackage string) generator.Package { func versionPackage(outputDirBase, outputPkgBase string, groupPkgName string, gv clientgentypes.GroupVersion, groupGoName string, boilerplate []byte, typesToGenerate []*types.Type, clientSetPackage, listersPackage string) generator.Package {
packagePath := filepath.Join(basePackage, groupPkgName, strings.ToLower(gv.Version.NonEmpty())) subdir := filepath.Join(groupPkgName, strings.ToLower(gv.Version.NonEmpty()))
outputDir := filepath.Join(outputDirBase, subdir)
outputPkg := filepath.Join(outputPkgBase, subdir)
return &generator.DefaultPackage{ return &generator.DefaultPackage{
PackageName: strings.ToLower(gv.Version.NonEmpty()), PackageName: strings.ToLower(gv.Version.NonEmpty()),
PackagePath: packagePath, PackagePath: outputPkg,
Source: outputDir,
HeaderText: boilerplate, HeaderText: boilerplate,
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) { GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
generators = append(generators, &versionInterfaceGenerator{ generators = append(generators, &versionInterfaceGenerator{
DefaultGen: generator.DefaultGen{ DefaultGen: generator.DefaultGen{
OptionalName: "interface", OptionalName: "interface",
}, },
outputPackage: packagePath, outputPackage: outputPkg,
imports: generator.NewImportTracker(), imports: generator.NewImportTracker(),
types: typesToGenerate, types: typesToGenerate,
internalInterfacesPackage: packageForInternalInterfaces(basePackage), internalInterfacesPackage: subdirForInternalInterfaces(outputPkgBase),
}) })
for _, t := range typesToGenerate { for _, t := range typesToGenerate {
@ -326,7 +363,7 @@ func versionPackage(basePackage string, groupPkgName string, gv clientgentypes.G
DefaultGen: generator.DefaultGen{ DefaultGen: generator.DefaultGen{
OptionalName: strings.ToLower(t.Name.Name), OptionalName: strings.ToLower(t.Name.Name),
}, },
outputPackage: packagePath, outputPackage: outputPkg,
groupPkgName: groupPkgName, groupPkgName: groupPkgName,
groupVersion: gv, groupVersion: gv,
groupGoName: groupGoName, groupGoName: groupGoName,
@ -334,7 +371,7 @@ func versionPackage(basePackage string, groupPkgName string, gv clientgentypes.G
imports: generator.NewImportTracker(), imports: generator.NewImportTracker(),
clientSetPackage: clientSetPackage, clientSetPackage: clientSetPackage,
listersPackage: listersPackage, listersPackage: listersPackage,
internalInterfacesPackage: packageForInternalInterfaces(basePackage), internalInterfacesPackage: subdirForInternalInterfaces(outputPkgBase),
}) })
} }
return generators return generators

View File

@ -31,13 +31,6 @@ func main() {
klog.InitFlags(nil) klog.InitFlags(nil)
genericArgs, customArgs := generatorargs.NewDefaults() genericArgs, customArgs := generatorargs.NewDefaults()
// Override defaults.
// TODO: move out of informer-gen
genericArgs.OutputPackagePath = "k8s.io/kubernetes/pkg/client/informers/informers_generated"
customArgs.VersionedClientSetPackage = "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
customArgs.InternalClientSetPackage = "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
customArgs.ListersPackage = "k8s.io/kubernetes/pkg/client/listers"
genericArgs.AddFlags(pflag.CommandLine) genericArgs.AddFlags(pflag.CommandLine)
customArgs.AddFlags(pflag.CommandLine) customArgs.AddFlags(pflag.CommandLine)
flag.Set("logtostderr", "true") flag.Set("logtostderr", "true")