---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: celery
  namespace: django
  labels:
    app: celery
    service: celery
spec:
  replicas: 1
  selector:
    matchLabels:
      app: celery
  template:
    metadata:
      labels:
        app: celery
        service: celery
      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: django
        vault.hashicorp.com/agent-inject-secret-django-postgresql: secrets/data/postgresql/apps/django
        vault.hashicorp.com/agent-inject-template-django-postgresql: |-
          {{- with secret "secrets/data/postgresql/apps/django" -}}
          DJANGO_POSTGRES_HOST=postgresql.django.svc.cluster.local
          DJANGO_POSTGRES_PORTS=5432
          DJANGO_POSTGRES_DATABASE=sarex_db
          DJANGO_POSTGRES_USER={{ index .Data.data "username" }}
          DJANGO_POSTGRES_PASSWORD={{ index .Data.data "password" }}
          {{- end -}}
        vault.hashicorp.com/agent-inject-secret-django-rabbitmq: secrets/data/rabbitmq/apps/django
        vault.hashicorp.com/agent-inject-template-django-rabbitmq: |-
          {{- with secret "secrets/data/rabbitmq/apps/django" -}}
          CELERY_RABBITMQ_HOST=rabbitmq.rabbitmq.svc.cluster.local
          CELERY_RABBITMQ_USER={{ index .Data.data "username" }}
          CELERY_RABBITMQ_PASSWORD={{ index .Data.data "password" }}
          CELERY_RABBITMQ_VHOST={{ index .Data.data "vhost" }}
          DJANGO_RABBIT_HOSTNAME=rabbitmq.rabbitmq.svc.cluster.local
          DJANGO_RABBIT_USER={{ index .Data.data "username" }}
          DJANGO_RABBIT_PASS={{ index .Data.data "password" }}
          DJANGO_RABBIT_VHOST={{ index .Data.data "vhost" }}
          {{- end -}}
        vault.hashicorp.com/agent-inject-secret-django-s3: secrets/data/minio/apps/django
        vault.hashicorp.com/agent-inject-template-django-s3: |-
          {{- with secret "secrets/data/minio/apps/django" -}}
          AWS_S3_ENDPOINT_URL=https://minio.contour.infra.sarex.tech
          S3_HOST=https://minio.contour.infra.sarex.tech
          {{- $buckets := index .Data.data "buckets" }}
          S3_BUCKET={{- if gt (len $buckets) 0 -}}{{ index (index $buckets 0) "name" }}{{- else -}}django{{- end -}}
          S3_LOGIN={{ index .Data.data "access_key" }}
          S3_PASSWORD={{ index .Data.data "secret_key" }}
          {{- end -}}
        vault.hashicorp.com/agent-inject-secret-django-kafka: secrets/data/kafka/apps/django
        vault.hashicorp.com/agent-inject-template-django-kafka: |-
          {{- with secret "secrets/data/kafka/apps/django" -}}
          KAFKA_BOOTSTRAP_SERVERS="[\"{{ index .Data.data.auth "bootstrap_servers" }}\"]"
          KAFKA_SECURITY_PROTOCOL={{ index .Data.data.auth "security_protocol" }}
          KAFKA_SASL_MECHANISM={{ index .Data.data.auth "sasl_mechanism" }}
          KAFKA_SASL_PLAIN_USERNAME={{ index .Data.data "username" }}
          KAFKA_SASL_PLAIN_PASSWORD={{ index .Data.data "password" }}
          {{- end -}}
        vault.hashicorp.com/agent-inject-secret-django-jwt-private: secrets/data/vault/common/rsa_keys
        vault.hashicorp.com/agent-inject-template-django-jwt-private: |-
          {{- with secret "secrets/data/vault/common/rsa_keys" -}}
          {{ index .Data.data "private_key" }}
          {{- end -}}
        vault.hashicorp.com/agent-inject-secret-django-jwt-public: secrets/data/vault/common/rsa_keys
        vault.hashicorp.com/agent-inject-template-django-jwt-public: |-
          {{- with secret "secrets/data/vault/common/rsa_keys" -}}
          {{ index .Data.data "public_key" }}
          {{- end -}}
        vault.hashicorp.com/agent-inject-secret-django-common: secrets/data/vault/common/django_auth
        vault.hashicorp.com/agent-inject-template-django-common: |-
          {{- with secret "secrets/data/vault/common/django_auth" -}}
          ZITADEL_ACCESS_TOKEN={{ index .Data.data "django_zitadel_access_token" }}
          {{- end -}}
    spec:
      serviceAccountName: django-vault
      volumes:
        - name: django-configmap
          configMap:
            name: django-configmap
            items:
              - key: production.py
                path: production.py
          defaultMode: 420
      containers:
        - name: celery
          image: cr.yandex/crp3ccidau046kdj8g9q/backend:production_a96dead0
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh", "-ec"]
          args:
            - |
              set -a
              [ -f /vault/secrets/django-postgresql ] && . /vault/secrets/django-postgresql
              [ -f /vault/secrets/django-rabbitmq ] && . /vault/secrets/django-rabbitmq
              [ -f /vault/secrets/django-s3 ] && . /vault/secrets/django-s3
              [ -f /vault/secrets/django-kafka ] && . /vault/secrets/django-kafka
              [ -f /vault/secrets/django-common ] && . /vault/secrets/django-common
              [ -f /vault/secrets/django-jwt-private ] && export JWT_PRIVATE_KEY="$(cat /vault/secrets/django-jwt-private)"
              [ -f /vault/secrets/django-jwt-public ] && export JWT_PUBLIC_KEY="$(cat /vault/secrets/django-jwt-public)"
              set +a
              exec celery -A config worker -B -l info -E -Q default -n default_worker.%h --concurrency=2

          ports:
            - name: http
              containerPort: 8000
              protocol: TCP
          env:
            - name: ALLOWED_HOSTS
              value: '*'
            - name: SERVER_USE_CHANGELOG
              value: "0"
            - name: SERVER_ZITADEL_ENABLED
              value: "False"
            - name: DJANGO_SETTINGS_MODULE
              value: config.settings.production
            - name: CELERY_REDIS_HOST
              value: redis
            - name: CELERY_REDIS_PORT
              value: "6379"
            - name: DJANGO_REDIS_HOST
              value: redis
            - name: DJANGO_REDIS_PORT
              value: "6379"
            - name: BIMV2_INTERNAL_HOST
              value: http://bim-backend-v2-service.bim-api
            - name: BIMV2_TIMEOUT
              value: "60"
            - name: JWT_KID
              value: "1"
            - name: PDM_SYNC
              value: "1"
            - name: KC_SYNC_ENABLE
              value: "0"
            - name: MEASUREMENTS_HOST
              value: http://measurements-service.measurements.svc.cluster.local:8000/api
            - name: MEASUREMENTS_USE_MEASUREMENTS
              value: "1"
            - name: SERVER_API_HOST
              value: https://wb.sarex.io
            - name: SERVER_HOST
              value: https://wb.sarex.io
            - name: WORKFLOWS_HOST
              value: https://wb.sarex.io
            - name: WORKFLOWS_BASE_HOST
              value: https://wb.sarex.io
            - name: WORKFLOWS_USE
              value: "1"
            - name: SERVER_S3_STREAM_IMPORT
              value: "1"
            - name: SERVER_SAVE_DIFF_DEM
              value: "1"
            - name: SERVER_USE_CLICKHOUSE
              value: "0"
            - name: SERVER_USE_CREATE_COMPARED_GEOTIFF_TASK
              value: "0"
            - name: SERVER_USE_DJANGO_STORAGE
              value: "1"
            - name: SERVER_USE_METASHAPE
              value: "0"
            - name: SERVER_CHANGELOG_MODE_SYSTEM_LOG
              value: "1"
            - name: SERVER_CHANGELOG_MODE
              value: "0"
            - name: SERVER_DJANGO_URLS
              value: "1"
            - name: CHECK_IMPORT_HASH
              value: "1"
            - name: EAV_ENABLE
              value: "1"
            - name: SERVER_CHECK_IMPORT_HASH
              value: "1"
            - name: SERVER_CHUNKED_PATH
              value: /tmp/chunked_uploads/%Y/%m/%d
            - name: SERVER_HIDE_USER_SCROLL_PERMISSIONS
              value: "0"
            - name: SERVER_USE_WRORKFLOW_STATUS
              value: "1"
            - name: ZITADEL_HOST
              value: https://zitadel-srx.wb.ru
            - name: SERVER_KAFKA_ENABLED
              value: "False"
            - name: KAFKA_TOPICS
              value: '{"planning": "message-hub-stage", "ams-sync": "ams-sync"}'
            - name: KAFKA_SSL_CAFILE
              value: /usr/local/share/ca-certificates/kafka.crt
            - name: KC_USE_REDIRECT_LOGOUT
              value: "False"
          resources:
            requests:
              cpu: "25m"
              memory: 128Mi
          volumeMounts:
            - name: django-configmap
              mountPath: /opt/sarex/config/settings/production.py
              subPath: production.py
      imagePullSecrets:
        - name: regcred
