# Role for storageos operator
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: storageos:operator
  labels:
    app: {{ template "storageos.name" . }}
    chart: {{ template "storageos.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
rules:
- apiGroups:
  - storageos.com
  resources:
  - storageosclusters
  - storageosclusters/status
  - storageosupgrades
  - storageosupgrades/status
  - jobs
  - jobs/status
  - nfsservers
  - nfsservers/status
  verbs:
  - "*"
- apiGroups:
  - apps
  resources:
  - statefulsets
  - daemonsets
  - deployments
  - replicasets
  verbs:
  - "*"
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - list
  - watch
  - get
  - update
  - create
  - patch
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - list
  - watch
  - get
  - update
  - patch
  - delete
  - create
- apiGroups:
  - ""
  resources:
  - events
  - namespaces
  - serviceaccounts
  - secrets
  - services
  - persistentvolumeclaims
  - persistentvolumes
  - configmaps
  - replicationcontrollers
  - pods/binding
  - endpoints
  verbs:
  - create
  - patch
  - get
  - list
  - delete
  - watch
  - update
- apiGroups:
  - rbac.authorization.k8s.io
  resources:
  - roles
  - rolebindings
  - clusterroles
  - clusterrolebindings
  verbs:
  - create
  - delete
- apiGroups:
  - storage.k8s.io
  resources:
  - storageclasses
  - volumeattachments
  - csinodeinfos
  verbs:
  - create
  - delete
  - watch
  - list
  - get
  - update
- apiGroups:
  - apiextensions.k8s.io
  resources:
  - customresourcedefinitions
  verbs:
  - create
  - delete
- apiGroups:
  - csi.storage.k8s.io
  resources:
  - csidrivers
  verbs:
  - create
  - delete
- apiGroups:
  - policy
  resources:
  - poddisruptionbudgets
  verbs:
  - list
  - watch
- apiGroups:
  - security.openshift.io
  resourceNames:
  - privileged
  resources:
  - securitycontextconstraints
  verbs:
  - create
  - delete
  - update
  - get
  - use

---

# Bind operator service account to storageos-operator role
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: storageos:operator
  labels:
    app: {{ template "storageos.name" . }}
    chart: {{ template "storageos.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
subjects:
- kind: ServiceAccount
  name: {{ template "storageos.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: storageos:operator
  apiGroup: rbac.authorization.k8s.io

{{- if .Values.podSecurityPolicy.enabled }}
---

# ClusterRole for using pod security policy.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: storageos:psp-user
  labels:
    app: {{ template "storageos.name" . }}
    chart: {{ template "storageos.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
rules:
- apiGroups: ["extensions"]
  resources: ["podsecuritypolicies"]
  verbs: ["use"]
  resourceNames:
  - {{ template "storageos.fullname" . }}-psp

---

# Bind pod security policy cluster role to the operator service account.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: storageos:psp-user
  labels:
    app: {{ template "storageos.name" . }}
    chart: {{ template "storageos.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
roleRef:
   apiGroup: rbac.authorization.k8s.io
   kind: ClusterRole
   name: storageos:psp-user
subjects:
  - kind: ServiceAccount
    name: {{ template "storageos.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}

{{- end }}
