Skip to content

Commit

Permalink
Optionally generate an init func for an informer (#2989)
Browse files Browse the repository at this point in the history
* Optionally generate an init func for an informer

* lint

* move condition to go template

* rename flag and fixes

* pass flag value via the generator
  • Loading branch information
skonto committed Mar 28, 2024
1 parent b8b7ca1 commit f69f148
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions codegen/cmd/injection-gen/args/args.go
Expand Up @@ -30,6 +30,7 @@ type CustomArgs struct {
ListersPackage string
ForceKinds string
ListerHasPointerElem bool
DisableInformerInit bool
}

// NewDefaults returns default arguments for the generator.
Expand All @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion codegen/cmd/injection-gen/generators/informer.go
Expand Up @@ -37,6 +37,7 @@ type injectionGenerator struct {
imports namer.ImportTracker
typedInformerPackage string
groupInformerFactoryPackage string
disableInformerInit bool
}

var _ generator.Generator = (*injectionGenerator)(nil)
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions codegen/cmd/injection-gen/generators/packages.go
Expand Up @@ -412,6 +412,7 @@ func versionInformerPackages(basePackage string, groupPkgName string, gv clientg
imports: generator.NewImportTracker(),
typedInformerPackage: typedInformerPackage,
groupInformerFactoryPackage: factoryPackagePath,
disableInformerInit: customArgs.DisableInformerInit,
})
return generators
},
Expand Down

0 comments on commit f69f148

Please sign in to comment.