From dbc6802082857d04a00342b22a2bbc909e5ac6fc Mon Sep 17 00:00:00 2001 From: herinckc <31827186+herinckc@users.noreply.github.com> Date: Tue, 5 Jan 2021 08:52:32 -0800 Subject: [PATCH] Update operator to support the WATCH_NAMESPACE env var (#1474) * add support to the operator for the WATCH_NAMESPACE env var Signed-off-by: Colton Herinckx * update the changelog Signed-off-by: Colton Herinckx * address PR comments - add PR number and link to changelog Signed-off-by: Colton Herinckx --- CHANGELOG.md | 1 + main.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cb599fe433..369afd9b667 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - Override the vhost on a RabbitMQ scaler via `vhostName` in the metadata. ([#1451](https://github.com/kedacore/keda/pull/1451)) - Optimize Kafka scaler's `getLagForPartition` function. ([#1464](https://github.com/kedacore/keda/pull/1464)) - Reduce unnecessary /scale requests from ScaledObject controller ([#1453](https://github.com/kedacore/keda/pull/1453)) +- Add support for the WATCH_NAMESPACE environment variable to the operator ([#1474](https://github.com/kedacore/keda/pull/1474)) ### Breaking Changes diff --git a/main.go b/main.go index d768fdaeea6..99edd95a0f0 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,16 @@ func init() { // +kubebuilder:scaffold:scheme } +// getWatchNamespace returns the namespace the operator should be watching for changes +func getWatchNamespace() (string, error) { + const WatchNamespaceEnvVar = "WATCH_NAMESPACE" + ns, found := os.LookupEnv(WatchNamespaceEnvVar) + if !found { + return "", fmt.Errorf("%s must be set", WatchNamespaceEnvVar) + } + return ns, nil +} + func main() { var metricsAddr string var enableLeaderElection bool @@ -64,6 +74,12 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) setupLog := ctrl.Log.WithName("setup") + namespace, err := getWatchNamespace() + if err != nil { + setupLog.Error(err, "failed to get watch namespace") + os.Exit(1) + } + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, MetricsBindAddress: metricsAddr, @@ -71,6 +87,7 @@ func main() { Port: 9443, LeaderElection: enableLeaderElection, LeaderElectionID: "operator.keda.sh", + Namespace: namespace, }) if err != nil { setupLog.Error(err, "unable to start manager")