{{- if .Values.serviceAccount.create -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ include "freeleaps-secret-operator.fullname" . }}
  labels:
    {{- include "freeleaps-secret-operator.labels" . | nindent 4 }}
rules:
  # Core resources
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create", "patch"]
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["get", "list", "watch"]

  # Custom resources - FreeleapsSecretStore (cluster-scoped)
  - apiGroups: ["freeleaps.com"]
    resources: ["freeleapssecretstores"]
    verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
  - apiGroups: ["freeleaps.com"]
    resources: ["freeleapssecretstores/status"]
    verbs: ["get", "update", "patch"]
  - apiGroups: ["freeleaps.com"]
    resources: ["freeleapssecretstores/finalizers"]
    verbs: ["update"]

  # Custom resources - FreeleapsSecret (namespaced)
  - apiGroups: ["freeleaps.com"]
    resources: ["freeleapssecrets"]
    verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
  - apiGroups: ["freeleaps.com"]
    resources: ["freeleapssecrets/status"]
    verbs: ["get", "update", "patch"]
  - apiGroups: ["freeleaps.com"]
    resources: ["freeleapssecrets/finalizers"]
    verbs: ["update"]

  # Kopf operator framework requirements
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create"]
  - apiGroups: ["zalando.org"]
    resources: ["clusterkopfpeerings"]
    verbs: ["list", "watch", "patch", "get"]
  - apiGroups: ["kopf.dev"]
    resources: ["clusterkopfpeerings"]
    verbs: ["list", "watch", "patch", "get"]
  - apiGroups: ["apiextensions.k8s.io"]
    resources: ["customresourcedefinitions"]
    verbs: ["list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include "freeleaps-secret-operator.fullname" . }}
  labels:
    {{- include "freeleaps-secret-operator.labels" . | nindent 4 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ include "freeleaps-secret-operator.fullname" . }}
subjects:
  - kind: ServiceAccount
    name: {{ include "freeleaps-secret-operator.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}
{{- end }}