apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "spamasaurusrex.fullname" . }}
  labels:
    {{- include "spamasaurusrex.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "spamasaurusrex.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "spamasaurusrex.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "spamasaurusrex.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.jsonServer.image.repository }}:{{ .Values.jsonServer.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.jsonServer.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
          - name: {{ .Release.Name }}-configmap
            mountPath: /data/db.json
            subPath: db.json
          - name: {{ .Release.Name }}-configmap
            mountPath: /usr/local/lib/node_modules/spamasaurusrex/public/index.html
            subPath: index.html
        - name: {{ .Chart.Name }}-sidecar
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.sidecar.image.repository }}:{{ .Values.sidecar.image.tag }}"
          imagePullPolicy: {{ .Values.sidecar.image.pullPolicy }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          command:
          - /bin/bash
          - -c
          - "/opt/update.sh -u {{ .Values.sidecar.targetUrl }}"
          volumeMounts:
          - name: {{ .Release.Name }}-configmap
            mountPath: /opt/update.sh
            subPath: update.sh
      volumes:
      - name: {{ .Release.Name }}-configmap
        configMap:
          name: {{ .Release.Name }}-configmap
          items:
          - key: db.json
            path: db.json
          - key: index.html
            path: index.html
          - key: update.sh
            path: update.sh
            mode: 0744
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}