{{- $root := . -}}

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-gerrit-replica-configmap
  labels:
    app.kubernetes.io/component: gerrit-replica
    app.kubernetes.io/instance: {{ .Release.Name }}
    chart: {{ template "gerrit-replica.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 4 }}
    {{- end }}
data:
  {{- range $key, $value := .Values.gerritReplica.etc.config }}
  {{ $key }}:
{{ toYaml $value | indent 4 }}
  {{- end }}
  {{- if not (hasKey .Values.gerritReplica.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 }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-gerrit-init-configmap
  labels:
    app.kubernetes.io/component: gerrit-replica
    app.kubernetes.io/instance: {{ .Release.Name }}
    chart: {{ template "gerrit-replica.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.gerritReplica.pluginManagement.cache.enabled }}
    pluginCacheDir: /var/mnt/plugins
    {{- if .Values.gerritReplica.pluginManagement.plugins }}
    plugins:
{{ toYaml .Values.gerritReplica.pluginManagement.plugins | indent 6}}
    {{- end }}
    {{- if .Values.gerritReplica.pluginManagement.libs }}
    libs:
{{ toYaml .Values.gerritReplica.pluginManagement.libs | indent 6}}
    {{- end }}
{{- range .Values.gerritReplica.additionalConfigMaps -}}
{{- if .data }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name:  {{ $root.Release.Name }}-{{ .name }}
  labels:
    app.kubernetes.io/component: gerrit-replica
    app.kubernetes.io/instance: {{ $root.Release.Name }}
    chart: {{ template "gerrit-replica.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 }}
