# Copyright 2020 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: participant-manager-datastore
  labels:
    app: participant-manager-datastore
spec:
  replicas: 1
  selector:
    matchLabels:
      app: participant-manager-datastore
  template:
    metadata:
      labels:
        app: participant-manager-datastore
      annotations:
        sidecar.istio.io/inject: "true"
    spec:
      containers:
        # application container
        - name: participant-manager-datastore
          image: gcr.io/scrumlaunch2-dev-apps/participant-manager-datastore:latest
          env:
            # DB ACCESS
            - name: DB_INSTANCE_URL
              value: "127.0.0.1" 
            - name: DB_NAME
              valueFrom:
                secretKeyRef:
                  name: participant-manager-datastore-credentials
                  key: dbname
            - name: DB_USER
              valueFrom:
                secretKeyRef:
                  name: participant-manager-datastore-credentials
                  key: dbusername
            - name: DB_PASS
              valueFrom:
                secretKeyRef:
                  name: participant-manager-datastore-credentials
                  key: dbpassword
            # AUTHENTICATION
            - name: CLIENT_ID
              valueFrom:
                secretKeyRef:
                  name: participant-manager-datastore-credentials
                  key: client_id
            - name: SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: participant-manager-datastore-credentials
                  key: secret_key
            # MAIL SERVER
            - name: MAIL_USERNAME
              valueFrom:
                secretKeyRef:
                  name: email-credentials
                  key: email_address
            - name: MAIL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: email-credentials
                  key: email_password
            - name: SMTP_HOSTNAME
              valueFrom:
                secretKeyRef:
                  name: email-credentials
                  key: smtp_hostname
            - name: MAIL_CONTACT_EMAIL
              valueFrom:
                secretKeyRef:
                  name: email-credentials
                  key: contact_email_address
            - name: MAIL_FROM_EMAIL
              valueFrom:
                secretKeyRef:
                  name: email-credentials
                  key: from_email_address
            # SERVER CALLS
            - name: HYDRA_ADMIN_URL
              value: "http://hydra-admin-np:50000"
            - name: SCIM_AUTH_URL
              value: "http://auth-server-np:50000/auth-server"
            # MISC
            - name: PARTICIPANT_MANAGER_BASE_URL
              valueFrom:
                secretKeyRef:
                  name: shared-secrets
                  key: base_url
            - name: PARTICIPANT_MANAGER_URL
              value: "$(PARTICIPANT_MANAGER_BASE_URL)/participant-manager"
            - name: GCP_BUCKET_NAME
              valueFrom:
                secretKeyRef:
                  name: shared-secrets
                  key: consent_bucket_name
            - name: STUDY_BUILDER_GCP_BUCKET_NAME
              valueFrom:
                secretKeyRef:
                  name: shared-secrets
                  key: study_resources_bucket_name
            - name: ORG_NAME
              valueFrom:
                secretKeyRef:
                  name: shared-secrets
                  key: org_name
            - name: LOG_PATH
              valueFrom:
                secretKeyRef:
                  name: shared-secrets
                  key: log_path
            - name: GOOGLE_APPLICATION_CREDENTIALS
              value: "/secrets/gcloud_key/key.json"

          ports:
          - containerPort: 8080
          readinessProbe:
            httpGet:
              path: /participant-manager-datastore/healthCheck
              port: 8080
            initialDelaySeconds: 180
            periodSeconds: 20
          resources:
            requests:
              memory: "400Mi"
              cpu: "50m"
          volumeMounts:
          - name: gcloud-key-volume
            mountPath: /secrets/gcloud_key
            readOnly: true
        # sidecar container
        - name: cloudsql-proxy
          image: gcr.io/cloudsql-docker/gce-proxy:latest
          command: ["/cloud_sql_proxy",
            "-instances=scrumlaunch2-dev-data:us-central1:mystudies=tcp:3306",
            "-credential_file=/secrets/gcloud_key/key.json"]
          volumeMounts:
          - name: gcloud-key-volume
            mountPath: /secrets/gcloud_key
            readOnly: true
      volumes:
      - name: gcloud-key-volume
        secret:
          secretName: participant-manager-gke-sa-gcloud-key
