kind: ConfigMap
metadata:
  name: broker-config
  namespace: imxc 
apiVersion: v1
data:
  init.sh: |-
    #!/bin/bash
    set -e
    set -x
    cp /etc/kafka-configmap/log4j.properties /etc/kafka/
    KAFKA_BROKER_ID=${HOSTNAME##*-}
    SEDS=("s/#init#broker.id=#init#/broker.id=$KAFKA_BROKER_ID/")
    LABELS="kafka-broker-id=$KAFKA_BROKER_ID"
    ANNOTATIONS=""
    hash kubectl 2>/dev/null || {
      SEDS+=("s/#init#broker.rack=#init#/#init#broker.rack=# kubectl not found in path/")
    } && {
      ZONE=$(kubectl get node "$NODE_NAME" -o=go-template='{{index .metadata.labels "failure-domain.beta.kubernetes.io/zone"}}')
      if [ $? -ne 0 ]; then
        SEDS+=("s/#init#broker.rack=#init#/#init#broker.rack=# zone lookup failed, see -c init-config logs/")
      elif [ "x$ZONE" == "x<no value>" ]; then
        SEDS+=("s/#init#broker.rack=#init#/#init#broker.rack=# zone label not found for node $NODE_NAME/")
      else
        SEDS+=("s/#init#broker.rack=#init#/broker.rack=$ZONE/")
        LABELS="$LABELS kafka-broker-rack=$ZONE"
      fi
        # Node Port 설정 주석처리
        # OUTSIDE_HOST=$(kubectl get node "$NODE_NAME" -o jsonpath='{.status.addresses[?(@.type=="InternalIP")].address}')
        OUTSIDE_HOST=kafka-outside-${KAFKA_BROKER_ID}
        GLOBAL_HOST=kafka-global-${KAFKA_BROKER_ID}
      if [ $? -ne 0 ]; then
        echo "Outside (i.e. cluster-external access) host lookup command failed"
      else
        OUTSIDE_PORT=3240${KAFKA_BROKER_ID}
        GLOBAL_PORT=3250${KAFKA_BROKER_ID}
        # datagate 도입했으므로 Kube DNS 기반 통신
        SEDS+=("s|#init#advertised.listeners=OUTSIDE://#init#|advertised.listeners=OUTSIDE://${OUTSIDE_HOST}:${OUTSIDE_PORT},GLOBAL://${GLOBAL_HOST}:${GLOBAL_PORT}|")
        ANNOTATIONS="$ANNOTATIONS kafka-listener-outside-host=$OUTSIDE_HOST kafka-listener-outside-port=$OUTSIDE_PORT"
      fi
      if [ ! -z "$LABELS" ]; then
        kubectl -n $POD_NAMESPACE label pod $POD_NAME $LABELS || echo "Failed to label $POD_NAMESPACE.$POD_NAME - RBAC issue?"
      fi
      if [ ! -z "$ANNOTATIONS" ]; then
        kubectl -n $POD_NAMESPACE annotate pod $POD_NAME $ANNOTATIONS || echo "Failed to annotate $POD_NAMESPACE.$POD_NAME - RBAC issue?"
      fi
    }
    printf '%s\n' "${SEDS[@]}" | sed -f - /etc/kafka-configmap/server.properties > /etc/kafka/server.properties.tmp
    [ $? -eq 0 ] && mv /etc/kafka/server.properties.tmp /etc/kafka/server.properties
  server.properties: |-
    log.dirs=/var/lib/kafka/data/topics
    ############################# Zookeeper #############################
    zookeeper.connect=zookeeper:2181
    #zookeeper.connection.timeout.ms=6000
    ############################# Group Coordinator Settings #############################
    #group.initial.rebalance.delay.ms=0
    ############################# Thread #############################
    #background.threads=10
    #num.recovery.threads.per.data.dir=1
    ############################# Topic #############################
    auto.create.topics.enable=true
    delete.topic.enable=true
    default.replication.factor=2
    ############################# Msg Replication #############################
    min.insync.replicas=1
    num.io.threads=10
    num.network.threads=4
    num.replica.fetchers=4
    replica.fetch.min.bytes=1
    socket.receive.buffer.bytes=1048576
    socket.send.buffer.bytes=1048576
    replica.socket.receive.buffer.bytes=1048576
    socket.request.max.bytes=204857600
    ############################# Partition #############################
    #auto.leader.rebalance.enable=true
    num.partitions=12
    ############################# Log size #############################
    message.max.bytes=204857600
    max.message.bytes=204857600
    ############################# Log Flush Policy #############################
    #log.flush.interval.messages=10000
    #log.flush.interval.ms=1000
    ############################# Log Retention Policy #############################
    log.retention.minutes=1
    offsets.retention.minutes=1440
    #log.retention.bytes=1073741824
    #log.segment.bytes=1073741824
    log.retention.check.interval.ms=10000
    ############################# Internal Topic Settings  #############################
    offsets.topic.replication.factor=1
    #transaction.state.log.replication.factor=1
    #transaction.state.log.min.isr=1
    ############################# ETC #############################
    listeners=OUTSIDE://:9094,PLAINTEXT://:9092,GLOBAL://:9095
    listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL,OUTSIDE:PLAINTEXT,GLOBAL:PLAINTEXT
    #listeners=PLAINTEXT://:9092
    inter.broker.listener.name=PLAINTEXT
    #init#broker.id=#init#
    #init#broker.rack=#init#
  log4j.properties: |-
    # Unspecified loggers and loggers with additivity=true output to server.log and stdout
    # Note that INFO only applies to unspecified loggers, the log level of the child logger is used otherwise
    log4j.rootLogger=INFO, stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=[%d] %p %m (%c)%n
    log4j.appender.kafkaAppender=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.kafkaAppender.DatePattern='.'yyyy-MM-dd-HH
    log4j.appender.kafkaAppender.File=${kafka.logs.dir}/server.log
    log4j.appender.kafkaAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.kafkaAppender.layout.ConversionPattern=[%d] %p %m (%c)%n
    log4j.appender.stateChangeAppender=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.stateChangeAppender.DatePattern='.'yyyy-MM-dd-HH
    log4j.appender.stateChangeAppender.File=${kafka.logs.dir}/state-change.log
    log4j.appender.stateChangeAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.stateChangeAppender.layout.ConversionPattern=[%d] %p %m (%c)%n
    log4j.appender.requestAppender=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.requestAppender.DatePattern='.'yyyy-MM-dd-HH
    log4j.appender.requestAppender.File=${kafka.logs.dir}/kafka-request.log
    log4j.appender.requestAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.requestAppender.layout.ConversionPattern=[%d] %p %m (%c)%n
    log4j.appender.cleanerAppender=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.cleanerAppender.DatePattern='.'yyyy-MM-dd-HH
    log4j.appender.cleanerAppender.File=${kafka.logs.dir}/log-cleaner.log
    log4j.appender.cleanerAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.cleanerAppender.layout.ConversionPattern=[%d] %p %m (%c)%n
    log4j.appender.controllerAppender=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.controllerAppender.DatePattern='.'yyyy-MM-dd-HH
    log4j.appender.controllerAppender.File=${kafka.logs.dir}/controller.log
    log4j.appender.controllerAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.controllerAppender.layout.ConversionPattern=[%d] %p %m (%c)%n
    log4j.appender.authorizerAppender=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.authorizerAppender.DatePattern='.'yyyy-MM-dd-HH
    log4j.appender.authorizerAppender.File=${kafka.logs.dir}/kafka-authorizer.log
    log4j.appender.authorizerAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.authorizerAppender.layout.ConversionPattern=[%d] %p %m (%c)%n
    # Change the two lines below to adjust ZK client logging
    log4j.logger.org.I0Itec.zkclient.ZkClient=INFO
    log4j.logger.org.apache.zookeeper=INFO
    # Change the two lines below to adjust the general broker logging level (output to server.log and stdout)
    log4j.logger.kafka=INFO
    log4j.logger.org.apache.kafka=INFO
    # Change to DEBUG or TRACE to enable request logging
    log4j.logger.kafka.request.logger=WARN, requestAppender
    log4j.additivity.kafka.request.logger=false
    # Uncomment the lines below and change log4j.logger.kafka.network.RequestChannel$ to TRACE for additional output
    # related to the handling of requests
    #log4j.logger.kafka.network.Processor=TRACE, requestAppender
    #log4j.logger.kafka.server.KafkaApis=TRACE, requestAppender
    #log4j.additivity.kafka.server.KafkaApis=false
    log4j.logger.kafka.network.RequestChannel$=WARN, requestAppender
    log4j.additivity.kafka.network.RequestChannel$=false
    log4j.logger.kafka.controller=TRACE, controllerAppender
    log4j.additivity.kafka.controller=false
    log4j.logger.kafka.log.LogCleaner=INFO, cleanerAppender
    log4j.additivity.kafka.log.LogCleaner=false
    log4j.logger.state.change.logger=TRACE, stateChangeAppender
    log4j.additivity.state.change.logger=false
    # Change to DEBUG to enable audit log for the authorizer
    log4j.logger.kafka.authorizer.logger=WARN, authorizerAppender
    log4j.additivity.kafka.authorizer.logger=false
