apiVersion: v1
kind: ConfigMap
metadata:
  namespace: kube-system
  name: kubevirt-scripts
  labels:
    kubernetes.io/minikube-addons: kubevirt
    addonmanager.kubernetes.io/mode: Reconcile
data:
  uninstall.sh: |
    #!/bin/bash

    kubectl delete kubevirt kubevirt -n kubevirt

    kubectl delete -f /manifests/kubevirt-operator.yaml

  install.sh: |
    #!/bin/bash

    export KUBEVIRT_VERSION=$(curl -s https://api.github.com/repos/kubevirt/kubevirt/releases | grep tag_name | grep -v -- - | sort -V | tail -1 | awk -F':' '{print $2}' | sed 's/,//' | xargs)
    echo "Installing KubeVirt version: $KUBEVIRT_VERSION"

    curl -Ls "https://github.com/kubevirt/kubevirt/releases/download/${KUBEVIRT_VERSION}/kubevirt-operator.yaml" -o /manifests/kubevirt-operator.yaml
    kubectl create -f /manifests/kubevirt-operator.yaml

    HARDWARE_EMULATION=$(grep -E 'svm|vmx' /proc/cpuinfo)
    if [ -z "$HARDWARE_EMULATION" ]; then
      echo "Using software emulation"

      echo "
    apiVersion: kubevirt.io/v1
    kind: KubeVirt
    metadata:
      name: kubevirt
      namespace: kubevirt
    spec:
      configuration:
        developerConfiguration:
          useEmulation: true
    " | kubectl apply -f -

    else

      echo "
    apiVersion: kubevirt.io/v1
    kind: KubeVirt
    metadata:
      name: kubevirt
      namespace: kubevirt
    spec: {}
    " | kubectl apply -f -

    fi

    sleep infinity
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    kubernetes.io/minikube-addons: kubevirt
    addonmanager.kubernetes.io/mode: Reconcile
  name: kubevirt-install-manager
  namespace: kube-system
spec:
  containers:
  - command:
    - /bin/bash
    - -c
    - /kubevirt-scripts/install.sh
    image: {{.CustomRegistries.Kubectl  | default .ImageRepository | default .Registries.Kubectl }}{{.Images.Kubectl}}
    imagePullPolicy: IfNotPresent
    name: kubevirt-provisioner
    lifecycle:
      preStop:
        exec:
          command:
          - /bin/bash
          - -c
          - /kubevirt-scripts/uninstall.sh
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /manifests
      name: tmp
    - mountPath: /kubevirt-scripts
      name: kubevirt-scripts
  terminationGracePeriodSeconds: 60
  volumes:
  - name: tmp
    emptyDir: {}
  - name: kubevirt-scripts
    configMap:
      defaultMode: 0777
      name: kubevirt-scripts
