apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "node-red.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "node-red.name" . }}
    helm.sh/chart: {{ include "node-red.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: 1
  strategy:
    type: {{ .Values.strategyType }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "node-red.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "node-red.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
      {{- if .Values.podAnnotations }}
      annotations:
        {{- range $key, $value := .Values.podAnnotations }}
        {{ $key }}: {{ $value | quote }}
        {{- end }}
      {{- end }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 1880
              protocol: TCP
          livenessProbe:
            httpGet:
              path: {{ .Values.livenessProbePath }}
              port: http
          readinessProbe:
            httpGet:
              path: {{ .Values.readinessProbePath }}
              port: http
          env:
            - name: FLOWS
              value: "{{ .Values.flows }}"
            - name: NODE_RED_ENABLE_SAFE_MODE
              value: "{{ .Values.safeMode }}"
            - name: NODE_RED_ENABLE_PROJECTS
              value: "{{ .Values.enableProjects }}"
            - name: NODE_OPTIONS
              value: "{{ .Values.nodeOptions }}"
            - name: TZ
              value: "{{ .Values.timezone }}"
          {{- with .Values.extraEnvs }}
{{ toYaml . | indent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /data
{{- if .Values.persistence.subPath }}
              subPath: {{ .Values.persistence.subPath }}
{{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
      volumes:
        - name: data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ if .Values.persistence.existingClaim }}{{ .Values.persistence.existingClaim }}{{- else }}{{ template "node-red.fullname" . }}{{- end }}
        {{- else }}
          emptyDir: {}
        {{ end }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
      securityContext:
        fsGroup: 1001
