apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
  name: zitadel
  namespace: zitadel
spec:
  interval: 5m
  timeout: 10m
  dependsOn:
    - name: postgresql
      namespace: zitadel
  postRenderers:
    - kustomize:
        patches:
          - target:
              group: apps
              version: v1
              kind: Deployment
              name: zitadel-idp-contour
            patch: |-
              - op: add
                path: /spec/template/spec/nodeSelector
                value:
                  node.deckhouse.io/group: generic
              - op: add
                path: /spec/template/spec/tolerations
                value:
                  - key: dedicated.deckhouse.io
                    operator: Equal
                    value: generic
              - op: replace
                path: /spec/template/metadata/annotations/vault.hashicorp.com~1agent-inject-template-zitadel-vault-config.yaml
                value: |-
                  {{- with secret "secrets/data/zitadel/postgresql" -}}
                  Database:
                    postgres:
                      User:
                        Password: |-
                          {{ index .Data.data "password" }}
                      Admin:
                        Password: |-
                          {{ index .Data.data "adminPassword" }}
                  FirstInstance:
                    Org:
                      Human:
                        Password: |-
                          {{ index .Data.data "humanPassword" }}
                  {{- end -}}
          - target:
              group: batch
              version: v1
              kind: Job
              name: zitadel-idp-contour-init
            patch: |-
              - op: add
                path: /spec/template/spec/nodeSelector
                value:
                  node.deckhouse.io/group: generic
              - op: add
                path: /spec/template/spec/tolerations
                value:
                  - key: dedicated.deckhouse.io
                    operator: Equal
                    value: generic
              - op: replace
                path: /spec/template/metadata/annotations/vault.hashicorp.com~1agent-inject-template-zitadel-vault-config.yaml
                value: |-
                  {{- with secret "secrets/data/zitadel/postgresql" -}}
                  Database:
                    postgres:
                      User:
                        Password: |-
                          {{ index .Data.data "password" }}
                      Admin:
                        Password: |-
                          {{ index .Data.data "adminPassword" }}
                  FirstInstance:
                    Org:
                      Human:
                        Password: |-
                          {{ index .Data.data "humanPassword" }}
                  {{- end -}}
          - target:
              group: batch
              version: v1
              kind: Job
              name: zitadel-idp-contour-setup
            patch: |-
              - op: add
                path: /spec/template/spec/nodeSelector
                value:
                  node.deckhouse.io/group: generic
              - op: add
                path: /spec/template/spec/tolerations
                value:
                  - key: dedicated.deckhouse.io
                    operator: Equal
                    value: generic
              - op: replace
                path: /spec/template/metadata/annotations/vault.hashicorp.com~1agent-inject-template-zitadel-vault-config.yaml
                value: |-
                  {{- with secret "secrets/data/zitadel/postgresql" -}}
                  Database:
                    postgres:
                      User:
                        Password: |-
                          {{ index .Data.data "password" }}
                      Admin:
                        Password: |-
                          {{ index .Data.data "adminPassword" }}
                  FirstInstance:
                    Org:
                      Human:
                        Password: |-
                          {{ index .Data.data "humanPassword" }}
                  {{- end -}}
  values:
    zitadel:
      configmapConfig:
        ExternalDomain: sarex-login.uralmine.com
        ExternalSecure: true
      debug:
        enabled: false
    postgresqlSecret:
      create: false
      vault:
        enabled: true
        role: zitadel
        authPath: auth/kubernetes
        secretPath: secrets/data/zitadel/postgresql
        secretKey: password
        kvVersion: 2
        fileName: zitadel-vault-config.yaml
    serviceAccount:
      create: true
      name: zitadel
    replicaCount: 1
    pdb:
      enabled: false
    env:
      - name: ZITADEL_DEFAULTINSTANCE_FEATURES_LOGINV2_REQUIRED
        value: "false"
      - name: ZITADEL_SYSTEMDEFAULTS_PASSWORDHASHER_VERIFIERS
        value: "bcrypt,pbkdf2"
      - name: ZITADEL_MACHINE_IDENTIFICATION_HOSTNAME_ENABLED
        value: "true"
      - name: ZITADEL_DATABASE_POSTGRES_HOST
        value: "postgresql.zitadel.svc.cluster.local"
      - name: ZITADEL_DATABASE_POSTGRES_PORT
        value: "5432"
      - name: ZITADEL_DATABASE_POSTGRES_USER_USERNAME
        value: "zitadel"
      - name: ZITADEL_DATABASE_POSTGRES_ADMIN_EXISTINGDATABASE
        value: "zitadel"
      - name: ZITADEL_DATABASE_POSTGRES_ADMIN_USERNAME
        value: "zitadel"
      - name: ZITADEL_DATABASE_POSTGRES_DATABASE
        value: "zitadel"
      - name: ZITADEL_DATABASE_POSTGRES_USER_SSL_MODE
        value: "disable"
      - name: ZITADEL_DATABASE_POSTGRES_ADMIN_SSL_MODE
        value: "disable"
      - name: ZITADEL_DEFAULTINSTANCE_ORG_HUMAN_USERNAME
        value: "zitadel-admin"
      - name: ZITADEL_DEFAULTINSTANCE_ORG_NAME
        value: "Sarex"
