Navigation Menu

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

client-go: support waiting for SharedInformerFactory shutdown #112200

Conversation

pohly
Copy link
Contributor

@pohly pohly commented Sep 2, 2022

What type of PR is this?

/kind feature

What this PR does / why we need it:

SharedInformerFactory starts goroutines in Start and those can be stopped by
closing the stop channel. However, there was no API that waits for the
goroutines.

This is a problem for unit testing. A test has to return while the informers
are still running, which may get flagged by tools like
https://github.com/uber-go/goleak or by klog/ktesting when those informers
lead to log output.

While at it, more documentation gets added to address
#65036.

Which issue(s) this PR fixes:

Fixes #65036 (as confirmed in #65036 (comment))

Special notes for your reviewer:

Does this PR introduce a user-facing change?

client-go: SharedInformerFactory supports waiting for goroutines during shutdown

/cc @alexzielenski

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/code-generation sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Sep 2, 2022
Copy link
Contributor

@alexzielenski alexzielenski left a comment

Choose a reason for hiding this comment

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

looks great so far. Would like to see the semantics for WaitForShutdown clarified a bit more in the documentation.

I'm also wondering what the best place would be to add tests for this. I'm not seeing any existing tests for the imformer-gen code. Hopefully an approver can comment about that.

for informerType, informer := range f.informers {
if !f.startedInformers[informerType] {
f.wg.Add(1)
informer := informer
Copy link
Contributor

Choose a reason for hiding this comment

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

nit -- this line surprised me. did the compiler yell

Copy link
Contributor Author

@pohly pohly Sep 2, 2022

Choose a reason for hiding this comment

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

No, it's worse: without it, when the goroutines run, they use the current value of the loop variable, typically the last entry in the slice.

This idiom creates a new variable for each loop iteration. I can add a comment explaining it.

Copy link
Contributor

Choose a reason for hiding this comment

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

shucks thats unfortunate. maybe a brief line about it yeah

Copy link
Member

Choose a reason for hiding this comment

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

This is a common go gotcha-- I had to think for a moment to see why the old version of the code didn't have this bug.

@pohly pohly force-pushed the client-go-shared-informer-factory-shutdown branch from 64834e9 to 5ffd088 Compare September 5, 2022 07:18
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 5, 2022
@pohly
Copy link
Contributor Author

pohly commented Sep 5, 2022

@alexzielenski : please take another look. I prefer to keep the PR always mergeable, therefore I updated the commit. Changes since initial review: https://github.com/kubernetes/kubernetes/compare/64834e96aa00e041717c110e9ff051b1f8ddf482..5ffd0887ed858e01d106074314b759695afffe3e

@pohly
Copy link
Contributor Author

pohly commented Sep 5, 2022

/retest

@leilajal
Copy link
Contributor

leilajal commented Sep 6, 2022

/cc @lavalamp
/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Sep 6, 2022
Copy link
Contributor

@alexzielenski alexzielenski left a comment

Choose a reason for hiding this comment

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

nit about the doc comment

otherwise looks good to me. ill hold off on proper /lgtm until someone more acquainted takes a look.

xref: https://kubernetes.slack.com/archives/C0EG7JC6T/p1662030952291459

@alexzielenski
Copy link
Contributor

/assign @lavalamp

@pohly pohly force-pushed the client-go-shared-informer-factory-shutdown branch 3 times, most recently from 29cd3f6 to 91071d8 Compare September 9, 2022 07:51
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Sep 9, 2022
func (f *sharedInformerFactory) Shutdown() {
f.lock.Lock()
f.shuttingDown = true
f.lock.Unlock()
Copy link
Member

Choose a reason for hiding this comment

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

it really doesn't matter here, but for more complex things I usually ask people to put it in an anonymous function so that defer can be used to unlock.

@lavalamp
Copy link
Member

Thanks!

/approve

your choice about which nits to fix

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 12, 2022
SharedInformerFactory starts goroutines in Start and those can be stopped by
closing the stop channel. However, there was no API that waits for the
goroutines.

This is a problem for unit testing. A test has to return while the informers
are still running, which may get flagged by tools like
https://github.com/uber-go/goleak or by klog/ktesting when those informers
lead to log output.

While at it, more documentation gets added to address
kubernetes#65036.
@pohly pohly force-pushed the client-go-shared-informer-factory-shutdown branch from 7487bc1 to e89d1d4 Compare September 12, 2022 19:05
@pohly
Copy link
Contributor Author

pohly commented Sep 12, 2022

I took your documentation updates but left the Lock/Unlock as it was. I think it's simple enough to not need a separate function.

@pohly
Copy link
Contributor Author

pohly commented Sep 12, 2022

@lavalamp: do you want to add the LGTM yourself or hand it back to @alexzielenski?

Copy link
Contributor

@alexzielenski alexzielenski left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 13, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alexzielenski, lavalamp, pohly

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

@alexzielenski
Copy link
Contributor

/retest

@k8s-ci-robot k8s-ci-robot merged commit 084a412 into kubernetes:master Sep 13, 2022
@k8s-ci-robot k8s-ci-robot added this to the v1.26 milestone Sep 13, 2022
@howardjohn
Copy link
Contributor

Are there any plans to add this for metadata SharedInformerFactory and dynamic DynamicSharedInformerFactory?

@pohly
Copy link
Contributor Author

pohly commented Dec 9, 2022

No plans, but if someone (you?) were to prepare a PR, it probably would get merged.

howardjohn added a commit to howardjohn/kubernetes that referenced this pull request Dec 12, 2022
k8s-ci-robot pushed a commit that referenced this pull request Mar 1, 2023
…114434)

* client-go: support `Shutdown()` for metadata and dynamic informers

Followup to #112200,
specifically
#112200 (comment).

* add comments

* Defer lock
k8s-publishing-bot pushed a commit to kubernetes/client-go that referenced this pull request Mar 1, 2023
…114434)

* client-go: support `Shutdown()` for metadata and dynamic informers

Followup to kubernetes/kubernetes#112200,
specifically
kubernetes/kubernetes#112200 (comment).

* add comments

* Defer lock

Kubernetes-commit: b99fe0d5b9896dd3fe9a2c1bc3b399a18ad080d2
rayowang pushed a commit to rayowang/kubernetes that referenced this pull request Feb 9, 2024
…ubernetes#114434)

* client-go: support `Shutdown()` for metadata and dynamic informers

Followup to kubernetes#112200,
specifically
kubernetes#112200 (comment).

* add comments

* Defer lock
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. area/code-generation cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SharedInformer documentation is missing or hard to find
6 participants