apiVersion: v1
kind: ServiceAccount
metadata:
  name: ascend-device-plugin-sa
  namespace: kube-system
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: pods-node-ascend-device-plugin-role
rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list", "update", "watch", "patch"]
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["get", "patch"]
  - apiGroups: [ "" ]
    resources: [ "nodes/proxy" ]
    verbs: [ "get" ]
  - apiGroups: [""]
    resources: ["nodes/status"]
    verbs: ["get", "patch", "update"]
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["get", "create", "update", "list", "watch"]
  - apiGroups: [ "" ]
    resources: [ "events" ]
    verbs: [ "create" ]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: pods-node-ascend-device-plugin-rolebinding
subjects:
  - kind: ServiceAccount
    name: ascend-device-plugin-sa
    namespace: kube-system
roleRef:
  kind: ClusterRole
  name: pods-node-ascend-device-plugin-role
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: ascend-device-plugin-daemonset
  namespace: kube-system
spec:
  selector:
    matchLabels:
      name: ascend-device-plugin-ds
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      ##### For Kubernetes versions lower than 1.19, seccomp is used with annotations.
      annotations:
        scheduler.alpha.kubernetes.io/critical-pod: ""
        seccomp.security.alpha.kubernetes.io/pod: runtime/default
      labels:
        name: ascend-device-plugin-ds
    spec:
      ##### For Kubernetes version 1.19 and above, seccomp is used with securityContext:seccompProfile
#      securityContext:
#        seccompProfile:
#          type: RuntimeDefault
      tolerations:
        - key: CriticalAddonsOnly
          operator: Exists
        - key: huawei.com/Ascend910
          operator: Exists
          effect: NoSchedule
        - key: huawei.com/Ascend310
          operator: Exists
          effect: NoSchedule
        - key: huawei.com/Ascend310P
          operator: Exists
          effect: NoSchedule
        - key: huawei.com/npu
          operator: Exists
          effect: NoSchedule
        - key: "device-plugin"
          operator: "Equal"
          value: "v2"
          effect: NoSchedule
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: servertype
                    operator: NotIn
                    values:
                      - soc
      priorityClassName: "system-node-critical"
      nodeSelector:
        workerselector: dls-worker-node
      serviceAccountName: ascend-device-plugin-sa
      hostPID: true
      initContainers:
      - name: init-log-setup
        image: ascend-k8sdeviceplugin:v3.0.0
        command: ['sh', '-c', 'mkdir -p /var/log/mindx-dl && chmod 755 /var/log/mindx-dl && chown root:root /var/log/mindx-dl && mkdir -p /var/log/mindx-dl/devicePlugin && chmod 750 /var/log/mindx-dl/devicePlugin && chown root:root /var/log/mindx-dl/devicePlugin']
        securityContext:
          runAsUser: 0
          runAsNonRoot: false
        volumeMounts:
          - name: log-path
            mountPath: /var/log
      containers:
      - image: ascend-k8sdeviceplugin:v3.0.0
        name: device-plugin-01
        resources:
          requests:
            memory: 500Mi
            cpu: 500m
          limits:
            memory: 500Mi
            cpu: 500m
        command: [ "/bin/bash", "-c", "--"]
        args: [ "device-plugin -logFile=/var/log/mindx-dl/devicePlugin/devicePlugin.log -logLevel=0 --enable-healthz=true --healthz-address=11251" ]
        livenessProbe:
          httpGet:
            path: /
            port: 11251
            scheme: HTTP
          initialDelaySeconds: 30
          periodSeconds: 15
          timeoutSeconds: 5
          failureThreshold: 3
        securityContext:
          privileged: true
          readOnlyRootFilesystem: true
        imagePullPolicy: Never
        volumeMounts:
          - name: device-plugin
            mountPath: /var/lib/kubelet/device-plugins
          - name: pod-resource
            mountPath: /var/lib/kubelet/pod-resources
          - name: hiai-driver
            mountPath: /usr/local/Ascend/driver
            readOnly: true
          - name: log-path
            mountPath: /var/log/mindx-dl/devicePlugin
            subPath: mindx-dl/devicePlugin
          - name: tmp
            mountPath: /tmp
          - name: lingqu-log
            mountPath: /var/log/lingqu
          - name: localtime
            mountPath: /etc/localtime
            readOnly: true
          - name: data-trace-file-dir
            mountPath: /user/cluster-info/datatrace-config
# runtime-managed mounts (from ascendplugin-910.yaml / ascendplugin-npu.yaml / ascendplugin-310.yaml / ascendplugin-310P.yaml)
# uncomment if container runtime needs these mounts, otherwise manage via ascend-docker-runtime
#          - name: docker-sock
#            mountPath: /run/docker.sock
#            readOnly: true
#          - name: docker-dir
#            mountPath: /run/docker
#            readOnly: true
#          - name: containerd
#            mountPath: /run/containerd
#            readOnly: true
        env:
          - name: MOUNT_BY_RUNTIME_FOR_DP
            value: "true"
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: HOST_IP
            valueFrom:
              fieldRef:
                fieldPath: status.hostIP
      volumes:
        - name: device-plugin
          hostPath:
            path: /var/lib/kubelet/device-plugins
        - name: pod-resource
          hostPath:
            path: /var/lib/kubelet/pod-resources
        - name: hiai-driver
          hostPath:
            path: /usr/local/Ascend/driver
        - name: log-path
          hostPath:
            path: /var/log
            type: Directory
        - name: data-trace-file-dir
          hostPath:
            path: /user/cluster-info/datatrace-config
            type: DirectoryOrCreate
        - name: tmp
          hostPath:
            path: /tmp
        - name: lingqu-log
          hostPath:
            path: /var/log/lingqu
            type: DirectoryOrCreate
        - name: localtime
          hostPath:
            path: /etc/localtime
# runtime-managed volumes (from ascendplugin-910.yaml)
# uncomment if container runtime needs these mounts, otherwise manage via ascend-docker-runtime
#        - name: docker-sock
#          hostPath:
#            path: /run/docker.sock
#        - name: docker-dir
#          hostPath:
#            path: /run/docker
#        - name: containerd
#          hostPath:
#            path: /run/containerd