apiVersion: v1
kind: ServiceAccount
metadata:
  name: machine-api-operator
  namespace: openshift-machine-api

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: machine-api-controllers
  namespace: openshift-machine-api

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: machine-api-termination-handler
  namespace: openshift-machine-api

---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: machine-api-controllers
  namespace: openshift-config
rules:
  - apiGroups:
      - ""
    resources:
      - configmaps
    verbs:
      - get
      - list
      - watch

---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: machine-api-controllers
  namespace: openshift-machine-api
rules:

  - apiGroups:
      - machine.openshift.io
    resources:
      - '*'
    verbs:
      - '*'

  - apiGroups:
      - healthchecking.openshift.io
    resources:
      - '*'
    verbs:
      - '*'

  - apiGroups:
      - ""
    resources:
      - configmaps
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete

  - apiGroups:
      - ""
    resources:
      - secrets
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete

  - apiGroups:
      - ""
    resources:
      - events
    verbs:
      - create
      - watch
      - list
      - patch

  - apiGroups:
      - metal3.io
    resources:
      - baremetalhosts
    verbs:
      - get
      - list
      - watch
      - update
      - patch

  - apiGroups:
      - metal3.io
    resources:
      - baremetalhosts/status
      - baremetalhosts/finalizers
    verbs:
      - update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: machine-api-controllers
rules:

  - apiGroups:
      - ""
    resources:
      - nodes
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete

  - apiGroups:
      - ""
    resources:
      - pods
    verbs:
      - get
      - list
      - watch

  - apiGroups:
      - ""
    resources:
      - pods/eviction
    verbs:
      - create

  - apiGroups:
      - authentication.k8s.io
    resources:
      - tokenreviews
    verbs:
      - create

  - apiGroups:
      - authorization.k8s.io
    resources:
      - subjectaccessreviews
    verbs:
      - create

# TODO(vikasc): Remove extensions/daemonsets permissions once all controllers have bumped kubernetes-drain
  - apiGroups:
      - extensions
    resources:
      - daemonsets
    verbs:
      - get
      - list
      - watch

  - apiGroups:
      - apps
    resources:
      - daemonsets
    verbs:
      - get
      - list
      - watch

  - apiGroups:
    - config.openshift.io
    resources:
    - infrastructures
    - dnses
    verbs:
    - get
    - list
    - watch

# the baremetal pod deployment uses hostNetwork, hostPort, and privileged
  - apiGroups:
      - security.openshift.io
    resources:
      - securitycontextconstraints
    verbs:
      - use
    resourceNames:
      - privileged

---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: machine-api-termination-handler
  namespace: openshift-machine-api
rules:
  - apiGroups:
      - machine.openshift.io
    resources:
      - machines
    verbs:
      - list
      - delete
  - apiGroups:
      - security.openshift.io
    resourceNames:
      - hostnetwork
    resources:
      - securitycontextconstraints
    verbs:
      - use

---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: machine-api-operator
  namespace: openshift-machine-api
rules:

  - apiGroups:
      - ""
    resources:
      - configmaps
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete

  - apiGroups:
      - apps
    resources:
      - deployments
      - daemonsets
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete

  - apiGroups:
      - machine.openshift.io
    resources:
      - '*'
    verbs:
      - list
      - watch

  - apiGroups:
      - ""
    resources:
      - secrets
    verbs:
      - get
      - create
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: machine-api-operator
rules:
  - apiGroups: ["authentication.k8s.io"]
    resources:
      - tokenreviews
    verbs: ["create"]
  - apiGroups: ["authorization.k8s.io"]
    resources:
      - subjectaccessreviews
    verbs: ["create"]
  - apiGroups:
      - config.openshift.io
    resources:
      - clusteroperators
      - clusteroperators/status
    verbs:
      - create
      - get
      - update

  - apiGroups:
      - config.openshift.io
    resources:
      - infrastructures
      - infrastructures/status
    verbs:
      - get

  - apiGroups:
      - config.openshift.io
    resources:
      - featuregates
      - featuregates/status
      - proxies
    verbs:
      - get
      - list
      - watch

  - apiGroups:
      - ""
    resources:
      - events
    verbs:
      - create
      - watch
      - list
      - patch

  - apiGroups:
      - metal3.io
    resources:
      - provisionings
    verbs:
      - get
      - list

  - apiGroups:
      - admissionregistration.k8s.io
    resources:
      - validatingwebhookconfigurations
      - mutatingwebhookconfigurations
    verbs:
      - get
      - list
      - watch
      - create
      - update

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: machine-api-controllers
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: machine-api-controllers
subjects:
  - kind: ServiceAccount
    name: machine-api-controllers
    namespace: openshift-machine-api

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: machine-api-controllers
  namespace: openshift-machine-api
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: machine-api-controllers
subjects:
  - kind: ServiceAccount
    name: machine-api-controllers
    namespace: openshift-machine-api

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: machine-api-controllers
  namespace: openshift-config
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: machine-api-controllers
subjects:
  - kind: ServiceAccount
    name: machine-api-controllers
    namespace: openshift-machine-api

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: machine-api-termination-handler
  namespace: openshift-machine-api
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: machine-api-termination-handler
subjects:
  - kind: ServiceAccount
    name: machine-api-termination-handler
    namespace: openshift-machine-api

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: machine-api-operator
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: machine-api-operator
subjects:
  - kind: ServiceAccount
    name: machine-api-operator
    namespace: openshift-machine-api

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: machine-api-operator
  namespace: openshift-machine-api
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: machine-api-operator
subjects:
  - kind: ServiceAccount
    name: machine-api-operator
    namespace: openshift-machine-api

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: prometheus-k8s-machine-api-operator
  namespace: openshift-machine-api
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: prometheus-k8s-machine-api-operator
subjects:
  - kind: ServiceAccount
    name: prometheus-k8s
    namespace: openshift-monitoring

---
# Roles needed by prometheus to scrape machine api Operator metrics endpoint
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: prometheus-k8s-machine-api-operator
  namespace: openshift-machine-api
rules:
  - apiGroups:
      - ""
    resources:
      - namespace/metrics
    verbs:
      - get
  - apiGroups:
      - ""
    resources:
      - services
      - endpoints
      - pods
    verbs:
      - get
      - list
      - watch
---
# Cluster-reader extension to view machine machine.openshift.io resources
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: machine-api-operator:cluster-reader
  labels:
    rbac.authorization.k8s.io/aggregate-to-cluster-reader: "true"
rules:
- apiGroups:
  - machine.openshift.io
  resources:
  - machinehealthchecks
  - machines
  - machinesets
  verbs:
  - get
  - list
  - watch
