Skip to content

Commit 96b53cc

Browse files
CLOUDP-322487 - Change webhook's CR and CRB resource names to include operator name and namespace (#393)
# Summary This pull request introduces Helm chart unit tests to the CI pipeline and ensures consistent naming for ClusterRole and ClusterRoleBinding resources in the Helm chart. The most important changes are grouped below: **Helm Chart Testing Integration:** * Added a `helm-tests` target to the `Makefile` to run Helm chart unit tests using the `helm-unittest` plugin. The target installs the plugin if necessary and runs tests in the `helm_chart` directory. * Created a `test_helm_unit` function in `.evergreen-functions.yml` to execute the new Helm unit tests as part of CI. * Added a `unit_tests_helm` task to `.evergreen.yml` and included it in the `unit_tests_task_group` to ensure Helm unit tests run with other unit tests in the CI pipeline. **Helm Chart Improvements and Testing:** * Updated `operator-roles-webhook.yaml` to dynamically generate consistent names for ClusterRole and ClusterRoleBinding resources based on the operator name and namespace, preventing naming conflicts across multiple installations. * Added a new Helm unit test suite (`webhook_clusterrole_test.yaml`) to verify that ClusterRole and ClusterRoleBinding names are consistent and unique per installation. # Example the new names: **binding** `<name>-<ns>-webhook-crb` **role** ` <name>-<ns>-webhook-cr` old names: **binding** `<name>-<ns>-webhook` **role** `mongodb-kubernetes-operator-mongodb-webhook` ## Proof of Work - green ci - helm chart unit test as part of unit test: [Link](https://spruce.mongodb.com/task/mongodb_kubernetes_unit_tests_unit_tests_helm_patch_b7211ae9f98ec21a895a77f164f74e66519f4bd4_68b6bd8e10a56d0007c1cfc5_25_09_02_09_49_06/logs?execution=0) (IMO that unit test is more for documentation than actually testing, its nice to have a view how those names are generated in the end) ## Checklist - [x] Have you linked a jira ticket and/or is the ticket in the title? - [x] Have you checked whether your jira ticket required DOCSP changes? - [x] Have you added changelog file? - use `skip-changelog` label if not needed - refer to [Changelog files and Release Notes](https://github.com/mongodb/mongodb-kubernetes/blob/master/CONTRIBUTING.md#changelog-files-and-release-notes) section in CONTRIBUTING.md for more details --------- Co-authored-by: Vivek Singh <[email protected]>
1 parent a106216 commit 96b53cc

9 files changed

+100
-16
lines changed

.evergreen-functions.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,16 @@ functions:
727727
params:
728728
files: [ "src/github.com/mongodb/mongodb-kubernetes/*.suite", "src/github.com/mongodb/mongodb-kubernetes/docker/mongodb-kubernetes-init-ops-manager/mmsconfiguration/*.suite" ]
729729

730+
test_helm_unit:
731+
- command: shell.exec
732+
type: test
733+
params:
734+
shell: bash
735+
working_dir: src/github.com/mongodb/mongodb-kubernetes
736+
script: |
737+
source .generated/context.export.env
738+
make helm-tests
739+
730740
test_python_unit:
731741
- command: shell.exec
732742
type: test

.evergreen.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ tasks:
275275
commands:
276276
- func: "test_python_unit"
277277

278+
- name: unit_tests_helm
279+
tags: [ "unit_tests" ]
280+
commands:
281+
- func: "test_helm_unit"
282+
278283
- name: sbom_tests
279284
tags: [ "unit_tests" ]
280285
# The SBOM tests run only on commit builds. Running this on patches might cause false-positive failures
@@ -665,6 +670,7 @@ task_groups:
665670
- lint_repo
666671
- unit_tests_golang
667672
- unit_tests_python
673+
- unit_tests_helm
668674
- sbom_tests
669675

670676
- name: gke_code_snippets_task_group

Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,17 @@ test-race: generate fmt vet manifests golang-tests-race
300300

301301
test: generate fmt vet manifests golang-tests
302302

