Skip to content

Commit 37f1651

Browse files
Merge pull request #91 from lpiwowar/lpiwowar/multi-namespace
Enable multi-namespace support
2 parents becbafb + 38af111 commit 37f1651

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

bundle/manifests/openstack-lightspeed-operator.clusterserviceversion.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ spec:
312312
installModes:
313313
- supported: true
314314
type: OwnNamespace
315-
- supported: false
315+
- supported: true
316316
type: SingleNamespace
317-
- supported: false
317+
- supported: true
318318
type: MultiNamespace
319319
- supported: false
320320
type: AllNamespaces

cmd/main.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"flag"
2222
"fmt"
2323
"os"
24+
"strings"
2425

2526
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2627
// to ensure that exec-entrypoint and run can make use of them.
@@ -126,13 +127,19 @@ func main() {
126127
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
127128
}
128129

129-
watchNamespace, err := getWatchNamespace()
130+
watchNamespaces, err := getWatchNamespaces()
130131
if err != nil {
131132
setupLog.Error(err, "unable to get WatchNamespace, "+
132133
"the manager will watch and manage resources in all namespaces")
133134
os.Exit(1)
134135
}
135136

137+
defaultNamespaces := make(map[string]cache.Config)
138+
for _, namespace := range watchNamespaces {
139+
defaultNamespaces[namespace] = cache.Config{}
140+
setupLog.Info(fmt.Sprintf("openstack-lightspeed operator watches %s namespace", namespace))
141+
}
142+
136143
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
137144
Scheme: scheme,
138145
Metrics: metricsServerOptions,
@@ -141,7 +148,7 @@ func main() {
141148
LeaderElection: enableLeaderElection,
142149
LeaderElectionID: "c83b0a4f.lightspeed.openstack.org",
143150
Cache: cache.Options{
144-
DefaultNamespaces: map[string]cache.Config{watchNamespace: {}},
151+
DefaultNamespaces: defaultNamespaces,
145152
},
146153
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
147154
// when the Manager ends. This requires the binary to immediately end when the
@@ -188,16 +195,18 @@ func main() {
188195
}
189196
}
190197

191-
// getWatchNamespace returns the Namespace the operator should be watching for changes
192-
func getWatchNamespace() (string, error) {
198+
// getWatchNamespaces returns a list of namespaces the operator should be watching for changes.
199+
// Note that this functions expects comma separated list in the WATCH_NAMESPACE env var.
200+
func getWatchNamespaces() ([]string, error) {
193201
// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
194202
// which specifies the Namespace to watch.
195203
// An empty value means the operator is running with cluster scope.
196204
var watchNamespaceEnvVar = "WATCH_NAMESPACE"
197205

198206
ns, found := os.LookupEnv(watchNamespaceEnvVar)
199207
if !found {
200-
return "", fmt.Errorf("%s must be set", watchNamespaceEnvVar)
208+
return []string{}, fmt.Errorf("%s must be set", watchNamespaceEnvVar)
201209
}
202-
return ns, nil
210+
211+
return strings.Split(ns, ","), nil
203212
}

config/manifests/bases/openstack-lightspeed-operator.clusterserviceversion.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ spec:
7878
installModes:
7979
- supported: true
8080
type: OwnNamespace
81-
- supported: false
81+
- supported: true
8282
type: SingleNamespace
83-
- supported: false
83+
- supported: true
8484
type: MultiNamespace
8585
- supported: false
8686
type: AllNamespaces

0 commit comments

Comments
 (0)