---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: synapse-worker
spec:
  serviceName: synapse-worker
  replicas: 3
  selector:
    matchLabels:
      app.kubernetes.io/name: synapse-worker
      app.kubernetes.io/instance: matrix
  template:
    metadata:
      labels:
        app.kubernetes.io/name: synapse-worker
        app.kubernetes.io/instance: matrix
      annotations:
        config: new
    spec:
      initContainers:
        - name: worker-name
          image: "busybox:1.34.1"
          command: ["sh", "-c", "sed s/WORKER_NAME/$WORKER_NAME/g /worker-template/worker.yaml | tee worker/worker.yaml"]
          env:
            - name: WORKER_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          volumeMounts:
            - name: worker-template
              mountPath: /worker-template
            - name: worker-config
              mountPath: /worker
      containers:
        - name: synapse
          image: "matrixdotorg/synapse:v1.44.0"
          imagePullPolicy: IfNotPresent
          args:
            - run    
            - -m
            - synapse.app.generic_worker
            - --config-path=/config/homeserver.yaml
            - --config-path=/worker/worker.yaml
          ports:
            - name: http
              containerPort: 8083
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /health
              port: 8083
          readinessProbe:
            httpGet:
              path: /health
              port: 8083
          volumeMounts:
            - name: config
              mountPath: /config
            - name: worker-config
              mountPath: /worker
            - name: data
              mountPath: /data
      volumes:
        - name: worker-template
          configMap:
            name: synapse-worker
        - name: config
          secret:
            secretName: main
        - name: data
          persistentVolumeClaim:
            claimName: synapse
        - name: worker-config
          emptyDir: {}