apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "pulse.fullname" . }}
  labels:
    {{- include "pulse.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "pulse.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "pulse.selectorLabels" . | nindent 8 }}
        {{- with .Values.server.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- $podAnnotations := merge (dict) .Values.podAnnotations .Values.server.podAnnotations }}
      {{- if $podAnnotations }}
      annotations:
        {{- toYaml $podAnnotations | nindent 8 }}
      {{- end }}
    spec:
      serviceAccountName: {{ include "pulse.serviceAccountName" . }}
      {{- $podSecurityContext :=
        ternary
          (merge (dict) .Values.podSecurityContext .Values.server.podSecurityContext)
          .Values.server.podSecurityContext
          .Values.podSecurityContext.enabled }}
      {{- if $podSecurityContext }}
      securityContext:
        {{- omit $podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: pulse
          image: "{{ .Values.image.repository }}:{{ default .Chart.AppVersion .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 7655
          {{- $containerSecurityContext :=
            ternary
              (merge (dict) .Values.containerSecurityContext .Values.server.securityContext)
              .Values.server.securityContext
              .Values.containerSecurityContext.enabled }}
          {{- if $containerSecurityContext }}
          securityContext:
            {{- omit $containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- $envList := list }}
          {{- range .Values.server.env }}
          {{- $envList = append $envList . }}
          {{- end }}
          {{- range .Values.server.extraEnv }}
          {{- $envList = append $envList . }}
          {{- end }}
          {{- $secret := .Values.server.secretEnv }}
          {{- $secretKeys := list }}
          {{- if $secret.keys }}
          {{- $secretKeys = $secret.keys }}
          {{- else if $secret.data }}
          {{- $secretKeys = keys $secret.data }}
          {{- end }}
          {{- $root := . }}
          {{- range $key := $secretKeys }}
          {{- $envList = append $envList (dict "name" $key "valueFrom" (dict "secretKeyRef" (dict "name" (include "pulse.serverSecretName" $root) "key" $key))) }}
          {{- end }}
          {{- if $envList }}
          env:
            {{- toYaml $envList | nindent 12 }}
          {{- end }}
          {{- $envFrom := concat .Values.server.envFrom .Values.server.extraEnvFrom }}
          {{- if $envFrom }}
          envFrom:
            {{- range $envFrom }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /data
            {{- with .Values.server.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          {{- with .Values.server.livenessProbe }}
          {{- if .enabled }}
          livenessProbe:
            httpGet:
              path: {{ .path }}
              port: http
            initialDelaySeconds: {{ .initialDelaySeconds }}
            periodSeconds: {{ .periodSeconds }}
            timeoutSeconds: {{ .timeoutSeconds }}
            failureThreshold: {{ .failureThreshold }}
          {{- end }}
          {{- end }}
          {{- with .Values.server.readinessProbe }}
          {{- if .enabled }}
          readinessProbe:
            httpGet:
              path: {{ .path }}
              port: http
            initialDelaySeconds: {{ .initialDelaySeconds }}
            periodSeconds: {{ .periodSeconds }}
            timeoutSeconds: {{ .timeoutSeconds }}
            failureThreshold: {{ .failureThreshold }}
          {{- end }}
          {{- end }}
          {{- with .Values.server.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      volumes:
        - name: data
          {{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
          persistentVolumeClaim:
            claimName: {{ include "pulse.fullname" . }}
          {{- else if and .Values.persistence.enabled .Values.persistence.existingClaim }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- with .Values.server.extraVolumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.server.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.server.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.server.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
