---
{{- if not .Values.knative.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "ollama.fullname" . }}
  namespace: {{ include "ollama.namespace" . }}
  labels:
    {{- include "ollama.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  {{- if or .Values.updateStrategy.type .Values.updateStrategy.rollingUpdate }}
  strategy: {{ .Values.updateStrategy | toYaml | nindent 4 }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "ollama.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "ollama.labels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- if .Values.hostIPC }}
      hostIPC: {{ .Values.hostIPC }}
      {{- end }}
      {{- if .Values.hostPID }}
      hostPID: {{ .Values.hostPID }}
      {{- end }}
      {{- if .Values.hostNetwork }}
      hostNetwork: {{ .Values.hostNetwork }}
      {{- end }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "ollama.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      {{- if .Values.runtimeClassName }}
      runtimeClassName: {{ .Values.runtimeClassName | quote }}
      {{- end }}
      {{- with .Values.initContainers }}
      initContainers:
        {{- tpl (toYaml . ) $ | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag |  default (ternary (printf "%s-rocm" .Chart.AppVersion) (.Chart.AppVersion) (and (.Values.ollama.gpu.enabled) (eq .Values.ollama.gpu.type "amd"))) }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          env:
            {{- if and .Values.ollama.gpu.enabled (or (eq .Values.ollama.gpu.type "nvidia") (not .Values.ollama.gpu.type))}}
            - name: PATH
              value: /usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
            {{- end}}
            {{- with .Values.extraEnv }}
              {{- toYaml . | nindent 12 }}
            {{- end }}
          envFrom:
            {{- with .Values.extraEnvFrom }}
              {{- toYaml . | nindent 12 }}
            {{- end }}
          args:
          {{- with .Values.extraArgs }}
             {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- if .Values.resources }}
          resources:
            {{- $limits := default dict .Values.resources.limits }}
            {{- if .Values.ollama.gpu.enabled }}
          # If gpu is enabled, it can either be a NVIDIA card or a AMD card
              {{- if or (eq .Values.ollama.gpu.type "nvidia") (not .Values.ollama.gpu.type) }}
          # NVIDIA is assumed by default if no value is set and GPU is enabled
          # NVIDIA cards can have mig enabled (i.e., the card is sliced into parts
          # Therefore, the first case is no migs enabled
                {{- if or (not .Values.ollama.gpu.mig) (not .Values.ollama.gpu.mig.enabled ) }}
                  {{- $gpuLimit := dict (.Values.ollama.gpu.nvidiaResource | default "nvidia.com/gpu") (.Values.ollama.gpu.number | default 1) }}
                  {{- $limits = merge $limits $gpuLimit }}
          # Second case is mig is enabled
                {{- else if or (.Values.ollama.gpu.mig.enabled) }}
          # Initialize empty dictionary
                  {{- $migDevices := dict -}}
          # Loop over the entries in the mig devices
                  {{- range $key, $value := .Values.ollama.gpu.mig.devices }}
                    {{- $migKey := printf "nvidia.com/mig-%s" $key -}}
                    {{- $migDevices = merge $migDevices (dict $migKey $value) -}}
                  {{- end }}
                  {{- $limits = merge $limits $migDevices}}
                {{- end }}
              {{- end }}
              {{- if eq .Values.ollama.gpu.type "amd" }}
                {{- $gpuLimit := dict "amd.com/gpu" (.Values.ollama.gpu.number | default 1) }}
                {{- $limits = merge $limits $gpuLimit }}
              {{- end }}
            {{- end }}
            {{- $ressources := deepCopy (dict "limits" $limits) | mergeOverwrite .Values.resources }}
            {{- toYaml $ressources | nindent 12 }}
          {{- end}}
          volumeMounts:
            - name: ollama-data
              mountPath: {{ .Values.ollama.mountPath | default "/root/.ollama" }}
              {{- if .Values.persistentVolume.subPath }}
              subPath: {{ .Values.persistentVolume.subPath }}
              {{- end }}
          {{- with .Values.volumeMounts }}
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: {{ .Values.livenessProbe.path }}
              port: http
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: {{ .Values.readinessProbe.path }}
              port: http
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- end }}
          {{- with .Values.lifecycle}}
          lifecycle:
            {{- toYaml . | nindent 12 }}
          {{- else }}
          {{- if or .Values.ollama.models.pull .Values.ollama.models.run }}
          lifecycle:
            postStart:
              exec:
                command:
                  - /bin/sh
                  - -c
                  - |
                    while ! /bin/ollama ps > /dev/null 2>&1; do
                      sleep 5
                    done
                    {{- if .Values.ollama.models.pull }}
                    echo "{{ include "ollama.modelPullList" . }}" | xargs -n1 /bin/ollama pull {{ternary "--insecure" "" .Values.ollama.insecure | toString }}
                    {{- end }}
                    {{- if .Values.ollama.models.run }}
                    echo "{{ include "ollama.modelRunList" . }}" | xargs -n1 /bin/ollama run
                    {{- end }}
          {{- end }}
          {{- end }}
      volumes:
        - name: ollama-data
          {{- if .Values.persistentVolume.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistentVolume.existingClaim |  default (printf "%s" (include "ollama.fullname" .)) }}
          {{- else }}
          emptyDir: { }
          {{- end }}
        {{- with .Values.volumes }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if or .Values.ollama.gpu.enabled .Values.tolerations }}
      tolerations:
        {{- if and .Values.ollama.gpu.enabled (and 
              ( or (eq .Values.ollama.gpu.type "nvidia") (not .Values.ollama.gpu.type)) 
              ( or (not .Values.ollama.gpu.mig) (not .Values.ollama.gpu.mig.enabled))
            ) }}
        - key: "{{(.Values.ollama.gpu.nvidiaResource | default "nvidia.com/gpu")}}"
          operator: Exists
          effect: NoSchedule
          {{- else if and .Values.ollama.gpu.enabled (and 
              ( or (eq .Values.ollama.gpu.type "nvidia") (not .Values.ollama.gpu.type)) 
              (( .Values.ollama.gpu.mig.enabled))
            ) }}
            {{- range $key, $value := .Values.ollama.gpu.mig.devices }}
        - key: nvidia.com/mig-{{ $key }}
          operator: Exists
          effect: NoSchedule
            {{- end }}
        {{- end }}
        {{- with .Values.tolerations }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- end }}
{{- end }}
