---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kotsadm
  labels:
    kots.io/kotsadm: "true"
    kots.io/backup: velero
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kotsadm
  template:
    metadata:
      labels:
        app: kotsadm
        kots.io/kotsadm: "true"
        kots.io/backup: velero
      annotations:
        backup.velero.io/backup-volumes: backup
        pre.hook.backup.velero.io/command: '["/backup.sh"]'
        pre.hook.backup.velero.io/timeout: 10m
    spec:
      securityContext:
        runAsUser: 1001
      serviceAccountName: kotsadm
      restartPolicy: Always
      volumes:
      - name: kubelet-client-cert
        secret:
          secretName: kubelet-client-cert
      - name: kurl-proxy-kotsadm-tls-cert
        secret:
          secretName: kotsadm-tls
      - name: kotsadm-web-scripts
        configMap:
          defaultMode: 511 # hex 777
          name: kotsadm-web-scripts
      - name: backup
        emptyDir: {}
      - name: migrations
        emptyDir:
          medium: Memory
      containers:
      - image: kotsadm/kotsadm:v1.50.2
        name: kotsadm
        command: ["bash"]
        args:
        - "/scripts/start-kotsadm-web.sh"
        ports:
        - name: http
          containerPort: 3000
        readinessProbe:
          failureThreshold: 3
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
          httpGet:
            path: /healthz
            port: 3000
            scheme: HTTP
        env:
          - name: AUTO_CREATE_CLUSTER
            value: "1"
          - name: AUTO_CREATE_CLUSTER_NAME
            value: "this-cluster"
          - name: AUTO_CREATE_CLUSTER_TOKEN
            valueFrom:
              secretKeyRef:
                name: kotsadm-cluster-token
                key: kotsadm-cluster-token
          - name: SHARED_PASSWORD_BCRYPT
            valueFrom:
              secretKeyRef:
                name: kotsadm-password
                key: passwordBcrypt
          - name: SESSION_KEY
            valueFrom:
              secretKeyRef:
                name: kotsadm-session
                key: key
          - name: POSTGRES_URI
            valueFrom:
              secretKeyRef:
                name: kotsadm-postgres
                key: uri
          - name: POSTGRES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: kotsadm-postgres
                key: password
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: API_ENCRYPTION_KEY
            valueFrom:
              secretKeyRef:
                name: kotsadm-encryption
                key: encryptionKey
          - name: S3_ENDPOINT
            valueFrom:
              secretKeyRef:
                name: kotsadm-s3
                key: endpoint
          - name: S3_BUCKET_NAME
            value: "kotsadm"
          - name: S3_ACCESS_KEY_ID
            valueFrom:
              secretKeyRef:
                name: kotsadm-s3
                key: access-key-id
          - name: S3_SECRET_ACCESS_KEY
            valueFrom:
              secretKeyRef:
                name: kotsadm-s3
                key: secret-access-key
          - name: S3_BUCKET_ENDPOINT
            value: "true"
          - name: API_ADVERTISE_ENDPOINT
            value: "http://localhost:8800"
          - name: API_ENDPOINT
            value: http://kotsadm.default.svc.cluster.local:3000
          - name: POD_OWNER_KIND
            value: deployment
          - name: KURL_PROXY_TLS_CERT_PATH
            value: /etc/kurl-proxy/ca/tls.crt
          - name: DEX_PGPASSWORD
            valueFrom:
              secretKeyRef:
                key: PGPASSWORD
                name: kotsadm-dex-postgres
        volumeMounts:
          - name: kotsadm-web-scripts
            mountPath: /scripts/start-kotsadm-web.sh
            subPath: start-kotsadm-web.sh
          - mountPath: /backup
            name: backup
          - name: kubelet-client-cert
            readOnly: true
            mountPath: /etc/kubernetes/pki/kubelet
          - name: kurl-proxy-kotsadm-tls-cert
            readOnly: true
            mountPath: /etc/kurl-proxy/ca
        resources:
          limits:
            cpu: 1
          requests:
            cpu: 100m
            memory: 100Mi

      initContainers:
      - name: schemahero-plan
        image: kotsadm/kotsadm-migrations:v1.50.2
        args: ["plan"]
        volumeMounts:
        - name: migrations
          mountPath: /migrations
        env:
        - name: SCHEMAHERO_DRIVER
          value: postgres
        - name: SCHEMAHERO_SPEC_FILE
          value: /tables
        - name: SCHEMAHERO_URI
          valueFrom:
            secretKeyRef:
              name: kotsadm-postgres
              key: uri
        - name: SCHEMAHERO_OUT
          value: /migrations/plan.yaml              
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 50m
            memory: 50Mi

      - name: schemahero-apply
        image: kotsadm/kotsadm-migrations:v1.50.2
        args: ["apply"]
        volumeMounts:
          - name: migrations
            mountPath: /migrations
        env:
        - name: SCHEMAHERO_DRIVER
          value: postgres
        - name: SCHEMAHERO_DDL
          value: /migrations/plan.yaml
        - name: SCHEMAHERO_URI
          valueFrom:
            secretKeyRef:
              name: kotsadm-postgres
              key: uri
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 50m
            memory: 50Mi

      - name: restore-db
        command:
        - "/restore-db.sh"
        env:
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              key: password
              name: kotsadm-postgres
        image: kotsadm/kotsadm:v1.50.2
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - mountPath: /backup
          name: backup
        resources:
          limits:
            cpu: 1
          requests:
            cpu: 100m
            memory: 100Mi

      - name: restore-s3
        command:
        - /restore-s3.sh
        env:
        - name: S3_ENDPOINT
          valueFrom:
            secretKeyRef:
              key: endpoint
              name: kotsadm-s3
        - name: S3_BUCKET_NAME
          value: kotsadm
        - name: S3_ACCESS_KEY_ID
          valueFrom:
            secretKeyRef:
              key: access-key-id
              name: kotsadm-s3
        - name: S3_SECRET_ACCESS_KEY
          valueFrom:
            secretKeyRef:
              key: secret-access-key
              name: kotsadm-s3
        - name: S3_BUCKET_ENDPOINT
          value: "true"
        image: kotsadm/kotsadm:v1.50.2
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - mountPath: /backup
          name: backup
        resources:
          limits:
            cpu: 1
          requests:
            cpu: 100m
            memory: 100Mi
