---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: csi-blobfuse-proxy
  namespace: kube-system
spec:
  selector:
    matchLabels:
      app: csi-blobfuse-proxy
  template:
    metadata:
      labels:
        app: csi-blobfuse-proxy
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: type
                    operator: NotIn
                    values:
                      - virtual-kubelet
      initContainers:
      - name: prepare-binaries
        image: mcr.microsoft.com/k8s/csi/blob-csi:latest
        command: ['sh', '-c', "cp /blobfuse-proxy/*.deb /tmp/"]
        volumeMounts:
        - mountPath: /tmp
          name: tmp-dir
      containers:
      - command:
          - nsenter
          - '--target'
          - '1'
          - '--mount'
          - '--uts'
          - '--ipc'
          - '--net'
          - '--pid'
          - '--'
          - sh
          - '-c'
          - |
            set -xe
            if (( "${INSTALL_BLOBFUSE}" == "true" ))
            then
              dpkg -i /tmp/packages-microsoft-prod.deb
              apt-get update && apt-get install -y blobfuse
            fi
            dpkg -i /tmp/blobfuse-proxy-v0.1.0.deb
            mkdir -p /var/lib/kubelet/plugins/blob.csi.azure.com
            echo "Enabling blobfuse proxy systemctl service"
            systemctl daemon-reload
            systemctl enable blobfuse-proxy
            systemctl start blobfuse-proxy
            echo "waiting for blobfuse-proxy service to start"
            sleep 3s
            # tail blobfuse proxy logs
            journalctl -u blobfuse-proxy -f
        image: mcr.microsoft.com/k8s/csi/blob-csi:latest
        imagePullPolicy: IfNotPresent
        name: sysctl-install-blobfuse-proxy
        env:
          - name: INSTALL_BLOBFUSE
            value: "true"
        resources:
          requests:
            cpu: 10m
        securityContext:
          privileged: true
      hostNetwork: true
      hostPID: true
      nodeSelector:
        kubernetes.io/os: linux
      priorityClassName: system-node-critical
      restartPolicy: Always
      tolerations:
        - operator: Exists
      volumes:
        - hostPath:
            path: /tmp
            type: DirectoryOrCreate
          name: tmp-dir
