apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "n8n.fullname" . }}
  labels:
    {{- include "n8n.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount | default 1 }}
  selector:
    matchLabels:
      {{- include "n8n.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
      labels:
        {{- include "n8n.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
        - name: http
          containerPort: 5678
          protocol: TCP
        {{- if .Values.runners.enabled }}
        - name: broker
          containerPort: {{ .Values.service.brokerPort | default 5679 }}
          protocol: TCP
        {{- end }}
        env:
        # N8N configuration from values
        - name: N8N_HOST
          valueFrom:
            configMapKeyRef:
              name: {{ include "n8n.fullname" . }}
              key: N8N_HOST
        - name: N8N_PROTOCOL
          valueFrom:
            configMapKeyRef:
              name: {{ include "n8n.fullname" . }}
              key: N8N_PROTOCOL
        - name: WEBHOOK_URL
          valueFrom:
            configMapKeyRef:
              name: {{ include "n8n.fullname" . }}
              key: WEBHOOK_URL
        - name: GENERIC_TIMEZONE
          valueFrom:
            configMapKeyRef:
              name: {{ include "n8n.fullname" . }}
              key: GENERIC_TIMEZONE
        - name: N8N_SECURE_COOKIE
          valueFrom:
            configMapKeyRef:
              name: {{ include "n8n.fullname" . }}
              key: N8N_SECURE_COOKIE
        - name: DB_TYPE
          valueFrom:
            configMapKeyRef:
              name: {{ include "n8n.fullname" . }}
              key: DB_TYPE
        - name: DB_SQLITE_FILE
          valueFrom:
            configMapKeyRef:
              name: {{ include "n8n.fullname" . }}
              key: DB_SQLITE_FILE
        # Sensitive values from Secret created by ExternalSecret
        - name: N8N_ENCRYPTION_KEY
          valueFrom:
            secretKeyRef:
              name: n8n-secrets
              key: N8N_ENCRYPTION_KEY
        - name: TELEGRAM_BOT_TOKEN
          valueFrom:
            secretKeyRef:
              name: n8n-secrets
              key: TELEGRAM_BOT_TOKEN
        {{- if .Values.runners.enabled }}
        - name: N8N_RUNNERS_AUTH_TOKEN
          valueFrom:
            secretKeyRef:
              name: {{ .Values.runners.authSecretName }}
              key: {{ .Values.runners.authSecretKey }}
        {{- end }}
        {{- if .Values.env }}
        {{- range $key, $value := .Values.env }}
        - name: {{ $key }}
          value: {{ $value | quote }}
        {{- end }}
        {{- end }}
        {{- if .Values.envFrom }}
        envFrom:
        {{- range $envFrom := .Values.envFrom }}
        {{- if $envFrom.configMapRef }}
        - configMapRef:
            name: {{ $envFrom.configMapRef.name }}
        {{- end }}
        {{- if $envFrom.secretRef }}
        - secretRef:
            name: {{ $envFrom.secretRef.name }}
        {{- end }}
        {{- end }}
        {{- end }}
        livenessProbe:
          httpGet:
            path: /healthz
            port: http
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /healthz
            port: http
          initialDelaySeconds: 10
          periodSeconds: 5
          timeoutSeconds: 3
          failureThreshold: 3
        resources:
          {{- toYaml .Values.resources | nindent 12 }}
        volumeMounts:
        - name: data
          mountPath: /root/.n8n
      volumes:
      - name: data
        {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ include "n8n.fullname" . }}
        {{- else }}
        emptyDir: {}
        {{- end }}
