From f69f148b7c7074ba7f02819dc011d2be8e2c2fd7 Mon Sep 17 00:00:00 2001 From: Stavros Kontopoulos Date: Thu, 28 Mar 2024 15:51:56 +0200 Subject: [PATCH] Optionally generate an init func for an informer (#2989) * Optionally generate an init func for an informer * lint * move condition to go template * rename flag and fixes * pass flag value via the generator --- codegen/cmd/injection-gen/args/args.go | 3 +++ codegen/cmd/injection-gen/generators/informer.go | 6 +++++- codegen/cmd/injection-gen/generators/packages.go | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/codegen/cmd/injection-gen/args/args.go b/codegen/cmd/injection-gen/args/args.go index 7cd8122d52..265b4e40ae 100644 --- a/codegen/cmd/injection-gen/args/args.go +++ b/codegen/cmd/injection-gen/args/args.go @@ -30,6 +30,7 @@ type CustomArgs struct { ListersPackage string ForceKinds string ListerHasPointerElem bool + DisableInformerInit bool } // NewDefaults returns default arguments for the generator. @@ -49,6 +50,8 @@ func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) { fs.BoolVar(&ca.ListerHasPointerElem, "lister-has-pointer-elem", false, "") fs.MarkDeprecated("lister-has-pointer-elem", "this flag has no effect") + + fs.BoolVar(&ca.DisableInformerInit, "disable-informer-init", false, "disable generating the init function for the informer") } // Validate checks the given arguments. diff --git a/codegen/cmd/injection-gen/generators/informer.go b/codegen/cmd/injection-gen/generators/informer.go index c4fb215f1d..c0b3bf3eaf 100644 --- a/codegen/cmd/injection-gen/generators/informer.go +++ b/codegen/cmd/injection-gen/generators/informer.go @@ -37,6 +37,7 @@ type injectionGenerator struct { imports namer.ImportTracker typedInformerPackage string groupInformerFactoryPackage string + disableInformerInit bool } var _ generator.Generator = (*injectionGenerator)(nil) @@ -98,6 +99,7 @@ func (g *injectionGenerator) GenerateType(c *generator.Context, t *types.Type, w Package: "context", Name: "WithValue", }), + "disableInformerInit": g.disableInformerInit, } sw.Do(injectionInformer, m) @@ -106,14 +108,16 @@ func (g *injectionGenerator) GenerateType(c *generator.Context, t *types.Type, w } var injectionInformer = ` +{{ if not .disableInformerInit }} func init() { {{.injectionRegisterInformer|raw}}(withInformer) } +{{ end }} // Key is used for associating the Informer inside the context.Context. type Key struct{} -func withInformer(ctx {{.contextContext|raw}}) ({{.contextContext|raw}}, {{.controllerInformer|raw}}) { +{{ if .disableInformerInit }} func WithInformer {{ else }} func withInformer {{ end }} (ctx {{.contextContext|raw}}) ({{.contextContext|raw}}, {{.controllerInformer|raw}}) { f := {{.factoryGet|raw}}(ctx) inf := f.{{.groupGoName}}().{{.versionGoName}}().{{.type|publicPlural}}() return {{ .contextWithValue|raw }}(ctx, Key{}, inf), inf.Informer() diff --git a/codegen/cmd/injection-gen/generators/packages.go b/codegen/cmd/injection-gen/generators/packages.go index 43768927a5..f0ca43f2bf 100644 --- a/codegen/cmd/injection-gen/generators/packages.go +++ b/codegen/cmd/injection-gen/generators/packages.go @@ -412,6 +412,7 @@ func versionInformerPackages(basePackage string, groupPkgName string, gv clientg imports: generator.NewImportTracker(), typedInformerPackage: typedInformerPackage, groupInformerFactoryPackage: factoryPackagePath, + disableInformerInit: customArgs.DisableInformerInit, }) return generators },