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: Watch Namespaces based on labels and label selectors #9976

Closed
wants to merge 123 commits into from

Conversation

davidjumani
Copy link
Contributor

@davidjumani davidjumani commented Aug 30, 2024

Description

The watchNamespaces setting allows users to restrict the namespaces in which Edge watches resources. Since this is a static list, users need to update it when they need to modify namespaces to watch
This feature aims to dynamically determine the list of namespaces to watch by defining labels on namespaces or filtering namespaces based on a label expression.

This feature identifies namespaces with label selectors. These can be :

  • Matching labels : All namespaces that contain a predefined list of labels will be added to the list of namespaces to watch. Eg.: All namespaces labelled with gloo-translate=enabled will be watched
  • Matching expressions : All namespaces that contain labels that match a given criteria will be watched. Eg.: All namespaces are labelled with environment in (prod, dev) will be watched

This will introduce a new setting to select namespaces :

apiVersion: gloo.solo.io/v1
kind: Settings
metadata:
  name: default
spec:
  watchNamespaceSelectors:
  - matchLabels:
      gloo-translate: enabled
  - matchExpressions:
    - key: env
      operator: In
      values:
        - prod
        - dev

API changes

  • Added the watchNamespaceSelectors to the settings CRC

Code changes

Added a KubeNamespaceWatcher to the setup snapshot emitter. This will trigger a new snapshot if any namespace has been created / deleted / modified and not necessarily if the namespace belongs to the list we watch. The syncer will determine whether to sync based on whether :

  • The settings CR has changed
  • The namespaces we watch has chagned

Context

Watch Namespaces based on labels and label selectors
Design doc

Interesting decisions

Testing steps

Added kubernetes e2e tests

Notes for reviewers

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works

@davidjumani davidjumani requested a review from a team as a code owner August 30, 2024 14:18
@github-actions github-actions bot added keep pr updated signals bulldozer to keep pr up to date with base branch work in progress signals bulldozer to keep pr open (don't auto-merge) labels Aug 30, 2024
@@ -1,11 +1,10 @@
package clients
package vault
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved this file to prevent an import cycle

Copy link

github-actions bot commented Aug 30, 2024

Visit the preview URL for this PR (updated for commit 19bcb53):

https://gloo-edge--pr9976-watch-namespace-sele-twteibok.web.app

(expires Tue, 01 Oct 2024 18:58:43 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 77c2b86e287749579b7ff9cadb81e099042ef677

@solo-changelog-bot
Copy link

Issues linked to changelog:
solo-io#9274

@davidjumani davidjumani changed the title feat: Watch Namespaces based on labels and label selectors [WIP] feat: Watch Namespaces based on labels and label selectors Aug 30, 2024
Comment on lines 210 to 219
func HandleResourceDeletion(snapshot *v1snap.ApiSnapshot, resource resources.Resource) error {
if _, ok := resource.(*sk_kubernetes.KubeNamespace); ok {
// Special case to handle namespace deletion
snapshot.RemoveAllResourcesInNamespace(resource.GetMetadata().GetName())
return nil
} else {
return snapshot.RemoveFromResourceList(resource)
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would change to

diff --git a/projects/gloo/pkg/validation/server.go b/projects/gloo/pkg/validation/server.go
index 7621d7433..630086eae 100644
--- a/projects/gloo/pkg/validation/server.go
+++ b/projects/gloo/pkg/validation/server.go
@@ -210,7 +210,9 @@ func (s *validator) Validate(ctx context.Context, req *validation.GlooValidation
 func HandleResourceDeletion(snapshot *v1snap.ApiSnapshot, resource resources.Resource) error {
        if _, ok := resource.(*sk_kubernetes.KubeNamespace); ok {
                // Special case to handle namespace deletion
-               snapshot.RemoveAllResourcesInNamespace(resource.GetMetadata().GetName())
+               snapshot.RemoveMatches(func(metadata *core.Metadata) bool {
+                       return resource.GetMetadata().GetNamespace() == metadata.GetNamespace()
+               })
                return nil
        } else {
                return snapshot.RemoveFromResourceList(resource)

after the solo-kit bump

.github/workflows/pr-kubernetes-tests.yaml Outdated Show resolved Hide resolved
pkg/utils/namespaces/namespaces.go Outdated Show resolved Hide resolved

resp, err := clientset.AuthorizationV1().SelfSubjectAccessReviews().Create(ctx, &selfCheck, metav1.CreateOptions{})
if err != nil {
return &FakeKubeNamespaceWatcher{}
Copy link
Contributor

Choose a reason for hiding this comment

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

For example, if we failed here and just silently returned a no-op, it would not be clear to a user or dev that we may be running into some RBAC issues. Returning a nil client and an error would lead to better debugging I think

pkg/utils/setuputils/setup_syncer.go Outdated Show resolved Hide resolved
Comment on lines 445 to 446
// Kubernetes' Secrets and namespace deletions are the only non-Solo API resource operations we support validation requests for.
// For a these resources, we want to skip validation on operations we don't support. We only support DELETEs.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Kubernetes' Secrets and namespace deletions are the only non-Solo API resource operations we support validation requests for.
// For a these resources, we want to skip validation on operations we don't support. We only support DELETEs.
// Kubernetes Secrets and Namespace are the only non-Solo API resources we support validation requests for.
// For a these resources, we want to skip validation on operations we don't support. For Namespaces, we
// support DELETE and UPDATE. For Secrets, we support DELETE.

@@ -49,6 +49,8 @@ type BaseTestingSuite struct {

func NewBaseTestingSuite(ctx context.Context, testInst *e2e.TestInstallation, testHelper *helper.SoloTestHelper, setup SimpleTestCase, testCase map[string]*TestCase) *BaseTestingSuite {
namespace = testInst.Metadata.InstallNamespace
ctx = context.WithValue(ctx, "namespace", namespace)
Copy link
Contributor

Choose a reason for hiding this comment

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

The provider has a reference to the gloogateway.Context which has the InstallationNamespace

@davidjumani davidjumani changed the title [WIP] feat: Watch Namespaces based on labels and label selectors feat: Watch Namespaces based on labels and label selectors Sep 20, 2024
@davidjumani
Copy link
Contributor Author

closing in favour of solo-io#10104

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keep pr updated signals bulldozer to keep pr up to date with base branch work in progress signals bulldozer to keep pr open (don't auto-merge)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants