{{- if .Values.worker.enabled }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{ .Release.Name }}-worker
  labels:
{{ include "chart_labels" . | indent 4 }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ .Release.Name }}-worker-ingressclass-viewer
  labels:
{{ include "chart_labels" . | indent 4 }}
rules:
  - apiGroups: ["networking.k8s.io"]
    resources: ["ingressclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ .Release.Name }}-worker-ingressclass-viewer-binding
  labels:
{{ include "chart_labels" . | indent 4 }}
subjects:
  - kind: ServiceAccount
    name: {{ .Release.Name }}-worker
    namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ .Release.Name }}-worker-ingressclass-viewer
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ .Release.Name }}-worker-pod-cm-creator
  labels:
{{ include "chart_labels" . | indent 4 }}
rules:
  - apiGroups:
      - ""
    resources:
      - "pods"
      - "configmaps"
      - "secrets"
      - "services"
      - "pods/log"
      - "pods/exec"
    verbs:  
      - "get"
      - "list"
      - "watch"
      - "create"
      - "update"
      - "patch"
      - "delete"
      - "deletecollection"
  - apiGroups:
      - ""
    resources:
      - persistentvolumeclaims
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - ""
    resources: 
      - "events"
    verbs: 
      - "create"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ .Release.Name }}-worker-pod-cm-creator-binding
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "chart_labels" . | indent 4 }}
subjects:
- kind: ServiceAccount
  name: {{ .Release.Name }}-worker
  namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ .Release.Name }}-worker-pod-cm-creator
  apiGroup: rbac.authorization.k8s.io
{{- end }}
