mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Fix lister-gen wrt gengo/v2
This commit is contained in:
		@@ -18,15 +18,15 @@ 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 {
 | 
				
			||||||
 | 
						OutputPackage string // must be a Go import-path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// PluralExceptions specify list of exceptions used when pluralizing certain types.
 | 
						// PluralExceptions specify list of exceptions used when pluralizing certain types.
 | 
				
			||||||
	// For example 'Endpoints:Endpoints', otherwise the pluralizer will generate 'Endpointes'.
 | 
						// For example 'Endpoints:Endpoints', otherwise the pluralizer will generate 'Endpointes'.
 | 
				
			||||||
	PluralExceptions []string
 | 
						PluralExceptions []string
 | 
				
			||||||
@@ -40,24 +40,27 @@ 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/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.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.
 | 
					// Validate checks the given arguments.
 | 
				
			||||||
func Validate(genericArgs *args.GeneratorArgs) error {
 | 
					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 {
 | 
						custom := genericArgs.CustomArgs.(*CustomArgs)
 | 
				
			||||||
		return fmt.Errorf("output package cannot be empty")
 | 
					
 | 
				
			||||||
 | 
						if len(custom.OutputPackage) == 0 {
 | 
				
			||||||
 | 
							return fmt.Errorf("--output-package must be specified")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,6 +24,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	"k8s.io/gengo/v2/generator"
 | 
						"k8s.io/gengo/v2/generator"
 | 
				
			||||||
	"k8s.io/gengo/v2/types"
 | 
						"k8s.io/gengo/v2/types"
 | 
				
			||||||
 | 
						"k8s.io/klog/v2"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"k8s.io/code-generator/cmd/client-gen/generators/util"
 | 
						"k8s.io/code-generator/cmd/client-gen/generators/util"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -31,7 +32,7 @@ import (
 | 
				
			|||||||
// expansionGenerator produces a file for a expansion interfaces.
 | 
					// expansionGenerator produces a file for a expansion interfaces.
 | 
				
			||||||
type expansionGenerator struct {
 | 
					type expansionGenerator struct {
 | 
				
			||||||
	generator.DefaultGen
 | 
						generator.DefaultGen
 | 
				
			||||||
	packagePath string
 | 
						outputPath string
 | 
				
			||||||
	types      []*types.Type
 | 
						types      []*types.Type
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -44,11 +45,16 @@ func (g *expansionGenerator) GenerateType(c *generator.Context, t *types.Type, w
 | 
				
			|||||||
	sw := generator.NewSnippetWriter(w, c, "$", "$")
 | 
						sw := generator.NewSnippetWriter(w, c, "$", "$")
 | 
				
			||||||
	for _, t := range g.types {
 | 
						for _, t := range g.types {
 | 
				
			||||||
		tags := util.MustParseClientGenTags(append(t.SecondClosestCommentLines, t.CommentLines...))
 | 
							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)
 | 
								sw.Do(expansionInterfaceTemplate, t)
 | 
				
			||||||
			if !tags.NonNamespaced {
 | 
								if !tags.NonNamespaced {
 | 
				
			||||||
				sw.Do(namespacedExpansionInterfaceTemplate, t)
 | 
									sw.Do(namespacedExpansionInterfaceTemplate, t)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return sw.Error()
 | 
						return sw.Error()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,14 +22,13 @@ import (
 | 
				
			|||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"strings"
 | 
						"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/args"
 | 
				
			||||||
	"k8s.io/gengo/v2/generator"
 | 
						"k8s.io/gengo/v2/generator"
 | 
				
			||||||
	"k8s.io/gengo/v2/namer"
 | 
						"k8s.io/gengo/v2/namer"
 | 
				
			||||||
	"k8s.io/gengo/v2/types"
 | 
						"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"
 | 
						"k8s.io/klog/v2"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -66,6 +65,8 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
 | 
				
			|||||||
		klog.Fatalf("Failed loading boilerplate: %v", err)
 | 
							klog.Fatalf("Failed loading boilerplate: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						customArgs := arguments.CustomArgs.(*listergenargs.CustomArgs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var packageList generator.Packages
 | 
						var packageList generator.Packages
 | 
				
			||||||
	for _, inputDir := range arguments.InputDirs {
 | 
						for _, inputDir := range arguments.InputDirs {
 | 
				
			||||||
		p := context.Universe.Package(inputDir)
 | 
							p := context.Universe.Package(inputDir)
 | 
				
			||||||
@@ -119,17 +120,20 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
 | 
				
			|||||||
		orderer := namer.Orderer{Namer: namer.NewPrivateNamer(0)}
 | 
							orderer := namer.Orderer{Namer: namer.NewPrivateNamer(0)}
 | 
				
			||||||
		typesToGenerate = orderer.OrderTypes(typesToGenerate)
 | 
							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{
 | 
							packageList = append(packageList, &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, &expansionGenerator{
 | 
									generators = append(generators, &expansionGenerator{
 | 
				
			||||||
					DefaultGen: generator.DefaultGen{
 | 
										DefaultGen: generator.DefaultGen{
 | 
				
			||||||
						OptionalName: "expansion_generated",
 | 
											OptionalName: "expansion_generated",
 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
					packagePath: filepath.Join(arguments.OutputBase, packagePath),
 | 
										outputPath: outputDir,
 | 
				
			||||||
					types:      typesToGenerate,
 | 
										types:      typesToGenerate,
 | 
				
			||||||
				})
 | 
									})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -138,7 +142,7 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
 | 
				
			|||||||
						DefaultGen: generator.DefaultGen{
 | 
											DefaultGen: generator.DefaultGen{
 | 
				
			||||||
							OptionalName: strings.ToLower(t.Name.Name),
 | 
												OptionalName: strings.ToLower(t.Name.Name),
 | 
				
			||||||
						},
 | 
											},
 | 
				
			||||||
						outputPackage:  arguments.OutputPackagePath,
 | 
											outputPackage:  outputPkg,
 | 
				
			||||||
						groupVersion:   gv,
 | 
											groupVersion:   gv,
 | 
				
			||||||
						internalGVPkg:  internalGVPkg,
 | 
											internalGVPkg:  internalGVPkg,
 | 
				
			||||||
						typeToGenerate: t,
 | 
											typeToGenerate: t,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -31,10 +31,6 @@ func main() {
 | 
				
			|||||||
	klog.InitFlags(nil)
 | 
						klog.InitFlags(nil)
 | 
				
			||||||
	genericArgs, customArgs := generatorargs.NewDefaults()
 | 
						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)
 | 
						genericArgs.AddFlags(pflag.CommandLine)
 | 
				
			||||||
	customArgs.AddFlags(pflag.CommandLine)
 | 
						customArgs.AddFlags(pflag.CommandLine)
 | 
				
			||||||
	flag.Set("logtostderr", "true")
 | 
						flag.Set("logtostderr", "true")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user