apiVersion: v1
kind: PersistentVolume
metadata:
  name: n8n-nfs-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    server: 192.168.1.5
    path: "/fs/1000/nfs/k8s/n8n"
---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace: home-apps
  name: n8n-nfs-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: ""
  resources:
    requests:
      storage: 10Gi
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: n8n
  namespace: home-apps
  labels:
    app: n8n
spec:
  replicas: 1
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: n8n
  template:
    metadata:
      labels:
        app: n8n
        name: n8n
    spec:
      containers:
        - name: n8n
          image: mirror.gcr.io/n8nio/n8n:2.26.3
          imagePullPolicy: IfNotPresent
          securityContext:
            runAsUser: 0
            runAsGroup: 0
            # fsGroup: 0
          command: ["/bin/sh", "-c"]
          args:
            - |
              ulimit -n 65536
              npm install -g moment lodash collect.js sugar markdown
              n8n start
          ports:
            - containerPort: 5678
              name: web
              protocol: TCP
          env:
            - name: GENERIC_TIMEZONE
              value: "Australia/Melbourne"
            - name: TZ
              value: "Australia/Melbourne"
            - name: N8N_PORT
              value: "5678"
            - name: NODE_FUNCTION_ALLOW_BUILTIN
              value: "*"
            - name: NODE_FUNCTION_ALLOW_EXTERNAL
              value: moment,lodash,collect.js,sugar,markdown
            # overwrite the default n8n webhook url
            - name: WEBHOOK_URL
              value: "https://n8n-webhook.junv.cc"
            - name: N8N_PAYLOAD_SIZE_MAX
              value: "10240"
            - name: N8N_DIAGNOSTICS_ENABLED
              value: "false"
            - name: EXECUTIONS_DATA_PRUNE
              value: "true"
            # clean up data older than 90 days
            - name: EXECUTIONS_DATA_MAX_AGE
              value: "240"
            - name: EXECUTIONS_DATA_MAX_AMOUNT
              value: "10000"
            - name: N8N_RUNNERS_ENABLED
              value: "true"
            - name: N8N_RUNNERS_MODE
              value: internal
            - name: N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS
              value: "true"
            - name: DB_TYPE
              value: postgresdb
            - name: DB_POSTGRESDB_HOST
              value: 192.168.1.2
            - name: DB_POSTGRESDB_PORT
              value: "5432"
            - name: DB_POSTGRESDB_DATABASE
              value: n8n
            - name: DB_POSTGRESDB_USER
              valueFrom:
                secretKeyRef:
                  name: postgres-credential
                  key: username
            - name: DB_POSTGRESDB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-credential
                  key: password
            - name: DB_POSTGRESDB_SSL
              value: "false"
            - name: DB_POSTGRESDB_USE_POOL
              value: "true"
            - name: DB_POSTGRESDB_USE_POOL_SIZE
              value: "10"
            - name: NODES_EXCLUDE
              value: "[]"
            - name: N8N_RESTRICT_FILE_ACCESS_TO
              value: /root/.n8n-files
            - name: N8N_SKIP_AUTH_ON_OAUTH_CALLBACK
              value: "true"
            - name: NODE_PATH
              value: /root/.n8n/node_modules
          volumeMounts:
            - name: data
              mountPath: "/root/.n8n"
          resources:
            requests:
              cpu: 400m
              memory: 1Gi
            limits:
              cpu: 1500m
              memory: 3Gi
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: n8n-nfs-pvc

---

apiVersion: v1
kind: Service
metadata:
  name: n8n
  labels:
    name: n8n
spec:
  type: ClusterIP
  selector:
    app: n8n
  ports:
    - name: n8n
      targetPort: web
      port: 80
      protocol: TCP

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: n8n-ingress
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/auth-url: "https://pass.junv.cc/oauth2/auth"
    nginx.ingress.kubernetes.io/auth-signin: "https://pass.junv.cc/oauth2/start?rd=https://$host$escaped_request_uri"
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - n8n.junv.cc
    secretName: n8n-tls-new
  rules:
  - host: n8n.junv.cc
    http:
      paths:
      - backend:
          service:
            name: n8n
            port:
              number: 80
        path: /
        pathType: Prefix

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: n8n-webhook-ingress
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/proxy-body-size: 102400m
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - n8n-webhook.junv.cc
    secretName: n8n-webhook-tls
  rules:
  - host: n8n-webhook.junv.cc
    http:
      paths:
      - backend:
          service:
            name: n8n
            port:
              number: 80
        path: /static
        pathType: Prefix
      - backend:
          service:
            name: n8n
            port:
              number: 80
        path: /webhook
        pathType: Prefix
      - backend:
          service:
            name: n8n
            port:
              number: 80
        path: /rest
        pathType: Prefix
      - backend:
          service:
            name: n8n
            port:
              number: 80
        path: /webhook-test
        pathType: Prefix
      - backend:
          service:
            name: n8n
            port:
              number: 80
        path: /api
        pathType: Prefix
      - backend:
          service:
            name: n8n
            port:
              number: 80
        path: /mcp
        pathType: Prefix
