{{- if and .Values.envoy.enabled (eq .Values.envoy.kind "daemonset") }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ include "common.names.fullname" . }}-envoy
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: envoy
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.envoy.updateStrategy }}
  updateStrategy: {{- toYaml .Values.envoy.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: envoy
  template:
    metadata:
      {{- if or .Values.envoy.podAnnotations .Values.commonAnnotations }}
      annotations:
        {{- if .Values.envoy.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.envoy.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.commonAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 8 }}
        {{- end }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: envoy
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.envoy.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.envoy.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "common.images.pullSecrets" ( dict "images" (list .Values.contour.image .Values.envoy.image) "global" .Values.global) | nindent 6 }}
      {{- if .Values.envoy.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.envoy.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.envoy.priorityClassName }}
      priorityClassName: {{ .Values.envoy.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.envoy.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.envoy.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.envoy.podAffinityPreset "component" "envoy" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.envoy.podAntiAffinityPreset "component" "envoy" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.envoy.nodeAffinityPreset.type "key" .Values.envoy.nodeAffinityPreset.key "values" .Values.envoy.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.envoy.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.envoy.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.envoy.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.envoy.tolerations "context" $) | nindent 8 }}
      {{- end }}
      terminationGracePeriodSeconds: {{ .Values.envoy.terminationGracePeriodSeconds }}
      hostNetwork: {{ .Values.envoy.hostNetwork }}
      dnsPolicy: {{ .Values.envoy.dnsPolicy }}
      {{- if .Values.envoy.podSecurityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.envoy.podSecurityContext.fsGroup }}
        {{- if .Values.envoy.podSecurityContext.sysctls }}
        sysctls:
          {{- toYaml .Values.envoy.podSecurityContext.sysctls | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        {{- if .Values.envoy.shutdownManager.enabled }}
        - command:
            - contour
          args:
            - envoy
            - shutdown-manager
          image: {{ include "common.images.image" ( dict "imageRoot" .Values.contour.image "global" .Values.global) }}
          imagePullPolicy: {{ .Values.contour.image.pullPolicy }}
          {{- if .Values.contour.extraEnvVars }}
          env:
            {{- include "common.tplvalues.render" (dict "value" .Values.contour.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          {{- if or .Values.contour.extraEnvVarsCM .Values.contour.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.contour.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.contour.extraEnvVarsCM "context" $ ) }}
            {{- end }}
            {{- if .Values.contour.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.contour.extraEnvVarsSecret "context" $ ) }}
            {{- end }}
          {{- end }}
          {{- if .Values.envoy.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.envoy.lifecycleHooks "context" $) | nindent 12 }}
          {{- else }}
          lifecycle:
            preStop:
              exec:
                command:
                  - contour
                  - envoy
                  - shutdown
          {{- end }}
          {{- if .Values.contour.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8090
            initialDelaySeconds: {{ .Values.contour.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.contour.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.contour.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.contour.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.contour.livenessProbe.failureThreshold }}
          {{- end }}
          name: shutdown-manager
          resources: {{- toYaml .Values.envoy.shutdownManager.resources | nindent 12 }}
          volumeMounts:
            - name: envoy-admin
              mountPath: /admin
          {{- if .Values.envoy.extraVolumeMounts }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.envoy.extraVolumeMounts "context" $ ) | nindent 12 }}
          {{- end }}
        {{- end }}
        - name: envoy
          {{- if .Values.envoy.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.envoy.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - envoy
          {{- end }}
          {{- if .Values.envoy.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.envoy.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - -c
            - /config/envoy.json
            - --service-cluster $(CONTOUR_NAMESPACE)
            - --service-node $(ENVOY_POD_NAME)
            - --log-level {{ .Values.envoy.logLevel }}
            {{- if .Values.envoy.extraArgs }}
            {{- include "common.tplvalues.render" (dict "value" .Values.envoy.extraArgs "context" $) | nindent 12 }}
            {{- end }}
          {{- end }}
          image: {{ include "common.images.image" ( dict "imageRoot" .Values.envoy.image "global" .Values.global ) }}
          imagePullPolicy: {{ .Values.envoy.image.pullPolicy }}
          {{- if .Values.envoy.containerSecurityContext.enabled }}
          securityContext:
            runAsUser: {{ .Values.envoy.containerSecurityContext.runAsUser }}
          {{- end }}
          env:
            - name: CONTOUR_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - name: ENVOY_POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
          {{- if .Values.envoy.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.envoy.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          {{- if or .Values.envoy.extraEnvVarsCM .Values.envoy.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.envoy.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.envoy.extraEnvVarsCM "context" $ ) }}
            {{- end }}
            {{- if .Values.envoy.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.envoy.extraEnvVarsSecret "context" $ ) }}
            {{- end }}
          {{- end }}
          ports:
            - containerPort: {{ .Values.envoy.containerPorts.http }}
              {{- if .Values.envoy.useHostPort }}
              hostPort: {{ .Values.envoy.hostPorts.http }}
              {{- end }}
              {{- if .Values.envoy.useHostIP }}
              hostIP: {{ .Values.envoy.hostIPs.http }}
              {{- end }}
              name: http
              protocol: TCP
            - containerPort: {{ .Values.envoy.containerPorts.https }}
              {{- if .Values.envoy.useHostPort }}
              hostPort: {{ .Values.envoy.hostPorts.https }}
              {{- end }}
              {{- if .Values.envoy.useHostIP }}
              hostIP: {{ .Values.envoy.hostIPs.https }}
              {{- end }}
              name: https
              protocol: TCP
            - containerPort: 8002
              name: metrics
              protocol: TCP
          {{- if .Values.envoy.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /ready
              port: 8002
            initialDelaySeconds: {{ .Values.envoy.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.envoy.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.envoy.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.envoy.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.envoy.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.envoy.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /ready
              port: 8002
            initialDelaySeconds: {{ .Values.envoy.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.envoy.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.envoy.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.envoy.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.envoy.livenessProbe.failureThreshold }}
          {{- end }}
          resources: {{ toYaml .Values.envoy.resources | nindent 12 }}
          volumeMounts:
            - name: envoy-config
              mountPath: /config
            - name: envoycert
              mountPath: /certs
            - name: envoy-admin
              mountPath: /admin
            {{- if .Values.envoy.extraVolumeMounts }}
              {{- include "common.tplvalues.render" ( dict "value" .Values.envoy.extraVolumeMounts "context" $ ) | nindent 12 }}
            {{- end }}
          lifecycle:
            preStop:
              {{- if .Values.envoy.shutdownManager.enabled }}
              httpGet:
                path: /shutdown
                port: 8090
                scheme: HTTP
              {{- else }}
              exec:
                command:
                  - sh
                  - '-c'
                  - sleep {{ .Values.envoy.terminationGracePeriodSeconds }}; kill 1
              {{- end }}
        {{- if .Values.envoy.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.envoy.sidecars "context" $ ) | nindent 8 }}
        {{- end }}
      initContainers:
        - command:
            - contour
          args:
            - bootstrap
            - /config/envoy.json
            - --xds-address={{ template "common.names.fullname" . }}
            - --xds-port={{ .Values.contour.service.ports.xds }}
            - --resources-dir=/config/resources
            - --envoy-cafile=/certs/ca.crt
            - --envoy-cert-file=/certs/tls.crt
            - --envoy-key-file=/certs/tls.key
          image: {{ include "common.images.image" ( dict "imageRoot" .Values.contour.image "global" .Values.global) }}
          imagePullPolicy: {{ .Values.contour.image.pullPolicy }}
          name: envoy-initconfig
          resources: {{ toYaml .Values.contour.resources | nindent 12 }}
          volumeMounts:
            - name: envoy-config
              mountPath: /config
            - name: envoycert
              mountPath: /certs
              readOnly: true
            - name: envoy-admin
              mountPath: /admin
            {{- if .Values.envoy.extraVolumeMounts }}
              {{- include "common.tplvalues.render" ( dict "value" .Values.envoy.extraVolumeMounts "context" $ ) | nindent 12 }}
            {{- end }}
          env:
            - name: CONTOUR_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            {{- if .Values.contour.extraEnvVars }}
              {{- include "common.tplvalues.render" (dict "value" .Values.contour.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.contour.extraEnvVarsCM .Values.contour.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.contour.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.contour.extraEnvVarsCM "context" $ ) }}
            {{- end }}
            {{- if .Values.contour.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.contour.extraEnvVarsSecret "context" $ ) }}
            {{- end }}
          {{- end }}
      {{- if .Values.envoy.initContainers }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.envoy.initContainers "context" $ ) | nindent 8 }}
      {{- end }}
      automountServiceAccountToken: {{ .Values.envoy.serviceAccount.automountServiceAccountToken }}
      serviceAccountName: {{ include "envoy.envoyServiceAccountName" . }}
      volumes:
        - name: envoy-admin
          emptyDir: {}
        - name: envoy-config
          emptyDir: {}
        - name: envoycert
          secret:
            secretName: {{ include "contour.envoy.certs-secret.name" . }}
        {{- if .Values.envoy.extraVolumes }}
          {{- include "common.tplvalues.render" ( dict "value" .Values.envoy.extraVolumes "context" $ ) | nindent 8 }}
        {{- end }}
      restartPolicy: Always
{{- end }}
