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

DefaultNamespaces Cache Option doesn't allow All Namespaces for List #2628

Closed
manno opened this issue Dec 22, 2023 · 7 comments
Closed

DefaultNamespaces Cache Option doesn't allow All Namespaces for List #2628

manno opened this issue Dec 22, 2023 · 7 comments
Labels
kind/support Categorizes issue or PR as a support question. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@manno
Copy link

manno commented Dec 22, 2023

I'm trying to limit the controller to watch config maps only in one namespace, while watching other resources in all namespaces. Mainly for performance reasons, I don't want the controller to trigger for any config map event in the cluster.

The cache design document describes the config as DefaultNamespaces map[string]*Config, while the code implements DefaultNamespaces map[string]Config. Since all options are nil-able I guess that's not an issue.

From the cache tests I'd expect this to allow list and get in all namespaces:

Cache: cache.Options{
	DefaultNamespaces: map[string]cache.Config{cache.AllNamespaces: {}},
},

However, it results in unable to list: test-7af124d because of unknown namespace for the cache.
I think that's because theList func in multi cache is missing the special handling, that was added to Get func:

diff --git a/pkg/cache/multi_namespace_cache.go b/pkg/cache/multi_namespace_cache.go
index 87c31a7c..8e58b0c0 100644
--- a/pkg/cache/multi_namespace_cache.go
+++ b/pkg/cache/multi_namespace_cache.go
@@ -237,6 +237,9 @@ func (c *multiNamespaceCache) List(ctx context.Context, list client.ObjectList,
        if listOpts.Namespace != corev1.NamespaceAll {
                cache, ok := c.namespaceToCache[listOpts.Namespace]
                if !ok {
+                       if global, hasGlobal := c.namespaceToCache[metav1.NamespaceAll]; hasGlobal {
+                               return global.List(ctx, list, opts...)
+                       }
                        return fmt.Errorf("unable to list: %v because of unknown namespace for the cache", listOpts.Namespace)
                }
                return cache.List(ctx, list, opts...)

If I understand the consequences of cache options correctly, they do limit Watch, but also affect the cached client. I need another client, e.g. APIReader, to retrieve configmaps in other namespaces.

@troy0820
Copy link
Member

/kind support

@k8s-ci-robot k8s-ci-robot added the kind/support Categorizes issue or PR as a support question. label Dec 25, 2023
@alvaroaleman
Copy link
Member

So you are limiting the watch for configmaps to one namespace using ByObject and then use DefaultNamespaces to wach everything else in all namespaces?

The DefaultNamespaces option does nothing for a type that has the Namespaces setting set. To quote from the godocs:

// Namespaces maps a namespace name to cache configs. If set, only the
// namespaces in this map will be cached.

Also, the default is to cache all namespaces without further selection logic, so putting cache.AllNamespaces: {} into DefaultNamespaces does nothing either.

And yeah, the client reads from the cache by default, hence if you limit the cache to a given namespace, you can't use the default client to read from a different namespace.

Does this answer your question? Do you think the godocs are unclear here?

@manno
Copy link
Author

manno commented Jan 10, 2024

So you are limiting the watch for configmaps to one namespace using ByObject and then use DefaultNamespaces to wach everything else in all namespaces.

Yes, that was my intention, as I expect configmaps to change a lot, but I'm only interested in changes to one specific config map (the controllers "config" file).

The DefaultNamespaces option does nothing for a type that has the Namespaces setting set. To quote from the godocs:

// Namespaces maps a namespace name to cache configs. If set, only the
// namespaces in this map will be cached.

I see now, that

// It is possible to have specific Config for just some namespaces
// but cache all namespaces by using the AllNamespaces const as the map key.
// This will then include all namespaces that do not have a more specific
// setting.
only refers to AllNamespaces/NamespaceAll in ByObject, like in this test.

However, the DefaultNamespace option suggests it's possible to use both together. That still leaves me confused about this godoc:

// DefaultNamespaces maps namespace names to cache configs. If set, only
// the namespaces in here will be watched and it will by used to default
// ByObject.Namespaces for all objects if that is nil.
//
// It is possible to have specific Config for just some namespaces
// but cache all namespaces by using the AllNamespaces const as the map key.
// This will then include all namespaces that do not have a more specific
// setting.

I read this and thought, it would apply DefaultNamespace to namespaces not listed in ByObject.Namespaces.

I'm pretty sure it works for Get, because of https://github.com/kubernetes-sigs/controller-runtime/pull/2539/files, but not for List: https://github.com/manno/cr-cache-test/blob/main/cachetest/cache_options_test.go#L65-L66

@manno manno changed the title DefaultNamespaces Cache Option doesn't allow All Namespaces DefaultNamespaces Cache Option doesn't allow All Namespaces for List Feb 28, 2024
oliviassss pushed a commit to kubernetes-sigs/aws-load-balancer-controller that referenced this issue May 24, 2024
…ernetes libs to v0.30.0 (#3707)

* go version, dep version

* refactor for controller-runtime udpate

* update tests, fix if statement

the controller-runtime strips DeletionTimestamp from manifests on
Create(). kubernetes-sigs/controller-runtime#2316

* refactor tests

* remove placeholder comments

* remove reconciler from WithOptions

* remove DefaultNamespace cache option

* remote `&& !hasGroupFinalizer`

This was causing e2e tests to fail when an ingress did not have the
group finalizer. The unit tests ing-1_been_deleted, and ing-6_been_deleted
will need reworked due to changes in the controller-runtime that cause
them to fail.

* update unit tests for ctrl client/fake >0.15

controller-runtime >=0.15 does not support creating (or adding the field via Update()) objects with a DeletionTimestamp. To work around this we add an annotation `unit-test/delete` to mark the ingresses that we want to test deletion. We check for this annotation and then call Delete(). This will set the DeletionTimestamp to the current date and time so we use IgnoreOtherFields to skip then comparing want to got.

relevant controller-runtime discussion/pr:

- kubernetes-sigs/controller-runtime#2184 (comment)
- kubernetes-sigs/controller-runtime#2316

* remove unused contexts

* add DefaultNamespaces cache config

* set opt.Cache.DefaultNamespaces conditionally

If WatchNamespace is set to corev1.NamespaceAll we should not set
DefaultNamespaces.

This code assumes that only one namespace is specified for
WatchNamespace. That decision was based on the help text for the flag
`watch-namespace`.

Related controller-runtime issue: kubernetes-sigs/controller-runtime#2628

* make crds

* set go to v1.22.3
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 28, 2024
oliviassss pushed a commit to oliviassss/aws-load-balancer-controller that referenced this issue May 30, 2024
…ernetes libs to v0.30.0 (kubernetes-sigs#3707)

* go version, dep version

* refactor for controller-runtime udpate

* update tests, fix if statement

the controller-runtime strips DeletionTimestamp from manifests on
Create(). kubernetes-sigs/controller-runtime#2316

* refactor tests

* remove placeholder comments

* remove reconciler from WithOptions

* remove DefaultNamespace cache option

* remote `&& !hasGroupFinalizer`

This was causing e2e tests to fail when an ingress did not have the
group finalizer. The unit tests ing-1_been_deleted, and ing-6_been_deleted
will need reworked due to changes in the controller-runtime that cause
them to fail.

* update unit tests for ctrl client/fake >0.15

controller-runtime >=0.15 does not support creating (or adding the field via Update()) objects with a DeletionTimestamp. To work around this we add an annotation `unit-test/delete` to mark the ingresses that we want to test deletion. We check for this annotation and then call Delete(). This will set the DeletionTimestamp to the current date and time so we use IgnoreOtherFields to skip then comparing want to got.

relevant controller-runtime discussion/pr:

- kubernetes-sigs/controller-runtime#2184 (comment)
- kubernetes-sigs/controller-runtime#2316

* remove unused contexts

* add DefaultNamespaces cache config

* set opt.Cache.DefaultNamespaces conditionally

If WatchNamespace is set to corev1.NamespaceAll we should not set
DefaultNamespaces.

This code assumes that only one namespace is specified for
WatchNamespace. That decision was based on the help text for the flag
`watch-namespace`.

Related controller-runtime issue: kubernetes-sigs/controller-runtime#2628

* make crds

* set go to v1.22.3
oliviassss added a commit to kubernetes-sigs/aws-load-balancer-controller that referenced this issue May 30, 2024
* update the traffic test for ingress (#3725)

* update the traffic test for ingress

* make crds

* prevent controller runtime complaining about SetupLogger() was never called (#3724)

* Update go to v1.22, controller-runtime dependency to v0.18.2, and kubernetes libs to v0.30.0 (#3707)

* go version, dep version

* refactor for controller-runtime udpate

* update tests, fix if statement

the controller-runtime strips DeletionTimestamp from manifests on
Create(). kubernetes-sigs/controller-runtime#2316

* refactor tests

* remove placeholder comments

* remove reconciler from WithOptions

* remove DefaultNamespace cache option

* remote `&& !hasGroupFinalizer`

This was causing e2e tests to fail when an ingress did not have the
group finalizer. The unit tests ing-1_been_deleted, and ing-6_been_deleted
will need reworked due to changes in the controller-runtime that cause
them to fail.

* update unit tests for ctrl client/fake >0.15

controller-runtime >=0.15 does not support creating (or adding the field via Update()) objects with a DeletionTimestamp. To work around this we add an annotation `unit-test/delete` to mark the ingresses that we want to test deletion. We check for this annotation and then call Delete(). This will set the DeletionTimestamp to the current date and time so we use IgnoreOtherFields to skip then comparing want to got.

relevant controller-runtime discussion/pr:

- kubernetes-sigs/controller-runtime#2184 (comment)
- kubernetes-sigs/controller-runtime#2316

* remove unused contexts

* add DefaultNamespaces cache config

* set opt.Cache.DefaultNamespaces conditionally

If WatchNamespace is set to corev1.NamespaceAll we should not set
DefaultNamespaces.

This code assumes that only one namespace is specified for
WatchNamespace. That decision was based on the help text for the flag
`watch-namespace`.

Related controller-runtime issue: kubernetes-sigs/controller-runtime#2628

* make crds

* set go to v1.22.3

* restrict resolve resolveViaVPCENIs to fargate only (#3709)

* Added helm envFrom value parameter for cluster-name (#3683)

* Added helm envFrom value parameter for cluster-name

* Update README.md file

* Add envFrom configuration to values.yaml

* Remove empty line in values.yaml

* feat: disable default helm labels (#3574)

* feat: disable helm labels

* fix: doc

* Update README.md (#3638)

Remove extra / in crds

---------

Co-authored-by: Luke Arntz <luke@blue42.net>
Co-authored-by: Omer Aplatony <61663422+omerap12@users.noreply.github.com>
Co-authored-by: Rémi BUISSON <remi-buisson@orange.fr>
Co-authored-by: Mike Wilson <hyperbolic2346@users.noreply.github.com>
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Jun 27, 2024
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

@k8s-ci-robot k8s-ci-robot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 27, 2024
@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

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-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Categorizes issue or PR as a support question. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

No branches or pull requests

5 participants