{{- if not .Values.global.omitSidecarInjectorConfigMap }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: istio-sidecar-injector
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ template "istio.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    istio: sidecar-injector
data:
  config: |-
    policy: {{ .Values.global.proxy.autoInject }}
    template: |-
      initContainers:
      - name: istio-init
{{- if contains "/" .Values.global.proxy_init.image }}
        image: "{{ .Values.global.proxy_init.image }}"
{{- else }}
        image: "{{ .Values.global.hub }}/{{ .Values.global.proxy_init.image }}:{{ .Values.global.tag }}"
{{- end }}
        args:
        - "-p"
        - {{ "[[ .MeshConfig.ProxyListenPort ]]" }}
        - "-u"
        - 1337
        - "-m"
        - {{ "[[ or (index .ObjectMeta.Annotations \"sidecar.istio.io/interceptionMode\") .ProxyConfig.InterceptionMode.String ]]" }}
        - "-i"
        {{ "[[ if (isset .ObjectMeta.Annotations \"traffic.sidecar.istio.io/includeOutboundIPRanges\") -]]" }}
        {{ "- \"[[ index .ObjectMeta.Annotations \"traffic.sidecar.istio.io/includeOutboundIPRanges\"  ]]\"" }}
        {{ "[[ else -]]" }}
        - "{{ .Values.global.proxy.includeIPRanges }}"
        {{ "[[ end -]]" }}
        - "-x"
        {{ "[[ if (isset .ObjectMeta.Annotations \"traffic.sidecar.istio.io/excludeOutboundIPRanges\") -]]" }}
        {{ "- \"[[ index .ObjectMeta.Annotations \"traffic.sidecar.istio.io/excludeOutboundIPRanges\"  ]]\"" }}
        {{ "[[ else -]]" }}
        - "{{ .Values.global.proxy.excludeIPRanges }}"
        {{ "[[ end -]]" }}
        - "-b"
        {{ "[[ if (isset .ObjectMeta.Annotations \"traffic.sidecar.istio.io/includeInboundPorts\") -]]" }}
        {{ "- \"[[ index .ObjectMeta.Annotations \"traffic.sidecar.istio.io/includeInboundPorts\"  ]]\"" }}
        {{ "[[ else -]]" }}
        - {{ "[[ range .Spec.Containers -]][[ range .Ports -]][[ .ContainerPort -]], [[ end -]][[ end -]][[ end]]" }}
        - "-d"
        {{ "[[ if (isset .ObjectMeta.Annotations \"traffic.sidecar.istio.io/excludeInboundPorts\") -]]" }}
        {{ "- \"[[ index .ObjectMeta.Annotations \"traffic.sidecar.istio.io/excludeInboundPorts\" ]]\"" }}
        {{ "[[ else -]]" }}
        - "{{ .Values.global.proxy.excludeInboundPorts }}"
        {{ "[[ end -]]" }}
        imagePullPolicy: {{ .Values.global.imagePullPolicy }}
        securityContext:
          capabilities:
            add:
            - NET_ADMIN
          {{ if .Values.global.proxy.privileged }}
          privileged: true
          {{ end -}}
        restartPolicy: Always
      {{ if eq .Values.global.proxy.enableCoreDump true }}
      - name: enable-core-dump
        args:
        - -c
        - sysctl -w kernel.core_pattern=/etc/istio/proxy/core.%e.%p.%t && ulimit -c unlimited
        command:
          - /bin/sh
        image: {{ .Values.global.hub }}/proxy_init:{{ .Values.global.tag }}
        imagePullPolicy: IfNotPresent
        resources: {}
        securityContext:
          privileged: true
      {{ end }}
      containers:
      - name: istio-proxy
        image: {{ "[[ if (isset .ObjectMeta.Annotations \"sidecar.istio.io/proxyImage\") -]]" }}
        {{ "\"[[ index .ObjectMeta.Annotations \"sidecar.istio.io/proxyImage\" ]]\"" }}
        {{ "[[ else -]]" }}
        {{ .Values.global.hub }}/{{ .Values.global.proxy.image }}:{{ .Values.global.tag }}
        {{ "[[ end -]]" }}
        args:
        - proxy
        - sidecar
        - --configPath
        - {{ "[[ .ProxyConfig.ConfigPath ]]" }}
        - --binaryPath
        - {{ "[[ .ProxyConfig.BinaryPath ]]" }}
        - --serviceCluster
        {{ "[[ if ne \"\" (index .ObjectMeta.Labels \"app\") -]]" }}
        - {{ "[[ index .ObjectMeta.Labels \"app\" ]]" }}
        {{ "[[ else -]]" }}
        - "istio-proxy"
        {{ "[[ end -]]" }}
        - --drainDuration
        - {{ "[[ formatDuration .ProxyConfig.DrainDuration ]]" }}
        - --parentShutdownDuration
        - {{ "[[ formatDuration .ProxyConfig.ParentShutdownDuration ]]" }}
        - --discoveryAddress
        - {{ "[[ .ProxyConfig.DiscoveryAddress ]]" }}
        - --discoveryRefreshDelay
        - {{ "[[ formatDuration .ProxyConfig.DiscoveryRefreshDelay ]]" }}
        - --zipkinAddress
        - {{ "[[ .ProxyConfig.ZipkinAddress ]]" }}
        - --connectTimeout
        - {{ "[[ formatDuration .ProxyConfig.ConnectTimeout ]]" }}
      {{- if .Values.global.proxy.envoyStatsd.enabled }}
        - --statsdUdpAddress
        - {{ "[[ .ProxyConfig.StatsdUdpAddress ]]" }}
      {{- end }}
        - --proxyAdminPort
        - {{ "[[ .ProxyConfig.ProxyAdminPort ]]" }}
        - --controlPlaneAuthPolicy
        - {{ "[[ or (index .ObjectMeta.Annotations \"sidecar.istio.io/controlPlaneAuthPolicy\") .ProxyConfig.ControlPlaneAuthPolicy ]]" }}
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: INSTANCE_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: ISTIO_META_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: ISTIO_META_INTERCEPTION_MODE
          value: {{ "[[ or (index .ObjectMeta.Annotations \"sidecar.istio.io/interceptionMode\") .ProxyConfig.InterceptionMode.String ]]" }}
        imagePullPolicy: {{ .Values.global.imagePullPolicy }}
        securityContext:
          {{ if .Values.global.proxy.privileged }}
          privileged: true
          {{ end -}}
          readOnlyRootFilesystem: true
          {{ "[[ if eq (or (index .ObjectMeta.Annotations \"sidecar.istio.io/interceptionMode\") .ProxyConfig.InterceptionMode.String) \"TPROXY\" -]]" }}
          capabilities:
            add:
            - NET_ADMIN
          runAsGroup: 1337
          {{ "[[ else -]]" }}
          runAsUser: 1337
          {{ "[[ end -]]" }}
        restartPolicy: Always
        resources:
          {{ "[[ if (isset .ObjectMeta.Annotations \"sidecar.istio.io/proxyCPU\") -]]" }}
          requests:
            cpu: {{ "\"[[ index .ObjectMeta.Annotations \"sidecar.istio.io/proxyCPU\" ]]\"" }}
            memory: {{ "\"[[ index .ObjectMeta.Annotations \"sidecar.istio.io/proxyMemory\" ]]\"" }}
        {{ "[[ else -]]" }}
{{- if .Values.global.proxy.resources }}
{{ toYaml .Values.global.proxy.resources | indent 10 }}
{{- end }}
        {{ "[[ end -]]" }}
        volumeMounts:
        - mountPath: /etc/istio/proxy
          name: istio-envoy
        - mountPath: /etc/certs/
          name: istio-certs
          readOnly: true
      volumes:
      - emptyDir:
          medium: Memory
        name: istio-envoy
      - name: istio-certs
        secret:
          optional: true
          {{ "[[ if eq .Spec.ServiceAccountName \"\" -]]" }}
          secretName: istio.default
          {{ "[[ else -]]" }}
          secretName: {{ "[[ printf \"istio.%s\" .Spec.ServiceAccountName ]]"  }}
          {{ "[[ end -]]" }}
{{- end }}
