{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{/* Adding the helper in configmap.yaml for better readability */}}
{{- define "victoriametrics.vmauth.defaultConfig" -}}
unauthorized_user:
  url_map:
  - discover_backend_ips: true
    src_paths:
    - /select/.*
    url_prefix: http://{{ include "victoriametrics.vmselect.fullname" . }}:{{ .Values.vmselect.service.ports.http }}
  - discover_backend_ips: true
    src_paths:
    - /insert/.*
    url_prefix: http://{{ include "victoriametrics.vminsert.fullname" . }}:{{ .Values.vminsert.service.ports.http }}
{{- end }}

{{- if and .Values.vmauth.enabled (not .Values.vmauth.existingSecret) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "victoriametrics.vmauth.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: victoriametrics
    app.kubernetes.io/component: vmauth
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
stringData:
  {{- /* Convert the default configuration and extra overrides */ -}}
  {{- $defaultConfiguration := include "victoriametrics.vmauth.defaultConfig" . | fromYaml -}}
  {{- $overrideConfiguration := include "common.tplvalues.render" (dict "value" .Values.vmauth.configOverrides "context" $) | fromYaml -}}

  {{- /* Merge both maps and render the configuration */}}
  auth.yml: |
    {{- mergeOverwrite $defaultConfiguration $overrideConfiguration | toYaml | nindent 4 }}
{{- end }}
