apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "openreplay.fullname" . }}
  namespace: {{ default .Values.namespace .Release.namespace }}
  labels:
    {{- include "openreplay.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "openreplay.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        openreplayRolloutID: {{ randAlphaNum 5 | quote }} # Restart nginx after every deployment
      {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "openreplay.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "openreplay.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ default .Chart.Name .Release.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}/{{ .Values.image.name }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
          {{- range $key, $value := .Values.env }}
          - name: "{{ $key }}"
            value: "{{ $value }}"
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- if .Values.pvc }}
      {{- if eq .Values.pvc.name "hostPath" }}
          volumeMounts:
          - mountPath: {{ .Values.pvc.mountPath }}
            name: datadir
      volumes:
      - name: datadir
        hostPath:
          # Ensure the file directory is created.
          path: {{ .Values.pvc.hostMountPath }}
          type: DirectoryOrCreate
      {{- else }}
          volumeMounts:
          - name: {{ .Values.pvc.name }}
            mountPath: {{ .Values.pvc.mountPath }}
      volumes:
      - name: {{ .Values.pvc.name }}
        persistentVolumeClaim:
          claimName: {{ .Values.pvc.volumeName }}
      {{- 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 }}
