---

# Copyright 2022 The Knative Authors
#
# 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
#
#     http://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.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: knative-kafka-controller-post-install
  labels:
    kafka.eventing.knative.dev/release: devel
rules:
  - apiGroups:
      - apps
    resources:
      - deployments
    verbs:
      - delete
      - deletecollection

  # sources.knative.dev resources and finalizers we care about.
  - apiGroups:
      - sources.knative.dev
    resources:
      - kafkasources
      - kafkasources/status
    verbs:
      - get
      - list

  - apiGroups:
      - admissionregistration.k8s.io
    resources:
      - mutatingwebhookconfigurations
    resourceNames:
      - defaulting.webhook.kafka.sources.knative.dev
      - defaulting.webhook.kafka.messaging.knative.dev
    verbs:
      - delete

  - apiGroups:
      - admissionregistration.k8s.io
    resources:
      - validatingwebhookconfigurations
    resourceNames:
      - validation.webhook.kafka.sources.knative.dev
      - config.webhook.kafka.sources.knative.dev
      - validation.webhook.kafka.messaging.knative.dev
    verbs:
      - delete

  - apiGroups:
      - "*"
    resources:
      - services
    resourceNames:
      - kafka-source-webhook
      - kafka-controller
      - kafka-webhook
      - kafka-ch-dispatcher
    verbs:
      - delete

  # to be able to list channel services and patch them
  - apiGroups:
      - "*"
    resources:
      - services
    verbs:
      - list
      - patch

  - apiGroups:
      - apps
    resources:
      - deployments
    resourceNames:
      - kafka-controller-manager
      - kafka-webhook
      - kafka-ch-controller
      - kafka-ch-dispatcher
      - kafka-source-dispatcher
    verbs:
      - delete

  # to be able to check readiness of the new dataplane
  - apiGroups:
      - apps
    resources:
      - deployments
    resourceNames:
      - kafka-channel-dispatcher
      - kafka-channel-receiver
      - kafka-controller
    verbs:
      - get

  - apiGroups:
      - rbac.authorization.k8s.io
    resources:
      - clusterrolebindings
    resourceNames:
      - eventing-sources-kafka-controller
      - eventing-sources-kafka-controller-addressable-resolver
      - kafka-webhook
      - kafka-ch-controller
      - kafka-ch-dispatcher
      - eventing-sources-kafka-controller-podspecable-binding
    verbs:
      - delete
  - apiGroups:
      - rbac.authorization.k8s.io
    resources:
      - clusterroles
    resourceNames:
      - eventing-sources-kafka-controller
      - kafka-webhook
      - kafka-ch-controller
      - kafka-addressable-resolver
      - kafka-channelable-manipulator
      - kafka-ch-dispatcher
    verbs:
      - delete

  - apiGroups:
      - "*"
    resources:
      - serviceaccounts
    resourceNames:
      - kafka-controller-manager
      - kafka-webhook
      - kafka-ch-controller
      - kafka-ch-dispatcher
    verbs:
      - delete

  - apiGroups:
      - "*"
    resources:
      - secrets
    resourceNames:
      - kafka-source-webhook-certs
      - messaging-webhook-certs
    verbs:
      - delete

  - apiGroups:
      - "*"
    resources:
      - horizontalpodautoscalers
    resourceNames:
      - kafka-webhook
    verbs:
      - delete

  - apiGroups:
      - "*"
    resources:
      - poddisruptionbudgets
    resourceNames:
      - kafka-webhook
    verbs:
      - delete

  - apiGroups:
    - "*"
    resources:
      - leases
    resourceNames:
      - kafkachannel-dispatcher.knative.dev.eventing-kafka.pkg.channel.consolidated.reconciler.dispatcher.reconciler.00-of-01
    verbs:
      - delete

  # to be able to read the old configmap and migrate
  # and then delete them
  - apiGroups:
      - "*"
    resources:
      - configmaps
    resourceNames:
      - config-leader-election-kafkachannel
      - config-kafka
    verbs:
      - get
      - delete

  # to be able update config in the new configmap
  - apiGroups:
      - "*"
    resources:
      - configmaps
    resourceNames:
      - kafka-channel-config
    verbs:
      - patch