303-
# all-tests will run golang and python tests without race (used locally)
304-
all-tests: test python-tests
303+
# helm-tests will run helm chart unit tests
304+
helm-tests:
305+
@echo "Running helm chart unit tests..."
306+
@if ! helm plugin list | grep -q unittest; then \
307+
echo "Installing helm-unittest plugin..."; \
308+
helm plugin install https://github.com/helm-unittest/helm-unittest; \
309+
fi
310+
helm unittest helm_chart --color
311+
312+
# all-tests will run golang, python, and helm tests without race (used locally)
313+
all-tests: test python-tests helm-tests
305314

306315
# Build manager binary
307316
manager: generate fmt vet
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: helm chart - webhook per namespace
3+
kind: fix
4+
date: 2025-09-02
5+
---
6+
7+
* Changed webhook ClusterRole and ClusterRoleBinding default names to include the namespace. This ensures that multiple operator installations in different namespaces don't conflict with each other.

helm_chart/templates/operator-roles-webhook.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11

22
{{/* This cluster role and binding is necessary to allow the operator to automatically register ValidatingWebhookConfiguration. */}}
33
{{- if and .Values.operator.webhook.registerConfiguration .Values.operator.webhook.installClusterRole }}
4-
{{- if not (lookup "rbac.authorization.k8s.io/v1" "ClusterRole" "" "mongodb-kubernetes-operator-mongodb-webhook") }}
4+
{{- $webhookClusterRoleName := printf "%s-%s-webhook-cr" .Values.operator.name (include "mongodb-kubernetes-operator.namespace" .) }}
5+
{{- $webhookClusterRoleBindingName := printf "%s-%s-webhook-crb" .Values.operator.name (include "mongodb-kubernetes-operator.namespace" .) }}
56
---
67
kind: ClusterRole
78
apiVersion: rbac.authorization.k8s.io/v1
89
metadata:
9-
name: {{.Values.operator.baseName}}-operator-mongodb-webhook
10+
name: {{ $webhookClusterRoleName }}
1011
rules:
1112
- apiGroups:
1213
- "admissionregistration.k8s.io"
@@ -28,17 +29,16 @@ rules:
2829
- create
2930
- update
3031
- delete
31-
{{- end }}
3232
---
3333

3434
kind: ClusterRoleBinding
3535
apiVersion: rbac.authorization.k8s.io/v1
3636
metadata:
37-
name: {{ .Values.operator.name }}-{{ include "mongodb-kubernetes-operator.namespace" . }}-webhook-binding
37+
name: {{ $webhookClusterRoleBindingName }}
3838
roleRef:
3939
apiGroup: rbac.authorization.k8s.io
4040
kind: ClusterRole
41-
name: {{.Values.operator.baseName}}-operator-mongodb-webhook
41+
name: {{ $webhookClusterRoleName }}
4242
subjects:
4343
- kind: ServiceAccount
4444
name: {{ .Values.operator.name }}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
suite: test webhook consistent clusterrole and binding
2+
templates:
3+
- operator-roles-webhook.yaml
4+
tests:
5+
- it: should have consistent ClusterRole and ClusterRoleBinding names
6+
set:
7+
operator.webhook.registerConfiguration: true
8+
operator.webhook.installClusterRole: true
9+
asserts:
10+
- hasDocuments:
11+
count: 2
12+
- isKind:
13+
of: ClusterRole
14+
documentIndex: 0
15+
- isKind:
16+
of: ClusterRoleBinding
17+
documentIndex: 1
18+
- equal:
19+
path: metadata.name
20+
value: mongodb-kubernetes-operator-NAMESPACE-webhook-cr
21+
documentIndex: 0
22+
- equal:
23+
path: metadata.name
24+
value: mongodb-kubernetes-operator-NAMESPACE-webhook-crb
25+
documentIndex: 1
26+
- equal:
27+
path: roleRef.name
28+
value: mongodb-kubernetes-operator-NAMESPACE-webhook-cr
29+
documentIndex: 1
30+
31+
# Test that different installations get unique names (prevents conflicts)
32+
- it: should create unique names per installation
33+
set:
34+
operator.name: my-operator
35+
operator.namespace: custom-ns
36+
operator.webhook.registerConfiguration: true
37+
operator.webhook.installClusterRole: true
38+
release:
39+
namespace: custom-ns
40+
asserts:
41+
- equal:
42+
path: metadata.name
43+
value: my-operator-custom-ns-webhook-cr
44+
documentIndex: 0
45+
- equal:
46+
path: metadata.name
47+
value: my-operator-custom-ns-webhook-crb
48+
documentIndex: 1
49+
- equal:
50+
path: roleRef.name
51+
value: my-operator-custom-ns-webhook-cr
52+
documentIndex: 1

