# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Role held by controllers that auto-register workloads. Current implementation
# has the controller included in the gateway, but could be moved to separate service
# or to a future release of Istiod. Note this is different from the auto-registration
# for VMs, where the IP address is the one reported by the workload.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  namespace: istio-system
  name: autoregistration
rules:
  - apiGroups: [ "" ]
    resources:
      - "services"
      - "endpoints"
    verbs:
      - "get"
  - apiGroups: [ "networking.istio.io" ]
    verbs: [ "get", "watch", "list", "update", "patch", "create", "delete" ]
    resources: [ "workloadentries", "workloadgroups" ]
  - apiGroups: [ "" ]
    resources: [ "services", "endpoints" ]
    verbs: [ "get", "list", "watch", "update", "patch", "create", "delete" ]
  - apiGroups: [ "discovery.k8s.io" ]
    resources: [ "endpointslices" ]
    verbs: [ "get", "list", "watch", "update", "patch", "create", "delete" ]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  namespace: istio-system
  name: discovery
rules:
  - apiGroups: [ "" ]
    resources:
      - "services"
      - "endpoints"
    verbs:
      - "get"
  - apiGroups: [ "networking.istio.io" ]
    verbs: [ "get", "watch", "list", "update", "patch", "create", "delete" ]
    resources: [ "workloadentries", "workloadgroups" ]
  - apiGroups: [ "" ]
    resources: [ "pods", "nodes", "services", "namespaces", "endpoints" ]
    verbs: [ "get", "list", "watch" ]
  - apiGroups: [ "discovery.k8s.io" ]
    resources: [ "endpointslices" ]
    verbs: [ "get", "list", "watch" ]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  namespace: istio-system
  name: hgate-autoregistration
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: autoregistration
subjects:
  - kind: ServiceAccount
    name: default
    namespace: istio-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  namespace: istio-system
  name: hgate-discovery
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: discovery
subjects:
  - kind: ServiceAccount
    name: default
    namespace: istio-system
