---
# Frontend component (Javascript served by Apache httpd)
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: frontend
  labels:
    app: fmn
spec:
  replicas: 1
  selector:
    app: fmn
    deploymentconfig: frontend
  strategy:
    type: Rolling
    activeDeadlineSeconds: 21600
    rollingParams:
      intervalSeconds: 1
      maxSurge: 25%
      maxUnavailable: 25%
      timeoutSeconds: 600
      updatePeriodSeconds: 1
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: fmn
        deploymentconfig: frontend
    spec:
      containers:
        - name: frontend
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: httpdir-volume
              mountPath: /httpdir
          env:
            - name: APP_SCRIPT
              value: ".s2i/run-frontend.sh"
          readinessProbe:
            timeoutSeconds: 1
            initialDelaySeconds: 5
            httpGet:
              path: /
              port: 8080
          livenessProbe:
            timeoutSeconds: 1
            initialDelaySeconds: 20
            httpGet:
              path: /
              port: 8080
      volumes:
        - name: httpdir-volume
          emptyDir: {}
  triggers:
    - type: ConfigChange
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
          - frontend
        from:
          kind: ImageStreamTag
          name: fmn:latest
---
# API component (Python/FastAPI served by Uvicorn)
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: api
  labels:
    app: fmn
spec:
  replicas: 1
  selector:
    app: fmn
    deploymentconfig: api
  strategy:
    type: Rolling
    activeDeadlineSeconds: 21600
    rollingParams:
      intervalSeconds: 1
      maxSurge: 25%
      maxUnavailable: 25%
      timeoutSeconds: 600
      updatePeriodSeconds: 1
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: fmn
        deploymentconfig: api
    spec:
      containers:
        - name: api
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: etc-fmn
              mountPath: "/etc/fmn"
              readOnly: true
            - name: ipa-config-volume
              mountPath: /etc/ipa
              readOnly: true
            - name: keytab-volume
              mountPath: /etc/keytabs
              readOnly: true
            - name: fedora-messaging-ca-volume
              mountPath: /etc/pki/fedora-messaging/ca
              readOnly: true
            - name: fedora-messaging-key-volume
              mountPath: /etc/pki/fedora-messaging/key
              readOnly: true
            - name: fedora-messaging-crt-volume
              mountPath: /etc/pki/fedora-messaging/crt
              readOnly: true

          env:
            - name: APP_SCRIPT
              value: ".s2i/run-api.sh"
            - name: OIDC_CLIENT_SECRET
              valueFrom:
                secretKeyRef:
                  name: fmn
                  key: oidc-client-secret
            - name: KRB5_CONFIG
              value: /etc/ipa/krb5.conf
            - name: KRB5_CLIENT_KTNAME
              value: /etc/keytabs/service.keytab
            - name: CACHE__SETUP_ARGS__PASSWORD
              valueFrom:
                secretKeyRef:
                  name: fmn
                  key: redis-password
            - name: CACHE__SETUP_ARGS__HASH_KEY
              valueFrom:
                secretKeyRef:
                  name: fmn
                  key: cache-hash-key
            - name: FEDORA_MESSAGING_CONF
              value: /etc/fmn/api.toml

          readinessProbe:
            timeoutSeconds: 1
            initialDelaySeconds: 5
            httpGet:
              path: /api/v1/healthz/ready
              port: 8080
          livenessProbe:
            timeoutSeconds: 1
            initialDelaySeconds: 20
            httpGet:
              path: /api/v1/healthz/live
              port: 8080

      volumes:
        - name: etc-fmn
          configMap:
            name: fmn
        - name: ipa-config-volume
          configMap:
            name: ipa-client-config
        - name: keytab-volume
          secret:
            secretName: keytab
        - name: fedora-messaging-ca-volume
          secret:
            secretName: fedora-messaging-ca
        - name: fedora-messaging-key-volume
          secret:
            secretName: fedora-messaging-key
        - name: fedora-messaging-crt-volume
          secret:
            secretName: fedora-messaging-crt
  triggers:
    - type: ConfigChange
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
          - api
        from:
          kind: ImageStreamTag
          name: fmn:latest
---
# Consumer component (Fedora Messaging consume command)
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: consumer
  labels:
    app: fmn
