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

Conversation

skonto
Copy link
Contributor

@skonto skonto commented Mar 14, 2024

Changes

 Knative Injection (for cert-manager)
${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh "injection" \
  knative.dev/serving/pkg/net-certmanager/client/certmanager github.com/cert-manager/cert-manager/pkg/apis \
  "certmanager:v1 acme:v1" \
  --skipInitFuncForInformer \
  --go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt

and then on demand register the right informer:

if shouldEnableNetCertManagerController(ctx, client) {
		injection.Default.RegisterInformer(challenge.WithInformer)
		injection.Default.RegisterInformer(v1certificate.WithInformer)
		injection.Default.RegisterInformer(certificaterequest.WithInformer)
		injection.Default.RegisterInformer(clusterissuer.WithInformer)
		injection.Default.RegisterInformer(issuer.WithInformer)
		ctors = append(ctors, certificate.NewController) // add the net-certmanager controller
	}
  • We could have used a tag to skip per type but these are external types. In the feature we can do this per type for our
    types eg. +skipInitInformer and thus pass this directly to the InformerGenerator.
  • We could expand it to filteredInformers if we want.

/assign @dprotaso

/kind enhancement

Release Note


Docs


@knative-prow knative-prow bot added kind/enhancement approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Mar 14, 2024
@knative-prow knative-prow bot requested review from creydr and ReToCode March 14, 2024 11:36
@knative-prow knative-prow bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Mar 14, 2024
@skonto skonto requested review from dprotaso and removed request for creydr March 14, 2024 11:36
Copy link

codecov bot commented Mar 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.68%. Comparing base (adefab4) to head (4a9843c).
Report is 10 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2989   +/-   ##
=======================================
  Coverage   78.68%   78.68%           
=======================================
  Files         188      188           
  Lines       11051    11051           
=======================================
  Hits         8695     8695           
  Misses       2092     2092           
  Partials      264      264           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -36,13 +37,17 @@ func main() {
genericArgs.AddFlags(pflag.CommandLine)
customArgs.AddFlags(pflag.CommandLine)
flag.Set("logtostderr", "true")
skipInitFuncForInformer := flag.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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, I was not sure if I should touch those.

Comment on lines 48 to 50
if *skipInitFuncForInformer {
os.Setenv(generators.SkipInitFuncForInformerEnv, "true")
}
Copy link
Member

Choose a reason for hiding this comment

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

Not needed if we setup args properly

Comment on lines 107 to 111
if os.Getenv(SkipInitFuncForInformerEnv) == "true" {
sw.Do(strings.ReplaceAll(injectionInformer, "withInformer", "WithInformer"), m)
} else {
sw.Do(initFunc+injectionInformer, m)
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we pass the flag value to the context map m and just do the if logic in the template?

Copy link
Contributor Author

@skonto skonto Mar 19, 2024

Choose a reason for hiding this comment

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

Let me try.

Copy link
Contributor Author

@skonto skonto Mar 19, 2024

Choose a reason for hiding this comment

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

Ok I didn't see it was using go templates behind the scenes, now it makes sense.

@skonto
Copy link
Contributor Author

skonto commented Mar 26, 2024

@dprotaso gentle ping

@@ -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

@skonto
Copy link
Contributor Author

skonto commented Mar 27, 2024

@dprotaso pls take another look, thanks for the review!

@@ -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.

@skonto
Copy link
Contributor Author

skonto commented Mar 28, 2024

@dprotaso ready, could you stamp it?

@dprotaso
Copy link
Member

/lgtm
/approve

thanks @skonto

@knative-prow knative-prow bot added the lgtm Indicates that a PR is ready to be merged. label Mar 28, 2024
Copy link

knative-prow bot commented Mar 28, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dprotaso, skonto

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow bot merged commit f69f148 into knative:main Mar 28, 2024
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/enhancement lgtm Indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants