{{- $root := . }}
{{- if .Values.controller.sidecars.configAutoReload.enabled }}
{{- range $key, $val := .Values.controller.JCasC.configScripts }}
{{- if $val }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "jenkins.casc.configName" (list (printf "config-%s" $key) $ )}}
  namespace: {{ template "jenkins.namespace" $root }}
  labels:
    {{- include "jenkins.labels" $root | nindent 4 }}
    {{ template "jenkins.fullname" $root }}-jenkins-config: "true"
{{- if $root.Values.controller.JCasC.configMapAnnotations }}
  annotations:
{{ toYaml $root.Values.controller.JCasC.configMapAnnotations | indent 4 }}
{{- end }}
data:
  {{ $key }}.yaml: |-
{{ tpl $val $| indent 4 }}
{{- end }}
{{- end }}
{{- if .Values.controller.JCasC.defaultConfig }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "jenkins.casc.configName" (list "jcasc-config" $ )}}
  namespace: {{ template "jenkins.namespace" $root }}
  labels:
    {{- include "jenkins.labels" $root | nindent 4 }}
    {{ template "jenkins.fullname" $root }}-jenkins-config: "true"
{{- if $root.Values.controller.JCasC.configMapAnnotations }}
  annotations:
{{ toYaml $root.Values.controller.JCasC.configMapAnnotations | indent 4 }}
{{- end }}
data:
  jcasc-default-config.yaml: |-
    {{- include "jenkins.casc.defaults" . | nindent 4 }}
{{- end}}
{{- $configScripts := toYaml .Values.controller.JCasC.configScripts }}
{{- if and .Values.controller.JCasC.securityRealm (not (contains "securityRealm:" $configScripts)) (not .Values.controller.JCasC.defaultConfig) }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "jenkins.casc.configName" (list "config-securityrealm" $ )}}
  namespace: {{ template "jenkins.namespace" $root }}
  labels:
    "app.kubernetes.io/name": {{ template "jenkins.name" $root}}
    {{- if .Values.renderHelmLabels }}
    "helm.sh/chart": "{{ $root.Chart.Name }}-{{ $root.Chart.Version | replace "+" "_" }}"
    {{- end }}
    "app.kubernetes.io/managed-by": "{{ $.Release.Service }}"
    "app.kubernetes.io/instance": "{{ $.Release.Name }}"
    "app.kubernetes.io/component": "{{ $.Values.controller.componentName }}"
    {{ template "jenkins.fullname" $root }}-jenkins-config: "true"
{{- if $root.Values.controller.JCasC.configMapAnnotations }}
  annotations:
{{ toYaml $root.Values.controller.JCasC.configMapAnnotations | indent 4 }}
{{- end }}
data:
  securityrealm.yaml: |-
    jenkins:
      securityRealm:
        {{- tpl .Values.controller.JCasC.securityRealm . | nindent 8 }}
{{- end }}
{{- if and .Values.controller.JCasC.authorizationStrategy (not (contains "authorizationStrategy:" $configScripts)) (not .Values.controller.JCasC.defaultConfig) }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "jenkins.casc.configName" (list "config-authorizationstrategy" $ )}}
  namespace: {{ template "jenkins.namespace" $root }}
  labels:
    "app.kubernetes.io/name": {{ template "jenkins.name" $root}}
    {{- if .Values.renderHelmLabels }}
    "helm.sh/chart": "{{ $root.Chart.Name }}-{{ $root.Chart.Version | replace "+" "_" }}"
    {{- end }}
    "app.kubernetes.io/managed-by": "{{ $.Release.Service }}"
    "app.kubernetes.io/instance": "{{ $.Release.Name }}"
    "app.kubernetes.io/component": "{{ $.Values.controller.componentName }}"
    {{ template "jenkins.fullname" $root }}-jenkins-config: "true"
{{- if $root.Values.controller.JCasC.configMapAnnotations }}
  annotations:
{{ toYaml $root.Values.controller.JCasC.configMapAnnotations | indent 4 }}
{{- end }}
data:
  authorizationstrategy.yaml: |-
    jenkins:
      authorizationStrategy:
        {{- tpl .Values.controller.JCasC.authorizationStrategy . | nindent 8 }}
{{- end }}
{{- end }}
