WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit abe3548

Browse files
committed
Add mongodb
1 parent 7eefc0f commit abe3548

File tree

8 files changed

+146
-3
lines changed

8 files changed

+146
-3
lines changed

apps/faf-icebreaker/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ spec:
6262
volumes:
6363
- name: geolite-db
6464
emptyDir: {}
65+
securityContext:
66+
fsGroup: 1000

cluster/storage/values.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ managedStorages:
4343
size: 50Gi
4444
pvc:
4545
namespace: faf-apps
46+
- pv:
47+
name: mongodb
48+
size: 20Gi
49+
pvc:
50+
namespace: faf-infra
4651
- pv:
4752
name: wordpress
4853
size: 10Gi
@@ -76,9 +81,6 @@ managedStorages:
7681
# - name: mariadb
7782
# namespace: faf-apps
7883
# size: 20Gi
79-
# - name: mongodb
80-
# namespace: faf-apps
81-
# size: 20Gi
8284
# size: 10Gi
8385
# - name: nodebb
8486
# namespace: faf-apps

infra/mongodb/Chart.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
apiVersion: v2
2+
name: mongodb
3+
version: 1.0.0
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: mongodb
5+
labels:
6+
app: mongodb
7+
data:
8+
MONGO_INITDB_ROOT_USERNAME: "root"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: secrets.infisical.com/v1alpha1
2+
kind: InfisicalSecret
3+
metadata:
4+
name: mongodb
5+
namespace: faf-infra
6+
spec:
7+
authentication:
8+
universalAuth:
9+
credentialsRef:
10+
secretName: infisical-machine-identity
11+
secretNamespace: faf-ops
12+
secretsScope:
13+
projectSlug: {{.Values.infisical.projectSlug}}
14+
envSlug: {{.Values.infisical.envSlug}}
15+
secretsPath: "/mongodb"
16+
managedSecretReference:
17+
secretName: mongodb
18+
secretNamespace: faf-infra
19+
creationPolicy: "Owner"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: mongodb
5+
labels:
6+
app: mongodb
7+
spec:
8+
selector:
9+
app: mongodb
10+
ports:
11+
- port: 27017
12+
targetPort: 27017
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
apiVersion: apps/v1
2+
kind: StatefulSet
3+
metadata:
4+
name: mongodb
5+
labels:
6+
app: mongodb
7+
spec:
8+
serviceName: mongodb
9+
replicas: 1
10+
revisionHistoryLimit: 10
11+
selector:
12+
matchLabels:
13+
app: mongodb
14+
template:
15+
metadata:
16+
labels:
17+
app: mongodb
18+
spec:
19+
containers:
20+
- image: mongo:7.0.14
21+
imagePullPolicy: Always
22+
name: mongodb
23+
ports:
24+
- containerPort: 27017
25+
protocol: TCP
26+
envFrom:
27+
- configMapRef:
28+
name: mongodb
29+
- secretRef:
30+
name: mongodb
31+
volumeMounts:
32+
- name: mongodb-pvc
33+
mountPath: /var/lib/mongodbql/data
34+
restartPolicy: Always
35+
volumes:
36+
- name: config
37+
configMap:
38+
name: mongodb
39+
- name: mongodb-pvc
40+
persistentVolumeClaim:
41+
claimName: mongodb-pvc

scripts/init-mongodb.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/sh
2+
# Setup rabbitmq vhost and users
3+
export NAMESPACE="faf-infra"
4+
5+
# fail on errors
6+
set -e
7+
8+
. ./k8s-helpers.sh
9+
10+
check_resource_exists_or_fail secret mongodb
11+
check_resource_exists_or_fail statefulset mongodb
12+
check_resource_exists_or_fail pod mongodb-0
13+
14+
ADMIN_USER=$(get_config_value mongodb MONGO_INITDB_ROOT_USERNAME)
15+
ADMIN_PASSWORD=$(get_secret_value mongodb MONGO_INITDB_ROOT_PASSWORD)
16+
17+
run_mongo_query() {
18+
kubectl -n $NAMESPACE exec -i mongodb-0 -- mongosh --quiet --username "$ADMIN_USER" --password "$ADMIN_PASSWORD" --authenticationDatabase admin --eval "$1"
19+
}
20+
21+
# Function to check if a user exists
22+
user_exists() {
23+
DATABASE=$1
24+
USERNAME=$2
25+
RESULT=$(run_mongo_query "db.getSiblingDB(\"$DATABASE\").getUser(\"$USERNAME\");")
26+
27+
if [ "$RESULT" != "null" ]; then
28+
return 0 # User exists (true)
29+
else
30+
return 1 # User does not exist (false)
31+
fi
32+
}
33+
34+
create_user_and_db() {
35+
SERVICE_NAMESPACE=$1
36+
SERVICE_NAME=$2
37+
DB_USER=$(NAMESPACE=$SERVICE_NAMESPACE get_config_value "$SERVICE_NAME" "$3")
38+
DB_PASSWORD=$(NAMESPACE=$SERVICE_NAMESPACE get_secret_value "$SERVICE_NAME" "$4")
39+
DB_NAME=$(NAMESPACE=$SERVICE_NAMESPACE get_config_value "$SERVICE_NAME" "$5")
40+
41+
# Create user if it does not exist
42+
if user_exists "$DB_NAME" "$DB_USER"; then
43+
echo "User $DB_USER already exists in db $DB_NAME. Skipping user creation."
44+
else
45+
run_mongo_query <<MONGODB_SCRIPT
46+
use ${MONGO_NODEBB_DATABASE};
47+
db.createUser( { user: "${DB_USER}", pwd: "${DB_PASSWORD}", roles: [ "readWrite" ] } );
48+
db.grantRolesToUser("${DB_NAME}",[{ role: "clusterMonitor", db: "admin" }]);
49+
MONGODB_SCRIPT
50+
echo "User $DB_USER created in db $DB_NAME."
51+
fi
52+
}
53+
54+
create_user_and_db faf-apps wikijs DB_USER DB_PASS DB_NAME
55+
56+
echo "All users and databases have been processed."

0 commit comments

Comments
 (0)