##
## Kubernetes Job — Fine-tuning Mistral 7B on Fogbreak data.
## Run manually when enough approved training data is collected.
##
## kubectl apply -f k8s/finetune-job.yaml
##
apiVersion: batch/v1
kind: Job
metadata:
  name: fogbreak-finetune
  namespace: fogbreak-ai
  labels:
    app: fogbreak
    component: finetune
spec:
  backoffLimit: 2
  activeDeadlineSeconds: 86400  # 24-hour timeout
  template:
    metadata:
      labels:
        app: fogbreak
        component: finetune
    spec:
      restartPolicy: OnFailure
      containers:
        - name: finetune
          image: fogbreak/ai-finetune:latest
          command: ["python", "-m", "ai.finetune.train", "train"]
          args:
            - "--data-dir"
            - "/data/training_data"
            - "--output-dir"
            - "/data/models/fogbreak-mistral-ft"
            - "--epochs"
            - "3"
            - "--batch-size"
            - "4"
            - "--learning-rate"
            - "2e-5"
          resources:
            requests:
              cpu: "4"
              memory: "32Gi"
              nvidia.com/gpu: 1
            limits:
              cpu: "8"
              memory: "64Gi"
              nvidia.com/gpu: 1
          env:
            - name: HF_TOKEN
              valueFrom:
                secretKeyRef:
                  name: fogbreak-ai-secrets
                  key: hf-token
            - name: FOGBREAK_DB_HOST
              value: "fogbreak-db"
            - name: FOGBREAK_DB_NAME
              value: "fogbreak"
            - name: FOGBREAK_DB_USER
              valueFrom:
                secretKeyRef:
                  name: fogbreak-db-secrets
                  key: username
                  optional: true
            - name: FOGBREAK_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: fogbreak-db-secrets
                  key: password
                  optional: true
          volumeMounts:
            - name: data
              mountPath: /data
            - name: shm
              mountPath: /dev/shm
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: finetune-data
        - name: shm
          emptyDir:
            medium: Memory
            sizeLimit: 8Gi
      tolerations:
        - key: nvidia.com/gpu
          operator: Exists
          effect: NoSchedule
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: finetune-data
  namespace: fogbreak-ai
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
  storageClassName: fast-ssd
