---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: cloudstack-csi-controller
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cloudstack-csi-controller-role
rules:
  - apiGroups: [""]
    resources: ["nodes", "pods"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims/status"]
    verbs: ["update", "patch"]
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "update", "delete", "patch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]
  - apiGroups: ["coordination.k8s.io"]
    resources: ["leases"]
    verbs: ["get", "watch", "list", "delete", "update", "create"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses", "csinodes"]
    verbs: ["get", "list", "watch"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["volumeattachments"]
    verbs: ["get", "list", "watch", "update", "patch"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["volumeattachments/status"]
    verbs: ["patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cloudstack-csi-controller-binding
subjects:
  - kind: ServiceAccount
    name: cloudstack-csi-controller
    namespace: kube-system
roleRef:
  kind: ClusterRole
  name: cloudstack-csi-controller-role
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: storage.k8s.io/v1
kind: CSIDriver
metadata:
  name: csi.cloudstack.apache.org
spec:
  # Supports only persistent volumes.
  volumeLifecycleModes:
    - Persistent
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cloudstack-csi-controller
  namespace: kube-system
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 0
  selector:
    matchLabels:
      app.kubernetes.io/name: cloudstack-csi-controller
  template:
    metadata:
      labels:
        app.kubernetes.io/name: cloudstack-csi-controller
        app.kubernetes.io/part-of: cloudstack-csi-driver
    spec:
      serviceAccountName: cloudstack-csi-controller
      nodeSelector:
        kubernetes.io/os: linux
        node-role.kubernetes.io/control-plane: ""
      tolerations:
        - effect: NoExecute
          operator: Exists
        - effect: NoSchedule
          operator: Exists

      containers:
        - name: cloudstack-csi-controller
          image: quay.io/apalia/cloudstack-csi-driver:0.0.2
          imagePullPolicy: Always
          args:
            - "-endpoint=$(CSI_ENDPOINT)"
            - "-cloudstackconfig=/etc/cloudstack-csi-driver/cloud-config"
            - "-debug"
          env:
            - name: CSI_ENDPOINT
              value: unix:///var/lib/csi/sockets/pluginproxy/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
            - name: cloudstack-conf
              mountPath: /etc/cloudstack-csi-driver

        - name: external-provisioner
          image: k8s.gcr.io/sig-storage/csi-provisioner:v2.0.4
          imagePullPolicy: IfNotPresent
          args:
            - "--csi-address=$(ADDRESS)"
            - "--v=5"
            - "--feature-gates=Topology=true"
          env:
            - name: ADDRESS
              value: /var/lib/csi/sockets/pluginproxy/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/

        - name: external-attacher
          image: k8s.gcr.io/sig-storage/csi-attacher:v3.0.2
          imagePullPolicy: IfNotPresent
          args:
            - "--csi-address=$(ADDRESS)"
            - "--v=5"
          env:
            - name: ADDRESS
              value: /var/lib/csi/sockets/pluginproxy/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/

      volumes:
        - name: socket-dir
          emptyDir: {}
        - name: cloudstack-conf
          secret:
            secretName: cloudstack-secret
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: cloudstack-csi-node
  namespace: kube-system
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: cloudstack-csi-node
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: cloudstack-csi-node
        app.kubernetes.io/part-of: cloudstack-csi-driver
    spec:
      nodeSelector:
        kubernetes.io/os: linux
      tolerations:
        - effect: NoExecute
          operator: Exists
        - effect: NoSchedule
          operator: Exists

      containers:
        - name: cloudstack-csi-node
          image: quay.io/apalia/cloudstack-csi-driver:0.0.2
          imagePullPolicy: Always
          args:
            - "-endpoint=$(CSI_ENDPOINT)"
            - "-cloudstackconfig=/etc/cloudstack-csi-driver/cloud-config"
            - "-nodeName=$(NODE_NAME)"
            - "-debug"
          env:
            - name: CSI_ENDPOINT
              value: unix:///csi/csi.sock
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
          securityContext:
            privileged: true
          volumeMounts:
            - name: plugin-dir
              mountPath: /csi
            - name: kubelet-dir
              mountPath: /var/lib/kubelet
              # needed so that any mounts setup inside this container are
              # propagated back to the host machine.
              mountPropagation: Bidirectional
            - name: device-dir
              mountPath: /dev
            - name: cloud-init-dir
              mountPath: /run/cloud-init/
            - name: cloudstack-conf
              mountPath: /etc/cloudstack-csi-driver

        - name: node-driver-registrar
          image: k8s.gcr.io/sig-storage/csi-node-driver-registrar:v2.0.1
          imagePullPolicy: IfNotPresent
          args:
            - "--csi-address=$(ADDRESS)"
            - "--kubelet-registration-path=$(DRIVER_REG_SOCK_PATH)"
            - "--v=5"
          env:
            - name: ADDRESS
              value: /csi/csi.sock
            - name: DRIVER_REG_SOCK_PATH
              value: /var/lib/kubelet/plugins/csi.cloudstack.apache.org/csi.sock
          securityContext:
            privileged: true
          volumeMounts:
            - name: plugin-dir
              mountPath: /csi
            - name: registration-dir
              mountPath: /registration

      volumes:
        - name: plugin-dir
          hostPath:
            path: /var/lib/kubelet/plugins/csi.cloudstack.apache.org/
            type: DirectoryOrCreate
        - name: kubelet-dir
          hostPath:
            path: /var/lib/kubelet
            type: Directory
        - name: device-dir
          hostPath:
            path: /dev
            type: Directory
        - name: registration-dir
          hostPath:
            path: /var/lib/kubelet/plugins_registry
            type: Directory
        - name: cloud-init-dir
          hostPath:
            path: /run/cloud-init/
            type: Directory
        - name: cloudstack-conf
          secret:
            secretName: cloudstack-secret

