{{- if .Values.ha.enabled }}
{{- if .Values.ha.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ include "kache-service.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "kache-service.labels" . | nindent 4 }}
---
{{- end }}
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: {{ include "kache-service.fullname" . }}-leader-election
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "kache-service.labels" . | nindent 4 }}
rules:
  - apiGroups:
      - coordination.k8s.io
    resources:
      - leases
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ include "kache-service.fullname" . }}-leader-election
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "kache-service.labels" . | nindent 4 }}
subjects:
  - kind: ServiceAccount
    name: {{ include "kache-service.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ include "kache-service.fullname" . }}-leader-election
{{- end }}
