Skip to content
This repository was archived by the owner on May 24, 2023. It is now read-only.

Commit f8ef1e0

Browse files
authored
Add support for watching namespaces (#178)
1 parent e1de434 commit f8ef1e0

8 files changed

+59
-13
lines changed

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/manifests/k8s.nginx.org_nginxingresscontrollers.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: apiextensions.k8s.io/v1
22
kind: CustomResourceDefinition
33
metadata:
44
annotations:
5-
controller-gen.kubebuilder.io/version: v0.4.1
5+
controller-gen.kubebuilder.io/version: v0.6.1
66
creationTimestamp: null
77
name: nginxingresscontrollers.k8s.nginx.org
88
spec:

bundle/manifests/nginx-ingress-operator.clusterserviceversion.yaml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ spec:
346346
- --upstream=http://127.0.0.1:8080/
347347
- --logtostderr=true
348348
- --v=10
349-
image: registry.redhat.io/openshift4/ose-kube-rbac-proxy:v4.7
349+
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.8.0
350350
name: kube-rbac-proxy
351351
ports:
352352
- containerPort: 8443
@@ -359,7 +359,12 @@ spec:
359359
- --leader-elect
360360
command:
361361
- /manager
362-
image: registry.connect.redhat.com/nginx/nginx-ingress-operator:0.4.0
362+
env:
363+
- name: WATCH_NAMESPACE
364+
valueFrom:
365+
fieldRef:
366+
fieldPath: metadata.annotations['olm.targetNamespaces']
367+
image: nginx/nginx-ingress-operator:0.4.0
363368
livenessProbe:
364369
httpGet:
365370
path: /healthz
@@ -422,11 +427,11 @@ spec:
422427
serviceAccountName: nginx-ingress-operator-controller-manager
423428
strategy: deployment
424429
installModes:
425-
- supported: false
430+
- supported: true
426431
type: OwnNamespace
427-
- supported: false
432+
- supported: true
428433
type: SingleNamespace
429-
- supported: false
434+
- supported: true
430435
type: MultiNamespace
431436
- supported: true
432437
type: AllNamespaces

config/crd/bases/k8s.nginx.org_nginxingresscontrollers.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
44
kind: CustomResourceDefinition
55
metadata:
66
annotations:
7-
controller-gen.kubebuilder.io/version: v0.4.1
7+
controller-gen.kubebuilder.io/version: v0.6.1
88
creationTimestamp: null
99
name: nginxingresscontrollers.k8s.nginx.org
1010
spec:

config/default/manager_auth_proxy_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ spec:
1010
spec:
1111
containers:
1212
- name: kube-rbac-proxy
13-
image: registry.redhat.io/openshift4/ose-kube-rbac-proxy:v4.7
13+
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.8.0
1414
args:
1515
- "--secure-listen-address=0.0.0.0:8443"
1616
- "--upstream=http://127.0.0.1:8080/"

config/manager/manager.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,8 @@ spec:
5252
requests:
5353
cpu: 250m
5454
memory: 64Mi
55+
env:
56+
- name: WATCH_NAMESPACE
57+
value: ""
5558
serviceAccountName: controller-manager
5659
terminationGracePeriodSeconds: 10

config/manifests/bases/nginx-ingress-operator.clusterserviceversion.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ spec:
162162
deployments: null
163163
strategy: ""
164164
installModes:
165-
- supported: false
165+
- supported: true
166166
type: OwnNamespace
167-
- supported: false
167+
- supported: true
168168
type: SingleNamespace
169-
- supported: false
169+
- supported: true
170170
type: MultiNamespace
171171
- supported: true
172172
type: AllNamespaces

main.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323
runt "runtime"
24+
"strings"
2425

2526
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2627
// to ensure that exec-entrypoint and run can make use of them.
@@ -31,6 +32,7 @@ import (
3132
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3233
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3334
ctrl "sigs.k8s.io/controller-runtime"
35+
"sigs.k8s.io/controller-runtime/pkg/cache"
3436
"sigs.k8s.io/controller-runtime/pkg/healthz"
3537
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3638

@@ -97,14 +99,35 @@ func main() {
9799

98100
printVersion()
99101

100-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
102+
watchNamespace, err := getWatchNamespace()
103+
if err != nil {
104+
setupLog.Error(err, "unable to get WatchNamespace, "+
105+
"the manager will watch and manage resources in all Namespaces")
106+
}
107+
108+
options := ctrl.Options{
101109
Scheme: scheme,
102110
MetricsBindAddress: metricsAddr,
103111
Port: 9443,
104112
HealthProbeBindAddress: probeAddr,
105113
LeaderElection: enableLeaderElection,
106114
LeaderElectionID: "ca5c10a7.nginx.org",
107-
})
115+
Namespace: watchNamespace,
116+
}
117+
118+
// Add support for MultiNamespace set in WATCH_NAMESPACE (e.g ns1,ns2)
119+
if strings.Contains(watchNamespace, ",") {
120+
setupLog.Info("manager set up with multiple namespaces", "namespaces", watchNamespace)
121+
// configure cluster-scoped with MultiNamespacedCacheBuilder
122+
options.Namespace = ""
123+
options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(watchNamespace, ","))
124+
}
125+
126+
if options.Namespace != "" {
127+
setupLog.Info("manager set up with namespace", "namespace", options.Namespace)
128+
}
129+
130+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
108131
if err != nil {
109132
setupLog.Error(err, "unable to start manager")
110133
os.Exit(1)
@@ -146,3 +169,17 @@ func main() {
146169
os.Exit(1)
147170
}
148171
}
172+
173+
// getWatchNamespace returns the Namespace the operator should be watching for changes
174+
func getWatchNamespace() (string, error) {
175+
// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
176+
// which specifies the Namespace to watch.
177+
// An empty value means the operator is running with cluster scope.
178+
watchNamespaceEnvVar := "WATCH_NAMESPACE"
179+
180+
ns, found := os.LookupEnv(watchNamespaceEnvVar)
181+
if !found {
182+
return "", fmt.Errorf("%s must be set", watchNamespaceEnvVar)
183+
}
184+
return ns, nil
185+
}

0 commit comments

Comments
 (0)