{{- if .Values.agent.enabled }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: akri-agent-daemonset
  labels: {{- include "akri.labels" . | nindent 4 }}
    app.kubernetes.io/name: akri-agent
    app.kubernetes.io/component: agent
spec:
  selector:
    matchLabels: {{- include "akri.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/name: akri-agent
  template:
    metadata:
      labels: {{- include "akri.labels" . | nindent 8 }}
        app.kubernetes.io/name: akri-agent
        app.kubernetes.io/component: agent
    spec:
      nodeSelector:
        "kubernetes.io/os": linux
        {{- if .Values.agent.nodeSelectors }}
          {{- toYaml .Values.agent.nodeSelectors | nindent 8 }}
        {{- end }}
      {{- if .Values.rbac.enabled }}
      serviceAccountName: 'akri-agent-sa'
      {{- end }}
      containers:
      - name: akri-agent
        {{- $repository := .Values.agent.image.repository -}}
        {{- if .Values.useDevelopmentContainers }}
        {{- if .Values.useLatestContainers }}
        image: {{ printf "%s:%s" $repository (default "latest-dev" .Values.agent.image.tag) | quote }}
        {{- else }}
        image: {{ printf "%s:%s" $repository (default (printf "v%s-dev" .Chart.AppVersion) .Values.agent.image.tag) | quote }}
        {{- end }}
        {{- else }}
        {{- if .Values.useLatestContainers }}
        image: {{ printf "%s:%s" $repository (default "latest" .Values.agent.image.tag) | quote }}
        {{- else }}
        image: {{ printf "%s:%s" $repository (default (printf "v%s" .Chart.AppVersion) .Values.agent.image.tag) | quote }}
        {{- end }}
        {{- end }}
        {{- with .Values.agent.image.pullPolicy }}
        imagePullPolicy: {{ . }}
        {{- end }}
        resources:
          requests:
            memory: {{ .Values.agent.resources.memoryRequest }}
            cpu: {{ .Values.agent.resources.cpuRequest }}
          limits:
            memory: {{ .Values.agent.resources.memoryLimit }}
            cpu: {{ .Values.agent.resources.cpuLimit }}
        {{- if .Values.agent.securityContext }}
        securityContext:
        {{- toYaml .Values.agent.securityContext | nindent 10 }}
        {{- else }}
        securityContext:
          privileged: true
        {{- end}}
        env:
          {{- if .Values.agent.allowDebugEcho }}
          - name: ENABLE_DEBUG_ECHO
            value: "1"
          {{- /* environment variable to tell the Agent's embedded debug echo Discovery Handler whether its instances are shared */}}
          - name: DEBUG_ECHO_INSTANCES_SHARED
            value: {{ .Values.debugEcho.configuration.shared | quote }}
          {{- end }}
          - name: HOST_CRICTL_PATH
            value: /usr/bin/crictl
          - name: HOST_RUNTIME_ENDPOINT
            value: unix:///host/run/containerd/containerd.sock
          - name: HOST_IMAGE_ENDPOINT
            value: unix:///host/run/containerd/containerd.sock
          - name: AGENT_NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: DISCOVERY_HANDLERS_DIRECTORY
            value: /var/lib/akri
        volumeMounts:
          - name: discovery-handlers
            mountPath: /var/lib/akri
          - name: device-plugin
            mountPath: /var/lib/kubelet/device-plugins
          - name: var-run-dockershim
            mountPath: /host/run/containerd/containerd.sock
          {{- if .Values.agent.host.udev }}
          - name: devices
            mountPath: /run/udev
          {{- end }}
        {{- if .Values.prometheus.enabled }}
        ports:
          - name: {{ .Values.prometheus.portName | quote }}
            containerPort: {{ .Values.prometheus.port }}
        {{- end }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
      - name: discovery-handlers
        hostPath:
          path: {{ .Values.agent.host.discoveryHandlers }}
      - name: device-plugin
        hostPath:
          path: "{{ .Values.agent.host.kubeletDevicePlugins }}"
      - name: var-run-dockershim
        hostPath:
          {{- if ne "" .Values.agent.host.containerRuntimeSocket }}
          path: {{.Values.agent.host.containerRuntimeSocket }}
          {{- else if eq .Values.kubernetesDistro "microk8s" }}
          path: "/var/snap/microk8s/common/run/containerd.sock"
          {{- else if eq .Values.kubernetesDistro "k3s" }}
          path: "/run/k3s/containerd/containerd.sock"
          {{- else if eq .Values.kubernetesDistro "k8s" }}
          path: "/run/containerd/containerd.sock"
          {{- else }}
          # Please set container runtime socket by either selecting the appropriate K8s distro `kubernetesDistro=<k8s|k3s|microk8s>` 
          # or setting `agent.host.containerRuntimeSocket=/container/runtime.sock`. 
          # See https://docs.akri.sh/user-guide/cluster-setup for more information.
          # Using K8s default "/run/containerd/containerd.sock" for now.
          path: "/run/containerd/containerd.sock"
          {{- end }}
      {{- if .Values.agent.host.udev }}
      - name: devices
        hostPath:
          path: "{{ .Values.agent.host.udev }}"
      {{- end }}
{{- end }}
