Skip to content

Commit 772d18f

Browse files
[mongodb] add custom user creation at initialization (CloudPirates-io#153)
* [mongodb] add init-script to create custom user with db and password * [mongodb] update readme with new values * [mongodb] bump chart version to 0.2.0 * Update CHANGELOG.md Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> --------- Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 82c4bb6 commit 772d18f

File tree

7 files changed

+112
-6
lines changed

7 files changed

+112
-6
lines changed

charts/mongodb/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Changelog
22

3-
## 0.1.9 (2025-09-16)
3+
## 0.2.0 (2025-09-24)
44

5-
* [mongo] chore(deps): update docker.io/mongo:8.0.13 Docker digest to cf340b1 ([#98](https://github.com/CloudPirates-io/helm-charts/pull/98))
5+
* [mongodb] add custom user creation at initialization ([#153](https://github.com/CloudPirates-io/helm-charts/pull/153))

charts/mongodb/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v2
22
name: mongodb
33
description: MongoDB a flexible NoSQL database for scalable, real-time data management
44
type: application
5-
version: 0.1.9
5+
version: 0.2.0
66
appVersion: "8.0.13"
77
keywords:
88
- mongodb

charts/mongodb/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ The following table lists the configurable parameters of the MongoDB chart and t
101101
| `auth.existingSecretPasswordKey` | Key in existing secret containing MongoDB password | `""` |
102102
| `config` | MongoDB configuration options | `{}` |
103103

104+
### Custom User Configuration
105+
| Parameter | Description | Default |
106+
| --------------------------- | ---------------------------------------------------------------------------------- | ---------------------------------------- |
107+
| `customUser` | Optional user to be created at initialisation with a custom password and database | `{}` |
108+
| `customUser.name` | Name of the custom user to be created | `""` |
109+
| `customUser.database` | Name of the database to be created | `""` |
110+
| `customUser.password` | Password to be used for the custom user | `""` |
111+
| `customUser.existingSecret` | Existing secret, in which username, password and database name are saved | `""` |
112+
| `customUser.secretKeys` | Name of keys in existing secret to use the custom user name, password and database | `{name: "", database: "", password: ""}` |
113+
104114
### Persistence Parameters
105115

106116
| Parameter | Description | Default |
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{- if and .Values.customUser (or .Values.customUser.name .Values.customUser.existingSecret) }}
2+
apiVersion: v1
3+
kind: ConfigMap
4+
metadata:
5+
name: {{ include "mongodb.fullname" . }}-custom-user-script
6+
namespace: {{ .Release.Namespace }}
7+
labels:
8+
{{- include "mongodb.labels" . | nindent 4 }}
9+
data:
10+
custom-user.sh: |
11+
#!/bin/sh
12+
set -e
13+
mongosh --eval "db.getSiblingDB(\"$MONGO_INITDB_DATABASE\").createUser({user: \"$MONGO_CUSTOM_USERNAME\", pwd: \"$MONGO_CUSTOM_USER_PASSWORD\", roles: [ \"readWrite\", \"dbAdmin\" ]})"
14+
{{- end }}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{- if and .Values.customUser .Values.customUser.name (not .Values.customUser.existingSecret) }}
2+
apiVersion: v1
3+
kind: Secret
4+
metadata:
5+
name: {{ include "mongodb.fullname" . }}-custom-user-secret
6+
namespace: {{ .Release.Namespace }}
7+
labels:
8+
{{- include "mongodb.labels" . | nindent 4 }}
9+
{{- if .Values.commonAnnotations }}
10+
annotations:
11+
{{- include "mongodb.annotations" . | nindent 4 }}
12+
{{- end }}
13+
type: Opaque
14+
data:
15+
CUSTOM_DB: {{ .Values.customUser.database | default .Values.customUser.name | b64enc | quote }}
16+
CUSTOM_USER: {{ .Values.customUser.name | b64enc | quote }}
17+
CUSTOM_PASSWORD: {{ .Values.customUser.password | default (randAlphaNum 32) | b64enc | quote }}
18+
{{- end }}

charts/mongodb/templates/statefulset.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,47 @@ spec:
4848
name: {{ include "mongodb.secretName" . }}
4949
key: {{ include "mongodb.secretPasswordKey" . }}
5050
{{- end }}
51+
{{- if and .Values.customUser (or .Values.customUser.name .Values.customUser.existingSecret) }}
52+
- name: MONGO_CUSTOM_USERNAME
53+
valueFrom:
54+
secretKeyRef:
55+
{{- if .Values.customUser.existingSecret }}
56+
name: {{ .Values.customUser.existingSecret }}
57+
{{- else }}
58+
name: {{ include "mongodb.fullname" . }}-custom-user-secret
59+
{{- end }}
60+
{{- if and .Values.customUser.secretKeys .Values.customUser.secretKeys.name }}
61+
key: {{ .Values.customUser.secretKeys.name }}
62+
{{- else }}
63+
key: CUSTOM_USER
64+
{{- end }}
65+
- name: MONGO_CUSTOM_USER_PASSWORD
66+
valueFrom:
67+
secretKeyRef:
68+
{{- if .Values.customUser.existingSecret }}
69+
name: {{ .Values.customUser.existingSecret }}
70+
{{- else }}
71+
name: {{ include "mongodb.fullname" . }}-custom-user-secret
72+
{{- end }}
73+
{{- if and .Values.customUser.secretKeys .Values.customUser.secretKeys.password }}
74+
key: {{ .Values.customUser.secretKeys.password }}
75+
{{- else }}
76+
key: CUSTOM_PASSWORD
77+
{{- end }}
78+
- name: MONGO_INITDB_DATABASE
79+
valueFrom:
80+
secretKeyRef:
81+
{{- if .Values.customUser.existingSecret }}
82+
name: {{ .Values.customUser.existingSecret }}
83+
{{- else }}
84+
name: {{ include "mongodb.fullname" . }}-custom-user-secret
85+
{{- end }}
86+
{{- if and .Values.customUser.secretKeys .Values.customUser.secretKeys.database }}
87+
key: {{ .Values.customUser.secretKeys.database }}
88+
{{- else }}
89+
key: CUSTOM_DB
90+
{{- end }}
91+
{{- end }}
5192
{{- range .Values.extraEnv }}
5293
- name: {{ .name }}
5394
value: {{ .value | quote }}
@@ -82,6 +123,10 @@ spec:
82123
volumeMounts:
83124
- name: data
84125
mountPath: {{ .Values.persistence.mountPath }}
126+
{{- if and .Values.customUser (or .Values.customUser.name .Values.customUser.existingSecret) }}
127+
- name: custom-user-script
128+
mountPath: /docker-entrypoint-initdb.d/
129+
{{- end }}
85130
{{- if or .Values.config.content .Values.config.existingConfigmap }}
86131
- name: config
87132
mountPath: /etc/mongo
@@ -94,6 +139,11 @@ spec:
94139
- name: data
95140
emptyDir: {}
96141
{{- end }}
142+
{{- if and .Values.customUser (or .Values.customUser.name .Values.customUser.existingSecret) }}
143+
- name: custom-user-script
144+
configMap:
145+
name: mongodb-custom-user-script
146+
{{- end }}
97147
{{- if or .Values.config.content .Values.config.existingConfigmap }}
98148
- name: config
99149
configMap:

charts/mongodb/values.yaml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ config:
5454
## @param config.content Include your custom MongoDB configurations here as string
5555
content: |
5656
systemLog:
57-
quiet: false
57+
quiet: true
5858
verbosity: 0
5959
net:
6060
bindIpAll: true
@@ -63,6 +63,22 @@ config:
6363
## param config.existingConfigmapKey Name of the key in the Configmap that should be used
6464
existingConfigmapKey: ""
6565

66+
## @section customUser Optional user to be created at initialisation with a custom password and database
67+
customUser: {}
68+
## @param customUser.name Name of the custom user to be created
69+
# name: ""
70+
## @param customUser.database Name of the database to be created
71+
# database: ""
72+
## @param customUser.password Password to be used for the custom user
73+
# password: ""
74+
## @param customUser.existingSecret Existing secret, in which username, password and database name are saved
75+
# existingSecret: ""
76+
## @param customUser.secretKeys Name of keys in existing secret to use the custom user name, password and database
77+
# secretKeys:
78+
# name: ""
79+
# password: ""
80+
# database: ""
81+
6682
persistence:
6783
## @param persistence.enabled Enable persistent storage
6884
enabled: true
@@ -137,8 +153,6 @@ readinessProbe:
137153

138154
## @param extraEnv Additional environment variables to set
139155
extraEnv: []
140-
# - name: EXTRA_VAR
141-
# value: "extra_value"
142156

143157
## @param extraVolumes Additional volumes to add to the pod
144158
extraVolumes: []

0 commit comments

Comments
 (0)