{{- if .Values.managerRbac.create }}
{{- /*
This file contains the various namespace-scoped roles + bindings that the traffic-manager needs.
This will likely expand over time as we move more things from the clients
domain into the traffic-manager.  But the good news there is that it will
require less permissions in clientRbac.yaml
*/}}
{{- if .Values.managerRbac.namespaced }}

{{- if not (has (include "traffic-manager.namespace" .) .Values.managerRbac.namespaces) }}
{{- fail (printf "Namespace %s must be included in managerRbac.namespaces (managerRbac.namespaces contains: %s)" (include "traffic-manager.namespace" .) (join "," .Values.managerRbac.namespaces)) }}
{{- end }}

{{- range .Values.managerRbac.namespaces }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: traffic-manager
  namespace: {{ . }}
  labels:
    {{- include "telepresence.labels" $ | nindent 4 }}
rules:
- apiGroups:
  - ""
  resources:
  - services
  verbs:
  - update {{/* Only needed for upgrade of older versions */}}
- apiGroups:
  - ""
  resources:
  - pods
  - services
  verbs:
  - list
  - get
  - watch
- apiGroups:
  - ""
  resources:
  - configmaps
  verbs:
  - create
- apiGroups:
  - ""
  resources:
  - configmaps
  verbs:
  - list
  - get
  - watch
  - update
  - delete
  resourceNames:
  - telepresence-agents
  - telepresence-intercept-env
- apiGroups:
  - "apps"
  resources:
  - deployments
  - replicasets
  - statefulsets
  verbs:
  - get
  - list
  - patch
  - update {{/* Only needed for upgrade of older versions */}}
- apiGroups:
    - "events.k8s.io"
  resources:
    - events
  verbs:
    - get
    - watch
{{- if eq . (include "traffic-manager.namespace" $) }}
{{- /* Must be able to get the manager namespace in order to get the cluster-id */}}
- apiGroups:
  - ""
  resources:
  - namespaces
  resourceNames:
  - {{ . }}
  verbs:
  - get
{{- /*
Must be able to make an unsuccessful attempt to create a dummy service in order to receive
the error message containing correct service CIDR
*/}}
- apiGroups:
  - ""
  resources:
  - services
  verbs:
  - create
{{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: traffic-manager
  namespace: {{ . }}
  labels:
    {{- include "telepresence.labels" $ | nindent 4 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: traffic-manager
subjects:
- kind: ServiceAccount
  name: traffic-manager
  namespace: {{ include "traffic-manager.namespace" $ }}
{{- end }}

{{- else }}

---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: {{ include "traffic-manager.namespace" . }}
  name: traffic-manager
  labels:
    {{- include "telepresence.labels" . | nindent 4 }}
rules:
- apiGroups:
  - ""
  resources:
  - services
  verbs:
  - create

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: traffic-manager
  namespace: {{ include "traffic-manager.namespace" . }}
  labels:
    {{- include "telepresence.labels" . | nindent 4 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: traffic-manager
subjects:
- kind: ServiceAccount
  name: traffic-manager
  namespace: {{ include "traffic-manager.namespace" . }}
{{- end }}
{{- end }}
