apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "envoy.fullname" . }}
  labels:
    app: {{ template "envoy.name" . }}
    chart: {{ template "envoy.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  {{- if .Values.service.enabled }}
  serviceName: {{ .Values.service.name }}
  {{- end }}
  replicas: {{ .Values.replicaCount }}
  terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
  selector:
    matchLabels:
      app: {{ template "envoy.name" . }}
      release: {{ .Release.Name }}
  strategy:
{{ .Values.strategy | indent 4 }}
  template:
    metadata:
      labels:
        app: {{ template "envoy.name" . }}
        release: {{ .Release.Name }}
        component: controller
      {{- if .Values.podLabels }}
        ## Custom pod labels
        {{- range $key, $value := .Values.podLabels }}
        {{ $key }}: {{ $value | quote }}
        {{- end }}
      {{- end }}
      annotations:
        checksum/config: {{ include (print .Template.BasePath "/configmap.yaml") . | sha256sum }}
      {{- if .Values.podAnnotations }}
        ## Custom pod annotations
        {{- range $key, $value := .Values.podAnnotations }}
        {{ $key }}: {{ $value | quote }}
        {{- end }}
      {{- end }}
    spec:
      securityContext:
{{ toYaml .Values.securityContext | indent 8 }}
{{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
+{{- end }}
      containers:

        - name: envoy
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
{{ toYaml .Values.command | indent 12 }}
          args:
{{ toYaml .Values.args | indent 12 }}
          ports:
{{- with .Values.ports }}
  {{- range $key, $port := . }}
            - name: {{ $key }}
{{ toYaml $port | indent 14 }}
  {{- end }}
{{- end }}

          livenessProbe:
{{ toYaml .Values.livenessProbe | indent 12 }}
          readinessProbe:
{{ toYaml .Values.readinessProbe | indent 12 }}
          env:
          {{- range $key, $value := .Values.env }}
            - name: {{ $key | upper | replace "." "_" }}
              value: {{ $value | quote }}
          {{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
          volumeMounts:
            - name: config
              mountPath: /config
          {{- range $key, $value := .Values.secretMounts }}
            - name: {{ $key }}
              mountPath: {{ $value.mountPath }}
          {{- end }}

    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
      volumes:
        - name: config
          configMap:
            name: {{ template "envoy.fullname" . }}
      {{- range $key, $value := .Values.secretMounts }}
        - name: {{ $key }}
          secret:
            secretName: {{ $value.secretName }}
            defaultMode: {{ $value.defaultMode }}
      {{- end }}
