{{- if .Values.preloadJob.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name: ollama-preload
  namespace: llm
  labels:
    app.kubernetes.io/name: ollama-preload
spec:
  backoffLimit: 3
  template:
    spec:
      serviceAccountName: default
      restartPolicy: Never
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: topology.kubernetes.io/zone
                    operator: In
                    values:
                      - az-a
      tolerations:
        - key: node-role.kubernetes.io/control-plane
          operator: Equal
          effect: NoSchedule
      initContainers:
        - name: model-cache-init
          image: ollama/ollama:latest
          imagePullPolicy: IfNotPresent
          command:
            - sh
            - -c
            - |
              set -e
              echo "Starting Ollama server to cache models..."
              ollama serve &
              OLLAMA_PID=$!
              sleep 10
              echo "Caching hot-tier models..."
              {{- range .Values.preloadJob.hotModels }}
              echo "Checking if {{ . }} is cached..."
              if ollama ls | grep -q "{{ . }}"; then
                echo "✓ {{ . }} already cached, skipping"
              else
                echo "Pulling {{ . }}..."
                ollama pull {{ . }}
              fi
              {{- end }}
              echo "Model cache initialization complete"
              kill $OLLAMA_PID || true
              wait $OLLAMA_PID 2>/dev/null || true
          volumeMounts:
            - name: models
              mountPath: /root/.ollama
          env:
            - name: OLLAMA_HOST
              value: "127.0.0.1:11434"

      containers:
        - name: cache-populate
          image: curlimages/curl:latest
          imagePullPolicy: IfNotPresent
          command:
            - sh
            - -c
            - |
              set -e
              echo "Waiting for Ollama pod to be ready..."
              until curl -f http://ollama.llm.svc.cluster.local:11434/api/tags 2>/dev/null; do
                echo "Ollama not ready, waiting..."
                sleep 5
              done
              echo "Ollama is ready, populating local cache..."
              {{- range .Values.preloadJob.hotModels }}
              echo "Checking if {{ . }} is already cached..."
              if curl -s http://ollama.llm.svc.cluster.local:11434/api/tags | grep -q "{{ . }}"; then
                echo "✓ {{ . }} already cached, skipping"
              else
                echo "Caching {{ . }} locally..."
                curl -X POST http://ollama.llm.svc.cluster.local:11434/api/pull \
                  -H "Content-Type: application/json" \
                  -d '{"name":"{{ . }}","stream":false}'
              fi
              {{- end }}
              echo "Local cache population complete"

      volumes:
        - name: models
          emptyDir: {}
{{- end }}
