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(k8s): watch specific namespaces (PoC) #12877

Closed
wants to merge 14 commits into from

Conversation

lukidzi
Copy link
Contributor

@lukidzi lukidzi commented Feb 17, 2025

Motivation

By default, Kuma watches for changes across all namespaces. This simplifies the initial user experience, making it easier to get started. However, at some point, users may want to restrict the information available to the control plane to enhance security, or reduce unnecessary resource consumption.

Implementation information

This feature requires modifying permissions across multiple components:

  1. Extracting ClusterRole options into Role
  2. Restricting webhooks to operate only within specific namespaces
  3. Kubernetes manager
  4. Event listener

If watchNamespaces is set:

Step 1:

I moved most namespace-scoped resources to separate Role objects, which are created in watchNamespaces and must be set up by the user before deployment. Instead of using only a ClusterRole, we now create a ClusterRole with a more limited scope, a Role in the system namespace, and Roles in each user-provided namespace.

Step 2:

In Helm, we validate whether the user has provided specific namespaces. If so, we configure webhooks to only monitor those namespaces along with the systemNamespace.

Step 3:

We need to define a defaultNamespaces parameter for the Kubernetes cache. This cache retrieves information from the Kubernetes API and, by default, fetches resources from all namespaces. This change introduces a way to limit requests to specific namespaces: watchNamespaces, systemNamespace, and the CNI namespace.

Additionally, in many parts of the code, we perform .List operations, which require a namespace to be specified. Instead of adding the namespace to each request individually, I introduced a wrapper that iterates over and requests resources from each watched namespace.

Since some resources are cluster-scoped, I also introduced a way to check whether a resource is cluster-scoped. If it is, the request is made for all resources. Because defaultNamespaces configures the default informers in the manager, we don't need to define predicates for each controller.

Step 4:

We create separate informers using the Kubernetes client, which fetches resources differently from the default cache. Since there is no built-in option to list resources across multiple namespaces, we need to create an informer for each namespace.

Supporting documentation

https://medium.com/@timebertt/kubernetes-controllers-at-scale-clients-caches-conflicts-patches-explained-aa0f7a8b4332

xref: https://github.com/Kong/kong-mesh/issues/7408

Signed-off-by: Lukasz Dziedziak <[email protected]>
Signed-off-by: Lukasz Dziedziak <[email protected]>
Signed-off-by: Lukasz Dziedziak <[email protected]>
Signed-off-by: Lukasz Dziedziak <[email protected]>
@lukidzi lukidzi added ci/skip-test PR: Don't run unit and e2e tests (maybe this is just a doc change) ci/skip-e2e-test PR: Don't run e2e tests labels Feb 17, 2025
Copy link
Contributor

Reviewer Checklist

🔍 Each of these sections need to be checked by the reviewer of the PR 🔍:
If something doesn't apply please check the box and add a justification if the reason is non obvious.

  • Is the PR title satisfactory? Is this part of a larger feature and should be grouped using > Changelog?
  • PR description is clear and complete. It Links to relevant issue as well as docs and UI issues
  • This will not break child repos: it doesn't hardcode values (.e.g "kumahq" as an image registry)
  • IPv6 is taken into account (.e.g: no string concatenation of host port)
  • Tests (Unit test, E2E tests, manual test on universal and k8s)
    • Don't forget ci/ labels to run additional/fewer tests
  • Does this contain a change that needs to be notified to users? In this case, UPGRADE.md should be updated.
  • Does it need to be backported according to the backporting policy? (this GH action will add "backport" label based on these file globs, if you want to prevent it from adding the "backport" label use no-backport-autolabel label)

Signed-off-by: Lukasz Dziedziak <[email protected]>
Signed-off-by: Lukasz Dziedziak <[email protected]>
Signed-off-by: Lukasz Dziedziak <[email protected]>
Signed-off-by: Lukasz Dziedziak <[email protected]>
Signed-off-by: Lukasz Dziedziak <[email protected]>
Signed-off-by: Lukasz Dziedziak <[email protected]>
Signed-off-by: Lukasz Dziedziak <[email protected]>
@lukidzi lukidzi added ci/run-full-matrix PR: Runs all possible e2e test combination (expensive use carefully) and removed ci/skip-test PR: Don't run unit and e2e tests (maybe this is just a doc change) ci/skip-e2e-test PR: Don't run e2e tests labels Feb 18, 2025
Signed-off-by: Lukasz Dziedziak <[email protected]>
@lukidzi lukidzi marked this pull request as ready for review February 18, 2025 21:23
@lukidzi lukidzi requested a review from a team as a code owner February 18, 2025 21:23
@lukidzi lukidzi requested review from slonka and Automaat February 18, 2025 21:23
@lukidzi lukidzi marked this pull request as draft February 19, 2025 13:15
@lukidzi lukidzi changed the title feat(k8s): watch specific namespaces feat(k8s): watch specific namespaces (PoC) Feb 19, 2025
@lukidzi
Copy link
Contributor Author

lukidzi commented Feb 20, 2025

Closing, let's reopen when it's required

@lukidzi lukidzi closed this Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci/run-full-matrix PR: Runs all possible e2e test combination (expensive use carefully)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant