Skip to content

Commit

Permalink
Use installed namespace if WATCH_NAMESPACE not set
Browse files Browse the repository at this point in the history
Also, allow the operator to be installed in either OwnNamespace or
AllNamespaces installMode for better flexibility on where the operator
can be installed.

Signed-off-by: Joel Smith <[email protected]>
  • Loading branch information
joelsmith committed Sep 21, 2023
1 parent c39d74a commit cf77249
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ Only custom resource named `keda` in the namespace where the operator was
installed (typically, `keda`) will trigger the installation, reconfiguration,
or removal of the KEDA Controller resources.

The operator will behave in this manner whether it is installed with the
`AllNamespaces` or `OwnNamespace` install mode. While the operator more
closely matches the `OwnNamespace` semantics, `AllNamespaces` is a
supported installation mode to allow it to be installed to namespaces with
existing `OperatorGroups` which require that installation mode.

There should be only one KEDA Controller in the cluster.

### `KedaController` Spec
Expand Down Expand Up @@ -412,7 +418,7 @@ spec:
## Uninstallation

### How to uninstall KEDA Controller
Locate installed `KEDA` Operator in `keda` namespace and then remove created `KedaController` resoure or simply delete the `KedaController` resource:
Locate installed `KEDA` Operator in `keda` namespace and then remove created `KedaController` resource or simply delete the `KedaController` resource:

```bash
kubectl delete -n keda -f config/samples/keda_v1alpha1_kedacontroller.yaml
Expand Down
8 changes: 6 additions & 2 deletions bundle/manifests/keda.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,11 @@ spec:
`KedaController` resource, please refer to [KedaController Example](https://github.com/kedacore/keda-olm-operator#kedacontroller-spec)
for more deatils on available options.\n\nOnly resource named `keda` in the namespace
where the KEDA OLM Operator is installed (typically `keda`) will trigger the installation,
reconfiguration or removal of the KEDA Controller resource.\n\nThere could be
reconfiguration or removal of the KEDA Controller resource.\n\nThe operator will behave
in this manner whether it is installed with the AllNamespaces or OwnNamespace install
mode. While the operator more closely matches the OwnNamespace semantics, AllNamespaces
is a supported installation mode to allow it to be installed to namespaces with
existing OperatorGroups which require that installation mode.\n\nThere should be
only one KEDA Controller in the cluster. \n"
displayName: KEDA
icon:
Expand Down Expand Up @@ -630,7 +634,7 @@ spec:
type: SingleNamespace
- supported: false
type: MultiNamespace
- supported: false
- supported: true
type: AllNamespaces
keywords:
- keda
Expand Down
8 changes: 6 additions & 2 deletions config/manifests/bases/keda.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,11 @@ spec:
`KedaController` resource, please refer to [KedaController Example](https://github.com/kedacore/keda-olm-operator#kedacontroller-spec)
for more deatils on available options.\n\nOnly resource named `keda` in the namespace
where the KEDA OLM Operator is installed (typically `keda`) will trigger the installation,
reconfiguration or removal of the KEDA Controller resource.\n\nThere could be
reconfiguration or removal of the KEDA Controller resource.\n\nThe operator will behave
in this manner whether it is installed with the AllNamespaces or OwnNamespace install
mode. While the operator more closely matches the OwnNamespace semantics, AllNamespaces
is a supported installation mode to allow it to be installed to namespaces with
existing OperatorGroups which require that installation mode.\n\nThere should be
only one KEDA Controller in the cluster. \n"
displayName: KEDA
icon:
Expand Down Expand Up @@ -626,7 +630,7 @@ spec:
serviceAccountName: keda-olm-operator
strategy: deployment
installModes:
- supported: false
- supported: true
type: OwnNamespace
- supported: false
type: SingleNamespace
Expand Down
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 cf77249

Please sign in to comment.