---
# Патч backend для контура УГМК.
#
# ВНИМАНИЕ: args — список, kustomize заменяет его ЦЕЛИКОМ (JSON merge patch),
# поэтому здесь продублирован весь скрипт из apps/pm/base/backend.yaml.
# При правке args в base их нужно продублировать и сюда.
# podAnnotations — map, мержится по ключам, поэтому переопределён только
# один ключ agent-inject-template-pm-s3; остальные vault-аннотации берутся из base.
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
  name: backend
  namespace: pm
spec:
  values:
    services:
      backend:
        image:
          name:
            _default: cr.yandex/crp3ccidau046kdj8g9q/pm-backend:production_fa0f1551

        deployment:
          args:
            _default:
              - |
                set -a
                [ -f /vault/secrets/pm-db ] && . /vault/secrets/pm-db
                [ -f /vault/secrets/pm-rabbitmq ] && . /vault/secrets/pm-rabbitmq
                [ -f /vault/secrets/pm-s3 ] && . /vault/secrets/pm-s3
                [ -f /vault/secrets/pm-kafka ] && . /vault/secrets/pm-kafka
                set +a
                python manage.py makemigrations pm --noinput
                python manage.py migrate
                exec /opt/sarex/entrypoint.sh
        envs:
          - name: USERS_INTERNAL_HOST
            value:
              _default: "http://backend-svc.django.svc.cluster.local:80"

          - name: CELERY_REDIS_HOST
            value:
              _default: "redis.pm.svc.cluster.local"

          - name: RESOURCES_INTERNAL_HOST
            value:
              _default: "http://iam-backend.iam.svc.cluster.local:8000"

          - name: EAV_HOST
            value:
              _default: "http://backend-svc.eav.svc.cluster.local:80"

          - name: EAV_API_PREFIX
            value:
              _default: "/api/v0"

          - name: EAV_API_PREFIX_V1
            value:
              _default: "/api/v1"

          - name: TRACING_INSECURE
            value:
              _default: "False"

          - name: SERVER_ENABLE_SYNC_RESOURCES
            value:
              _default: "True"

          - name: SERVER_DELETED_TASK_MAX_AGE_DAYS
            value:
              _default: "1"

          - name: SERVER_EXPIRED_TASK_NOTIFICATION_HOUR
            value:
              _default: "17"

          - name: LANG
            value:
              _default: "C.UTF-8"

          - name: LC_ALL
            value:
              _default: "C.UTF-8"

          - name: PYTHONUTF8
            value:
              _default: "1"

          - name: CACHE_SSL
            value:
              _default: "False"

          - name: CACHE_SSL_CA_CERTS
            value:
              _default: ""

          # base: "False". При False CACHE_CLIENT = None (config/settings/base.py:190),
          # и GET /api/pm/msp/external-task-info/ падает с
          # AttributeError: 'NoneType' object has no attribute 'get' —
          # sarex/pm/utils/broker_producer.py:38 дёргает .get() без проверки на None.
          - name: CACHE_ENABLE
            value:
              _default: "True"

          # Новые: в base их нет. Дефолт CacheSettings.host — localhost,
          # т.е. без них включённый кэш стучится в собственный под.
          # Redis разворачивается тут же — apps/pm/d8-ugmk-prod/redis.yaml.
          - name: CACHE_HOST
            value:
              _default: "redis.pm.svc.cluster.local"

          - name: CACHE_PORT
            value:
              _default: "6379"

          - name: CLICKHOUSE_ENABLE
            value:
              _default: "False"

          - name: KAFKA_ENABLE
            value:
              _default: "True"

          - name: KAFKA_BOOTSTRAP_SERVERS
            value:
              _default: '["kafka-kafka-contour.kafka.svc.cluster.local:9092"]'

          - name: KAFKA_SECURITY_PROTOCOL
            value:
              _default: "SASL_SSL"

          - name: KAFKA_SSL_CAFILE
            value:
              _default: "/usr/local/share/ca-certificates/kafka.crt"

          - name: KAFKA_TOPICS
            value:
              _default: '{"planning": "message-hub-prod"}'

          - name: AUTH_PUBLIC_TOKEN_URL
            value:
              _default: "https://sarex.uralmine.com/api/token/public/"

          - name: SERVER_HOST
            value:
              _default: "https://sarex.uralmine.com"

          - name: SERVER_API_HOST
            value:
              _default: "https://sarex.uralmine.com"

          - name: SERVER_DEBUG
            value:
              _default: "False"

          - name: SERVER_ALLOWED_HOSTS
            value:
              _default: '["*"]'

          - name: SERVER_USE_OTEL
            value:
              _default: "False"

          - name: SERVER_VERIFY_SSL
            value:
              _default: "False"

          - name: SERVER_LOG_LEVEL
            value:
              _default: "INFO"

        volumes:
          _default:
            - name: kafka-ca-cert
              mountPath:
                _default: /usr/local/share/ca-certificates/kafka.crt
              subPath:
                _default: kafka.crt
              readOnly:
                _default: true
              configMap:
                name:
                  _default: kafka-ca-cert
                items:
                  - key: kafka.crt
                    path:
                      _default: kafka.crt

        podAnnotations:
          _default:
            vault.hashicorp.com/agent-inject-template-pm-s3: |-
              {{- with secret "secrets/data/minio/apps/pm" -}}
              S3_HOST={{ index .Data.data.client "endpoint" }}
              S3_LOGIN={{ index .Data.data "access_key" }}
              S3_PASSWORD={{ index .Data.data "secret_key" }}
              S3_VERIFY=False
              {{- $buckets := index .Data.data "buckets" }}
              S3_BUCKET={{ if gt (len $buckets) 0 }}{{ index (index $buckets 0) "name" }}{{ else }}sarex-media-storage{{ end }}
              {{- end -}}
            vault.hashicorp.com/agent-inject-secret-pm-kafka: secrets/data/kafka/apps/pm
            vault.hashicorp.com/agent-inject-template-pm-kafka: |-
              {{- with secret "secrets/data/kafka/apps/pm" -}}
              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 -}}
