apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
{{ include "reloader-helm3.annotations" . | indent 4 }}
{{- if .Values.reloader.deployment.annotations }}
{{ toYaml .Values.reloader.deployment.annotations | indent 4 }}
{{- end }}
  labels:
{{ include "reloader-labels.chart" . | indent 4 }}
{{- if .Values.reloader.deployment.labels }}
{{ toYaml .Values.reloader.deployment.labels | indent 4 }}
{{- end }}
{{- if .Values.reloader.matchLabels }}
{{ toYaml .Values.reloader.matchLabels | indent 4 }}
{{- end }}
  name: {{ template "reloader-fullname" . }}
  namespace: {{ .Values.namespace | default .Release.Namespace }}
spec:
{{- if not (.Values.reloader.enableHA) }}
  replicas: {{ min .Values.reloader.deployment.replicas 1 }}
{{- else }}
  replicas: {{ .Values.reloader.deployment.replicas }}
{{- end}}
  revisionHistoryLimit: {{ .Values.reloader.deployment.revisionHistoryLimit }}
  selector:
    matchLabels:
      app: {{ template "reloader-fullname" . }}
      release: {{ .Release.Name | quote }}
{{- if .Values.reloader.matchLabels }}
{{ toYaml .Values.reloader.matchLabels | indent 6 }}
{{- end }}
  template:
    metadata:
{{- if .Values.reloader.deployment.pod.annotations }}
      annotations:
{{ toYaml .Values.reloader.deployment.pod.annotations | indent 8 }}
{{- end }}
      labels:
{{ include "reloader-labels.chart" . | indent 8 }}
{{- if .Values.reloader.deployment.labels }}
{{ toYaml .Values.reloader.deployment.labels | indent 8 }}
{{- end }}
{{- if .Values.reloader.matchLabels }}
{{ toYaml .Values.reloader.matchLabels | indent 8 }}
{{- end }}
    spec:
      {{- with .Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.reloader.deployment.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.reloader.deployment.nodeSelector | indent 8 }}
      {{- end }}
      {{- if or (.Values.reloader.deployment.affinity) (.Values.reloader.enableHA) }}
      affinity:
      {{- if .Values.reloader.deployment.affinity }}
{{ toYaml .Values.reloader.deployment.affinity | indent 8 }}
      {{- else }}
{{ include "reloader-podAntiAffinity" . | indent 8 }}
      {{- end }}
      {{- end }}
      {{- if .Values.reloader.deployment.tolerations }}
      tolerations:
{{ toYaml .Values.reloader.deployment.tolerations | indent 8 }}
      {{- end }}
      {{- if .Values.reloader.deployment.topologySpreadConstraints }}
      topologySpreadConstraints:
{{ toYaml .Values.reloader.deployment.topologySpreadConstraints | indent 8 }}
      {{- end }}
      {{- if .Values.reloader.deployment.priorityClassName }}
      priorityClassName: {{ .Values.reloader.deployment.priorityClassName }}
      {{- end }}
      containers:
      {{- if .Values.global.imageRegistry }}
      - image: "{{ .Values.global.imageRegistry }}/{{ .Values.image.name }}:{{ .Values.image.tag }}"
      {{- else }}
      {{- if .Values.image.digest }}
      - image: "{{ .Values.image.repository }}@{{ .Values.image.digest }}"
      {{- else }}
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
      {{- end }}
      {{- end }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        name: {{ template "reloader-fullname" . }}
        env:
        - name: GOMAXPROCS
        {{- if .Values.reloader.deployment.gomaxprocsOverride }}
          value: {{ .Values.reloader.deployment.gomaxprocsOverride | quote }}
        {{- else }}
          valueFrom:
            resourceFieldRef:
              resource: limits.cpu
              divisor: '1'
        {{- end }}
        - name: GOMEMLIMIT
        {{- if .Values.reloader.deployment.gomemlimitOverride }}
          value: {{ .Values.reloader.deployment.gomemlimitOverride | quote }}
        {{- else }}
          valueFrom:
            resourceFieldRef:
              resource: limits.memory
              divisor: '1'
        {{- end }}
      {{- range $name, $value := .Values.reloader.deployment.env.open }}
      {{- if not (empty $value) }}
        - name: {{ $name | quote }}
          value: {{ $value | quote }}
      {{- end }}
      {{- end }}
      {{- $secret_name := include "reloader-fullname" . }}
      {{- range $name, $value := .Values.reloader.deployment.env.secret }}
      {{- if not ( empty $value) }}
        - name: {{ $name | quote }}
          valueFrom:
            secretKeyRef:
              name: {{ $secret_name }}
              key: {{ $name | quote }}
      {{- end }}
      {{- end }}
      {{- range $secret, $values := .Values.reloader.deployment.env.existing }}
      {{- range $name, $key := $values }}
      {{- if not ( empty $name) }}
        - name: {{ $name | quote }}
          valueFrom:
            secretKeyRef:
              name: {{ $secret | quote }}
              key: {{ $key | quote }}
      {{- end }}
      {{- end }}
      {{- end }}
      {{- range $name, $value := .Values.reloader.deployment.env.field }}
      {{- if not ( empty $value) }}
        - name: {{ $name | quote }}
          valueFrom:
            fieldRef:
              fieldPath: {{ $value | quote}}
      {{- end }}
      {{- end }}
      {{- if eq .Values.reloader.watchGlobally false }}
        - name: KUBERNETES_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
      {{- end }}
      {{- if .Values.reloader.enableHA }}
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
      {{- end }}
      {{- if .Values.reloader.enableMetricsByNamespace }}
        - name: METRICS_COUNT_BY_NAMESPACE
          value: enabled
      {{- end }}
        ports:
        - name: http
          containerPort: 9090
        livenessProbe:
          httpGet:
            path: /live
            port: http
          timeoutSeconds: {{ .Values.reloader.deployment.livenessProbe.timeoutSeconds | default "5" }}
          failureThreshold: {{ .Values.reloader.deployment.livenessProbe.failureThreshold | default "5" }}
          periodSeconds: {{ .Values.reloader.deployment.livenessProbe.periodSeconds | default "10" }}
          successThreshold: {{ .Values.reloader.deployment.livenessProbe.successThreshold | default "1" }}
          initialDelaySeconds: {{ .Values.reloader.deployment.livenessProbe.initialDelaySeconds | default "10" }}
        readinessProbe:
          httpGet:
            path: /metrics
            port: http
          timeoutSeconds: {{ .Values.reloader.deployment.readinessProbe.timeoutSeconds | default "5" }}
          failureThreshold: {{ .Values.reloader.deployment.readinessProbe.failureThreshold | default "5" }}
          periodSeconds: {{ .Values.reloader.deployment.readinessProbe.periodSeconds | default "10" }}
          successThreshold: {{ .Values.reloader.deployment.readinessProbe.successThreshold | default "1" }}
          initialDelaySeconds: {{ .Values.reloader.deployment.readinessProbe.initialDelaySeconds | default "10" }}

        {{- $containerSecurityContext := .Values.reloader.deployment.containerSecurityContext | default dict }}
        {{- if .Values.reloader.readOnlyRootFileSystem }}
          {{- $_ := set $containerSecurityContext "readOnlyRootFilesystem" true }}
        {{- end }}

        securityContext:
          {{- toYaml $containerSecurityContext | nindent 10 }}

      {{- if (or (.Values.reloader.deployment.volumeMounts) (eq .Values.reloader.readOnlyRootFileSystem true)) }}
        volumeMounts:
          {{- if eq .Values.reloader.readOnlyRootFileSystem true }}
          - mountPath: /tmp/
            name: tmp-volume
          {{- end }}
          {{- with .Values.reloader.deployment.volumeMounts }}
          {{- . | toYaml | nindent 10 }}
          {{- end }}
      {{- end }}
      {{- if or (.Values.reloader.logFormat) (.Values.reloader.logLevel) (.Values.reloader.ignoreSecrets) (.Values.reloader.ignoreNamespaces) (.Values.reloader.namespaceSelector) (.Values.reloader.resourceLabelSelector) (.Values.reloader.ignoreConfigMaps) (.Values.reloader.custom_annotations) (eq .Values.reloader.isArgoRollouts true) (eq .Values.reloader.reloadOnCreate true) (eq .Values.reloader.reloadOnDelete true) (ne .Values.reloader.reloadStrategy "default") (.Values.reloader.enableHA) (.Values.reloader.autoReloadAll)}}
        args:
          {{- if .Values.reloader.logFormat }}
          - "--log-format={{ .Values.reloader.logFormat }}"
          {{- end }}
          {{- if .Values.reloader.logLevel }}
          - "--log-level={{ .Values.reloader.logLevel }}"
          {{- end }}
          {{- if .Values.reloader.ignoreSecrets }}
          - "--resources-to-ignore=secrets"
          {{- end }}
          {{- if .Values.reloader.ignoreConfigMaps }}
          - "--resources-to-ignore=configMaps"
          {{- end }}
          {{- if .Values.reloader.ignoreNamespaces }}
          - "--namespaces-to-ignore={{ .Values.reloader.ignoreNamespaces }}"
          {{- end }}
          {{- if .Values.reloader.namespaceSelector }}
          - "--namespace-selector={{ .Values.reloader.namespaceSelector }}"
          {{- end }}
          {{- if .Values.reloader.resourceLabelSelector }}
          - "--resource-label-selector={{ .Values.reloader.resourceLabelSelector }}"
          {{- end }}
          {{- if .Values.reloader.custom_annotations }}
            {{- if .Values.reloader.custom_annotations.configmap }}
          - "--configmap-annotation"
          - "{{ .Values.reloader.custom_annotations.configmap }}"
            {{- end }}
            {{- if .Values.reloader.custom_annotations.secret }}
          - "--secret-annotation"
          - "{{ .Values.reloader.custom_annotations.secret }}"
            {{- end }}
            {{- if .Values.reloader.custom_annotations.auto }}
          - "--auto-annotation"
          - "{{ .Values.reloader.custom_annotations.auto }}"
            {{- end }}
            {{- if .Values.reloader.custom_annotations.secret_auto }}
          - "--secret-auto-annotation"
          - "{{ .Values.reloader.custom_annotations.secret_auto }}"
            {{- end }}
            {{- if .Values.reloader.custom_annotations.configmap_auto }}
          - "--configmap-auto-annotation"
          - "{{ .Values.reloader.custom_annotations.configmap_auto }}"
            {{- end }}
            {{- if .Values.reloader.custom_annotations.search }}
          - "--auto-search-annotation"
          - "{{ .Values.reloader.custom_annotations.search }}"
            {{- end }}
            {{- if .Values.reloader.custom_annotations.match }}
          - "--search-match-annotation"
          - "{{ .Values.reloader.custom_annotations.match }}"
            {{- end }}
            {{- if .Values.reloader.webhookUrl }}
          - "--webhook-url"
          - "{{ .Values.reloader.webhookUrl }}"
            {{- end }}
          {{- end }}
          {{- if eq .Values.reloader.isArgoRollouts true }}
          - "--is-Argo-Rollouts={{ .Values.reloader.isArgoRollouts }}"
          {{- end }}
          {{- if eq .Values.reloader.reloadOnCreate true }}
          - "--reload-on-create={{ .Values.reloader.reloadOnCreate }}"
          {{- end }}
          {{- if eq .Values.reloader.reloadOnDelete true }}
          - "--reload-on-delete={{ .Values.reloader.reloadOnDelete }}"
          {{- end }}
          {{- if eq .Values.reloader.syncAfterRestart true }}
          - "--sync-after-restart={{ .Values.reloader.syncAfterRestart }}"
          {{- end }}
          {{- if ne .Values.reloader.reloadStrategy "default" }}
          - "--reload-strategy={{ .Values.reloader.reloadStrategy }}"
          {{- end }}
          {{- if or (gt (int .Values.reloader.deployment.replicas) 1) (.Values.reloader.enableHA) }}
          - "--enable-ha=true"
          {{- end}}
          {{- if eq .Values.reloader.autoReloadAll true }}
          - "--auto-reload-all=true"
          {{- end -}}
      {{- end }}
      {{- if .Values.reloader.deployment.resources }}
        resources:
{{ toYaml .Values.reloader.deployment.resources | indent 10 }}
      {{- end }}
{{- if .Values.reloader.deployment.securityContext }}
      securityContext: {{ toYaml .Values.reloader.deployment.securityContext | nindent 8 }}
{{- end }}
      serviceAccountName: {{ template "reloader-serviceAccountName" . }}
{{- if hasKey .Values.reloader.deployment "automountServiceAccountToken" }}
      automountServiceAccountToken: {{ .Values.reloader.deployment.automountServiceAccountToken }}
{{- end }}
    {{- if (or (.Values.reloader.deployment.volumes) (eq .Values.reloader.readOnlyRootFileSystem true)) }}
      volumes:
        {{- if eq .Values.reloader.readOnlyRootFileSystem true }}
        - emptyDir: {}
          name: tmp-volume
        {{- end }}
        {{- with .Values.reloader.deployment.volumes }}
          {{- . | toYaml | nindent 8 }}
        {{- end }}
    {{- end }}
