# This YAML file contains driver-registrar & csi driver nodeplugin API objects
# that are necessary to run CSI nodeplugin for rclone
kind: DaemonSet
apiVersion: apps/v1
metadata:
  name: csi-nodeplugin-rclone
  namespace: kube-system
spec:
  selector:
    matchLabels:
      app: csi-nodeplugin-rclone
  template:
    metadata:
      labels:
        app: csi-nodeplugin-rclone
    spec:
      serviceAccountName: csi-nodeplugin-rclone
      #hostNetwork: true
      #dnsPolicy: ClusterFirstWithHostNet
      dnsPolicy: Default
      containers:
        - name: node-driver-registrar
          #image: k8s.gcr.io/sig-storage/csi-node-driver-registrar:v2.4.0
          image: registry.cn-hangzhou.aliyuncs.com/google_containers/csi-node-driver-registrar:v2.4.0
          lifecycle:
            preStop:
              exec:
                command:
                  [
                    "/bin/sh",
                    "-c",
                    "rm -rf /registration/csi-rclone /registration/csi-rclone-reg.sock",
                  ]
          args:
            - --v=5
            - --csi-address=/plugin/csi.sock
            - --kubelet-registration-path=/var/lib/kubelet/plugins/csi-rclone/csi.sock
          env:
            - name: KUBE_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
          volumeMounts:
            - name: plugin-dir
              mountPath: /plugin
            - name: registration-dir
              mountPath: /registration
        - name: rclone
          securityContext:
            privileged: true
            capabilities:
              add: ["SYS_ADMIN"]
            allowPrivilegeEscalation: true
          #image: dvcrn/csi-rclone-reloaded:v1.4.0
          image: 112.95.163.90:5010/csi-rclone-reloaded:v1.4.0
          args:
            - "/bin/csi-rclone-plugin"
            - "--nodeid=$(NODE_ID)"
            - "--endpoint=$(CSI_ENDPOINT)"
            # - "2>&1 > /opt/log/a.log"
          env:
            - name: NODE_ID
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: CSI_ENDPOINT
              value: unix://plugin/csi.sock
            #- name: RCLONE_LOG_FILE
            #  value: /opt/log/a.log
            #- name: RCLONE_LOG_LEVEL
            #  value: DEBUG
          imagePullPolicy: "Always"
          lifecycle:
            postStart:
              exec:
                command:
                  [
                    "/bin/sh",
                    "-c",
                    "mount -t fuse.rclone | while read -r mount; do umount $(echo $mount | awk '{print $3}') ; done",
                  ]
          volumeMounts:
            - name: plugin-dir
              mountPath: /plugin
            - name: pods-mount-dir
              mountPath: /var/lib/kubelet/pods
              mountPropagation: "Bidirectional"
            #- name: test-dir
            #  mountPath: /opt/log
      volumes:
        - name: plugin-dir
          hostPath:
            path: /var/lib/kubelet/plugins/csi-rclone
            type: DirectoryOrCreate
        - name: pods-mount-dir
          hostPath:
            path: /var/lib/kubelet/pods
            type: Directory
        - hostPath:
            path: /var/lib/kubelet/plugins_registry
            type: DirectoryOrCreate
          name: registration-dir
        #- name: test-dir
        #  hostPath:
        #    path: /home/pcm/abc
        #    type: DirectoryOrCreate
