{{- if and .Values.istio.install (not .Release.IsInstall) -}}
{{ if .Values.rbac.install }}
{{- $serviceName := include "istio.name" . -}}
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
metadata:
  name: {{ $serviceName }}-pilot
rules:
- apiGroups: ["config.istio.io"]
  resources: ["*"]
  verbs: ["*"]
- apiGroups: ["apiextensions.k8s.io"]
  resources: ["customresourcedefinitions"]
  verbs: ["*"]
- apiGroups: ["istio.io"]
  resources: ["istioconfigs", "istioconfigs.istio.io"]
  verbs: ["*"]
- apiGroups: ["extensions"]
  resources: ["thirdpartyresources", "thirdpartyresources.extensions", "ingresses", "ingresses/status"]
  verbs: ["*"]
- apiGroups: [""]
  resources: ["configmaps", "endpoints", "pods", "services"]
  verbs: ["*"]
- apiGroups: [""]
  resources: ["namespaces", "nodes", "secrets"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["admissionregistration.k8s.io"]
  resources: ["externaladmissionhookconfigurations"]
  verbs: ["create", "update", "delete"]
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
metadata:
  name: {{ $serviceName }}-initializer
rules:
- apiGroups: ["*"]
  resources: ["deployments", "statefulsets", "jobs", "cronjobs", "daemonsets", "replicasets", "replicationcontrollers"]
  verbs: ["initialize", "patch", "watch", "list"]
- apiGroups: ["*"]
  resources: ["configmaps"]
  verbs: ["get", "list", "watch"]
---
# Mixer CRD needs to watch and list CRDs
# It also uses discovery API to discover Kinds of config.istio.io
# K8s adapter needs to list pods, services etc.
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
metadata:
  name: {{ $serviceName }}-mixer
rules:
- apiGroups: ["config.istio.io"] # Istio CRD watcher
  resources: ["*"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["apiextensions.k8s.io"]
  resources: ["customresourcedefinitions"]
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources: ["configmaps", "endpoints", "pods", "services", "namespaces", "secrets"]
  verbs: ["get", "list", "watch"]
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
metadata:
  name: {{ $serviceName }}-ca
rules:
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["create", "get", "watch", "list", "update"]
- apiGroups: [""]
  resources: ["serviceaccounts"]
  verbs: ["get", "watch", "list"]
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
metadata:
  name: {{ $serviceName }}-sidecar
rules:
- apiGroups: ["istio.io"]
  resources: ["istioconfigs"]
  verbs: ["get", "watch", "list"]
- apiGroups: ["extensions"]
  resources: ["thirdpartyresources", "ingresses"]
  verbs: ["get", "watch", "list", "update"]
- apiGroups: [""]
  resources: ["configmaps", "pods", "endpoints", "services"]
  verbs: ["get", "watch", "list"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
metadata:
  name: {{ $serviceName }}-pilot-admin-role-binding
subjects:
- kind: ServiceAccount
  name: {{ $serviceName }}-pilot-service-account
  namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ $serviceName }}-pilot
  apiGroup: rbac.authorization.k8s.io
---
# Grant permissions to the Sidecar initializer
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
metadata:
  name: {{ $serviceName }}-initializer-admin-role-binding
subjects:
- kind: ServiceAccount
  name: {{ $serviceName }}-initializer-service-account
  namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ $serviceName }}-initializer
  apiGroup: rbac.authorization.k8s.io
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
metadata:
  name: {{ $serviceName }}-ca-role-binding
subjects:
- kind: ServiceAccount
  name: {{ $serviceName }}-ca-service-account
  namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ $serviceName }}-ca
  apiGroup: rbac.authorization.k8s.io
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
metadata:
  name: {{ $serviceName }}-ingress-admin-role-binding
subjects:
- kind: ServiceAccount
  name: {{ $serviceName }}-ingress-service-account
  namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ $serviceName }}-pilot
  apiGroup: rbac.authorization.k8s.io
---
# Grant permissions to the Egress controller.
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: {{ $serviceName }}-egress-admin-role-binding
subjects:
- kind: ServiceAccount
  name: {{ $serviceName }}-egress-service-account
  namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ $serviceName }}-pilot
  apiGroup: rbac.authorization.k8s.io
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
metadata:
  name: {{ $serviceName }}-sidecar-role-binding
subjects:
- kind: ServiceAccount
  name: default
  namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ $serviceName }}-sidecar
  apiGroup: rbac.authorization.k8s.io
---
# Grant permissions to Mixer.
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/{{ required "A valid .Values.rbac.apiVersion entry required!" .Values.rbac.apiVersion }}
metadata:
  name: {{ $serviceName }}-mixer-admin-role-binding
subjects:
- kind: ServiceAccount
  name: {{ $serviceName }}-mixer-service-account
  namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ $serviceName }}-mixer
  apiGroup: rbac.authorization.k8s.io
{{ end }}
{{- end -}}
