---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    app: kubevirt-node-labeller
  name: kubevirt-node-labeller
  namespace: "{{ namespace }}"
spec:
  selector:
    matchLabels:
      app: kubevirt-node-labeller
  template:
    metadata:
      labels:
        app: kubevirt-node-labeller
    spec:
      serviceAccount: kubevirt-node-labeller
      containers:
      - name: kubevirt-node-labeller-sleeper
        image: "{{ node_labeller_registry }}/node-labeller:{{ node_labeller_version }}"
        command: ["sleep"]
        args: ["infinity"]
      initContainers:

        - name: kvm-info-nfd-plugin
          image: "{{ kvm_info_registry }}/kvm-info-nfd-plugin:{{ kvm_info_version }}"
          command: ["/bin/sh","-c"]
          args: ["cp /usr/bin/kvm-caps-info-nfd-plugin /etc/kubernetes/node-feature-discovery/source.d/;"]
          imagePullPolicy: Always
          volumeMounts:
            - name: nfd-source
              mountPath: "/etc/kubernetes/node-feature-discovery/source.d/"

        - name: kubevirt-cpu-nfd-plugin
          image: "{{ cpu_plugin_registry }}/cpu-nfd-plugin:{{ cpu_plugin_version }}"
          command: ["/bin/sh","-c"]
          args: ["cp /plugin/dest/cpu-nfd-plugin /etc/kubernetes/node-feature-discovery/source.d/; cp /config/cpu-plugin-configmap.yaml /etc/kubernetes/node-feature-discovery/source.d/cpu-plugin-configmap.yaml;"]
          imagePullPolicy: Always
          volumeMounts:
            - name: nfd-source
              mountPath: "/etc/kubernetes/node-feature-discovery/source.d/"
            - mountPath: /config
              name: cpu-config

        - name: libvirt
          image: "{{ virt_launcher_registry }}/virt-launcher:{{ virt_launcher_version }}"
          command: ["/bin/sh","-c"]
          args: ["if [ ! -e /dev/kvm ] && [ $(grep '\\<kvm\\>' /proc/misc | wc -l) -eq 0 ]; then echo 'exiting due to missing kvm device'; exit 0; fi; if [ ! -e /dev/kvm ]; then mknod /dev/kvm c 10 $(grep '\\<kvm\\>' /proc/misc | cut -f 1 -d' '); fi; libvirtd -d; chmod o+rw /dev/kvm; virsh domcapabilities --machine q35 --arch x86_64 --virttype kvm > /etc/kubernetes/node-feature-discovery/source.d/virsh_domcapabilities.xml; cp -r /usr/share/libvirt/cpu_map /etc/kubernetes/node-feature-discovery/source.d/"]
          imagePullPolicy: Always
          securityContext:
            privileged: true
          volumeMounts:
            - name: nfd-source
              mountPath: "/etc/kubernetes/node-feature-discovery/source.d/"
        - name: kubevirt-node-labeller
          securityContext:
            privileged: true
          command: ["/bin/sh","-c"]
          args: ["if [ ! -e /dev/kvm ] && [ $(grep '\\<kvm\\>' /proc/misc | wc -l) -eq 0 ]; then echo 'exiting due to missing kvm device'; exit 0; fi; if [ ! -e /dev/kvm ]; then mknod /dev/kvm c 10 $(grep '\\<kvm\\>' /proc/misc | cut -f 1 -d' '); fi; ./usr/sbin/node-labeller"]
          env:
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          image: "{{ node_labeller_registry }}/node-labeller:{{ node_labeller_version }}"
          volumeMounts:
            - name: nfd-source
              mountPath: "/etc/kubernetes/node-feature-discovery/source.d/"

      volumes:
        - name: nfd-source
          emptyDir: {}
        - name: cpu-config
          configMap:
            name: kubevirt-cpu-plugin-configmap