apiVersion: apps/v1
kind: Deployment
metadata:
  name: ollama
  namespace: llm
  labels:
    app.kubernetes.io/name: ollama
    app.kubernetes.io/part-of: llm
spec:
  replicas: {{ .Values.replicaCount }}
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app.kubernetes.io/name: ollama
  template:
    metadata:
      labels:
        app.kubernetes.io/name: ollama
        app.kubernetes.io/part-of: llm
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: topology.kubernetes.io/zone
                    operator: In
                    values:
                      - {{ .Values.nodeAffinity.zone }}
      tolerations:
        - key: node-role.kubernetes.io/control-plane
          operator: Equal
          value: ""
          effect: NoSchedule

      initContainers:
        - name: preload-model
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - sh
            - -c
            - |
              set -e
              echo "Starting Ollama server for model preload..."
              ollama serve &
              OLLAMA_PID=$!
              sleep 10
              {{- range .Values.preloadJob.hotModels }}
              echo "Preloading {{ . }}..."
              if ollama ls | grep -q "{{ . }}"; then
                echo "✓ {{ . }} already cached"
              else
                ollama pull {{ . }}
              fi
              {{- end }}
              echo "Model preload complete"
              kill $OLLAMA_PID || true
              wait $OLLAMA_PID 2>/dev/null || true
          volumeMounts:
            - name: models-cache
              mountPath: /root/.ollama/models
          env:
            - name: OLLAMA_HOST
              value: "127.0.0.1:11434"

      containers:
        - name: ollama
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - containerPort: 11434
              name: http
          env:
            {{- range $key, $value := .Values.env }}
            - name: {{ $key }}
              value: "{{ $value }}"
            {{- end }}
            - name: OLLAMA_MODELS_MINIO_ENDPOINT
              valueFrom:
                secretKeyRef:
                  name: ollama-minio
                  key: endpoint
            - name: OLLAMA_MODELS_MINIO_BUCKET
              valueFrom:
                secretKeyRef:
                  name: ollama-minio
                  key: bucket
            - name: OLLAMA_MODELS_MINIO_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: ollama-minio
                  key: access_key
            - name: OLLAMA_MODELS_MINIO_SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: ollama-minio
                  key: secret_key
          resources:
            requests:
              cpu: {{ .Values.resources.requests.cpu }}
              memory: {{ .Values.resources.requests.memory }}
            limits:
              cpu: {{ .Values.resources.limits.cpu }}
              memory: {{ .Values.resources.limits.memory }}
          livenessProbe:
            httpGet:
              path: /
              port: 11434
            initialDelaySeconds: 30
            periodSeconds: 10
          readinessProbe:
            httpGet:
              path: /
              port: 11434
            initialDelaySeconds: 10
            periodSeconds: 5
          volumeMounts:
            - name: models-cache
              mountPath: /root/.ollama/models

      volumes:
        - name: models-cache
          persistentVolumeClaim:
            claimName: ollama-models-cache
