# StatefulSet to run the actual Consul server cluster.
{{- if (or (and (ne (.Values.server.enabled | toString) "-") .Values.server.enabled) (and (eq (.Values.server.enabled | toString) "-") .Values.global.enabled)) }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "consul.fullname" . }}-server-config
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ template "consul.name" . }}
    chart: {{ template "consul.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
data:
  extra-from-values.json: |-
{{ tpl .Values.server.extraConfig . | trimAll "\"" | indent 4 }}
  {{- if .Values.global.bootstrapACLs }}
  acl-config.json: |-
    {
      "acl": {
        "enabled": true,
        "default_policy": "deny",
        "down_policy": "extend-cache",
        "enable_token_persistence": true
      }
    }
  {{- end }}
  {{- if and .Values.connectInject.enabled .Values.connectInject.centralConfig.enabled }}
  central-config.json: |-
    {
      "enable_central_service_config": true
    }
  {{- if gt (len .Values.connectInject.centralConfig.proxyDefaults) 3 }}
  proxy-defaults-config.json: |-
    {
      "config_entries": {
        "bootstrap": [
          {
            "kind": "proxy-defaults",
            "name": "global",
            {{- if and .Values.meshGateway.enabled .Values.meshGateway.globalMode }}
            "mesh_gateway": {
              "mode": {{ .Values.meshGateway.globalMode | quote }}
            },
            {{- end }}
            "config":
{{ tpl .Values.connectInject.centralConfig.proxyDefaults . | trimAll "\"" | indent 14 }}
          }
        ]
      }
    }
  {{- else if and .Values.meshGateway.enabled .Values.meshGateway.globalMode }}
  proxy-defaults-config.json: |-
    {
      "config_entries": {
        "bootstrap": [
          {
            "kind": "proxy-defaults",
            "name": "global",
            "mesh_gateway": {
              "mode": {{ .Values.meshGateway.globalMode | quote }}
            }
          }
        ]
      }
    }
  {{- end }}
  {{- end }}
{{- end }}
