{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and .Values.notificationController.enabled .Values.notificationController.rbac.create }}
{{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.notificationController.image "chart" .Chart ) ) }}
{{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-manager" (include "flux.notification-controller.fullname.namespace" .) | trunc 63 | trimSuffix "-" }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flux
    app.kubernetes.io/component: notification-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - ""
    resources:
      - events
    verbs:
      - create
      - patch
  - apiGroups:
      - ""
    resources:
      - secrets
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - image.fluxcd.io
    resources:
      - imagerepositories
    verbs:
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - image.fluxcd.io
    resources:
      - imagerepositories/status
    verbs:
      - get
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - alerts
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - alerts/status
    verbs:
      - get
      - patch
      - update
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - providers
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - providers/status
    verbs:
      - get
      - patch
      - update
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - receivers
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - receivers/status
    verbs:
      - get
      - patch
      - update
  - apiGroups:
      - source.fluxcd.io
    resources:
      - buckets
    verbs:
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - source.fluxcd.io
    resources:
      - buckets/status
    verbs:
      - get
  - apiGroups:
      - source.fluxcd.io
    resources:
      - gitrepositories
    verbs:
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - source.fluxcd.io
    resources:
      - gitrepositories/status
    verbs:
      - get
  - apiGroups:
      - source.fluxcd.io
    resources:
      - helmrepositories
    verbs:
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - source.fluxcd.io
    resources:
      - helmrepositories/status
    verbs:
      - get
  - apiGroups:
      - source.fluxcd.io
    resources:
      - ocirepositories
    verbs:
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - source.fluxcd.io
    resources:
      - ocirepositories/status
    verbs:
      - get
    {{- if .Values.notificationController.rbac.rules }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.notificationController.rbac.rules "context" $ ) | nindent 2 }}
    {{- end }}
---
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-alert-editor" (include "flux.notification-controller.fullname.namespace" .) | trunc 63 | trimSuffix "-" }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flux
    app.kubernetes.io/component: notification-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - alerts
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - alerts/status
    verbs:
      - get
---
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-alert-viewer" (include "flux.notification-controller.fullname.namespace" .) | trunc 63 | trimSuffix "-" }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flux
    app.kubernetes.io/component: notification-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - alerts
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - alerts/status
    verbs:
      - get
---
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-provider-editor" (include "flux.notification-controller.fullname.namespace" .) | trunc 63 | trimSuffix "-" }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flux
    app.kubernetes.io/component: notification-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - providers
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - providers/status
    verbs:
      - get
---
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-provider-viewer" (include "flux.notification-controller.fullname.namespace" .) | trunc 63 | trimSuffix "-" }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flux
    app.kubernetes.io/component: notification-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - providers
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - providers/status
    verbs:
      - get
---
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-receiver-editor" (include "flux.notification-controller.fullname.namespace" .) | trunc 63 | trimSuffix "-" }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flux
    app.kubernetes.io/component: notification-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - receivers
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - receivers/status
    verbs:
      - get
---
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-receiver-viewer" (include "flux.notification-controller.fullname.namespace" .) | trunc 63 | trimSuffix "-" }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flux
    app.kubernetes.io/component: notification-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - receivers
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - notification.toolkit.fluxcd.io
    resources:
      - receivers/status
    verbs:
      - get
{{- end }}
