{{- /*
# Copyright 2022 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.
*/}}
{{- if .Values.collector.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: gmp-system:collector
  {{- if .Values.commonLabels }}
  labels:
    {{- include "prometheus-engine.labels" . | nindent 4 }}
  {{- end }}
rules:
- resources:
  - endpoints
  - nodes
  - nodes/metrics
  - pods
  - services
  apiGroups: [""]
  verbs: ["get", "list", "watch"]
- resources:
  - configmaps
  apiGroups: [""]
  verbs: ["get"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]
{{- end }}
{{- if .Values.operator.rbac.create -}}
  {{- if .Values.collector.rbac.create }}
---
  {{- end }}
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: operator
  namespace: {{.Values.namespace.system}}
  {{- if .Values.commonLabels }}
  labels:
    {{- include "prometheus-engine.labels" . | nindent 4 }}
  {{- end }}
rules:
- resources:
  - pods
  apiGroups: [""]
  verbs: ["list", "watch"]
- resources:
  - secrets
  apiGroups: [""]
  verbs: ["list", "watch", "create"]
- resources:
  - secrets
  apiGroups: [""]
  resourceNames: ["collection", "rules", "alertmanager"]
  verbs: ["get", "patch", "update"]
- resources:
  - configmaps
  apiGroups: [""]
  verbs: ["list", "watch", "create"]
- resources:
  - configmaps
  apiGroups: [""]
  resourceNames: ["collector", "rule-evaluator", "rules-generated"]
  verbs: ["get", "patch", "update"]
- resources:
  - daemonsets
  apiGroups: ["apps"]
  resourceNames: ["collector"]
  verbs: ["get", "list", "watch"]
- resources:
  - deployments
  apiGroups: ["apps"]
  resourceNames: ["rule-evaluator"]
  verbs: ["get", "list", "watch"]
- resources:
  - deployments/scale
  apiGroups: ["apps"]
  resourceNames: ["rule-evaluator"]
  verbs: ["get", "patch", "update"]
- resources:
  - services
  apiGroups: [""]
  resourceNames: ["alertmanager"]
  verbs: ["get", "list", "watch"]
- resources:
  - statefulsets
  apiGroups: ["apps"]
  resourceNames: ["alertmanager"]
  verbs: ["get", "list", "watch"]
- resources:
  - statefulsets/scale
  apiGroups: ["apps"]
  resourceNames: ["alertmanager"]
  verbs: ["get", "patch", "update"]
- resources:
  - verticalpodautoscalers
  apiGroups: ["autoscaling.k8s.io"]
  verbs: ["create", "delete", "get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: operator
  namespace: {{.Values.namespace.public}}
  {{- if .Values.commonLabels }}
  labels:
    {{- include "prometheus-engine.labels" . | nindent 4 }}
  {{- end }}
rules:
- resources:
  - secrets
  apiGroups: [""]
  verbs: ["get", "list", "watch"]
- resources:
  - operatorconfigs
  apiGroups: ["monitoring.googleapis.com"]
  verbs: ["get", "update", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: gmp-system:operator
rules:
# Resources controlled by the operator.
- resources:
  - clusterpodmonitorings
  - clusterrules
  - globalrules
  - clusternodemonitorings
  - podmonitorings
  - rules
  apiGroups: ["monitoring.googleapis.com"]
  verbs: ["get", "list", "watch"]
- resources:
  - clusterpodmonitorings/status
  - clusterrules/status
  - globalrules/status
  - clusternodemonitorings/status
  - podmonitorings/status
  - rules/status
  apiGroups: ["monitoring.googleapis.com"]
  verbs: ["get", "patch", "update"]
- resources:
  - customresourcedefinitions
  resourceNames: ["verticalpodautoscalers.autoscaling.k8s.io"]
  apiGroups: ["apiextensions.k8s.io"]
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: gmp-system:operator:webhook-admin
  {{- if .Values.commonLabels }}
  labels:
    {{- include "prometheus-engine.labels" . | nindent 4 }}
  {{- end }}
rules:
# Permission to inject CA bundles into webhook configs of fixed name.
- resources:
  - validatingwebhookconfigurations
  - mutatingwebhookconfigurations
  apiGroups: ["admissionregistration.k8s.io"]
  resourceNames:
  - gmp-operator.gmp-system.monitoring.googleapis.com
  verbs: ["get", "patch", "update", "watch"]
# Permission to delete legacy webhook config the operator directly created
# in previous versions.
- resources:
  - validatingwebhookconfigurations
  - mutatingwebhookconfigurations
  apiGroups: ["admissionregistration.k8s.io"]
  resourceNames:
  - gmp-operator
  verbs: ["delete"]
{{- end -}}
