From 03a167a13f41444ff30f4dc9228eba231215608c Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Mon, 16 Dec 2024 15:26:19 +0100 Subject: [PATCH] feat(bridge): Add telegram --- manifests/bridges/kustomization.yaml | 1 + manifests/bridges/telegram/create_db.sh | 14 ++++++ manifests/bridges/telegram/job.yaml | 46 +++++++++++++++++++ manifests/bridges/telegram/kustomization.yaml | 18 ++++++++ manifests/bridges/telegram/service.yaml | 16 +++++++ manifests/bridges/telegram/statefulset.yaml | 37 +++++++++++++++ manifests/deployment.yaml | 6 +++ 7 files changed, 138 insertions(+) create mode 100644 manifests/bridges/telegram/create_db.sh create mode 100644 manifests/bridges/telegram/job.yaml create mode 100644 manifests/bridges/telegram/kustomization.yaml create mode 100644 manifests/bridges/telegram/service.yaml create mode 100644 manifests/bridges/telegram/statefulset.yaml diff --git a/manifests/bridges/kustomization.yaml b/manifests/bridges/kustomization.yaml index 4508c1b..c61e223 100644 --- a/manifests/bridges/kustomization.yaml +++ b/manifests/bridges/kustomization.yaml @@ -3,6 +3,7 @@ kind: Kustomization resources: - ./signal + - ./telegram - ./messenger - ./instagram - ./whatsapp diff --git a/manifests/bridges/telegram/create_db.sh b/manifests/bridges/telegram/create_db.sh new file mode 100644 index 0000000..fc899eb --- /dev/null +++ b/manifests/bridges/telegram/create_db.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +echo "Checking if $TELEGRAM_DB database exists..." + +if psql -lqt | cut -d \| -f 1 | grep -qw $TELEGRAM_DB; then + echo "Database exists, skipping creation" +else + echo "Database does not exist, creating..." + createdb $TELEGRAM_DB + createuser $TELEGRAM_USER + psql -c "ALTER USER $TELEGRAM_USER WITH ENCRYPTED PASSWORD '$TELEGRAM_PASSWORD';" + psql -c "GRANT ALL PRIVILEGES ON DATABASE $TELEGRAM_DB TO $TELEGRAM_USER;" + psql -c "ALTER DATABASE $TELEGRAM_DB OWNER TO $TELEGRAM_USER;" +fi diff --git a/manifests/bridges/telegram/job.yaml b/manifests/bridges/telegram/job.yaml new file mode 100644 index 0000000..c00a407 --- /dev/null +++ b/manifests/bridges/telegram/job.yaml @@ -0,0 +1,46 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: telegram-db-init + namespace: synapse +spec: + template: + spec: + containers: + - name: telegram-db-init + image: postgres:15.10 + command: ["/bin/bash", "/data/create_db.sh"] + env: + - name: PGHOST + value: synapse-db-rw.synapse.svc.cluster.local + - name: PGUSER + valueFrom: + secretKeyRef: + name: synapse-db-superuser + key: username + - name: PGPASSWORD + valueFrom: + secretKeyRef: + name: synapse-db-superuser + key: password + - name: TELEGRAM_USER + valueFrom: + secretKeyRef: + name: mautrix-telegram-db + key: username + - name: TELEGRAM_PASSWORD + valueFrom: + secretKeyRef: + name: mautrix-telegram-db + key: password + - name: TELEGRAM_DB + value: telegram + volumeMounts: + - name: create-db + mountPath: /data + volumes: + - name: create-db + configMap: + name: telegram-db-creation + restartPolicy: Never + backoffLimit: 4 diff --git a/manifests/bridges/telegram/kustomization.yaml b/manifests/bridges/telegram/kustomization.yaml new file mode 100644 index 0000000..a5e2d82 --- /dev/null +++ b/manifests/bridges/telegram/kustomization.yaml @@ -0,0 +1,18 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +namespace: synapse + +resources: + - service.yaml + - statefulset.yaml + - job.yaml + +generatorOptions: + labels: + app: telegram + +configMapGenerator: + - name: telegram-db-creation + behavior: create + files: + - create_db.sh diff --git a/manifests/bridges/telegram/service.yaml b/manifests/bridges/telegram/service.yaml new file mode 100644 index 0000000..73046b2 --- /dev/null +++ b/manifests/bridges/telegram/service.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Service +metadata: + name: telegram + namespace: synapse + labels: + app.kubernetes.io/name: telegram +spec: + ports: + - name: http + port: 29317 + protocol: TCP + targetPort: 29317 + selector: + app: telegram + publishNotReadyAddresses: true diff --git a/manifests/bridges/telegram/statefulset.yaml b/manifests/bridges/telegram/statefulset.yaml new file mode 100644 index 0000000..f5eae6e --- /dev/null +++ b/manifests/bridges/telegram/statefulset.yaml @@ -0,0 +1,37 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: telegram + namespace: synapse +spec: + selector: + matchLabels: + app: telegram + serviceName: telegram + replicas: 1 + minReadySeconds: 10 + template: + metadata: + labels: + app: telegram + spec: + terminationGracePeriodSeconds: 10 + containers: + - name: telegram + image: dock.mau.dev/mautrix/telegram:v0.15.2 + command: [ + "/usr/bin/python3", + "-m", "mautrix_telegram", + "--config", "/data/config.yaml", + "--no-update", + ] + ports: + - containerPort: 29328 + volumeMounts: + - mountPath: "/data/config.yaml" + name: telegram-config-file + subPath: config.yaml + volumes: + - name: telegram-config-file + secret: + secretName: telegram-secret-config diff --git a/manifests/deployment.yaml b/manifests/deployment.yaml index bc1dddd..c0867f6 100644 --- a/manifests/deployment.yaml +++ b/manifests/deployment.yaml @@ -36,6 +36,9 @@ spec: - mountPath: "/data/signal.yaml" name: signal-secret-registration subPath: signal.yaml + - mountPath: "/data/telegram.yaml" + name: telegram-secret-registration + subPath: telegram.yaml - mountPath: "/data/messenger.yaml" name: messenger-secret-registration subPath: messenger.yaml @@ -61,6 +64,9 @@ spec: - name: signal-secret-registration secret: secretName: signal-secret-registration + - name: telegram-secret-registration + secret: + secretName: telegram-secret-registration - name: messenger-secret-registration secret: secretName: messenger-secret-registration