{{- if not .Values.config.existingConfig }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "pomerium.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ template "pomerium.name" . }}
    helm.sh/chart: {{ template "pomerium.chart" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
data:
  config.yaml: |
{{- if and .Values.config.existingPolicy .Values.config.extraOpts }}
{{ fail "Cannot use config.extraOpts with config.existingPolicy" }}
{{- end }}
{{- if and .Values.config.existingPolicy .Values.config.policy }}
{{ fail "Cannot use config.policy with config.existingPolicy" }}
{{- end }}
{{- if .Values.config.extraOpts }}
{{ toYaml .Values.config.extraOpts | indent 4 -}}
{{- end -}}
{{- if .Values.metrics.enabled }}
    metrics_address: :{{ .Values.metrics.port }}
{{- end -}}
{{- if .Values.tracing.enabled }}
    tracing_debug: {{ .Values.tracing.debug }}
    tracing_provider: {{ required "tracing_provider is required for tracing" .Values.tracing.provider }}

{{- if eq .Values.tracing.provider "jaeger" }}
    tracing_jaeger_collector_endpoint: {{ required "collector_endpoint is required for jaeoger tracing" .Values.tracing.jaeger.collector_endpoint }}
    tracing_jaeger_agent_endpoint: {{ required "agent_endpoint is required for jaeger tracing" .Values.tracing.jaeger.agent_endpoint }}
{{- end -}}

{{- end -}}
{{- if .Values.forwardAuth.enabled }}
    forward_auth_url: https://{{ template "pomerium.forwardAuth.name" . }}
{{- end -}}
{{- if .Values.config.policy }}
    policy: 
{{ toYaml .Values.config.policy | indent 6 }}
{{- end -}}
{{- end }}
