# 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: study-builder
  labels:
    app: study-builder
spec:
  replicas: 1
  selector:
    matchLabels:
      app: study-builder
  template:
    metadata:
      labels:
        app: study-builder
      annotations:
        sidecar.istio.io/inject: "true"
    spec:
      containers:
        - name: study-builder
          image: gcr.io/<PREFIX>-<ENV>-apps/study-builder:latest
          env:
            # DB ACCESS
            - name: DB_USER
              valueFrom:
                secretKeyRef:
                  name: study-builder-credentials
                  key: dbusername
            - name: DB_PASS
              valueFrom:
                secretKeyRef:
                  name: study-builder-credentials
                  key: dbpassword
            - name: DB_NAME
              valueFrom:
                secretKeyRef:
                  name: study-builder-credentials
                  key: dbname
            - name: DB_INSTANCE_URL
              value: "127.0.0.1"      
            # MAIL SERVER
            - name: FROM_EMAIL_ADDRESS
              valueFrom:
                secretKeyRef:
                  name: email-credentials
                  key: email_address
            - name: MAIL_CONTACT_EMAIL
              valueFrom:
                secretKeyRef:
                  name: email-credentials
                  key: contact_email_address
            - name: FROM_EMAIL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: email-credentials
                  key: email_password
            - name: SMTP_HOSTNAME
              valueFrom:
                secretKeyRef:
                  name: email-credentials
                  key: smtp_hostname
            - name: SMTP_USE_IP_ALLOWLIST
              valueFrom:
                secretKeyRef:
                  name: email-credentials
                  key: smtp_use_ip_allowlist
            - name: FROM_EMAIL_DOMAIN
              valueFrom:
                secretKeyRef:
                  name: email-credentials
                  key: from_email_domain
            #  AUTHENTICATION
            - name: CLIENT_ID
              valueFrom:
                secretKeyRef:
                  name: study-builder-credentials
                  key: client_id
            - name: SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: study-builder-credentials
                  key: secret_key
            # SERVER CALLS
            - name: STUDY_BUILDER_BASE_URL
              valueFrom:
                secretKeyRef:
                  name: shared-secrets
                  key: studies_base_url
            - name: PARTICIPANT_USER_DATASTORE_URL
              value: "http://participant-user-datastore-np:50000/participant-user-datastore"
            - name: RESPONSE_DATASTORE_URL
              value: "http://response-datastore-np:50000/response-datastore"
            - name: SCIM_AUTH_URL
              value: "http://auth-server-np:50000/auth-server"
            # MISC
            - name: 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: CATALINA_OPTS # server timezone
              value: -Duser.timezone=America/New_York
            - name: GOOGLE_APPLICATION_CREDENTIALS
              value: "/secrets/gcloud_key/key.json"
          ports:
          - containerPort: 8080
          readinessProbe:
            httpGet:
              path: /studybuilder/healthCheck.do
              port: 8080
            initialDelaySeconds: 180
            periodSeconds: 20
          resources:
            requests:
              memory: "500Mi"
              cpu: "50m"
          volumeMounts:
          - name: gcloud-key-volume
            mountPath: /secrets/gcloud_key
            readOnly: true
        - name: cloudsql-proxy
          image: gcr.io/cloudsql-docker/gce-proxy:latest
          command: ["/cloud_sql_proxy",
            "-instances=<PREFIX>-<ENV>-data:<LOCATION>: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: study-builder-gke-sa-gcloud-key
