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

{{- if and .Values.server.rbac.create .Values.server.enabled -}}
{{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.server.image "chart" .Chart ) ) }}
{{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
{{- if .Values.rbac.singleNamespace }}
kind: RoleBinding
{{ else }}
kind: ClusterRoleBinding
{{- end }}
metadata:
  name: {{ include "argo-workflows.server.fullname.namespace" . }}
  {{- if .Values.rbac.singleNamespace }}
  namespace: {{ .Release.Namespace | quote }}
  {{- end }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: argo-workflows
    app.kubernetes.io/component: server
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  {{- if .Values.rbac.singleNamespace }}
  kind: Role
  {{ else }}
  kind: ClusterRole
  {{- end }}
  name: {{ include "argo-workflows.server.fullname.namespace" . }}
subjects:
  - kind: ServiceAccount
    name: {{ include "argo-workflows.server.serviceAccountName" . }}
    namespace: {{ .Release.Namespace | quote }}

{{- if .Values.server.clusterWorkflowTemplates.enabled }}
---
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
kind: ClusterRoleBinding
metadata:
  name: {{ include "argo-workflows.server.fullname.namespace" . }}-cluster-template
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: argo-workflows
    app.kubernetes.io/component: server
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ include "argo-workflows.server.fullname.namespace" . }}-cluster-template
subjects:
  - kind: ServiceAccount
    name: {{ include "argo-workflows.server.serviceAccountName" . }}
    namespace: {{ .Release.Namespace | quote }}
{{- end -}}
{{- end -}}
