Skip to content

Commit 5f07d66

Browse files
committed
Add ability to watch multiple namespaces without watching all namespaces in a cluster.
1 parent ac3eff7 commit 5f07d66

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

cmd/postgres-operator/main.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strconv"
2424
"strings"
2525
"time"
26+
"unicode"
2627

2728
"go.opentelemetry.io/otel"
2829
"k8s.io/apimachinery/pkg/util/validation"
@@ -89,8 +90,29 @@ func initManager() (runtime.Options, error) {
8990
options.LeaderElectionNamespace = os.Getenv("PGO_NAMESPACE")
9091
}
9192

92-
if namespace := os.Getenv("PGO_TARGET_NAMESPACE"); len(namespace) > 0 {
93-
options.Cache.DefaultNamespaces = map[string]runtime.CacheConfig{namespace: {}}
93+
// Check PGO_TARGET_NAMESPACE for backwards compatibility with
94+
// "singlenamespace" installations
95+
singlenamespace := strings.TrimSpace(os.Getenv("PGO_TARGET_NAMESPACE"))
96+
97+
// Check PGO_TARGET_NAMESPACES for non-cluster-wide, multi-namespace
98+
// installations
99+
multinamespace := strings.TrimSpace(os.Getenv("PGO_TARGET_NAMESPACES"))
100+
101+
// Initialize DefaultNamespaces if any target namespaces are set
102+
if len(singlenamespace) > 0 || len(multinamespace) > 0 {
103+
options.Cache.DefaultNamespaces = map[string]runtime.CacheConfig{}
104+
}
105+
106+
if len(singlenamespace) > 0 {
107+
options.Cache.DefaultNamespaces[singlenamespace] = runtime.CacheConfig{}
108+
}
109+
110+
if len(multinamespace) > 0 {
111+
for _, namespace := range strings.FieldsFunc(multinamespace, func(c rune) bool {
112+
return c != '-' && !unicode.IsLetter(c) && !unicode.IsNumber(c)
113+
}) {
114+
options.Cache.DefaultNamespaces[namespace] = runtime.CacheConfig{}
115+
}
94116
}
95117

96118
options.Controller.GroupKindConcurrency = map[string]int{

cmd/postgres-operator/main_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,19 @@ func TestInitManager(t *testing.T) {
8686
assert.Assert(t, cmp.Len(options.Cache.DefaultNamespaces, 1),
8787
"expected only one configured namespace")
8888

89-
for k := range options.Cache.DefaultNamespaces {
90-
assert.Equal(t, k, "some-such")
91-
}
89+
assert.Assert(t, cmp.Contains(options.Cache.DefaultNamespaces, "some-such"))
90+
})
91+
92+
t.Run("PGO_TARGET_NAMESPACES", func(t *testing.T) {
93+
t.Setenv("PGO_TARGET_NAMESPACES", "some-such,another-one")
94+
95+
options, err := initManager()
96+
assert.NilError(t, err)
97+
assert.Assert(t, cmp.Len(options.Cache.DefaultNamespaces, 2),
98+
"expect two configured namespaces")
99+
100+
assert.Assert(t, cmp.Contains(options.Cache.DefaultNamespaces, "some-such"))
101+
assert.Assert(t, cmp.Contains(options.Cache.DefaultNamespaces, "another-one"))
92102
})
93103

94104
t.Run("PGO_WORKERS", func(t *testing.T) {

0 commit comments

Comments
 (0)