---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tripbot
spec:
  selector:
    matchLabels:
      app: tripbot-server
  replicas: 1
  template:
    metadata:
      labels:
        app: tripbot-server
    spec:
      containers:

        # tripbot is a chatbot, but he also has a webserver
      - name: tripbot
        image: adanalife/tripbot:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
        readinessProbe:
          httpGet:
            path: /health/ready
            port: 80
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 3
          failureThreshold: 1
        livenessProbe:
          httpGet:
            path: /health/live
            port: 80
          initialDelaySeconds: 60
          periodSeconds: 10
          timeoutSeconds: 3
          failureThreshold: 1
        resources:
          requests:
            memory: 128Mi
            cpu: 2
            ephemeral-storage: 1Gi
          limits:
            memory: 256Mi
            cpu: 2
        env:
         - name: DATABASE_HOST
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: DATABASE_HOST
         - name: DATABASE_DB
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: DATABASE_DB
         - name: DATABASE_USER
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: DATABASE_USER
         - name: DATABASE_PASS
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: DATABASE_PASS
         - name: TWITCH_CLIENT_ID
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: TWITCH_CLIENT_ID
         - name: TWITCH_CLIENT_SECRET
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: TWITCH_CLIENT_SECRET
         - name: TWITCH_AUTH_TOKEN
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: TWITCH_AUTH_TOKEN
         - name: SENTRY_DSN
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: SENTRY_DSN
         - name: TRIPBOT_HTTP_AUTH
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: TRIPBOT_HTTP_AUTH
         - name: TWILIO_AUTH_TOKEN
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: TWILIO_AUTH_TOKEN
         - name: TWILIO_FROM_NUM
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: TWILIO_FROM_NUM
         - name: TWILIO_TO_NUM
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: TWILIO_TO_NUM
         - name: TWILIO_ACCT_SID
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: TWILIO_ACCT_SID
         - name: GOOGLE_MAPS_API_KEY
           valueFrom:
             secretKeyRef:
               name: tripbot-secrets
               key: GOOGLE_MAPS_API_KEY
         - name: GOOGLE_APPLICATION_CREDENTIALS
           value: "/opt/tripbot/infra/google/tripbot.json"
        volumeMounts:
        # mount dashcam folder
        #TODO: does tripbot really need access to this?
        - name: dashcam-volume
          mountPath: /opt/data/Dashcam/_all
          readOnly: true
        # mount google-creds secret
        - name: google-creds
          mountPath: /opt/tripbot/infra/google
          readOnly: true
        - name: log
          mountPath: /opt/tripbot/log

          # we use nginx as a proxy in front of the tripbot server
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /etc/nginx # mount nginx-conf volumn to /etc/nginx
          readOnly: true
          name: nginx-conf
        - mountPath: /var/log/nginx
          name: log

      volumes:
      - name: dashcam-volume
        persistentVolumeClaim:
          claimName: dashcam-from-host-claim
      - name: nginx-conf
        configMap:
          name: nginx-conf # place ConfigMap `nginx-conf` on /etc/nginx
          items:
            - key: nginx.conf
              path: nginx.conf
            - key: virtualhost.conf
              path: virtualhost/virtualhost.conf
      - name: google-creds
        secret:
          secretName: google-creds
      - name: log
        emptyDir: {}
