# ==================================================
# kubernetes/deployment.yaml -- marker-api (Kubernetes)
# ==================================================
apiVersion: v1
kind: ConfigMap
metadata:
  name: marker-api-config
data:
  OLLAMA_HOST: "http://10.0.1.127:11434"
  DEESEEK_OCR_MODEL: "deepseek-ocr"
  AMD_COMPUTE: "false"
  TORCH_DEVICE: ""
  MODEL_DTYPE: "float32"
  PORT: "8000"
  HOST: "0.0.0.0"
  GUNICORN_WORKERS: "2"
  GUNICORN_THREADS: "4"
  GUNICORN_TIMEOUT: "300"
  FLASK_DEBUG: "0"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: marker-api
  labels:
    app: marker-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: marker-api
  template:
    metadata:
      labels:
        app: marker-api
    spec:
      containers:
        - name: marker-api
          image: marker-api:latest          # build/push your own image
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8000
              name: http
              protocol: TCP
          envFrom:
            - configMapRef:
                name: marker-api-config
          resources:
            requests:
              cpu: "2"
              memory: "4Gi"
              # Set to marker.nvidia.com/gpu or amd.com/gpu if using GPU plugin
              nvidia.com/gpu: "0"           # remove if using AMD/ROCm via volume mounts
          volumeMounts:
            - name: conversion-results
              mountPath: /app/conversion_results
            - name: uploads
              mountPath: /app/uploads
          livenessProbe:
            httpGet:
              path: /health
              port: 8000
            initialDelaySeconds: 120
            periodSeconds: 30
            timeoutSeconds: 5
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /health
              port: 8000
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 3
            failureThreshold: 3
      volumes:
        - name: conversion-results
          persistentVolumeClaim:
            claimName: marker-api-pvc
        - name: uploads
          emptyDir:
            sizeLimit: 2Gi   # adjustable; use PVC for larger data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: marker-api-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
---
apiVersion: v1
kind: Service
metadata:
  name: marker-api
  labels:
    app: marker-api
spec:
  selector:
    app: marker-api
  ports:
    - port: 8000
      targetPort: 8000
      protocol: TCP
      name: http
  type: ClusterIP
---
# For node-local AMD GPU support in K8s, bind /dev/kfd and /dev/dri:
# Use a DaemonSet with hostPaths instead, or mount via GPU plugin.
# Example DaemonSet for AMD GPU nodes:
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: marker-api-gpu
  labels:
    app: marker-api
spec:
  selector:
    matchLabels:
      app: marker-api
  template:
    metadata:
      labels:
        app: marker-api
    spec:
      hostNetwork: true
      hostPID: true
      containers:
        - name: marker-api
          image: marker-api:latest
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8000
              hostPort: 8000
          envFrom:
            - configMapRef:
                name: marker-api-config
          resources:
            requests:
              cpu: "2"
              memory: "4Gi"
            limits:
              cpu: "4"
              memory: "8Gi"
          volumeMounts:
            - name: conversion-results
              mountPath: /app/conversion_results
            - name: kfd
              mountPath: /dev/kfd
            - name: dri
              mountPath: /dev/dri
      volumes:
        - name: conversion-results
          persistentVolumeClaim:
            claimName: marker-api-pvc
        - name: kfd
          hostPath:
            path: /dev/kfd
        - name: dri
          hostPath:
            path: /dev/dri
