Skip to content

Commit f0316fa

Browse files
authored
Enhance the Helm chart to make it easier to install the Operator into, and monitor a single namespace (#587)
1 parent 4dad097 commit f0316fa

File tree

5 files changed

+107
-7
lines changed

5 files changed

+107
-7
lines changed

docs/installation/01_installation.adoc

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,45 @@ To set the watch namespaces when installing with helm set the `watchNamespaces`
301301
----
302302
helm install \
303303
--namespace <namespace> \
304-
--set watchNamespaces=payments,catalog,customers <1>
304+
--set watchNamespaces=payments,catalog,customers \
305+
coherence-operator \
306+
coherence/coherence-operator
307+
----
308+
309+
The `payments`, `catalog` and `customers` namespaces will be watched by the Operator.
310+
311+
==== Set the Watch Namespace to the Operator's Install Namespace
312+
313+
When installing the Operator using the Helm chart, there is a convenience value that can be set if the
314+
Operator should only monitor the same namespace that it is installed into.
315+
By setting the `onlySameNamespace` value to `true` the watch namespace will be set to the installation namespace.
316+
If the `onlySameNamespace` value is set to `true` then any value set for the `watchNamespaces` value will be ignored.
317+
318+
For example, the command below will set `onlySameNamespace` to true, and the Operator will be installed into,
319+
and only monitor the `coh-testing` namespace.
320+
321+
[source,bash]
322+
----
323+
helm install \
324+
--namespace coh-testing \
325+
--set onlySameNamespace=true \
326+
coherence-operator \
327+
coherence/coherence-operator
328+
----
329+
330+
In the example below, the `onlySameNamespace` is set to true, so the Operator will be installed into,
331+
and only monitor the `coh-testing` namespace. Even though the `watchNamespaces` value is set, it will be ignored.
332+
333+
[source,bash]
334+
----
335+
helm install \
336+
--namespace coh-testing \
337+
--set watchNamespaces=payments,catalog,customers \
338+
--set onlySameNamespace=true \
305339
coherence-operator \
306340
coherence/coherence-operator
307341
----
308342
309-
<1> The `payments`, `catalog` and `customers` namespaces will be watched by the Operator.
310343
311344
== Set the Operator Image
312345

helm-charts/coherence-operator/templates/deployment.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,14 @@ spec:
132132
- name: OPERATOR_IMAGE
133133
value: {{ .Values.defaultCoherenceUtilsImage | quote }}
134134
- name: WATCH_NAMESPACE
135-
{{- if .Values.clusterRoles }}
136-
value: {{ .Values.watchNamespaces | quote }}
135+
{{- if .Values.onlySameNamespace }}
136+
value: {{ .Release.Namespace | quote }}
137137
{{- else }}
138+
{{- if .Values.clusterRoles }}
139+
value: {{ .Values.watchNamespaces | quote }}
140+
{{- else }}
138141
value: {{ .Release.Namespace | quote }}
142+
{{- end }}
139143
{{- end }}
140144
image: {{ .Values.image }}
141145
ports:

helm-charts/coherence-operator/values.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ livenessProbe:
155155
# ref: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
156156
resources:
157157

158+
# onlySameNamespace is a flag to indicate that the Operator should only monitor and control
159+
# Coherence clusters in the same namespace that it is installed into.
160+
# If this flag is set to true, any watchNamespaces value will be ignored, as this
161+
# will automatically be set to the same namespace the Operator is installed into.
162+
# The default is false, the Operator will monitor all namespaces
163+
onlySameNamespace: false
164+
158165
# clusterRoles controls whether the Helm chart will create RBAC ClusterRole and bindings for the Operator
159166
# These are required if the Operator will watch multiple namespaces.
160167
# If set to false then the Operator will only watch the namespace it is deployed into.

pkg/operator/operator.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates.
2+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* http://oss.oracle.com/licenses/upl.
55
*/
@@ -73,6 +73,9 @@ const (
7373
FlagWebhookSecret = "webhook-secret"
7474
FlagWebhookService = "webhook-service"
7575

76+
// EnvVarWatchNamespace is the environment variable to use to set the watch namespace(s)
77+
EnvVarWatchNamespace = "WATCH_NAMESPACE"
78+
7679
// OCI Node Labels
7780

7881
// LabelOciNodeFaultDomain is the OCI Node label for the fault domain.
@@ -385,10 +388,9 @@ func GetWatchNamespace() []string {
385388
// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
386389
// which specifies the Namespace to watch.
387390
// An empty value means the operator is running with cluster scope.
388-
var watchNamespaceEnvVar = "WATCH_NAMESPACE"
389391
var watches []string
390392

391-
ns, found := os.LookupEnv(watchNamespaceEnvVar)
393+
ns, found := os.LookupEnv(EnvVarWatchNamespace)
392394
if !found || ns == "" || strings.TrimSpace(ns) == "" {
393395
return watches
394396
}

test/e2e/helm/helm_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
coh "github.com/oracle/coherence-operator/api/v1"
1414
"github.com/oracle/coherence-operator/pkg/operator"
1515
"github.com/oracle/coherence-operator/test/e2e/helper"
16+
"github.com/oracle/coherence-operator/test/e2e/helper/matchers"
1617
appsv1 "k8s.io/api/apps/v1"
1718
corev1 "k8s.io/api/core/v1"
1819
"k8s.io/apimachinery/pkg/api/errors"
@@ -140,6 +141,59 @@ func TestDisableWebhooks(t *testing.T) {
140141
g.Expect(c.Args).Should(ContainElements("operator", "--enable-leader-election", "--enable-webhook=false"))
141142
}
142143

144+
func TestSetOnlySameNamespace(t *testing.T) {
145+
g := NewGomegaWithT(t)
146+
result, err := helmInstall("--set", "onlySameNamespace=true")
147+
g.Expect(err).NotTo(HaveOccurred())
148+
g.Expect(result).NotTo(BeNil())
149+
150+
dep := &appsv1.Deployment{}
151+
err = result.Get("coherence-operator", dep)
152+
g.Expect(err).NotTo(HaveOccurred())
153+
154+
c := findContainer("manager", dep)
155+
g.Expect(c).NotTo(BeNil())
156+
157+
ns := helper.GetTestNamespace()
158+
g.Expect(c.Env).NotTo(BeNil())
159+
g.Expect(c.Env).To(matchers.HaveEnvVar(corev1.EnvVar{Name: operator.EnvVarWatchNamespace, Value: ns}))
160+
}
161+
162+
func TestSetOnlySameNamespaceIgnoresWatchNamespaces(t *testing.T) {
163+
g := NewGomegaWithT(t)
164+
result, err := helmInstall("--set", "watchNamespaces=foo", "--set", "onlySameNamespace=true")
165+
g.Expect(err).NotTo(HaveOccurred())
166+
g.Expect(result).NotTo(BeNil())
167+
168+
dep := &appsv1.Deployment{}
169+
err = result.Get("coherence-operator", dep)
170+
g.Expect(err).NotTo(HaveOccurred())
171+
172+
c := findContainer("manager", dep)
173+
g.Expect(c).NotTo(BeNil())
174+
175+
ns := helper.GetTestNamespace()
176+
g.Expect(c.Env).NotTo(BeNil())
177+
g.Expect(c.Env).To(matchers.HaveEnvVar(corev1.EnvVar{Name: operator.EnvVarWatchNamespace, Value: ns}))
178+
}
179+
180+
func TestSetWatchNamespaces(t *testing.T) {
181+
g := NewGomegaWithT(t)
182+
result, err := helmInstall("--set", "watchNamespaces=foo")
183+
g.Expect(err).NotTo(HaveOccurred())
184+
g.Expect(result).NotTo(BeNil())
185+
186+
dep := &appsv1.Deployment{}
187+
err = result.Get("coherence-operator", dep)
188+
g.Expect(err).NotTo(HaveOccurred())
189+
190+
c := findContainer("manager", dep)
191+
g.Expect(c).NotTo(BeNil())
192+
193+
g.Expect(c.Env).NotTo(BeNil())
194+
g.Expect(c.Env).To(matchers.HaveEnvVar(corev1.EnvVar{Name: operator.EnvVarWatchNamespace, Value: "foo"}))
195+
}
196+
143197
func TestBasicHelmInstall(t *testing.T) {
144198
g := NewGomegaWithT(t)
145199
cmd, err := createHelmCommand()

0 commit comments

Comments
 (0)