Skip to content

Commit

Permalink
Now namespaced with WATCH_NAMESPACE env
Browse files Browse the repository at this point in the history
  • Loading branch information
alb-car committed Jan 16, 2024
1 parent 8c2a204 commit 8f17b46
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 39 deletions.
49 changes: 36 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package main

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 All @@ -28,6 +30,7 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

Expand Down Expand Up @@ -65,25 +68,31 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
watchNamespace, err := getWatchNamespace()
if err != nil {
setupLog.Error(err, "unable to get WatchNamespace, "+
"the manager will watch and manage resources in all Namespaces")
}

options := ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "f09e1e85.scc-digitalhub.github.io",
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
// speeds up voluntary leader transitions as the new leader don't have to wait
// LeaseDuration time first.
//
// In the default scaffold provided, the program ends immediately after
// the manager stops, so would be fine to enable this option. However,
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
// LeaderElectionReleaseOnCancel: true,
})
Namespace: watchNamespace, // namespaced-scope when the value is not an empty string
}

// Add support for MultiNamespace set in WATCH_NAMESPACE (e.g ns1,ns2)
if strings.Contains(watchNamespace, ",") {
setupLog.Info("manager set up with multiple namespaces", "namespaces", watchNamespace)
// configure cluster-scoped with MultiNamespacedCacheBuilder
options.Namespace = ""
options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(watchNamespace, ","))
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
Expand Down Expand Up @@ -113,3 +122,17 @@ func main() {
os.Exit(1)
}
}

// getWatchNamespace returns the Namespace the operator should be watching for changes
func getWatchNamespace() (string, error) {
// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
// which specifies the Namespace to watch.
// 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 ns, nil
}
5 changes: 4 additions & 1 deletion config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ spec:
- /manager
args:
- --leader-elect
image: controller:latest
image: ghcr.io/scc-digitalhub/apigw-operator:latest
name: manager
env:
- name: WATCH_NAMESPACE
value: "apigw-operator-system"
securityContext:
allowPrivilegeEscalation: false
capabilities:
Expand Down
6 changes: 3 additions & 3 deletions config/rbac/role_binding.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
kind: RoleBinding
metadata:
labels:
app.kubernetes.io/name: clusterrolebinding
app.kubernetes.io/name: rolebinding
app.kubernetes.io/instance: manager-rolebinding
app.kubernetes.io/component: rbac
app.kubernetes.io/created-by: apigw-operator
Expand All @@ -11,7 +11,7 @@ metadata:
name: manager-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
kind: Role
name: manager-role
subjects:
- kind: ServiceAccount
Expand Down
60 changes: 38 additions & 22 deletions deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,15 @@ rules:
- patch
- update
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: apigw-operator-manager-role
rules:
- apiGroups:
- networking.k8s.io
resources:
- ingresses
verbs:
- watch
- create
- list
- delete
- apiGroups:
- operator.scc-digitalhub.github.io
resources:
Expand All @@ -199,30 +201,40 @@ rules:
- patch
- update
- apiGroups:
- networking.k8s.io
- ""
resources:
- ingresses
- services
verbs:
- watch
- create
- list
- delete

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: apigw-operator-manager-role
rules:
- apiGroups:
- ""
- operator.scc-digitalhub.github.io
resources:
- secrets
- apigws
verbs:
- watch
- create
- list
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
- operator.scc-digitalhub.github.io
resources:
- services
- apigws/status
verbs:
- watch
- list
- get
- patch
- update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
Expand Down Expand Up @@ -288,19 +300,20 @@ subjects:
namespace: apigw-operator-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
kind: RoleBinding
metadata:
labels:
app.kubernetes.io/component: rbac
app.kubernetes.io/created-by: apigw-operator
app.kubernetes.io/instance: manager-rolebinding
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: clusterrolebinding
app.kubernetes.io/name: rolebinding
app.kubernetes.io/part-of: apigw-operator
name: apigw-operator-manager-rolebinding
namespace: apigw-operator-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
kind: Role
name: apigw-operator-manager-role
subjects:
- kind: ServiceAccount
Expand Down Expand Up @@ -420,6 +433,9 @@ spec:
- --leader-elect
command:
- /manager
env:
- name: WATCH_NAMESPACE
value: apigw-operator-system
image: ghcr.io/scc-digitalhub/apigw-operator:latest
livenessProbe:
httpGet:
Expand Down

0 comments on commit 8f17b46

Please sign in to comment.