-
Notifications
You must be signed in to change notification settings - Fork 1.2k
✨ Add support for creating namespaced watches #692
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
Conversation
Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA. It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
Welcome @lulf! |
Hi @lulf. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: lulf The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
0423490
to
f7fdf3a
Compare
f7fdf3a
to
3f7f74c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, but it would be nice if we had some testing for this, e.G. in pkg/controller/controller_integration_test.go
.
What I am not 100% sure about is if it makes sense to make something as specific as GetInformerForKindInNamespace
part of our public API, because there are more ways to scope watches (e.G. LabelSelector and FieldSelector) and with the current approach we end up with a dedicated func for each of those.
An alternate approach could be to use the functional opts pattern to allow passing in additional opts for the watches.
Assigning shawn as I heard he knows a lot about the cache :)
/assign @shawn-hurley
/ok-to-test
Edit: Also thinking about this, I am not sure if this really make sense because the client will still create a non-namespaced watch once you start using it for the given GVK. This means that you end up with both a namespaced and a non-namespaced watch and use the former for the client and the latter for the watch. This effectively makes the whole thing have the same effect as a Predicate
except that you create another cache.
@alvaroaleman Thanks for taking a look.
I'm very open to alternative approaches. The main motivation is the ability to create watches in both the 'controller namespace' as well as watches for other namespaces, without requiring cluster-wide access to a particular resource. For example, with this PR, I'm able to create a manager watching deployments in any namespace, but for secrets, I can create a watch in a specific namespace (because I don't want to give my controller access to secrets all over the cluster). Its not 100% clear to me what you mean by a non-namespaced watch. It doesn't seem to create a non-namespaced watch in my testing (at least it doesn't throw any permission error), could you point me to where this happens? Happy to add some tests for this, just want to get some feedback on the approach and suggestions. Thanks again! |
Seems I was too quick with the comment, as you updated Setting the |
@alvaroaleman Yes, it would lead to cache duplication, that is the 'cost' of the added flexibility I guess, but I think it only impact users of this functionality, and existing usage should remain as is. Maybe if there was a smarter way to share cache between informers for different namespaces... Setting namespace on the mgr does not cover my use case, because it allows only watching resources in a single namespace. The specific flexibility I need is the ability to watch some resources (that the controller "owns", i.e. CRD) in all namespaces, and then watch resources created by the controller (such as secrets, deployments etc) in the same namespace as the controller. The main point here is that I don't want my controller to read secrets in other namespaces than the same as where the controller is running. I'm looking at your initial idea, maybe changing GetInformers to
I don't know the code that well, so apologies if what I'm saying is incorrect! |
I think it impacts everyone because whenever a namespaced object is retrieved, we create a namespaced cache for the given type even if we already have a non-namespaced one. |
Just pushed a change so that informer_cache.Get() passes nil as the namespace argument instead of object.key, which should make cache behavior identical to earlier. What I was thinking is that the existing usages of @alvaroaleman Does this address that particular concern? |
Yeah it addresses the concern but it unfortunately means this PR won't solve your problem anymore as it now uses the non-namespaced cache for reading again :grimace: This is unfortunate a tricky problem, IMHO it would be best to continue discussing possible approaches in #244 |
Good point. I will close this for now, but might have another go (and add a test for it) 😉 thanks for spending time looking at it, @alvaroaleman! |
@alvaroaleman I was unable to reopen this due to force-pushing, but I have raised #694 which takes a slighly different approach, and overrides the namespace if cache is configured to watch all namespace. Also, the interface is a bit different. |
Extends the Source type with an optional Namespace parameter, which will override the default namespace by adding the namespace as a key in the informer map. Existing usage will remain as is, sharing the same key as before, whereas if namespace is specified, a new informer will be created for that case.
This allows creating an operator watching resources in multiple namespaces without requiring cluster-wide privileges for that resource, as desired in #687 .
Simple tests suggest that it works for me, but there are probably pitfalls I have not considered.
I'd love some feedback on this approach and if you think this is the right or wrong direction.