{{- if .Values.worker.enabled }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    app: {{ .Release.Name }}-worker
{{ include "chart_labels" . | indent 4 }}
  name: {{ .Release.Name }}-worker
  namespace: {{ .Release.Namespace }}
spec:
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: {{ .Release.Name }}-worker
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}-worker
    spec:
      containers:
        - env:
            - name: GPUSTACK_SERVER_URL
              value: http://{{ .Release.Name }}-server.{{ .Release.Namespace }}.svc:{{ .Values.server.apiPort }}
            - name: GPUSTACK_WORKER_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: GPUSTACK_RUNTIME_DEPLOY
              value: "Kubernetes"
            - name: GPUSTACK_RUNTIME_DEPLOY_MIRRORED_DEPLOYMENT
              value: "true"
            - name: GPUSTACK_RUNTIME_DEPLOY_MIRRORED_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: GPUSTACK_RUNTIME_KUBERNETES_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: GPUSTACK_RUNTIME_KUBERNETES_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: GPUSTACK_WORKER_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            {{- if eq .Values.worker.gpuVendor "hygon" }}
            - name: ROCM_PATH
              value: /opt/dtk
            - name: ROCM_SMI_LIB_PATH
              value: /opt/hyhal/lib
            {{- end }}
          envFrom:
            - secretRef:
                name: registration-token
                optional: false
            - configMapRef:
                name: worker-config
                optional: true
          image: {{ include "gpustack.image" . }}:{{ include "gpustack.imageTag" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          name: {{ .Release.Name }}-worker
          resources: {}
          securityContext:
            allowPrivilegeEscalation: true
            capabilities: {}
            privileged: true
            readOnlyRootFilesystem: false
            runAsNonRoot: false
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - name: gpustack-data-dir
              mountPath: /var/lib/gpustack
            - name: cdi
              mountPath: /var/run/cdi
            - name: kubelet-device-plugins
              mountPath: /var/lib/kubelet/device-plugins
            {{- if eq .Values.worker.gpuVendor "amd" }}
            - name: gpustack-amd-driver
              mountPath: /opt/rocm
              readOnly: true
            {{- end }}
            {{- if eq .Values.worker.gpuVendor "ascend" }}
            - name: gpustack-ascend-driver
              mountPath: /usr/local/Ascend/driver
              readOnly: true
            - name: gpustack-ascend-toolkit
              mountPath: /usr/local/Ascend/ascend-toolkit
              readOnly: true
            {{- end }}
            {{- if eq .Values.worker.gpuVendor "hygon" }}
            - name: gpustack-hygon-driver
              mountPath: /opt/hyhal
              readOnly: true
            - name: gpustack-hygon-toolkit
              mountPath: /opt/dtk
              readOnly: true
            {{- end }}
            {{- if eq .Values.worker.gpuVendor "metax" }}
            - name: gpustack-metax-driver
              mountPath: /opt/mxdriver
              readOnly: true
            - name: gpustack-metax-toolkit
              mountPath: /opt/maca
              readOnly: true
            {{- end }}
            {{- if eq .Values.worker.gpuVendor "iluvatar" }}
            - name: gpustack-iluvatar-toolkit
              mountPath: /usr/local/corex
              readOnly: true
            {{- end }}
            {{- if eq .Values.worker.gpuVendor "cambricon" }}
            - name: gpustack-cambricon-bin
              mountPath: /usr/bin/cnmon
            - name: gpustack-cambricon-toolkit
              mountPath: /usr/local/neuware
              readOnly: true
            {{- end }}
            {{- if eq .Values.worker.gpuVendor "thead" }}
            - name: gpustack-thead-toolkit
              mountPath: /usr/local/PPU_SDK
              readOnly: true
            {{- end }}
            {{- with .Values.worker.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          ports:
            - name: api
              containerPort: {{ .Values.worker.port }}
              protocol: TCP
            - name: metrics
              containerPort: {{ .Values.worker.metricsPort }}
              protocol: TCP
          readinessProbe:
            httpGet:
              path: /readyz
              port: api
            initialDelaySeconds: 5
            periodSeconds: 10
            timeoutSeconds: 2
            failureThreshold: 3
            successThreshold: 1
          livenessProbe:
            httpGet:
              path: /healthz
              port: api
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 2
            failureThreshold: 5
            successThreshold: 1
      volumes:
        - name: gpustack-data-dir
          hostPath:
            path: {{ .Values.worker.dataDir }}
            type: DirectoryOrCreate
        - name: cdi
          hostPath:
            path: /var/run/cdi
            type: DirectoryOrCreate
        - name: kubelet-device-plugins
          hostPath:
            path: /var/lib/kubelet/device-plugins
            type: DirectoryOrCreate
        {{- if eq .Values.worker.gpuVendor "amd" }}
        - name: gpustack-amd-driver
          hostPath:
            path: /opt/rocm
            type: DirectoryOrCreate
        {{- end }}
        {{- if eq .Values.worker.gpuVendor "ascend" }}
        - name: gpustack-ascend-driver
          hostPath:
            path: /usr/local/Ascend/driver
            type: DirectoryOrCreate
        - name: gpustack-ascend-toolkit
          hostPath:
            path: /usr/local/Ascend/ascend-toolkit
            type: DirectoryOrCreate
        {{- end }}
        {{- if eq .Values.worker.gpuVendor "hygon" }}
        - name: gpustack-hygon-driver
          hostPath:
            path: /opt/hyhal
            type: DirectoryOrCreate
        - name: gpustack-hygon-toolkit
          hostPath:
            path: /opt/dtk
            type: DirectoryOrCreate
        {{- end }}
        {{- if eq .Values.worker.gpuVendor "metax" }}
        - name: gpustack-metax-driver
          hostPath:
            path: /opt/mxdriver
            type: DirectoryOrCreate
        - name: gpustack-metax-toolkit
          hostPath:
            path: /opt/maca
            type: DirectoryOrCreate
        {{- end }}
        {{- if eq .Values.worker.gpuVendor "iluvatar" }}
        - name: gpustack-iluvatar-toolkit
          hostPath:
            path: /usr/local/corex
            type: DirectoryOrCreate
        {{- end }}
        {{- if eq .Values.worker.gpuVendor "cambricon" }}
        - name: gpustack-cambricon-bin
          hostPath:
            path: /usr/bin/cnmon
            type: FileOrCreate
        - name: gpustack-cambricon-toolkit
          hostPath:
            path: /usr/local/neuware
            type: DirectoryOrCreate
        {{- end }}
        {{- if eq .Values.worker.gpuVendor "thead" }}
        - name: gpustack-thead-toolkit
          hostPath:
            path: /usr/local/PPU_SDK
            type: DirectoryOrCreate
        {{- end }}
        {{- with .Values.worker.extraVolumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      hostNetwork: true
      hostIPC: true
      serviceAccountName: {{ .Release.Name }}-worker
      dnsPolicy: ClusterFirstWithHostNet
      {{- if or (eq .Values.worker.gpuVendor "nvidia") (eq .Values.worker.gpuVendor "mthreads") }}
      runtimeClassName: {{ .Values.worker.gpuVendor }}
      {{- end }}
  updateStrategy:
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
    type: RollingUpdate
{{- end }}
