apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "node.fullname" . }}
  labels: {{- include "node.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels: {{- include "node.matchLabels" . | nindent 6 }}
  replicas: {{ .Values.replicas }}
  template:
    metadata:
      labels: {{- include "node.labels" . | nindent 8 }}
    spec:
{{- include "node.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.affinity }}
      affinity: {{- include "node.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "node.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "node.tplValue" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
      initContainers:
        {{- if .Values.getAppFromExternalRepository }}
        - name: git-clone-repository
          image: {{ include "git.image" . }}
          imagePullPolicy: {{ .Values.git.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              git clone {{ .Values.repository }} --branch {{ .Values.revision }} /app
          volumeMounts:
            - name: app
              mountPath: /app
        - name: npm-install
          image: {{ template "node.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          workingDir: /app
          command:
            - /bin/bash
            - -ec
            - |
              npm install
          env:
            - name: HOME
              value: /tmp
          volumeMounts:
            - name: app
              mountPath: /app
        {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "node.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              chown -R {{ .Values.securityContext.runAsUser }}:{{ .Values.securityContext.fsGroup }} {{ .Values.persistence.path }}
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.persistence.path }}
        {{- end }}
        {{- end }}
      containers:
        - name: node
          image: {{ template "node.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          env:
            {{- if .Values.mongodb.install }}
            - name: DATABASE_HOST
              value: {{ template "node.mongodb.fullname" . }}
            - name: DATABASE_PORT
              value: "27017"
            - name: DATABASE_USER
              value: {{ .Values.mongodb.auth.username | quote }}
            - name: DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "node.mongodb.fullname" . }}
                  key: mongodb-password
            - name: DATABASE_NAME
              value: {{ .Values.mongodb.auth.database | quote }}
            - name: DATABASE_CONNECTION_OPTIONS
              value: ""
            {{- else }}
            - name: DATABASE_HOST
              valueFrom:
                secretKeyRef:
                  name: {{ template "node.secretName" . }}
                  key: host
            {{- if not .Values.externaldb.broker.serviceInstanceName }}
            - name: DATABASE_NAME
              valueFrom:
                secretKeyRef:
                  name: {{ template "node.secretName" . }}
                  key: database
            {{- else }}
            - name: DATABASE_NAME
              value: ""
            {{- end }}
            - name: DATABASE_PORT
              valueFrom:
                secretKeyRef:
                  name: {{ template "node.secretName" . }}
                  key: port
            - name: DATABASE_USER
              valueFrom:
                secretKeyRef:
                  name: {{ template "node.secretName" . }}
                  key: username
            - name: DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "node.secretName" . }}
                  key: password
            {{- if .Values.externaldb.ssl }}
            - name: DATABASE_CONNECTION_OPTIONS
              value: "ssl=true"
            {{- else }}
            - name: DATABASE_CONNECTION_OPTIONS
              value: ""
            {{- end }}
            {{- end }}
            - name: DATA_FOLDER
              value: "/app"
            {{- if .Values.extraEnv }}
            {{- toYaml .Values.extraEnv | nindent 12 }}
            {{- end }}
          workingDir: /app
          command:
            - /bin/bash
            - -ec
            - |
              npm start
          ports:
            - name: http
              containerPort: {{ .Values.applicationPort }}
          livenessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 60
            timeoutSeconds: 5
            failureThreshold: 6
          readinessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 10
            timeoutSeconds: 3
            periodSeconds: 5
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.getAppFromExternalRepository }}
            - name: app
              mountPath: /app
            {{- end }}
            - name: data
              mountPath: {{ .Values.persistence.path }}
      volumes:
        {{- if .Values.getAppFromExternalRepository }}
        - name: app
          emptyDir: {}
        {{- end }}
        - name: data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ template "node.fullname" . }}-binding
          {{- else }}
          emptyDir: {}
          {{- end }}
