{{- if and .Values.server.enabled .Values.server.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1
{{- if .Values.rbac.singleNamespace }}
kind: Role
{{- else }}
kind: ClusterRole
{{- end }}
metadata:
  name: {{ include "argo-workflows.server.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/part-of: argo-workflows
    app.kubernetes.io/component: server
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - ""
    resources:
      - configmaps
      - events
    verbs:
      - get
      - watch
      - list
  - apiGroups:
      - ""
    resources:
      - pods
      - pods/exec
      - pods/log
    verbs:
      - get
      - list
      - watch
      - delete
  {{- if and .Values.server.auth.enabled .Values.server.auth.sso.enabled }}
  - apiGroups:
      - ""
    resources:
      - secrets
    resourceNames:
      - sso
    verbs:
      - get
      - update
  - apiGroups:
      - ""
    resources:
      - secrets
    verbs:
      - create
  {{- if .Values.server.auth.sso.rbac }}
  - apiGroups:
      - ""
    resources:
    - serviceaccounts
    verbs:
      - get
      - list
  {{- end }}
  {{- end }}
  - apiGroups:
      - ""
    resources:
      - secrets
    verbs:
      - get
      - list
  {{- if and .Values.server.auth.enabled .Values.server.auth.sso.enabled }}
    {{- if .Values.server.auth.sso.rbac }}
      {{- with .Values.server.auth.sso.rbac.secretWhitelist }}
    resourceNames: {{- toYaml . | nindent 6 }}
      {{- end }}
    {{- end }}
  {{- end }}
  - apiGroups:
      - ""
    resources:
      - events
    verbs:
      - watch
      - create
      - patch
  {{- if (include "argo-workflows.controller.persistence.enabled" .) }}
  - apiGroups:
      - ""
    resources:
      - secrets
    resourceNames:
      - {{ include "argo-workflows.controller.database.username.secret" . }}
      - {{ include "argo-workflows.controller.database.password.secret" . }}
    verbs:
      - get
  {{- end }}
  - apiGroups:
      - argoproj.io
    resources:
      - eventsources
      - sensors
      - workflows
      - workfloweventbindings
      - workflowtemplates
      - cronworkflows
    verbs:
      - create
      - get
      - list
      - watch
      - update
      - patch
      - delete
  - apiGroups:
      - dataflow.argoproj.io
    resources:
      - pipelines
    verbs:
      - create
      - get
      - list
      - watch
      - update
      - patch
      - delete

{{- if .Values.server.clusterWorkflowTemplates.enabled }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ include "argo-workflows.server.fullname" . }}-cluster-template
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/part-of: argo-workflows
    app.kubernetes.io/component: server
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - argoproj.io
    resources:
      - clusterworkflowtemplates
    verbs:
      - get
      - list
      - watch
      {{- if .Values.server.clusterWorkflowTemplates.enableEditing }}
      - create
      - update
      - patch
      - delete
      {{- end }}
{{- end }}
{{- end }}
