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

feat(fakeclient): push event on watched channel on add/update/delete #57504

Merged

Conversation

yue9944882
Copy link
Member

@yue9944882 yue9944882 commented Dec 21, 2017

What this PR does / why we need it:

This PR enables watch function for kubernetes fakeclient.

This fake client add watchReactorFunction by wrapping watch.NewFake which is a chan Event but actually nothing pushes objects into this channel. So all watch function called by fake client will never return or never receive any object.

This PR intercepts ReactionFunc of Create / Update / DeleteActionImpl and will push the requested object to channel.

Which issue(s) this PR fixes (optional, in fixes #(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):

Fixes #54075

Special notes for your reviewer:

Release note:

enable watch function for fake client

@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/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 21, 2017
@yue9944882 yue9944882 changed the title fix(fakeclient): write event to watch channel on add/update/delete [WIP] fix(fakeclient): write event to watch channel on add/update/delete Dec 21, 2017
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 21, 2017
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Dec 21, 2017
@yue9944882 yue9944882 changed the title [WIP] fix(fakeclient): write event to watch channel on add/update/delete fix(fakeclient): write event to watch channel on add/update/delete Dec 21, 2017
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 21, 2017
@yue9944882
Copy link
Member Author

yue9944882 commented Dec 21, 2017

/assign @sttts
/area client-libraries
/kind bug

@justinsb @ncdc PTAL
Any comment or review will be appreciated.

@yue9944882
Copy link
Member Author

BTW I previously submitted a PR and it's merged. Why didn't I have 'Contributor' tag on my dialog?

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. area/client-libraries labels Dec 21, 2017
@yue9944882 yue9944882 changed the title fix(fakeclient): write event to watch channel on add/update/delete fix(fakeclient): push event on watched channel on add/update/delete Dec 24, 2017
@dims
Copy link
Member

dims commented Dec 24, 2017

/ok-to-test

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Dec 24, 2017
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Dec 25, 2017
@yue9944882
Copy link
Member Author

/assign @lavalamp

@yue9944882 yue9944882 force-pushed the fix-fake-client-dummy-watch branch 2 times, most recently from b4c2fb2 to 3efe6a2 Compare December 31, 2017 16:19
@yue9944882
Copy link
Member Author

@deads2k Hi~ PTAL, thx.

decoder runtime.Decoder
lock sync.RWMutex
objects map[schema.GroupVersionResource][]runtime.Object
watchers map[schema.GroupVersionResource]map[string][]*watch.FakeWatcher
Copy link
Contributor

Choose a reason for hiding this comment

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

godoc explaining the map keys and values.

@deads2k
Copy link
Contributor

deads2k commented Jan 16, 2018

Minor comment. Squash into "real" and "generated" commits, then lgtm

/approve

@yue9944882
Copy link
Member Author

@stts @lavalamp @piosz @DirectXMan12
Hi, do you think about approving this PR plz?
Thx a lot.
cc @deads2k

@deads2k
Copy link
Contributor

deads2k commented Jan 16, 2018

@stts @lavalamp @piosz @DirectXMan12
Hi, do you think about approving this PR plz?
Thx a lot.
cc @deads2k

Don't forget about the squash to two commits

fix races with watch call

add test for non-namespace resource watch

add matching for all-namespace-watch

fix delete namespace watch & restrict test

fix multiple invocation on same resource & namespace

add descriptive doc for tracker.watchers
@yue9944882
Copy link
Member Author

@deads2k
Squashed. Thx for reminding.

@@ -71,7 +71,15 @@ func NewSimpleClientset(objects ...runtime.Object) *Clientset {

fakePtr := testing.Fake{}
fakePtr.AddReactor("*", "*", testing.ObjectReaction(o))
fakePtr.AddWatchReactor("*", testing.DefaultWatchReactor(watch.NewFake(), nil))
Copy link
Contributor

Choose a reason for hiding this comment

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

@sttts where does this get generated from?

@@ -127,7 +127,15 @@ func NewSimpleClientset(objects ...runtime.Object) *Clientset {

fakePtr := testing.Fake{}
fakePtr.AddReactor("*", "*", testing.ObjectReaction(o))
fakePtr.AddWatchReactor("*", testing.DefaultWatchReactor(watch.NewFake(), nil))
fakePtr.AddWatchReactor("*", func(action testing.Action) (handled bool, ret watch.Interface, err error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

@deads2k @sttts I believe it's generated from here.

Copy link
Contributor

Choose a reason for hiding this comment

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

@deads2k @sttts I believe it's generated from here.

lgtm

@deads2k
Copy link
Contributor

deads2k commented Jan 16, 2018

@sttts if its good for you, we can apply the label. The other changes are just for the generated code.

@deads2k
Copy link
Contributor

deads2k commented Jan 16, 2018

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 16, 2018
@deads2k deads2k added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 16, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

Approval requirements bypassed by manually added approval.

This pull-request has been approved by: deads2k, yue9944882

Associated issue: #54075

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

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@k8s-github-robot
Copy link

/test all [submit-queue is verifying that this PR is safe to merge]

@k8s-github-robot
Copy link

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions here.

@k8s-github-robot k8s-github-robot merged commit 268555a into kubernetes:master Jan 16, 2018
k8s-github-robot pushed a commit that referenced this pull request Mar 27, 2018
Automatic merge from submit-queue (batch tested with PRs 61195, 61479). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Use RaceFreeFakeWatcher in ObjectTracker to fix racy watch panics

**What this PR does / why we need it**:

The `FakeWatcher` added to `ObjectTracker` in #57504 allows sends on the result channel after it's closed; for example calling `Stop()` then `Add(obj)` will cause a panic. In my experience this has led to flaky tests when informers and controllers are running.

Replacing `FakeWatcher` with `RaceFreeFakeWatcher` fixes the problem, since `RaceFreeFakeWatcher` ignores additional events that occur after the watcher is stopped. It also panics instead of blocking when the result channel is full, which seems like a more useful behavior in tests than blocking.

I removed the `FakeWatchBufferSize` constant since `RaceFreeFakeWatcher` doesn't take a buffer size argument anymore. This seems fine since the `DefaultChanSize` constant is close to the `FakeWatchBufferSize` value (100 vs 128).

**Special notes for your reviewer**:

I can provide a minimal repro of a flaky test caused by the earlier behavior if necessary.

**Release note**:
```release-note
Fix racy panics when using fake watches with ObjectTracker
```
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/client-libraries 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. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fake clientset not sending events
9 participants