{{- if .Values.rbac.create -}}
{{if .Values.clusterRole.create -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "kubeinvaders.clusterRoleName" . }}
  labels:
    app: kubeinvaders
    chart: {{ template "kubeinvaders.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    {{- with .Values.additionalLabels }}
    {{- . | toYaml | nindent 4 }}
    {{- end }}
rules:
- apiGroups: [""]
  resources: ["pods", "pods/log"]
  verbs: ["delete"]
- apiGroups: ["batch", "extensions"]
  resources: ["jobs"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- apiGroups: ["*"]
  resources: ["*"]
  verbs: ["get", "watch", "list"]
{{- end -}}
{{- if or .Values.clusterRole.create .Values.clusterRole.name }}
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: {{ template "kubeinvaders.clusterRoleName" . }}
  labels:
    app: kubeinvaders
    chart: {{ template "kubeinvaders.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
subjects:
- kind: ServiceAccount
  name: {{ template "kubeinvaders.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ template "kubeinvaders.clusterRoleName" . }}
{{- end -}}
{{- end -}}
