{{- if and .Values.crds.install (not $.Values.crds.inline) }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: capsule:{{ .Release.Name }}:crds
  namespace: {{ .Release.Namespace | quote }}
  annotations:
    # create hook dependencies in the right order
    "helm.sh/hook-weight": "-3"
    {{- include "capsule.crds.annotations" . | nindent 4 }}
  labels:
    app.kubernetes.io/component: {{ include "capsule.crds.component" . | quote }}
    app.kubernetes.io/name: {{ include "capsule.crds.component" . | quote }}
    {{- include "capsule.selectorLabelInstance" . | nindent 4 }}
    {{- include "capsule.baselabels" . | nindent 4 }}
rules:
- apiGroups:
  - ""
  resources:
  - jobs
  verbs:
  - create
  - delete
- apiGroups:
  - apiextensions.k8s.io
  resources:
  - customresourcedefinitions
  resourceNames:
  - capsuleconfigurations.capsule.clastix.io
  - resourcepoolclaims.capsule.clastix.io
  - resourcepools.capsule.clastix.io
  - tenantresources.capsule.clastix.io
  - globaltenantresources.capsule.clastix.io
  - tenants.capsule.clastix.io
  - tenantowners.capsule.clastix.io
  - rulestatuses.capsule.clastix.io
  - customquotas.capsule.clastix.io
  - globalcustomquotas.capsule.clastix.io
  - globalresourcequotas.capsule.clastix.io
  - quantityledgers.capsule.clastix.io
  verbs:
  - create
  - delete
  - get
  - patch
  - update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: capsule:{{ .Release.Name }}:crds
  namespace: {{ .Release.Namespace | quote }}
  annotations:
    # create hook dependencies in the right order
    "helm.sh/hook-weight": "-2"
    {{- include "capsule.crds.annotations" . | nindent 4 }}
  labels:
    app.kubernetes.io/component: {{ include "capsule.crds.component" . | quote }}
    app.kubernetes.io/name: {{ include "capsule.crds.component" . | quote }}
    {{- include "capsule.selectorLabelInstance" . | nindent 4 }}
    {{- include "capsule.baselabels" . | nindent 4 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: capsule:{{ .Release.Name }}:crds
subjects:
  - kind: ServiceAccount
    name: {{ include "capsule.crds.name" . }}
    namespace: {{ .Release.Namespace | quote }}
{{- end }}
