apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kafka
  namespace: imxc 
spec:
  selector:
    matchLabels:
      app: kafka
  serviceName: "kafka-headless"
  replicas: 2
  updateStrategy:
    type: RollingUpdate
  podManagementPolicy: Parallel
  template:
    metadata:
      labels:
        app: kafka
      annotations:
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values: 
                    - kafka
              topologyKey: "kubernetes.io/hostname"
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
             - weight: 1
               podAffinityTerm:
                 labelSelector:
                    matchExpressions:
                      - key: "app"
                        operator: In
                        values: 
                        - zookeeper
                 topologyKey: "kubernetes.io/hostname"
      terminationGracePeriodSeconds: 30
      initContainers:
      - name: init-config
        image: {{ .Values.global.IMXC_IN_REGISTRY }}/kafka-initutils:{{ .Values.global.KAFKA_INITUTILS_VERSION }}
        env:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        command: ['/bin/bash', '/etc/kafka-configmap/init.sh']
        volumeMounts:
        - name: configmap
          mountPath: /etc/kafka-configmap
        - name: config
          mountPath: /etc/kafka
        - name: extensions
          mountPath: /opt/kafka/libs/extensions
      containers:
      - name: broker
        image: {{ .Values.global.IMXC_IN_REGISTRY }}/kafka:{{ .Values.global.KAFKA_VERSION }}
        resources:
          requests:
            cpu: 250m
            memory: 5000Mi
          limits:
            # This limit was intentionally set low as a reminder that
            # the entire Yolean/kubernetes-kafka is meant to be tweaked
            # before you run production workloads
            cpu: 500m
            memory: 10000Mi
        env:
        - name: CLASSPATH
          value: /opt/kafka/libs/extensions/*
        - name: KAFKA_LOG4J_OPTS
          value: -Dlog4j.configuration=file:/etc/kafka/log4j.properties
        - name: JMX_PORT
          value: "5555"
        - name: KAFKA_OPTS
          value: -javaagent:/opt/kafka/jmx_prometheus_javaagent-0.15.0.jar=9010:/opt/kafka/config.yaml
        ports:
        - name: inside
          containerPort: 9092
        - name: outside
          containerPort: 9094
        - name: global
          containerPort: 9095
        - name: jmx
          containerPort: 9010
        command:
        - ./bin/kafka-server-start.sh
        - /etc/kafka/server.properties
        lifecycle:
          preStop:
            exec:
             command: ["sh", "-ce", "rm -rf /var/lib/kafka/data/*;kill -s TERM 1; while $(kill -0 1 2>/dev/null); do sleep 1; done"]
#        readinessProbe:
#          tcpSocket:
#            port: 9092
#          timeoutSeconds: 1
        volumeMounts:
        - name: config
          mountPath: /etc/kafka
        - name: data
          mountPath: /var/lib/kafka/data
        - name: extensions
          mountPath: /opt/kafka/libs/extensions
      volumes:
      - name: configmap
        configMap:
          name: broker-config
      - name: config
        emptyDir: {}
      - name: extensions
        emptyDir: {}
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: kafka-broker
      resources:
        requests:
          storage: 30Gi
