# Source: cp-helm-charts/charts/cp-kafka/templates/statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: cp-helm-charts-cp-kafka
  labels:
    app: cp-kafka
    chart: cp-kafka-0.1.0
    release: cp-helm-charts
    heritage: Helm
    gitops.jenkins-x.io/pipeline: 'namespaces'
  annotations:
    meta.helm.sh/release-name: 'cp-helm-charts'
  namespace: kafka
spec:
  selector:
    matchLabels:
      app: cp-kafka
      release: cp-helm-charts
  serviceName: cp-helm-charts-cp-kafka-headless
  podManagementPolicy: OrderedReady
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: cp-kafka
        release: cp-helm-charts
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "5556"
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 1
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: "app"
                      operator: In
                      values:
                        - cp-kafka
                    - key: "release"
                      operator: In
                      values:
                        - cp-helm-charts
                topologyKey: "kubernetes.io/hostname"
      containers:
        - name: prometheus-jmx-exporter
          image: "solsson/kafka-prometheus-jmx-exporter@sha256:6f82e2b0464f50da8104acd7363fb9b995001ddff77d248379f8788e78946143"
          imagePullPolicy: "IfNotPresent"
          command:
            - java
            - -XX:+UnlockExperimentalVMOptions
            - -XX:+UseCGroupMemoryLimitForHeap
            - -XX:MaxRAMFraction=1
            - -XshowSettings:vm
            - -jar
            - jmx_prometheus_httpserver.jar
            - "5556"
            - /etc/jmx-kafka/jmx-kafka-prometheus.yml
          ports:
            - containerPort: 5556
          resources: {}
          volumeMounts:
            - name: jmx-config
              mountPath: /etc/jmx-kafka
        - name: cp-kafka-broker
          image: "confluentinc/cp-enterprise-kafka:6.0.1"
          imagePullPolicy: "IfNotPresent"
          securityContext:
            runAsUser: 0
          ports:
            - containerPort: 9092
              name: kafka
            - containerPort: 5555
              name: jmx
          resources:
            limits:
              cpu: 100m
              memory: 400Mi
            requests:
              cpu: 100m
              memory: 300Mi
          env:
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: KAFKA_HEAP_OPTS
              value: -Xms256M -Xmx256M
            - name: KAFKA_ZOOKEEPER_CONNECT
              value: "cp-helm-charts-cp-zookeeper-headless:2181"
            - name: KAFKA_LOG_DIRS
              value: "/opt/kafka/data-0/logs"
            - name: KAFKA_METRIC_REPORTERS
              value: "io.confluent.metrics.reporter.ConfluentMetricsReporter"
            - name: CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS
              value: "PLAINTEXT://cp-helm-charts-cp-kafka-headless:9092"
            - name: "KAFKA_AUTO_CREATE_TOPICS_ENABLE"
              value: "true"
            - name: "KAFKA_DEFAULT_REPLICATION_FACTOR"
              value: "1"
            - name: "KAFKA_LISTENER_SECURITY_PROTOCOL_MAP"
              value: "PLAINTEXT:PLAINTEXT,EXTERNAL:PLAINTEXT"
            - name: "KAFKA_MIN_INSYNC_REPLICAS"
              value: "1"
            - name: "KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR"
              value: "1"
            - name: KAFKA_JMX_PORT
              value: "5555"
          # This is required because the Downward API does not yet support identification of
          # pod numbering in statefulsets. Thus, we are required to specify a command which
          # allows us to extract the pod ID for usage as the Kafka Broker ID.
          # See: https://github.com/kubernetes/kubernetes/issues/31218
          command:
            - sh
            - -exc
            - |
              export KAFKA_BROKER_ID=${HOSTNAME##*-} && \
              export KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://${POD_NAME}.cp-helm-charts-cp-kafka-headless.${POD_NAMESPACE}:9092,EXTERNAL://${HOST_IP}:$((31090 + ${KAFKA_BROKER_ID})) && \
              exec /etc/confluent/docker/run
          volumeMounts:
            - name: datadir-0
              mountPath: /opt/kafka/data-0
      volumes:
        - name: jmx-config
          configMap:
            name: cp-helm-charts-cp-kafka-jmx-configmap
  volumeClaimTemplates:
    - metadata:
        name: datadir-0
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: "5Gi"
