apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
  namespace: resources
  labels:
    app: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
      annotations:
        traffic.sidecar.istio.io/excludeOutboundPorts: "8200"
        vault.hashicorp.com/agent-init-first: "true"
        vault.hashicorp.com/agent-inject: "true"
        vault.hashicorp.com/agent-pre-populate-only: "true"
        vault.hashicorp.com/auth-path: auth/kubernetes
        vault.hashicorp.com/role: resources
        vault.hashicorp.com/agent-inject-secret-resources-db: secrets/data/postgresql/apps/resources
        vault.hashicorp.com/agent-inject-template-resources-db: |-
          {{- with secret "secrets/data/postgresql/apps/resources" -}}
          DATABASE_HOST=postgresql.resources.svc.cluster.local
          DATABASE_PORT=5432
          DATABASE_NAME=resources_db
          DATABASE_USER={{ index .Data.data "username" }}
          DATABASE_PASSWORD={{ index .Data.data "password" }}
          {{- end -}}
        vault.hashicorp.com/agent-inject-secret-resources-s3: secrets/data/minio/apps/resources
        vault.hashicorp.com/agent-inject-template-resources-s3: |-
          {{- with secret "secrets/data/minio/apps/resources" -}}
          YC_S3_ENDPOINT_URL={{ index .Data.data.client "endpoint" }}
          YC_S3_BUCKET_NAME=resources
          YC_S3_ACCESS_KEY_ID={{ index .Data.data "access_key" }}
          YC_S3_SECRET_ACCESS_KEY={{ index .Data.data "secret_key" }}
          {{- end -}}
    spec:
      serviceAccountName: resources-vault
      volumes:
        - name: django-configmap
          configMap:
            name: django-configmap
            items:
              - key: production.py
                path: production.py
          defaultMode: 420
        - name: uwsgi-configmap
          configMap:
            name: uwsgi-configmap
            items:
              - key: uwsgi.ini
                path: uwsgi.ini
            defaultMode: 420

      containers:
        - name: backend
          image: cr.yandex/crp3ccidau046kdj8g9q/sarex-resources:prod_d642ef88
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh", "-ec"]
          args:
            - |
              set -a
              [ -f /vault/secrets/resources-db ] && . /vault/secrets/resources-db
              [ -f /vault/secrets/resources-s3 ] && . /vault/secrets/resources-s3
              set +a
              exec /server/entrypoint.sh

          ports:
            - name: http
              containerPort: 8000
              protocol: TCP

          env:
            - name: DJANGO_SETTINGS_MODULE
              value: config.settings.production
            - name: API_ADDRESS
              value: "8000"


          resources:
            requests:
              cpu: 25m
              memory: 100Mi

          volumeMounts:
            - name: django-configmap
              mountPath: /server/config/settings/production.py
              subPath: production.py
            - name: uwsgi-configmap
              mountPath: opt/server/uwsgi.ini
              subPath: uwsgi.ini

          livenessProbe:
            tcpSocket:
              port: 8000
            initialDelaySeconds: 20
            periodSeconds: 30
            timeoutSeconds: 3
            failureThreshold: 5

          readinessProbe:
            tcpSocket:
              port: 8000
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 3
            failureThreshold: 6

      imagePullSecrets:
        - name: regcred
