# Copyright (C) 2021  Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: shvsdb-deployment
  namespace: isecl
  labels:
    env: test
    app: shvsdb
spec:
  selector:
    matchLabels:
      app: shvsdb
  template:
    metadata:
      labels:
        app: shvsdb
    spec:
      securityContext:
        fsGroup: 2000
      initContainers:
        - image: busybox:1.32
          imagePullPolicy: Always
          name: chmod-db-data
          command:
            - /bin/chown
            - -R
            - "2000"
            - /var/lib/postgresql/data/pgdata
          volumeMounts:
            - name: shvs-db-data
              mountPath: /var/lib/postgresql/data/pgdata
      containers:
        - name: shvsdb
          image: postgres:11.7
          imagePullPolicy: Always
          securityContext:
            runAsUser: 2000
            runAsGroup: 2000
          args:
            - -c
            - hba_file=/etc/postgresql/config/pg_hba.conf
            - -c
            - config_file=/etc/postgresql/config/postgresql.conf
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_DB
              value: pgshvsdb
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: shvs-db-credentials
                  key: SHVS_DB_USERNAME
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: shvs-db-credentials
                  key: SHVS_DB_PASSWORD
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata/
          volumeMounts:
            - name: shvs-db-config
              mountPath: /etc/postgresql/config/
              readOnly: true
            - name: shvs-db-certs
              mountPath: /etc/postgresql/secrets/
              readOnly: true
            - name: shvs-db-data
              mountPath: /var/lib/postgresql/data/pgdata/
      volumes:
        - name: shvs-db-config
          configMap:
            name: shvs-db-config
            defaultMode: 0640
        - name: shvs-db-certs
          secret:
            secretName: shvs-db-certs
            defaultMode: 0640
        - name: shvs-db-data
          hostPath:
            path: /usr/local/kube/data/sgx-host-verification-service/pgdata
            type: DirectoryOrCreate
