apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "radar.fullname" . }}
  labels:
    {{- include "radar.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "radar.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "radar.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "radar.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          args:
            - --port={{ .Values.service.port }}
            - --no-browser
            - --timeline-storage={{ .Values.timeline.storage }}
            {{- if eq .Values.timeline.storage "sqlite" }}
            - --timeline-db={{ .Values.timeline.dbPath }}
            {{- end }}
            - --history-limit={{ .Values.timeline.historyLimit }}
            {{- if .Values.traffic.prometheusUrl }}
            - --prometheus-url={{ .Values.traffic.prometheusUrl }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          env:
            - name: HELM_CACHE_HOME
              value: "/tmp/helm/cache"
            - name: HELM_CONFIG_HOME
              value: "/tmp/helm/config"
            - name: HELM_DATA_HOME
              value: "/tmp/helm/data"
            {{- with .Values.env }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          {{- if .Values.probes.liveness.enabled }}
          livenessProbe:
            httpGet:
              path: /api/health
              port: http
            initialDelaySeconds: {{ .Values.probes.liveness.initialDelaySeconds }}
            periodSeconds: {{ .Values.probes.liveness.periodSeconds }}
            timeoutSeconds: {{ .Values.probes.liveness.timeoutSeconds }}
            failureThreshold: {{ .Values.probes.liveness.failureThreshold }}
          {{- end }}
          {{- if .Values.probes.readiness.enabled }}
          readinessProbe:
            httpGet:
              path: /api/health
              port: http
            initialDelaySeconds: {{ .Values.probes.readiness.initialDelaySeconds }}
            periodSeconds: {{ .Values.probes.readiness.periodSeconds }}
            timeoutSeconds: {{ .Values.probes.readiness.timeoutSeconds }}
            failureThreshold: {{ .Values.probes.readiness.failureThreshold }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: tmp
              mountPath: /tmp
            {{- if and .Values.persistence.enabled (eq .Values.timeline.storage "sqlite") }}
            - name: data
              mountPath: /data
            {{- end }}
      volumes:
        - name: tmp
          emptyDir: {}
        {{- if and .Values.persistence.enabled (eq .Values.timeline.storage "sqlite") }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ include "radar.fullname" . }}
        {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
