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

{{- $config := fromYaml (include "kubernetes-event-exporter.config" .) -}}
{{- if and .Values.rbac.create $config.leaderElection.enabled -}}
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
kind: Role
metadata:
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  name: {{ include "common.names.fullname" . }}-leader-election
  namespace: {{ .Release.Namespace | quote }}
rules:
  - apiGroups:
      - ""
    resourceNames:
      - {{ $config.leaderElection.leaderElectionID }}
    resources:
      - configmaps
    verbs:
      - get
      - update
      - patch
  - apiGroups:
      - ""
    resources:
      - configmaps
    verbs:
      - create
  - apiGroups:
      - coordination.k8s.io
    resourceNames:
      - {{ $config.leaderElection.leaderElectionID }}
    resources:
      - leases
    verbs:
      - get
      - update
      - patch
  - apiGroups:
      - coordination.k8s.io
    resources:
      - leases
    verbs:
      - create
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  name: {{ include "common.names.fullname" . }}-leader-election
  namespace: {{ .Release.Namespace | quote }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ include "common.names.fullname" . }}-leader-election
subjects:
  - kind: ServiceAccount
    name: {{ include "kubernetes-event-exporter.serviceAccountName" . }}
{{- end -}}