spec:
  # There can be as many as necessary
  replicas: 1
  selector:
    app: fmn
    deploymentconfig: consumer
  strategy:
    type: Recreate
    recreateParams:
      mid:
        execNewPod:
          command: [/opt/app-root/bin/fmn, database, sync]
          containerName: consumer
          volumes:
            - etc-fmn
        failurePolicy: Abort
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: fmn
        deploymentconfig: consumer
    spec:
      containers:
        - name: consumer
          imagePullPolicy: Always
          volumeMounts:
            - name: etc-fmn
              mountPath: "/etc/fmn"
              readOnly: true
            - name: ipa-config-volume
              mountPath: /etc/ipa
              readOnly: true
            - name: keytab-volume
              mountPath: /etc/keytabs
              readOnly: true
            - name: fedora-messaging-ca-volume
              mountPath: /etc/pki/fedora-messaging/ca
              readOnly: true
            - name: fedora-messaging-key-volume
              mountPath: /etc/pki/fedora-messaging/key
              readOnly: true
            - name: fedora-messaging-crt-volume
              mountPath: /etc/pki/fedora-messaging/crt
              readOnly: true
          env:
            - name: APP_SCRIPT
              value: ".s2i/run-consumer.sh"
            - name: KRB5_CONFIG
              value: /etc/ipa/krb5.conf
            - name: KRB5_CLIENT_KTNAME
              value: /etc/keytabs/service.keytab
            - name: CACHE__SETUP_ARGS__PASSWORD
              valueFrom:
                secretKeyRef:
                  name: fmn
                  key: redis-password
            - name: CACHE__SETUP_ARGS__HASH_KEY
              valueFrom:
                secretKeyRef:
                  name: fmn
                  key: cache-hash-key
      volumes:
        - name: etc-fmn
          configMap:
            name: fmn
        - name: ipa-config-volume
          configMap:
            name: ipa-client-config
        - name: keytab-volume
          secret:
            secretName: keytab
        - name: fedora-messaging-ca-volume
          secret:
            secretName: fedora-messaging-ca
        - name: fedora-messaging-key-volume
          secret:
            secretName: fedora-messaging-key
        - name: fedora-messaging-crt-volume
          secret:
            secretName: fedora-messaging-crt
  triggers:
    - type: ConfigChange
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
          - consumer
        from:
          kind: ImageStreamTag
          name: fmn:latest
---
# Email sending component (custom python runner)
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: sender-email
  labels:
    app: fmn
spec:
  # There can be many pods for email.
  replicas: 1
  selector:
    app: fmn
    deploymentconfig: sender-email
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: fmn
        deploymentconfig: sender-email
    spec:
      containers:
        - name: sender-email
          imagePullPolicy: Always
          volumeMounts:
            - name: etc-fmn
              mountPath: "/etc/fmn"
              readOnly: true
            - name: fedora-messaging-ca-volume
              mountPath: /etc/pki/fedora-messaging/ca
              readOnly: true
            - name: fedora-messaging-key-volume
              mountPath: /etc/pki/fedora-messaging/key
              readOnly: true
            - name: fedora-messaging-crt-volume
              mountPath: /etc/pki/fedora-messaging/crt
              readOnly: true
          env:
            - name: APP_SCRIPT
              value: ".s2i/run-sender-email.sh"
      volumes:
        - name: etc-fmn
          configMap:
            name: fmn
        - name: fedora-messaging-ca-volume
          secret:
            secretName: fedora-messaging-ca
        - name: fedora-messaging-key-volume
          secret:
            secretName: fedora-messaging-key
        - name: fedora-messaging-crt-volume
          secret:
            secretName: fedora-messaging-crt
  triggers:
    - type: ConfigChange
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
          - sender-email
        from:
          kind: ImageStreamTag
          name: fmn:latest
---
# IRC sending component (custom python runner)
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: sender-irc
  labels:
    app: fmn
spec:
  # There can be only one pod for IRC
  # Staging is currently unable to connect to IRC outside because of firewall rules.
  replicas: {{ (env == 'production')|ternary('1', '0') }}
  selector:
    app: fmn
    deploymentconfig: sender-irc
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: fmn
        deploymentconfig: sender-irc
    spec:
      containers:
        - name: sender-irc
          imagePullPolicy: Always
          volumeMounts:
            - name: etc-fmn
              mountPath: "/etc/fmn"
              readOnly: true
            - name: fedora-messaging-ca-volume
              mountPath: /etc/pki/fedora-messaging/ca
              readOnly: true
            - name: fedora-messaging-key-volume
              mountPath: /etc/pki/fedora-messaging/key
              readOnly: true
            - name: fedora-messaging-crt-volume
              mountPath: /etc/pki/fedora-messaging/crt
              readOnly: true
          env:
            - name: APP_SCRIPT
              value: ".s2i/run-sender-irc.sh"
      volumes:
        - name: etc-fmn
          configMap:
            name: fmn
        - name: fedora-messaging-ca-volume
          secret:
            secretName: fedora-messaging-ca
        - name: fedora-messaging-key-volume
          secret:
            secretName: fedora-messaging-key
        - name: fedora-messaging-crt-volume
          secret:
            secretName: fedora-messaging-crt
  triggers:
    - type: ConfigChange
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
          - sender-irc
        from:
          kind: ImageStreamTag
          name: fmn:latest
