Skip to content

Commit 266039b

Browse files
committed
Sync MariaDB databases and users
1 parent 6024ea1 commit 266039b

File tree

5 files changed

+139
-1
lines changed

5 files changed

+139
-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: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# =======================================
2+
# Jesus, what the fuck is happening here?
3+
# =======================================
4+
#
5+
# 1. Create a service account
6+
# 2. Permit it to read configmaps and secrets in the faf-apps namespace
7+
# 3. Iterate over the databasesAndUsers list and create a job for each database
8+
# a) initContainer: Load the configmap and secret into environment variables. This must happen via k8s api, as we can't directly reference cm/secrets cross-namespace.
9+
# b) actual container: Load the env from file and create the database and user
10+
11+
apiVersion: v1
12+
kind: ServiceAccount
13+
metadata:
14+
name: init-apps
15+
16+
---
17+
18+
apiVersion: rbac.authorization.k8s.io/v1
19+
kind: RoleBinding
20+
metadata:
21+
name: allow-init-apps-read-app-config
22+
namespace: faf-apps
23+
subjects:
24+
- kind: ServiceAccount
25+
name: init-apps
26+
namespace: faf-infra
27+
roleRef:
28+
apiGroup: rbac.authorization.k8s.io
29+
kind: ClusterRole
30+
name: read-cm-secrets
31+
32+
---
33+
34+
{{- $wave := 1 }}
35+
{{- range .Values.databasesAndUsers }}
36+
---
37+
apiVersion: batch/v1
38+
kind: Job
39+
metadata:
40+
name: mariadb-sync-db-user-{{ $wave }}
41+
labels:
42+
app: mariadb-sync-db-user
43+
argocd.argoproj.io/instance: mariadb
44+
annotations:
45+
argocd.argoproj.io/hook: PostSync
46+
argocd.argoproj.io/hook-delete-policy: HookSucceeded
47+
argocd.argoproj.io/sync-wave: '{{ $wave }}'
48+
spec:
49+
backoffLimit: 1
50+
template:
51+
spec:
52+
serviceAccountName: init-apps
53+
54+
volumes:
55+
- name: config # We will store the apps config for database, username and password here
56+
emptyDir: {}
57+
58+
initContainers:
59+
- name: load-config
60+
image: alpine/kubectl
61+
command: ["/bin/sh", "-c"]
62+
args:
63+
- |
64+
mkdir -p /config
65+
66+
echo -n "SYNC_DATABASE=" > /config/env
67+
kubectl get cm {{ .configMapRef }} \
68+
-n faf-apps \
69+
-o jsonpath='{.data.{{ .databaseKey }}}' >> /config/env
70+
echo >> /config/env
71+
72+
echo -n "SYNC_USERNAME=" >> /config/env
73+
kubectl get cm {{ .configMapRef }} \
74+
-n faf-apps \
75+
-o jsonpath='{.data.{{ .usernameKey }}}' >> /config/env
76+
echo >> /config/env
77+
78+
echo -n "SYNC_PASSWORD=" >> /config/env
79+
kubectl get secret {{ .secretRef }} \
80+
-n faf-apps \
81+
-o jsonpath='{.data.{{ .passwordKey }}}' \
82+
| base64 -d >> /config/env
83+
echo >> /config/env
84+
85+
volumeMounts:
86+
- name: config
87+
mountPath: /config
88+
89+
containers:
90+
- name: mariadb-sync-db-user
91+
image: {{ $.Values.image.repository }}:{{ $.Values.image.tag }}
92+
imagePullPolicy: Always
93+
envFrom:
94+
- secretRef:
95+
name: mariadb
96+
97+
command: ["/bin/sh", "-c"]
98+
args:
99+
- |
100+
cat /config/env
101+
set -a
102+
. /config/env
103+
set +a
104+
105+
mariadb --host=mariadb --user=root --password="${MARIADB_ROOT_PASSWORD}" <<SQL_SCRIPT
106+
CREATE DATABASE IF NOT EXISTS \`${SYNC_DATABASE}\`;
107+
CREATE USER IF NOT EXISTS '${SYNC_USERNAME}'@'%' IDENTIFIED BY '${SYNC_PASSWORD}';
108+
GRANT ALL PRIVILEGES ON \`${SYNC_DATABASE}\`.* TO '${SYNC_USERNAME}'@'%';
109+
SQL_SCRIPT
110+
restartPolicy: Never
111+
{{- $wave = add $wave 1 }}
112+
{{- 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)