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 3 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
2 changes: 2 additions & 0 deletions codegen/cmd/injection-gen/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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.Bool("skipInitFuncForInformer", false, "Skip the init function for informer")
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 wire this to the CustomArgs structs like we do for the other flags

Also can we follow the flag naming convention we are using which is snake-case

I'd probably call this disable-informer-init

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 ok done

}

// 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)

skipInitFuncForInformer, _ := pflag.CommandLine.GetBool("skipInitFuncForInformer")

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",
}),
"skipInitFuncForInformer": skipInitFuncForInformer,
}

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 .skipInitFuncForInformer }}
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 .skipInitFuncForInformer }} 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