{{- if .Values.api.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: kargo-api
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "kargo.labels" . | nindent 4 }}
    {{- include "kargo.api.labels" . | nindent 4 }}
data:
  KARGO_NAMESPACE: {{ .Release.Namespace }}
  LOG_LEVEL: {{ quote .Values.api.logLevel }}
  {{- if .Values.kubeconfigSecrets.kargo }}
  KUBECONFIG: /etc/kargo/kubeconfig.yaml
  {{- end }}
  {{- if .Values.api.tls.enabled }}
  TLS_ENABLED: "true"
  TLS_CERT_PATH: /etc/kargo/tls.crt
  TLS_KEY_PATH: /etc/kargo/tls.key
  {{- end }}
  {{- if .Values.api.secretManagementEnabled }}
  SECRET_MANAGEMENT_ENABLED: "true"
  {{- end }}
  PERMISSIVE_CORS_POLICY_ENABLED: {{ quote .Values.api.permissiveCORSPolicyEnabled }}
  {{- if .Values.api.adminAccount.enabled }}
  ADMIN_ACCOUNT_ENABLED: "true"
  {{- if or .Values.api.tls.enabled (and .Values.api.ingress.enabled .Values.api.ingress.tls.enabled) }}
  ADMIN_ACCOUNT_TOKEN_ISSUER: https://{{ .Values.api.host }}
  {{- else }}
  ADMIN_ACCOUNT_TOKEN_ISSUER: http://{{ .Values.api.host }}
  {{- end }}
  ADMIN_ACCOUNT_TOKEN_AUDIENCE: {{ quote .Values.api.host }}
  ADMIN_ACCOUNT_TOKEN_TTL: {{ quote .Values.api.adminAccount.tokenTTL }}
  {{- end }}
  {{- if .Values.api.oidc.enabled }}
  OIDC_ENABLED: "true"
  OIDC_ADDITIONAL_SCOPES: {{ join "," .Values.api.oidc.additionalScopes }}
  {{- if .Values.api.oidc.globalServiceAccounts.namespaces }}
  GLOBAL_SERVICE_ACCOUNT_NAMESPACES: {{ .Release.Namespace }},{{ join "," .Values.api.oidc.globalServiceAccounts.namespaces }}
  {{- else }}
  GLOBAL_SERVICE_ACCOUNT_NAMESPACES: {{ .Release.Namespace }}
  {{- end }}
  {{- if .Values.api.oidc.dex.enabled }}
  {{- if or .Values.api.tls.enabled (and .Values.api.ingress.enabled .Values.api.ingress.tls.enabled) }}
  OIDC_ISSUER_URL: https://{{ .Values.api.host }}/dex
  {{- else }}
  OIDC_ISSUER_URL: http://{{ .Values.api.host }}/dex
  {{- end }}
  OIDC_CLIENT_ID: {{ quote .Values.api.host }}
  OIDC_CLI_CLIENT_ID: {{ .Values.api.host }}-cli
  DEX_ENABLED: "true"
  DEX_SERVER_ADDRESS: https://kargo-dex-server.{{ .Release.Namespace }}.svc
  DEX_CA_CERT_PATH: /etc/kargo/idp-ca.crt
  {{- else }}
  OIDC_ISSUER_URL: {{ quote .Values.api.oidc.issuerURL }}
  OIDC_CLIENT_ID: {{ quote .Values.api.oidc.clientID }}
  {{- if .Values.api.oidc.cliClientID }}
  OIDC_CLI_CLIENT_ID: {{ quote .Values.api.oidc.cliClientID }}
  {{- end }}
  {{- end }}
  {{- end }}
  {{- if .Values.api.argocd.urls }}
  ARGOCD_NAMESPACE: {{ .Values.controller.argocd.namespace | default "argocd" }}
  ARGOCD_URLS: {{ range $key, $val := .Values.api.argocd.urls }}{{ $key }}={{ $val }},{{- end }}
  {{- end }}
  ROLLOUTS_INTEGRATION_ENABLED: {{ quote .Values.api.rollouts.integrationEnabled }}
{{- end }}
