{{- if .Values.serviceAccount.csiNode.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ .Values.serviceAccount.csiNode.name }}
  labels:
    {{- include "cstor.csiNode.labels" . | nindent 4 }}
  namespace: {{ .Release.Namespace }}
{{- end }}
{{- if .Values.rbac.create }}
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: openebs-cstor-csi-registrar-role
  labels:
    {{- include "cstor.csiNode.labels" . | nindent 4 }}
rules:
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["persistentvolumes", "nodes", "services"]
    verbs: ["get", "list", "patch"]
  - apiGroups: ["*"]
    resources: ["cstorvolumeattachments", "cstorvolumes","cstorvolumeconfigs"]
    verbs: ["get", "list", "watch", "create", "update", "delete", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: openebs-cstor-csi-registrar-binding
  labels:
    {{- include "cstor.csiNode.labels" . | nindent 4 }}
subjects:
  - kind: ServiceAccount
    name: {{ .Values.serviceAccount.csiNode.name }}
    namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: openebs-cstor-csi-registrar-role
  apiGroup: rbac.authorization.k8s.io
{{- if .Values.rbac.pspEnabled }}
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: openebs-cstor-csi-node-role
  labels:
    {{- include "cstor.csiNode.labels" . | nindent 4 }}
rules:
- apiGroups: ['policy']
  resources: ['podsecuritypolicies']
  verbs:     ['use']
  resourceNames:
  - openebs-cstor-csi-node-psp
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: openebs-cstor-csi-node-binding
  labels:
    {{- include "cstor.csiNode.labels" . | nindent 4 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: openebs-cstor-csi-node-role
subjects:
  - kind: ServiceAccount
    name: {{ .Values.serviceAccount.csiNode.name }}
    namespace: {{ $.Release.Namespace }}
{{- end }}
{{- end }}
