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

{{- if and .Values.imageAutomationController.enabled .Values.imageAutomationController.rbac.create }}
{{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.imageAutomationController.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.image-automation-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: image-automation-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - image.toolkit.fluxcd.io
    resources:
      - imageupdateautomations
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - image.toolkit.fluxcd.io
    resources:
      - imageupdateautomations/status
    verbs:
      - get
      - patch
      - update
  - apiGroups:
      - source.toolkit.fluxcd.io
    resources:
      - gitrepositories
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - image.toolkit.fluxcd.io
    resources:
      - imagepolicies
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
    {{- if .Values.imageAutomationController.rbac.rules }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.imageAutomationController.rbac.rules "context" $ ) | nindent 2 }}
    {{- end }}
---
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-imgupdaut-editor" (include "flux.image-automation-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: image-automation-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - image.toolkit.fluxcd.io
    resources:
      - imageupdateautomations
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - image.toolkit.fluxcd.io
    resources:
      - imageupdateautomations/status
    verbs:
      - get
---
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-imgupdaut-viewer" (include "flux.image-automation-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: image-automation-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - image.toolkit.fluxcd.io
    resources:
      - imageupdateautomations
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - image.toolkit.fluxcd.io
    resources:
      - imageupdateautomations/status
    verbs:
      - get
  - apiGroups:
      - image.toolkit.fluxcd.io
    resources:
      - imagepolicies
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
{{- end }}
