---

# 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: apps/v1
kind: Deployment
metadata:
  name: kafka-source-controller
  namespace: knative-eventing
  labels:
    app: kafka-source-controller
    kafka.eventing.knative.dev/release: devel
spec:
  selector:
    matchLabels:
      app: kafka-source-controller
  template:
    metadata:
      name: kafka-source-controller
      labels:
        app: kafka-source-controller
        kafka.eventing.knative.dev/release: devel
    spec:
      securityContext:
        runAsNonRoot: true
      serviceAccountName: kafka-controller

      # To avoid node becoming SPOF, spread our replicas to different nodes and zones.
      topologySpreadConstraints:
        - maxSkew: 2
          topologyKey: topology.kubernetes.io/zone
          whenUnsatisfiable: ScheduleAnyway
          labelSelector:
            matchLabels:
              app: kafka-source-controller
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - podAffinityTerm:
                labelSelector:
                  matchLabels:
                    app: kafka-source-controller
                topologyKey: kubernetes.io/hostname
              weight: 100

      containers:
        - name: controller
          image: ko://knative.dev/eventing-kafka-broker/control-plane/cmd/kafka-source-controller
          imagePullPolicy: IfNotPresent
          env:
            - name: SOURCE_DATA_PLANE_CONFIG_MAP_NAMESPACE
              value: knative-eventing
            - name: SOURCE_DATA_PLANE_CONFIG_MAP_NAME
              value: kafka-source-sources
            - name: SOURCE_DATA_PLANE_CONFIG_FORMAT
              value: json
            - name: SOURCE_INGRESS_NAME
              value: kafka-source-ingress
            - name: SOURCE_GENERAL_CONFIG_MAP_NAME
              value: kafka-broker-config
            - name: SOURCE_SYSTEM_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: CONSUMER_DATA_PLANE_CONFIG_FORMAT
              value: json

            - name: SYSTEM_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace

            # How often (in seconds) the autoscaler tries to scale down the statefulset.
            - name: AUTOSCALER_REFRESH_PERIOD
              value: '100'

            # The number of virtual replicas each adapter pod can handle.
            - name: POD_CAPACITY
              value: '20'

            - name: SCHEDULER_CONFIG
              value: 'config-kafka-scheduler'

            - name: DESCHEDULER_CONFIG
              value: 'config-kafka-descheduler'

            - name: CONFIG_LEADERELECTION_NAME
              value: config-kafka-leader-election
            - name: CONFIG_LOGGING_NAME
              value: config-logging
            - name: CONFIG_OBSERVABILITY_NAME
              value: config-observability
            - name: METRICS_DOMAIN
              value: knative.dev/eventing
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          ports:
            - containerPort: 9090
              name: metrics
          terminationMessagePolicy: FallbackToLogsOnError
          terminationMessagePath: /dev/temination-log
          securityContext:
            allowPrivilegeEscalation: false
            privileged: false
            readOnlyRootFilesystem: true
      restartPolicy: Always
