Fix lister-gen wrt gengo/v2

This commit is contained in:
Tim Hockin 2023-12-20 11:06:05 -08:00
parent 1965f56f59
commit b82527b6fb
No known key found for this signature in database
4 changed files with 35 additions and 26 deletions

View File

@ -18,15 +18,15 @@ package args
import (
"fmt"
"path"
"github.com/spf13/pflag"
codegenutil "k8s.io/code-generator/pkg/util"
"k8s.io/gengo/v2/args"
)
// CustomArgs is used by the gengo framework to pass args specific to this generator.
type CustomArgs struct {
OutputPackage string // must be a Go import-path
// PluralExceptions specify list of exceptions used when pluralizing certain types.
// For example 'Endpoints:Endpoints', otherwise the pluralizer will generate 'Endpointes'.
PluralExceptions []string
@ -40,24 +40,27 @@ func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
}
genericArgs.CustomArgs = customArgs
if pkg := codegenutil.CurrentPackage(); len(pkg) != 0 {
genericArgs.OutputPackagePath = path.Join(pkg, "pkg/client/listers")
}
return genericArgs, customArgs
}
// AddFlags add the generator flags to the flag set.
func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {
fs.StringSliceVar(&ca.PluralExceptions, "plural-exceptions", ca.PluralExceptions, "list of comma separated plural exception definitions in Type:PluralizedType format")
fs.StringVar(&ca.OutputPackage, "output-package", "",
"the base Go import-path under which to generate results")
fs.StringSliceVar(&ca.PluralExceptions, "plural-exceptions", ca.PluralExceptions,
"list of comma separated plural exception definitions in Type:PluralizedType format")
}
// Validate checks the given arguments.
func Validate(genericArgs *args.GeneratorArgs) error {
_ = genericArgs.CustomArgs.(*CustomArgs)
if len(genericArgs.OutputBase) == 0 {
return fmt.Errorf("--output-base must be specified")
}
if len(genericArgs.OutputPackagePath) == 0 {
return fmt.Errorf("output package cannot be empty")
custom := genericArgs.CustomArgs.(*CustomArgs)
if len(custom.OutputPackage) == 0 {
return fmt.Errorf("--output-package must be specified")
}
return nil

View File

@ -24,6 +24,7 @@ import (
"k8s.io/gengo/v2/generator"
"k8s.io/gengo/v2/types"
"k8s.io/klog/v2"
"k8s.io/code-generator/cmd/client-gen/generators/util"
)
@ -31,8 +32,8 @@ import (
// expansionGenerator produces a file for a expansion interfaces.
type expansionGenerator struct {
generator.DefaultGen
packagePath string
types []*types.Type
outputPath string
types []*types.Type
}
// We only want to call GenerateType() once per group.
@ -44,11 +45,16 @@ func (g *expansionGenerator) GenerateType(c *generator.Context, t *types.Type, w
sw := generator.NewSnippetWriter(w, c, "$", "$")
for _, t := range g.types {
tags := util.MustParseClientGenTags(append(t.SecondClosestCommentLines, t.CommentLines...))
if _, err := os.Stat(filepath.Join(g.packagePath, strings.ToLower(t.Name.Name+"_expansion.go"))); os.IsNotExist(err) {
manualFile := filepath.Join(g.outputPath, strings.ToLower(t.Name.Name+"_expansion.go"))
if _, err := os.Stat(manualFile); err == nil {
klog.V(4).Infof("file %q exists, not generating", manualFile)
} else if os.IsNotExist(err) {
sw.Do(expansionInterfaceTemplate, t)
if !tags.NonNamespaced {
sw.Do(namespacedExpansionInterfaceTemplate, t)
}
} else {
return err
}
}
return sw.Error()

View File

@ -22,14 +22,13 @@ import (
"path/filepath"
"strings"
"k8s.io/code-generator/cmd/client-gen/generators/util"
clientgentypes "k8s.io/code-generator/cmd/client-gen/types"
listergenargs "k8s.io/code-generator/cmd/lister-gen/args"
"k8s.io/gengo/v2/args"
"k8s.io/gengo/v2/generator"
"k8s.io/gengo/v2/namer"
"k8s.io/gengo/v2/types"
"k8s.io/code-generator/cmd/client-gen/generators/util"
clientgentypes "k8s.io/code-generator/cmd/client-gen/types"
"k8s.io/klog/v2"
)
@ -66,6 +65,8 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
klog.Fatalf("Failed loading boilerplate: %v", err)
}
customArgs := arguments.CustomArgs.(*listergenargs.CustomArgs)
var packageList generator.Packages
for _, inputDir := range arguments.InputDirs {
p := context.Universe.Package(inputDir)
@ -119,18 +120,21 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
orderer := namer.Orderer{Namer: namer.NewPrivateNamer(0)}
typesToGenerate = orderer.OrderTypes(typesToGenerate)
packagePath := filepath.Join(arguments.OutputPackagePath, groupPackageName, strings.ToLower(gv.Version.NonEmpty()))
subdir := filepath.Join(groupPackageName, strings.ToLower(gv.Version.NonEmpty()))
outputDir := filepath.Join(arguments.OutputBase, subdir)
outputPkg := filepath.Join(customArgs.OutputPackage, subdir)
packageList = append(packageList, &generator.DefaultPackage{
PackageName: strings.ToLower(gv.Version.NonEmpty()),
PackagePath: packagePath,
PackagePath: outputPkg,
Source: outputDir,
HeaderText: boilerplate,
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
generators = append(generators, &expansionGenerator{
DefaultGen: generator.DefaultGen{
OptionalName: "expansion_generated",
},
packagePath: filepath.Join(arguments.OutputBase, packagePath),
types: typesToGenerate,
outputPath: outputDir,
types: typesToGenerate,
})
for _, t := range typesToGenerate {
@ -138,7 +142,7 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
DefaultGen: generator.DefaultGen{
OptionalName: strings.ToLower(t.Name.Name),
},
outputPackage: arguments.OutputPackagePath,
outputPackage: outputPkg,
groupVersion: gv,
internalGVPkg: internalGVPkg,
typeToGenerate: t,

View File

@ -31,10 +31,6 @@ func main() {
klog.InitFlags(nil)
genericArgs, customArgs := generatorargs.NewDefaults()
// Override defaults.
// TODO: move this out of lister-gen
genericArgs.OutputPackagePath = "k8s.io/kubernetes/pkg/client/listers"
genericArgs.AddFlags(pflag.CommandLine)
customArgs.AddFlags(pflag.CommandLine)
flag.Set("logtostderr", "true")