apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "flagger.fullname" . }}
  labels:
    helm.sh/chart: {{ template "flagger.chart" . }}
    app.kubernetes.io/name: {{ template "flagger.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
spec:
  replicas: {{ .Values.leaderElection.replicaCount }}
  {{- if eq .Values.leaderElection.enabled false }}
  strategy:
    type: Recreate
  {{- end }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "flagger.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ template "flagger.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
      annotations:
        {{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ template "flagger.serviceAccountName" . }}
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchLabels:
                    app.kubernetes.io/name: {{ template "flagger.name" . }}
                    app.kubernetes.io/instance: {{ .Release.Name }}
                topologyKey: kubernetes.io/hostname
      {{- if .Values.image.pullSecret }}
      imagePullSecrets:
        - name: {{ .Values.image.pullSecret }}
      {{- end }}
      volumes:
        {{- if .Values.istio.kubeconfig.secretName }}
        - name: kubeconfig
          secret:
            secretName: "{{ .Values.istio.kubeconfig.secretName }}"
        {{- end }}
      containers:
        - name: flagger
          securityContext:
            readOnlyRootFilesystem: true
            runAsUser: 10001
          volumeMounts:
            {{- if .Values.istio.kubeconfig.secretName }}
            - name: kubeconfig
              mountPath: "/tmp/istio-host"
            {{- end }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
          - name: http
            containerPort: 8080
          command:
          - ./flagger
          - -log-level={{ .Values.logLevel }}
          {{- if .Values.meshProvider }}
          - -mesh-provider={{ .Values.meshProvider }}
          {{- end }}
          {{- if .Values.prometheus.install }}
          - -metrics-server=http://{{ template "flagger.fullname" . }}-prometheus:9090
          {{- else }}
          - -metrics-server={{ .Values.metricsServer }}
          {{- end }}
          {{- if .Values.selectorLabels }}
          - -selector-labels={{ .Values.selectorLabels }}
          {{- end }}
          {{- if .Values.configTracking }}
          - -enable-config-tracking={{ .Values.configTracking.enabled }}
          {{- end }}
          {{- if .Values.namespace }}
          - -namespace={{ .Values.namespace }}
          {{- end }}
          {{- if .Values.slack.url }}
          - -slack-url={{ .Values.slack.url }}
          {{- end }}
          {{- if .Values.slack.user }}
          - -slack-user={{ .Values.slack.user }}
          {{- end }}
          {{- if .Values.slack.channel }}
          - -slack-channel={{ .Values.slack.channel }}
          {{- end }}
          {{- if .Values.msteams.url }}
          - -msteams-url={{ .Values.msteams.url }}
          {{- end }}
          {{- if .Values.leaderElection.enabled }}
          - -enable-leader-election=true
          - -leader-election-namespace={{ .Release.Namespace }}
          {{- end }}
          {{- if .Values.ingressAnnotationsPrefix }}
          - -ingress-annotations-prefix={{ .Values.ingressAnnotationsPrefix }}
          {{- end }}
          {{- if .Values.eventWebhook }}
          - -event-webhook={{ .Values.eventWebhook }}
          {{- end }}
          {{- if .Values.istio.kubeconfig.secretName }}
          - -kubeconfig-service-mesh=/tmp/istio-host/{{ .Values.istio.kubeconfig.key }}
          {{- end }}
          livenessProbe:
            exec:
              command:
              - wget
              - --quiet
              - --tries=1
              - --timeout=4
              - --spider
              - http://localhost:8080/healthz
            timeoutSeconds: 5
          readinessProbe:
            exec:
              command:
              - wget
              - --quiet
              - --tries=1
              - --timeout=4
              - --spider
              - http://localhost:8080/healthz
            timeoutSeconds: 5
          {{- if .Values.env }}
          env:
{{ toYaml .Values.env | indent 12 }}
          {{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