---
# Matrix sending component (custom python runner)
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: sender-matrix
  labels:
    app: fmn
spec:
  # Not sure how many concurrent connections are supported by Matrix with the
  # same token. Let's just have one for now.
  replicas: 1
  selector:
    app: fmn
    deploymentconfig: sender-matrix
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: fmn
        deploymentconfig: sender-matrix
    spec:
      containers:
        - name: sender-matrix
          imagePullPolicy: Always
          volumeMounts:
            - name: etc-fmn
              mountPath: "/etc/fmn"
              readOnly: true
            - name: fedora-messaging-ca-volume
              mountPath: /etc/pki/fedora-messaging/ca
              readOnly: true
            - name: fedora-messaging-key-volume
              mountPath: /etc/pki/fedora-messaging/key
              readOnly: true
            - name: fedora-messaging-crt-volume
              mountPath: /etc/pki/fedora-messaging/crt
              readOnly: true
          env:
            - name: APP_SCRIPT
              value: ".s2i/run-sender-matrix.sh"
      volumes:
        - name: etc-fmn
          configMap:
            name: fmn
        - name: fedora-messaging-ca-volume
          secret:
            secretName: fedora-messaging-ca
        - name: fedora-messaging-key-volume
          secret:
            secretName: fedora-messaging-key
        - name: fedora-messaging-crt-volume
          secret:
            secretName: fedora-messaging-crt
  triggers:
    - type: ConfigChange
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
          - sender-matrix
        from:
          kind: ImageStreamTag
          name: fmn:latest
---
# Redis, for caching
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: redis
  labels:
    app: fmn
    service: redis
spec:
  replicas: 1
  selector:
    app: fmn
    deploymentconfig: redis
  template:
    metadata:
      labels:
        app: fmn
        deploymentconfig: redis
    spec:
      containers:
        - name: redis
          image: redis:latest
          env:
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: fmn
                  key: redis-password
  triggers:
    - type: ConfigChange
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
          - redis
        from:
          kind: ImageStreamTag
          name: redis:latest
---
# Collectd stats collector
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: collectd
  labels:
    app: fmn
spec:
  replicas: 1
  selector:
    app: fmn
    deploymentconfig: collectd
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: fmn
        deploymentconfig: collectd
    spec:
      containers:
        - name: collectd
          imagePullPolicy: Always
          volumeMounts:
            - name: etc-fmn
              mountPath: "/etc/fmn"
              readOnly: true
          env:
            - name: APP_SCRIPT
              value: ".s2i/run-collectd.sh"
            - name: CACHE__SETUP_ARGS__PASSWORD
              valueFrom:
                secretKeyRef:
                  name: fmn
                  key: redis-password
            - name: CACHE__SETUP_ARGS__HASH_KEY
              valueFrom:
                secretKeyRef:
                  name: fmn
                  key: cache-hash-key

      volumes:
        - name: etc-fmn
          configMap:
            name: fmn
  triggers:
    - type: ConfigChange
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
          - collectd
        from:
          kind: ImageStreamTag
          name: fmn:latest
---
# Sendria, to display email notifications in staging
{% if env == 'staging' %}
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  labels:
    app: fmn
    service: sendria
  name: sendria
spec:
  replicas: 1
  selector:
    app: fmn
    deploymentconfig: sendria
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: fmn
        deploymentconfig: sendria
    spec:
      containers:
        - name: sendria
          image: sendria:latest
          ports:
            - containerPort: 1025
            - containerPort: 1080
          volumeMounts:
            - name: data
              mountPath: /home/sendria/data
          readinessProbe:
            timeoutSeconds: 1
            initialDelaySeconds: 30
            httpGet:
              path: /
              port: 1080
          livenessProbe:
            timeoutSeconds: 1
            initialDelaySeconds: 40
            httpGet:
              path: /
              port: 1080
      volumes:
        - name: data
          emptyDir: {}
  triggers:
    - type: ConfigChange
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
          - sendria
        from:
          kind: ImageStreamTag
          name: sendria:latest
{% endif %}
