kubernetes: client-go example fails due to race condition in fakes+informers

This is broken off of https://github.com/kubernetes/kubernetes/issues/88508 which was stalebot closed

What happened:

Run the example https://github.com/kubernetes/client-go/blob/master/examples/fake-client/main_test.go#L42. It will usually pass. However, there is an inherent race condition in the fake client and informer code.

In the real client, we do a list, then set up a watch with the resourceVersion returned in the list. However, the fake client ignores resourceVersions. This leads to a race between https://github.com/kubernetes/client-go/blob/866e6a7e3cf35d16a6d3d5175e36d05dfe4b0797/tools/cache/reflector.go#L302 and https://github.com/kubernetes/client-go/blob/866e6a7e3cf35d16a6d3d5175e36d05dfe4b0797/tools/cache/reflector.go#L414; any creates/updates during this time are dropped.

What you expected to happen:

Either:

  1. The race is fixed
  2. The example is updated to provide best practices for testing with informers and fakes. I looked through all of the k/k tests I could find, and it seems that instead of doing client.Create(...) they are generally using informer.Store.Add(...). Is this the recommended pattern?

How to reproduce it (as minimally and precisely as possible):

Run the example test many times. using https://godoc.org/golang.org/x/tools/cmd/stress is useful, as is dropping the timeout from 30s to something smaller to speed up the reproduction (although 30s will still reproduce it - if you are patient). Adding -race seems to trigger it more.

If this cannot reproduce, you can inject a time.Sleep between the two code points mentioned above to exacerbate the race and make it consistent.

/sig api-machinery

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 15 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I’d like to echo @howardjohn’s https://github.com/kubernetes/kubernetes/issues/95372#issuecomment-716179706

Testing with fake clients seems pretty fundamental to the k8s ecosystem and having them cooperate with informers would be a big win.