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

{{- if and .Values.rbac.create .Values.certgen.enabled }}
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
kind: Role
metadata:
  name: {{ template "envoy-gateway.certgen.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: envoy-gateway
    app.kubernetes.io/component: certgen
  {{- if or .Values.certgen.annotations .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.certgen.annotations .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - ""
    resources:
      - secrets
    verbs:
      - get
      - create
      - update
---
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
kind: RoleBinding
metadata:
  name: {{ template "envoy-gateway.certgen.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: envoy-gateway
    app.kubernetes.io/component: certgen
  {{- if or .Values.certgen.annotations .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.certgen.annotations .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ template "envoy-gateway.certgen.fullname" . }}
subjects:
  - kind: ServiceAccount
    name: {{ include "envoy-gateway.certgen.serviceAccountName" . }}
---
{{- if .Values.topologyInjector.enabled }}
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
kind: ClusterRole
metadata:
  name: {{ template "envoy-gateway.certgen.fullname.namespace" . }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  annotations:  {{- include "common.tplvalues.render" ( dict "value" .Values.topologyInjector.annotations .Values.commonAnnotations "context" $ ) | nindent 4 }}
rules:
  - apiGroups:
    - admissionregistration.k8s.io
    resources:
    - mutatingwebhookconfigurations
    verbs:
    - get
    - list
    - watch
  - apiGroups:
      - admissionregistration.k8s.io
    resources:
      - mutatingwebhookconfigurations
    resourceNames:
      - 'envoy-gateway-topology-injector.{{ include "common.names.namespace" . }}'
    verbs:
      - update
      - patch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ template "envoy-gateway.certgen.fullname.namespace" . }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.topologyInjector.annotations .Values.commonAnnotations "context" $ ) | nindent 4 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ template "envoy-gateway.certgen.fullname.namespace" . }}
subjects:
  - kind: ServiceAccount
    name: {{ include "envoy-gateway.certgen.serviceAccountName" . }}
    namespace: {{ include "common.names.namespace" . | quote }}
{{- end }}
{{- end }}
