Skip to content

Commit 9d30cd1

Browse files
Merge pull request #663 from Mahagon/fix_namespace_cache
global cache not used when watchNamespaces is empty in the helm values
2 parents 6dd6198 + 9b642bd commit 9d30cd1

3 files changed

Lines changed: 152 additions & 16 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ And then handle it accordingly in your monitor's implementation.
9696

9797
#### Examples
9898

99-
In [endpointmonitor_types.go](./pkg/apis/endpointmonitor/v1alpha1/endpointmonitor_types.go)
99+
In [endpointmonitor_types.go](./api/v1alpha1/endpointmonitor_types.go)
100100

101101
```yaml
102102
// UptimeRobotConfig defines the configuration for UptimeRobot Monitor Provider

‎cmd/main.go‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,17 @@ func main() {
142142
Metrics: metricsServerOptions,
143143
}
144144

145-
namespaces := []string{}
146145
// Add support for MultiNamespace set in WATCH_NAMESPACE (e.g ns1,ns2)
147146
// More Info: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/cache#MultiNamespacedCacheBuilder
148-
if strings.Contains(watchNamespace, ",") {
149-
setupLog.Info("Manager will be watching namespace(s) %q", watchNamespace)
150-
// configure cluster-scoped with MultiNamespacedCacheBuilder
151-
//options.Namespace = ""
152-
namespaces = strings.Split(watchNamespace, ",")
153-
} else {
154-
namespaces = []string{watchNamespace}
155-
}
156-
157-
options.NewCache = func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
158-
opts.DefaultNamespaces = map[string]cache.Config{}
159-
for _, ns := range namespaces {
160-
opts.DefaultNamespaces[ns] = cache.Config{}
147+
// Only restrict the cache when a non-empty WATCH_NAMESPACE is set. When empty, leave DefaultNamespaces nil
148+
// so the manager uses a global (all-namespace) cache. Setting DefaultNamespaces = {"": {}} would trigger
149+
// multiNamespaceCache whose List() does not fall back to the global informer for specific namespaces.
150+
if defaultNamespaces := buildDefaultNamespaces(watchNamespace); defaultNamespaces != nil {
151+
setupLog.Info("Manager will be watching namespace(s)", "namespaces", watchNamespace)
152+
options.NewCache = func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
153+
opts.DefaultNamespaces = defaultNamespaces
154+
return cache.New(config, opts)
161155
}
162-
return cache.New(config, opts)
163156
}
164157

165158
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
@@ -199,6 +192,26 @@ func main() {
199192
}
200193
}
201194

195+
// buildDefaultNamespaces returns the cache.Config map for the given comma-separated
196+
// watchNamespace value, or nil when watchNamespace is empty (cluster-scoped cache).
197+
func buildDefaultNamespaces(watchNamespace string) map[string]cache.Config {
198+
if watchNamespace == "" {
199+
return nil
200+
}
201+
result := map[string]cache.Config{}
202+
for _, ns := range strings.Split(watchNamespace, ",") {
203+
ns = strings.TrimSpace(ns)
204+
if ns == "" {
205+
continue
206+
}
207+
result[ns] = cache.Config{}
208+
}
209+
if len(result) == 0 {
210+
return nil
211+
}
212+
return result
213+
}
214+
202215
func getWatchNamespace() (string, error) {
203216
// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
204217
// which specifies the Namespace to watch.

‎cmd/main_test.go‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestBuildDefaultNamespaces(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
watchNamespace string
12+
wantNil bool
13+
wantKeys []string
14+
}{
15+
{
16+
name: "empty string returns nil (cluster-scoped cache)",
17+
watchNamespace: "",
18+
wantNil: true,
19+
},
20+
{
21+
name: "single namespace",
22+
watchNamespace: "my-namespace",
23+
wantKeys: []string{"my-namespace"},
24+
},
25+
{
26+
name: "multiple namespaces",
27+
watchNamespace: "ns1,ns2,ns3",
28+
wantKeys: []string{"ns1", "ns2", "ns3"},
29+
},
30+
{
31+
name: "namespaces with surrounding spaces are trimmed",
32+
watchNamespace: "ns1, ns2 , ns3",
33+
wantKeys: []string{"ns1", "ns2", "ns3"},
34+
},
35+
}
36+
37+
for _, tt := range tests {
38+
t.Run(tt.name, func(t *testing.T) {
39+
got := buildDefaultNamespaces(tt.watchNamespace)
40+
41+
if tt.wantNil {
42+
if got != nil {
43+
t.Errorf("expected nil, got %v", got)
44+
}
45+
return
46+
}
47+
48+
if got == nil {
49+
t.Fatal("expected non-nil map, got nil")
50+
}
51+
52+
if len(got) != len(tt.wantKeys) {
53+
t.Errorf("expected %d namespaces, got %d: %v", len(tt.wantKeys), len(got), got)
54+
}
55+
56+
for _, key := range tt.wantKeys {
57+
if _, ok := got[key]; !ok {
58+
t.Errorf("expected key %q in map, got %v", key, got)
59+
}
60+
}
61+
})
62+
}
63+
}
64+
65+
func TestGetWatchNamespace(t *testing.T) {
66+
tests := []struct {
67+
name string
68+
envVal string
69+
envSet bool
70+
wantNS string
71+
wantErr bool
72+
}{
73+
{
74+
name: "env var not set returns error",
75+
envSet: false,
76+
wantErr: true,
77+
},
78+
{
79+
name: "empty env var returns empty string (cluster-scoped)",
80+
envSet: true,
81+
envVal: "",
82+
wantNS: "",
83+
},
84+
{
85+
name: "single namespace",
86+
envSet: true,
87+
envVal: "my-namespace",
88+
wantNS: "my-namespace",
89+
},
90+
{
91+
name: "multiple namespaces passed through as-is",
92+
envSet: true,
93+
envVal: "ns1,ns2",
94+
wantNS: "ns1,ns2",
95+
},
96+
}
97+
98+
for _, tt := range tests {
99+
t.Run(tt.name, func(t *testing.T) {
100+
os.Unsetenv("WATCH_NAMESPACE")
101+
if tt.envSet {
102+
os.Setenv("WATCH_NAMESPACE", tt.envVal)
103+
defer os.Unsetenv("WATCH_NAMESPACE")
104+
}
105+
106+
got, err := getWatchNamespace()
107+
108+
if tt.wantErr {
109+
if err == nil {
110+
t.Error("expected error, got nil")
111+
}
112+
return
113+
}
114+
115+
if err != nil {
116+
t.Errorf("unexpected error: %v", err)
117+
}
118+
if got != tt.wantNS {
119+
t.Errorf("expected %q, got %q", tt.wantNS, got)
120+
}
121+
})
122+
}
123+
}

0 commit comments

Comments
 (0)