Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally generate an init func for an informer #2989

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions codegen/cmd/injection-gen/args/args.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading