Skip to content

Commit

Permalink
Use installed namespace if WATCH_NAMESPACE not set
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Smith <[email protected]>
  • Loading branch information
joelsmith committed Sep 19, 2023
1 parent c39d74a commit cff3f84
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"runtime"
"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 All @@ -46,6 +47,10 @@ var (
setupLog = ctrl.Log.WithName("setup")
)

const (
serviceAccountNamespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

Expand Down Expand Up @@ -134,12 +139,20 @@ func main() {
}

// getWatchNamespace returns the namespace the operator should be watching for changes
// it tries to read this information from env variable `WATCH_NAMESPACE`
// if not set, namespace `keda` is used
// It tries to read this information from env variable `WATCH_NAMESPACE`. If not set
// or empty, it attempts to determine which namespace it is running in via the
// automounted service account data. If unavailable, namespace `keda` is used
func getWatchNamespace() string {
ns, found := os.LookupEnv("WATCH_NAMESPACE")
if !found {
return "keda"
var ns string
var found bool
if ns, found = os.LookupEnv("WATCH_NAMESPACE"); found && len(ns) > 0 {
setupLog.Info(fmt.Sprintf("Using watch namespace '%s' from environment variable WATCH_NAMESPACE", ns))
} else if nsBytes, err := os.ReadFile(serviceAccountNamespaceFile); err == nil {
ns = strings.TrimSpace(string(nsBytes))
setupLog.Info(fmt.Sprintf("Using watch namespace '%s' from service account namespace specified in %s", ns, serviceAccountNamespaceFile))
} else {
ns = "keda"
setupLog.Info(fmt.Sprintf("Using default watch namespace '%s'", ns))
}
return ns
}

0 comments on commit cff3f84

Please sign in to comment.