{{- $root := . -}}

{{- if not .Values.gerrit.etc.existingConfigMapName }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-gerrit-configmap
  labels:
    app.kubernetes.io/component: gerrit
    app.kubernetes.io/instance: {{ .Release.Name }}
    chart: {{ template "gerrit.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 4 }}
    {{- end }}
data:
  {{- range $key, $value := .Values.gerrit.etc.config }}
  {{ $key }}:
{{ toYaml $value | indent 4 }}
  {{- end }}
  {{- if not (hasKey .Values.gerrit.etc.config "healthcheck.config") }}
  healthcheck.config: |-
    [healthcheck "auth"]
      # On new instances there may be no users to use for healthchecks
      enabled = false
    [healthcheck "querychanges"]
      # On new instances there won't be any changes to query
      enabled = false
  {{- end }}
---
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-gerrit-init-configmap
  labels:
    app.kubernetes.io/component: gerrit
    app.kubernetes.io/instance: {{ .Release.Name }}
    chart: {{ template "gerrit.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 4 }}
    {{- end }}
data:
  gerrit-init.yaml: |-
    {{ if .Values.caCert -}}
    caCertPath: /var/config/ca.crt
    {{- end }}
    pluginCacheEnabled: {{ .Values.gerrit.pluginManagement.cache.enabled }}
    pluginCacheDir: /var/mnt/plugins
    {{- if .Values.gerrit.pluginManagement.plugins }}
    plugins:
{{ toYaml .Values.gerrit.pluginManagement.plugins | indent 6}}
    {{- end }}
    {{- if .Values.gerrit.pluginManagement.libs }}
    libs:
{{ toYaml .Values.gerrit.pluginManagement.libs | indent 6}}
    {{- end }}
{{- range .Values.gerrit.additionalConfigMaps -}}
{{- if .data }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name:  {{ $root.Release.Name }}-{{ .name }}
  labels:
    app.kubernetes.io/component: gerrit
    app.kubernetes.io/instance: {{ $root.Release.Name }}
    chart: {{ template "gerrit.chart" $root }}
    heritage: {{ $root.Release.Service }}
    release: {{ $root.Release.Name }}
    {{- if $root.Values.additionalLabels }}
{{ toYaml $root.Values.additionalLabels | indent 4 }}
    {{- end }}
data:
{{ toYaml .data | indent 2 }}
{{- end }}
{{- end }}
