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 4 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
9 changes: 8 additions & 1 deletion codegen/cmd/injection-gen/generators/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
"k8s.io/klog/v2"

"github.com/spf13/pflag"
)

// injectionTestGenerator produces a file of listers for a given GroupVersion and
Expand Down Expand Up @@ -78,6 +80,8 @@ func (g *injectionGenerator) GenerateType(c *generator.Context, t *types.Type, w

klog.V(5).Info("processing type ", t)

disableInformerInit, _ := pflag.CommandLine.GetBool("disable-informer-init")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should be pulling this from the custom args struct and the funneling it into this generator

customArgs, ok := arguments.CustomArgs.(*informergenargs.CustomArgs)

packageList = append(packageList, versionInformerPackages(versionPackagePath, groupPackageName, gv, groupGoNames[groupPackageName], boilerplate, typesWithInformers, customArgs)...)

otherwise it's like we're reaching for globals.

The logic in packages is a bit verbose :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dprotaso done.


m := map[string]interface{}{
"groupGoName": namer.IC(g.groupGoName),
"versionGoName": namer.IC(g.groupVersion.Version.String()),
Expand All @@ -98,6 +102,7 @@ func (g *injectionGenerator) GenerateType(c *generator.Context, t *types.Type, w
Package: "context",
Name: "WithValue",
}),
"disableInformerInit": disableInformerInit,
}

sw.Do(injectionInformer, m)
Expand All @@ -106,14 +111,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
Loading