Skip to content

Commit 71e73e0

Browse files
committed
Sync MariaDB databases and users
1 parent 6024ea1 commit 71e73e0

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

infra/clusterroles/Chart.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: v2
2+
name: clusterroles
3+
version: 1.0.0
4+
5+
description: "Special cluster roles"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Roles to access configMaps and secrets in all namespaces.
2+
# This is a very dangerous role, only use it with care!
3+
apiVersion: rbac.authorization.k8s.io/v1
4+
kind: ClusterRole
5+
metadata:
6+
name: read-cm-secrets
7+
rules:
8+
- apiGroups: [""]
9+
resources: ["configmaps", "secrets"]
10+
verbs: ["get", "list"]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: init-apps
5+
namespace: db-namespace
6+
7+
---
8+
9+
apiVersion: rbac.authorization.k8s.io/v1
10+
kind: RoleBinding
11+
metadata:
12+
name: allow-init-apps-read-app-config
13+
namespace: faf-apps
14+
subjects:
15+
- kind: ServiceAccount
16+
name: init-apps
17+
namespace: faf-ops
18+
roleRef:
19+
apiGroup: rbac.authorization.k8s.io
20+
kind: ClusterRole
21+
name: read-cm-secrets
22+
23+
---
24+
25+
{{- $wave := 1 }}
26+
{{- range .Values.databasesAndUsers }}
27+
---
28+
apiVersion: batch/v1
29+
kind: Job
30+
metadata:
31+
name: mariadb-sync-db-user-{{ $wave }}
32+
labels:
33+
app: mariadb-sync-db-user
34+
argocd.argoproj.io/instance: mariadb
35+
annotations:
36+
argocd.argoproj.io/hook: PostSync
37+
argocd.argoproj.io/hook-delete-policy: HookSucceeded
38+
argocd.argoproj.io/sync-wave: '{{ $wave }}'
39+
spec:
40+
backoffLimit: 1
41+
template:
42+
spec:
43+
serviceAccountName: init-apps
44+
containers:
45+
- name: mariadb-sync-db-user
46+
image: {{ $.Values.image.repository }}:{{ $.Values.image.tag }}
47+
imagePullPolicy: Always
48+
env:
49+
- name: SYNC_DATABASE
50+
valueFrom:
51+
configMapKeyRef:
52+
name: {{ .configMapRef }}
53+
key: {{ .databaseKey }}
54+
- name: SYNC_USERNAME
55+
valueFrom:
56+
configMapKeyRef:
57+
name: {{ .configMapRef }}
58+
key: {{ .usernameKey }}
59+
- name: SYNC_PASSWORD
60+
valueFrom:
61+
secretKeyRef:
62+
name: {{ .secretRef }}
63+
key: {{ .passwordKey }}
64+
envFrom:
65+
- secretRef:
66+
name: mariadb
67+
68+
command: ["/bin/sh", "-c"]
69+
args:
70+
- |
71+
mariadb --user=root --password="${MARIADB_ROOT_PASSWORD}" <<SQL_SCRIPT
72+
CREATE DATABASE IF NOT EXISTS \`${SYNC_DATABASE}\`;
73+
CREATE USER IF NOT EXISTS '${SYNC_USERNAME}'@'%' IDENTIFIED BY '${SYNC_PASSWORD}';
74+
GRANT ALL PRIVILEGES ON \`${SYNC_DATABASE}\`.* TO '${SYNC_USERNAME}'@'%';
75+
SQL_SCRIPT
76+
restartPolicy: Never
77+
{{- $wave = add $wave 1 }}
78+
{{- end }}

infra/mariadb/templates/statefulset.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ spec:
1717
app: mariadb
1818
spec:
1919
containers:
20-
- image: mariadb:12.1
20+
- image: {{ $.Values.image.repository }}:{{ $.Values.image.tag }}
2121
imagePullPolicy: Always
2222
name: mariadb
2323
ports:

infra/mariadb/values.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1+
image:
2+
repository: "mariadb"
3+
tag: "12.1"
4+
15
infisical-secret:
26
name: mariadb
7+
8+
databasesAndUsers:
9+
- configMapRef: faf-api
10+
secretRef: faf-api
11+
databaseKey: DATABASE_NAME
12+
usernameKey: DATABASE_USERNAME
13+
passwordKey: DATABASE_USERNAME

0 commit comments

Comments
 (0)