public/mongodb-kubernetes-multi-cluster.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ subjects:
203203
kind: ClusterRole
204204
apiVersion: rbac.authorization.k8s.io/v1
205205
metadata:
206-
name: mongodb-kubernetes-operator-mongodb-webhook
206+
name: mongodb-kubernetes-operator-multi-cluster-mongodb-webhook-cr
207207
rules:
208208
- apiGroups:
209209
- "admissionregistration.k8s.io"
@@ -230,11 +230,11 @@ rules:
230230
kind: ClusterRoleBinding
231231
apiVersion: rbac.authorization.k8s.io/v1
232232
metadata:
233-
name: mongodb-kubernetes-operator-multi-cluster-mongodb-webhook-binding
233+
name: mongodb-kubernetes-operator-multi-cluster-mongodb-webhook-crb
234234
roleRef:
235235
apiGroup: rbac.authorization.k8s.io
236236
kind: ClusterRole
237-
name: mongodb-kubernetes-operator-mongodb-webhook
237+
name: mongodb-kubernetes-operator-multi-cluster-mongodb-webhook-cr
238238
subjects:
239239
- kind: ServiceAccount
240240
name: mongodb-kubernetes-operator-multi-cluster

public/mongodb-kubernetes-openshift.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ subjects:
203203
kind: ClusterRole
204204
apiVersion: rbac.authorization.k8s.io/v1
205205
metadata:
206-
name: mongodb-kubernetes-operator-mongodb-webhook
206+
name: mongodb-kubernetes-operator-mongodb-webhook-cr
207207
rules:
208208
- apiGroups:
209209
- "admissionregistration.k8s.io"
@@ -230,11 +230,11 @@ rules:
230230
kind: ClusterRoleBinding
231231
apiVersion: rbac.authorization.k8s.io/v1
232232
metadata:
233-
name: mongodb-kubernetes-operator-mongodb-webhook-binding
233+
name: mongodb-kubernetes-operator-mongodb-webhook-crb
234234
roleRef:
235235
apiGroup: rbac.authorization.k8s.io
236236
kind: ClusterRole
237-
name: mongodb-kubernetes-operator-mongodb-webhook
237+
name: mongodb-kubernetes-operator-mongodb-webhook-cr
238238
subjects:
239239
- kind: ServiceAccount
240240
name: mongodb-kubernetes-operator

public/mongodb-kubernetes.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ subjects:
203203
kind: ClusterRole
204204
apiVersion: rbac.authorization.k8s.io/v1
205205
metadata:
206-
name: mongodb-kubernetes-operator-mongodb-webhook
206+
name: mongodb-kubernetes-operator-mongodb-webhook-cr
207207
rules:
208208
- apiGroups:
209209
- "admissionregistration.k8s.io"
@@ -230,11 +230,11 @@ rules:
230230
kind: ClusterRoleBinding
231231
apiVersion: rbac.authorization.k8s.io/v1
232232
metadata:
233-
name: mongodb-kubernetes-operator-mongodb-webhook-binding
233+
name: mongodb-kubernetes-operator-mongodb-webhook-crb
234234
roleRef:
235235
apiGroup: rbac.authorization.k8s.io
236236
kind: ClusterRole
237-
name: mongodb-kubernetes-operator-mongodb-webhook
237+
name: mongodb-kubernetes-operator-mongodb-webhook-cr
238238
subjects:
239239
- kind: ServiceAccount
240240
name: mongodb-kubernetes-operator

0 commit comments

Comments
 (0)