apiVersion: apps/v1
kind: Deployment
metadata:
  name: daniscarpa-backend
  namespace: backend
  labels:
    app: daniscarpa-backend
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 20%
      maxUnavailable: 0
  replicas: 3
  selector:
    matchLabels:
      app: daniscarpa-backend
  template:
    metadata:
      labels:
        app: daniscarpa-backend
    spec:
      containers:
      - name: daniscarpa-backend
        image: gcr.io/daniscarpa/backend:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 5000
        readinessProbe:
          httpGet:
            path: /time
            port: 5000
          initialDelaySeconds: 3
          periodSeconds: 10
          timeoutSeconds: 5
        env:
        - name: DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: database-url
              key: db-url
        - name: SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: auth
              key: secret-key
        - name: FLASK_APP
          value: /app/api/api.py
      - name: cloudsql-proxy
        image: gcr.io/cloudsql-docker/gce-proxy:1.14
        command:
        - /cloud_sql_proxy
        - -instances=daniscarpa:europe-west6:daniscarpa-db=tcp:5432
        - -credential_file=/secrets/cloudsql/credentials.json
        - -verbose=false
        lifecycle:
          preStop:
            exec:
              command:
              - /bin/sh
              - -c
              - while [ $(netstat -ltn | grep 21080 | wc -l) -eq 1 ]; do sleep 5; done
        volumeMounts:
        - name: cloudsql-instance-credentials
          mountPath: /secrets/cloudsql
          readOnly: true

      volumes:
      - name: cloudsql-instance-credentials
        secret:
          secretName: cloudsql-instance-credentials
