apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "coredns.fullname" . }}
data:
  Corefile: |-
    .:53 {
      {{- range $key, $plugin := .Values.plugins }}
      {{- if $plugin.enabled }}
      {{- if eq "kubernetes" $key }}
        kubernetes {{ $plugin.clusterDomain }} {{ $plugin.clusterCidr }}
      {{- end }}
      {{- if eq "etcd" $key }}
        etcd {{ range $plugin.zones }}{{ . }} {{ end }}{
          {{ if $plugin.path }}path {{ $plugin.path }}{{ end }}
          endpoint {{ $plugin.endpoint }}
        }
      {{- end }}
      {{- if eq "loadbalance" $key }}
        loadbalance {{ default "round_robin" $plugin.policy }}
      {{- end }}
      {{- if eq "log" $key }}
        log
      {{- end }}
      {{- if eq "errors" $key }}
        errors
      {{- end }}
      {{- if eq "health" $key }}
        health
      {{- end }}
      {{- if eq "prometheus" $key }}
        prometheus 0.0.0.0:{{ $plugin.port }}
      {{- end }}
      {{- if eq "proxy" $key }}
        proxy . /etc/resolv.conf
      {{- end }}
      {{- if eq "cache" $key }}
        cache 30
      {{- end }}
      {{- end }}
      {{- end }}
      {{- range .Values.zoneFiles }}
        file /etc/coredns/{{ .filename }} {{ .domain }}
      {{- end }}
    }
  {{- range .Values.zoneFiles }}
  {{ .filename }}: {{ toYaml .contents | indent 4 }}
  {{- end }}
