---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: comfyui-data
  namespace: ai-stack
spec:
  accessModes: [ReadWriteOnce]
  storageClassName: local-path
  resources:
    requests:
      storage: 30Gi

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: comfyui
  namespace: ai-stack
spec:
  replicas: 1
  selector:
    matchLabels:
      app: comfyui
  template:
    metadata:
      labels:
        app: comfyui
    spec:
      securityContext:
        supplementalGroups: [44, 992]
      initContainers:
        - name: setup
          image: python:3.11-slim
          command: ["/bin/bash", "-c"]
          args:
            - |
              if [ ! -d /workspace/ComfyUI ]; then
                apt-get update -qq && apt-get install -y git -qq
                cd /workspace && git clone --depth=1 https://github.com/comfyanonymous/ComfyUI.git
                pip install -q --no-cache-dir -r /workspace/ComfyUI/requirements.txt
                # Intel Extension for PyTorch (CPU+XPU)
                pip install -q --no-cache-dir intel-extension-for-pytorch oneccl_bind_pt \
                  --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/cpu/us/ || true
                echo "Setup complete"
              else
                echo "Already setup"
              fi
          volumeMounts:
            - name: comfyui-data
              mountPath: /workspace
      containers:
        - name: comfyui
          image: python:3.11-slim
          command: ["/bin/bash", "-c"]
          args:
            - |
              cd /workspace/ComfyUI
              python main.py --listen 0.0.0.0 --port 8188 --cpu
          ports:
            - containerPort: 8188
          env:
            - name: PYTORCH_ENABLE_MPS_FALLBACK
              value: "1"
          volumeMounts:
            - name: comfyui-data
              mountPath: /workspace
            - name: dri
              mountPath: /dev/dri
          resources:
            requests:
              memory: "2Gi"
              cpu: "500m"
            limits:
              memory: "8Gi"
              cpu: "4000m"
          livenessProbe:
            httpGet:
              path: /
              port: 8188
            initialDelaySeconds: 300
            periodSeconds: 30
      volumes:
        - name: comfyui-data
          persistentVolumeClaim:
            claimName: comfyui-data
        - name: dri
          hostPath:
            path: /dev/dri
            type: Directory

---
apiVersion: v1
kind: Service
metadata:
  name: comfyui
  namespace: ai-stack
spec:
  selector:
    app: comfyui
  ports:
    - port: 80
      targetPort: 8188
  type: ClusterIP

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: comfyui
  namespace: ai-stack
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/proxy-body-size: "500m"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "600"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "600"
spec:
  ingressClassName: nginx
  tls:
    - hosts:
        - comfyui.cloud.mielito.com
      secretName: comfyui-tls
  rules:
    - host: comfyui.cloud.mielito.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: comfyui
                port:
                  number: 80
