{{- $apiVersions := (include "k8s.apiVersions" .) | fromJson }}
{{- $svcName := (include "k8s.serviceName" .) }}
{{- $labels := (include "k8s.labels" .) | fromJson }}
{{- $configMapName := (include "k8s.configMapName" .) }}
{{- $harnessVersionConfigMaps := .Values.harnessVersionConfigMaps | default false }}
{{- if .Values.configMap.environmentVariables }}
---
apiVersion: {{ get $apiVersions "ConfigMap" }}
kind: ConfigMap
metadata:
  name: {{ $configMapName }}
  labels:
  {{- range $key, $value := $labels }}
    {{ $key }}: {{ $value }}
  {{- end }}
  annotations:
  {{- with .Values.configMap.annotations }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- if not $harnessVersionConfigMaps }}
    harness.io/skip-versioning: "true"
  {{- end }}
data:
  {{- toYaml .Values.configMap.environmentVariables | nindent 2 }}
{{- end }}

{{- if .Values.configMap.additionalConfigs }}
{{- range $acm := .Values.configMap.additionalConfigs }}
  {{- if or (not $acm.name) (not $acm.environmentVariables) }}
    {{- fail "error: additionalConfigs require name and environmentVariables!" }}
  {{- end }}
---
apiVersion: {{ get $apiVersions "ConfigMap" }}
kind: ConfigMap
metadata:
  name: {{ $acm.name }}
  labels:
  {{- range $key, $value := $labels }}
    {{ $key }}: {{ $value }}
  {{- end }}
  annotations:
  {{- with $acm.annotations }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- if not $harnessVersionConfigMaps }}
    harness.io/skip-versioning: "true"
  {{- end }}
data:
  {{- toYaml $acm.environmentVariables | nindent 2 }}
{{- end }}
{{- end }}

{{- $configs := (.Files.Glob "configs/**.yaml") }}
{{- if not (empty $configs) }}
{{- range $cfg, $_ := $configs }}
---
apiVersion: {{ get $apiVersions "ConfigMap" }}
kind: ConfigMap
metadata:
  name: config.{{ $cfg | replace "configs/" "" | replace ".yaml" "" }}
  labels:
  {{- range $key, $value := $labels }}
    {{ $key }}: {{ $value }}
  {{- end }}
  annotations:
    {{- with $.Values.configMap.annotations }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- if not $harnessVersionConfigMaps }}
    harness.io/skip-versioning: "true"
  {{- end }}
data:
  {{- tpl (($.Files.Glob $cfg).AsConfig) $ | nindent 2 }}
{{- end }}
{{- end }}

{{/*
!!! IMPOPTANT !!!
Careful when modifying this configmap group, it will break if it contains tabs rather than spaces.
*/}}
{{- if .Values.configMap.appConfig.enabled }}
{{- $jcfg := .Files.Get (cat "configs/" (.Values.configMap.appConfig.jsonFilename | default "dev") ".json" | nospace) }}
{{- if $jcfg }}
{{- $jname := .Values.configMap.appConfig.nameOverride | default "config" }}
---
apiVersion: {{ get $apiVersions "ConfigMap" }}
kind: ConfigMap
metadata:
  name: config.{{ $svcName }}
  labels:
  {{- range $key, $value := $labels }}
    {{ $key }}: {{ $value }}
  {{- end }}
{{- if or .Values.configMap.annotations (not $harnessVersionConfigMaps) }}
  annotations:
  {{- with .Values.configMap.annotations }}
{{- toYaml . | nindent 4 }}
  {{- end }}
  {{- if not $harnessVersionConfigMaps }}
    harness.io/skip-versioning: "true"
  {{- end }}
{{- end }}
data:
{{ $jname | indent 2 }}: |
{{ $jcfg | indent 4 }}
{{- end }}
{{- end }}

{{- if .Values.worker.workers }}
{{- range $worker := .Values.worker.workers }}
{{- if ($worker.configMap).environmentVariables }}
{{- $workerName := (cat ($worker.name | required "Worker name required!") "-worker" | nospace) }}
---
apiVersion: {{ get $apiVersions "ConfigMap" }}
kind: ConfigMap
metadata:
  name: {{ $configMapName }}-{{ $workerName }}
  labels:
  {{- range $key, $value := $labels }}
    {{- if eq $key "app.kubernetes.io/component" }}
      {{- $value = "service-worker" }}
    {{- else if or (eq $key "app.kubernetes.io/name") (eq $key "app") }}
      {{- $value = (cat $svcName "-" $workerName | nospace) }}
    {{- end }}
    {{ $key }}: {{ $value }}
  {{- end }}
  annotations:
  {{- with $worker.annotations }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- if not $harnessVersionConfigMaps }}
    harness.io/skip-versioning: "true"
  {{- end }}
data:
  {{- with $.Values.worker.commonVariables }}
  {{- toYaml . | nindent 2 }}
  {{- end }}
  {{- toYaml $worker.configMap.environmentVariables | nindent 2 }}
{{- end }}
{{- end }}
{{- end }}

{{- if and (not .Values.istio.forceDisabled) .Values.istio.bootstrapConfig }}
---
apiVersion: {{ get $apiVersions "ConfigMap" }}
kind: ConfigMap
metadata:
  name: {{ include "istio.bootstrapConfigMapName" . }}
  labels:
  {{- range $key, $value := $labels }}
    {{ $key }}: {{ $value }}
  {{- end }}
  annotations:
    {{- with .Values.configMap.annotations }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- if not $harnessVersionConfigMaps }}
    harness.io/skip-versioning: "true"
  {{- end }}
data:
  custom_bootstrap.json: |
    {{ $.Values.istio.bootstrapConfig | toJson }}
{{- end }}
