Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ spec:
installModes:
- supported: true
type: OwnNamespace
- supported: false
- supported: true
type: SingleNamespace
- supported: false
- supported: true
type: MultiNamespace
- supported: false
type: AllNamespaces
Expand Down
21 changes: 15 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"flag"
"fmt"
"os"
"strings"

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

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

defaultNamespaces := make(map[string]cache.Config)
for _, namespace := range watchNamespaces {
defaultNamespaces[namespace] = cache.Config{}
setupLog.Info(fmt.Sprintf("openstack-lightspeed operator watches %s namespace", namespace))
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsServerOptions,
Expand All @@ -141,7 +148,7 @@ func main() {
LeaderElection: enableLeaderElection,
LeaderElectionID: "c83b0a4f.lightspeed.openstack.org",
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{watchNamespace: {}},
DefaultNamespaces: defaultNamespaces,
},
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
Expand Down Expand Up @@ -188,16 +195,18 @@ func main() {
}
}

// getWatchNamespace returns the Namespace the operator should be watching for changes
func getWatchNamespace() (string, error) {
// getWatchNamespaces returns a list of namespaces the operator should be watching for changes.
// Note that this functions expects comma separated list in the WATCH_NAMESPACE env var.
func getWatchNamespaces() ([]string, error) {
// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
// which specifies the Namespace to watch.
Comment thread
umago marked this conversation as resolved.
// An empty value means the operator is running with cluster scope.
var watchNamespaceEnvVar = "WATCH_NAMESPACE"

ns, found := os.LookupEnv(watchNamespaceEnvVar)
if !found {
return "", fmt.Errorf("%s must be set", watchNamespaceEnvVar)
return []string{}, fmt.Errorf("%s must be set", watchNamespaceEnvVar)
}
return ns, nil

return strings.Split(ns, ","), nil
Comment thread
umago marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ spec:
installModes:
- supported: true
type: OwnNamespace
- supported: false
- supported: true
type: SingleNamespace
- supported: false
- supported: true
type: MultiNamespace
- supported: false
type: AllNamespaces
Expand Down
Loading