apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "k8s-pulse.fullname" . }}
  labels:
    {{- include "k8s-pulse.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "k8s-pulse.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "k8s-pulse.selectorLabels" . | nindent 8 }}
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "k8s-pulse.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 }}
          {{- if .Values.kubeconfig.enabled }}
          env:
            - name: KUBECONFIG
              value: {{ .Values.kubeconfig.mountPath | quote }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.service.targetPort }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /healthz
              port: http
            initialDelaySeconds: 15
            periodSeconds: 15
            timeoutSeconds: 5
            failureThreshold: 5
          readinessProbe:
            httpGet:
              path: /healthz
              port: http
            initialDelaySeconds: 10
            periodSeconds: 15
            timeoutSeconds: 5
            failureThreshold: 5
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- if or .Values.persistence.enabled .Values.kubeconfig.enabled }}
          volumeMounts:
            {{- if .Values.persistence.enabled }}
            - name: data
              mountPath: /app/data
            {{- end }}
            {{- if .Values.kubeconfig.enabled }}
            - name: kubeconfig-volume
              mountPath: {{ .Values.kubeconfig.mountPath | quote }}
              subPath: {{ .Values.kubeconfig.secretKey | default "config" | quote }}
              readOnly: true
            {{- end }}
          {{- end }}
      {{- if or .Values.persistence.enabled .Values.kubeconfig.enabled }}
      volumes:
        {{- if .Values.persistence.enabled }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ include "k8s-pulse.fullname" . }}-data
        {{- end }}
        {{- if .Values.kubeconfig.enabled }}
        - name: kubeconfig-volume
          secret:
            secretName: {{ include "k8s-pulse.kubeconfigSecretName" . }}
        {{- end }}
      {{- 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 }}
