apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "kubemirror.fullname" . }}
  labels:
    {{- include "kubemirror.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "kubemirror.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- toYaml .Values.podAnnotations | nindent 8 }}
      labels:
        {{- include "kubemirror.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "kubemirror.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      {{- with .Values.priorityClassName }}
      priorityClassName: {{ . }}
      {{- end }}
      containers:
        - name: controller
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /kubemirror
          args:
            - --metrics-bind-address={{ .Values.controller.metricsBindAddress }}
            - --health-probe-bind-address={{ .Values.controller.healthProbeBindAddress }}
            {{- if .Values.controller.leaderElect }}
            - --leader-elect
            {{- end }}
            - --leader-election-id={{ .Values.controller.leaderElectionID }}
            - --max-targets={{ .Values.controller.maxTargets }}
            - --worker-threads={{ .Values.controller.workerThreads }}
            - --rate-limit-qps={{ .Values.controller.rateLimitQPS }}
            - --rate-limit-burst={{ .Values.controller.rateLimitBurst }}
            {{- if .Values.controller.verifySourceFreshness }}
            - --verify-source-freshness=true
            {{- end }}
            {{- if .Values.controller.lazyWatcherInit }}
            - --lazy-watcher-init=true
            {{- end }}
            - --watcher-scan-interval={{ .Values.controller.watcherScanInterval }}
            {{- if .Values.controller.excludedNamespaces }}
            - --excluded-namespaces={{ .Values.controller.excludedNamespaces }}
            {{- end }}
            {{- if .Values.controller.includedNamespaces }}
            - --included-namespaces={{ .Values.controller.includedNamespaces }}
            {{- end }}
            {{- if .Values.controller.resourceTypes }}
            - --resource-types={{ join "," .Values.controller.resourceTypes }}
            {{- end }}
            - --discovery-interval={{ .Values.controller.discoveryInterval }}
            - --resync-period={{ .Values.controller.resyncPeriod }}
          ports:
            - name: metrics
              containerPort: 8080
              protocol: TCP
            - name: health
              containerPort: 8081
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /healthz
              port: health
            initialDelaySeconds: 15
            periodSeconds: 20
            timeoutSeconds: 5
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /readyz
              port: health
            initialDelaySeconds: 5
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
      terminationGracePeriodSeconds: 10
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
