{{- if .Values.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: {{ template "kubeapps.kubeops.fullname" . }}
  labels:{{ include "kubeapps.extraAppLabels" . | nindent 4 }}
    app: {{ template "kubeapps.kubeops.fullname" . }}
rules:
  - apiGroups:
      - ""
    resources:
      - secrets
    verbs:
      - get
      - create
      - delete
  - apiGroups:
      - "kubeapps.com"
    resources:
      - apprepositories
    verbs:
      - get
      - list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ template "kubeapps.kubeops.fullname" . }}
  labels:{{ include "kubeapps.extraAppLabels" . | nindent 4 }}
    app: {{ template "kubeapps.kubeops.fullname" . }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ template "kubeapps.kubeops.fullname" . }}
subjects:
  - kind: ServiceAccount
    name: {{ template "kubeapps.kubeops.fullname" . }}
    namespace: {{ .Release.Namespace }}
{{- if .Values.allowNamespaceDiscovery }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: "kubeapps:controller:kubeops-ns-discovery-{{ .Release.Namespace }}"
  labels:{{ include "kubeapps.extraAppLabels" . | nindent 4 }}
    app: {{ template "kubeapps.kubeops.fullname" . }}
rules:
  - apiGroups:
      - ""
    resources:
      - namespaces
    verbs:
      - list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: "kubeapps:controller:kubeops-ns-discovery-{{ .Release.Namespace }}"
  labels:{{ include "kubeapps.extraAppLabels" . | nindent 4 }}
    app: {{ template "kubeapps.kubeops.fullname" . }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: "kubeapps:controller:kubeops-ns-discovery-{{ .Release.Namespace }}"
subjects:
  - kind: ServiceAccount
    name: {{ template "kubeapps.kubeops.fullname" . }}
    namespace: {{ .Release.Namespace }}
{{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: "kubeapps:controller:kubeops-operators-{{ .Release.Namespace }}"
  labels:{{ include "kubeapps.extraAppLabels" . | nindent 4 }}
    app: {{ template "kubeapps.kubeops.fullname" . }}
rules:
  - apiGroups:
      - packages.operators.coreos.com
    resources:
      - packagemanifests/icon
    verbs:
      - get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: "kubeapps:controller:kubeops-operators-{{ .Release.Namespace }}"
  labels:{{ include "kubeapps.extraAppLabels" . | nindent 4 }}
    app: {{ template "kubeapps.kubeops.fullname" . }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: "kubeapps:controller:kubeops-operators-{{ .Release.Namespace }}"
subjects:
  - kind: ServiceAccount
    name: {{ template "kubeapps.kubeops.fullname" . }}
    namespace: {{ .Release.Namespace }}
{{- end }}
