---
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
  name: iam-backend
  namespace: iam

spec:
  interval: 10m

  chart:
    spec:
      chart: universal-chart
      version: "0.1.9"
      sourceRef:
        kind: HelmRepository
        name: yc-oci-charts
        namespace: flux-system
      interval: 10m

  install:
    remediation:
      retries: 3

  upgrade:
    remediation:
      retries: 3

  values:
    global:
      env: _default

    services:
      backend:
        enabled: true

        serviceAccount:
          enabled:
            _default: true
          name:
            _default: iam-vault

        image:
          name:
            _default: cr.yandex/crp3ccidau046kdj8g9q/iams:contour_dada80a8
          pullPolicy:
            _default: IfNotPresent

        deployment:
          enabled: true

          name:
            _default: iam-backend

          replicaCount:
            _default: 1
            production: 4

          port:
            _default: 8080

          command:
            _default: ["/bin/sh", "-ec"]
          args:
            _default:
              - |
                set -a
                [ -f /vault/secrets/iam-db ] && . /vault/secrets/iam-db
                [ -f /vault/secrets/iam-zitadel ] && . /vault/secrets/iam-zitadel
                [ -f /vault/secrets/iam-kafka ] && . /vault/secrets/iam-kafka
                [ -f /vault/secrets/iam-s3 ] && . /vault/secrets/iam-s3
                set +a
                exec ./server

          probes:
            liveness:
              enabled: false
            readiness:
              enabled: false

        service:
          enabled: true

          name:
            _default: iam-backend

          type:
            _default: ClusterIP

          port:
            _default: 8000

          targetPort:
            _default: 8080

          portName:
            _default: http

        imagePullSecrets:
          enabled:
            _default: true
          name:
            _default: regcred

        volumes:
          _default:
            - name: iam-zitadel-org-rules
              mountPath:
                _default: /config/zitadel/org-rules-prod.json
              subPath:
                _default: org-rules-prod.json
              readOnly:
                _default: true
              configMap:
                name:
                  _default: iam-zitadel-org-rules
                items:
                  - key: org-rules-prod.json
                    path:
                      _default: org-rules-prod.json

        labels:
          monitoring: prometheus

        envs:
          - name: ENVIRONMENT
            value:
              _default: "prod"

          - name: LOG_LEVEL
            value:
              _default: "debug"

          - name: AUTH_ENABLED
            value:
              _default: "true"

          - name: HTTP_PORT
            value:
              _default: "8080"

          - name: HTTP_READ_BUFFER_SIZE
            value:
              _default: "131072"

          - name: DB_MIGRATIONS_PATH
            value:
              _default: "migrations"

          - name: ZITADEL_ENABLED
            value:
              _default: "true"

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

          - name: ZITADEL_ORG_RULES_FILE
            value:
              _default: "config/zitadel/org-rules-prod.json"

          - name: S3_ENABLED
            value:
              _default: "true"

          - name: S3_ENDPOINT_URL
            value:
              _default: "https://storage.yandexcloud.net"

          - name: S3_BUCKET_NAME
            value:
              _default: "sarex-media-storage"

          - name: S3_REGION
            value:
              _default: "ru-central1"

          - name: S3_PRESIGN_EXPIRES
            value:
              _default: "1h"

          - name: KAFKA_ENABLED
            value:
              _default: "false"

          - name: KAFKA_SSL_CAFILE
            value:
              _default: "/etc/ca-certificates/Yandex/ca-cert"

          - name: KAFKA_TOPIC_LEGACY_AMS_SYNC
            value:
              _default: "ams-sync"

        podAnnotations:
          _default:
            traffic.sidecar.istio.io/excludeOutboundPorts: "4317,4318,9411,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: iam
            vault.hashicorp.com/agent-inject-secret-iam-db: secrets/data/apps/iam/postgres
            vault.hashicorp.com/agent-inject-template-iam-db: |-
              {{- with secret "secrets/data/apps/iam/postgres" -}}
              DB_DSN=postgres://{{ index .Data.data "username" }}:{{ index .Data.data "password" }}@{{ index .Data.data "host" }}:{{ index .Data.data "port" }}/{{ index .Data.data "database" }}
              {{- end -}}
            vault.hashicorp.com/agent-inject-secret-iam-zitadel: secrets/data/vault/common/django_auth
            vault.hashicorp.com/agent-inject-template-iam-zitadel: |-
              {{- with secret "secrets/data/vault/common/django_auth" -}}
              ZITADEL_ACCESS_TOKEN={{ index .Data.data "django_zitadel_access_token" }}
              {{- end -}}
            vault.hashicorp.com/agent-inject-secret-iam-kafka: secrets/data/kafka/apps/iam
            vault.hashicorp.com/agent-inject-template-iam-kafka: |-
              {{- with secret "secrets/data/kafka/apps/iam" -}}
              KAFKA_SECURITY_PROTOCOL={{ index .Data.data.auth "security_protocol" }}
              KAFKA_SASL_MECHANISM={{ index .Data.data.auth "sasl_mechanism" }}
              KAFKA_BROKERS={{ index .Data.data.auth "bootstrap_servers" }}
              KAFKA_SASL_PLAIN_USERNAME={{ index .Data.data "username" }}
              KAFKA_SASL_PLAIN_PASSWORD={{ index .Data.data "password" }}
              {{- end -}}
            vault.hashicorp.com/agent-inject-secret-iam-s3: secrets/data/minio/apps/iam
            vault.hashicorp.com/agent-inject-template-iam-s3: |-
              {{- with secret "secrets/data/minio/apps/iam" -}}
              S3_ACCESS_KEY_ID={{ index .Data.data "access_key" }}
              S3_SECRET_ACCESS_KEY={{ index .Data.data "secret_key" }}
              {{- end -}}

        commitSha: ""
        gitlabUri: ""
        gitlabJobUrl: ""
        owner: ""
