added watch namespaces support

This commit is contained in:
Safwan
2026-07-14 13:09:14 +05:00
parent 0cfbe10125
commit 0ef47cdc4d
7 changed files with 270 additions and 26 deletions
+19 -9
View File
@@ -45,6 +45,20 @@ func AddOptionalSchemes(argoRolloutsEnabled, deploymentConfigEnabled, csiEnabled
}
}
// buildDefaultNamespaces returns the controller-runtime cache namespace scoping
// for the given watched namespaces. An empty input yields nil, meaning the cache
// watches all namespaces.
func buildDefaultNamespaces(namespaces []string) map[string]cache.Config {
if len(namespaces) == 0 {
return nil
}
out := make(map[string]cache.Config, len(namespaces))
for _, ns := range namespaces {
out[ns] = cache.Config{}
}
return out
}
// ManagerOptions contains options for creating a new Manager.
type ManagerOptions struct {
Config *config.Config
@@ -79,13 +93,11 @@ func NewManager(opts ManagerOptions) (ctrl.Manager, error) {
RetryPeriod: &le.RetryPeriod,
}
if cfg.WatchedNamespace != "" {
if nsScope := buildDefaultNamespaces(cfg.WatchedNamespaces); nsScope != nil {
mgrOpts.Cache = cache.Options{
DefaultNamespaces: map[string]cache.Config{
cfg.WatchedNamespace: {},
},
DefaultNamespaces: nsScope,
}
opts.Log.Info("namespace filtering enabled", "namespace", cfg.WatchedNamespace)
opts.Log.Info("namespace filtering enabled", "namespaces", cfg.WatchedNamespaces)
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), mgrOpts)
@@ -130,11 +142,9 @@ func NewManagerWithRestConfig(opts ManagerOptions, restConfig *rest.Config) (ctr
RetryPeriod: &le.RetryPeriod,
}
if cfg.WatchedNamespace != "" {
if nsScope := buildDefaultNamespaces(cfg.WatchedNamespaces); nsScope != nil {
mgrOpts.Cache = cache.Options{
DefaultNamespaces: map[string]cache.Config{
cfg.WatchedNamespace: {},
},
DefaultNamespaces: nsScope,
}
}
+19
View File
@@ -9,6 +9,25 @@ import (
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
)
func TestBuildDefaultNamespaces(t *testing.T) {
if got := buildDefaultNamespaces(nil); got != nil {
t.Errorf("empty input should return nil, got %v", got)
}
if got := buildDefaultNamespaces([]string{}); got != nil {
t.Errorf("empty slice should return nil, got %v", got)
}
got := buildDefaultNamespaces([]string{"team-a", "team-b", "team-c"})
if len(got) != 3 {
t.Fatalf("expected 3 entries, got %d", len(got))
}
for _, ns := range []string{"team-a", "team-b", "team-c"} {
if _, ok := got[ns]; !ok {
t.Errorf("missing namespace %q in %v", ns, got)
}
}
}
func TestAddOptionalSchemesRegistersCSI(t *testing.T) {
// Reset to a clean scheme for the test.
runtimeScheme = runtime.NewScheme()