| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | set -eu |
| 4 | set -o pipefail |
| 5 | |
| 6 | KAFKA_VERSION="${KAFKA_VERSION:-3.9.1}" |
| 7 | KAFKA_HOME="/opt/kafka-${KAFKA_VERSION}" |
| 8 | |
| 9 | if [ ! -d "${KAFKA_HOME}" ]; then |
| 10 | echo 'Error: KAFKA_VERSION '$KAFKA_VERSION' not available in this image at '$KAFKA_HOME |
| 11 | exit 1 |
| 12 | fi |
| 13 | |
| 14 | cd "${KAFKA_HOME}" || exit 1 |
| 15 | |
| 16 | # discard all empty/commented lines from default config and copy to /tmp |
| 17 | sed -e '/^#/d' -e '/^$/d' config/server.properties >/tmp/server.properties |
| 18 | |
| 19 | echo "########################################################################" >>/tmp/server.properties |
| 20 | |
| 21 | # emulate kafka_configure_from_environment_variables from bitnami/bitnami-docker-kafka |
| 22 | for var in "${!KAFKA_CFG_@}"; do |
| 23 | key="$(echo "$var" | sed -e 's/^KAFKA_CFG_//g' -e 's/_/\./g' -e 's/.*/\L&/')" |
| 24 | sed -e '/^'$key'/d' -i"" /tmp/server.properties |
| 25 | value="${!var}" |
| 26 | echo "$key=$value" >>/tmp/server.properties |
| 27 | done |
| 28 | |
| 29 | # use KRaft if KAFKA_VERSION is 4.0.0 or newer |
| 30 | if printf "%s\n4.0.0" "$KAFKA_VERSION" | sort -V | head -n1 | grep -q "^4.0.0$"; then |
| 31 | # remove zookeeper-only options from server.properties and setup storage |
| 32 | sed -e '/zookeeper.connect/d' \ |
| 33 | -i /tmp/server.properties |
| 34 | bin/kafka-storage.sh format -t "$KAFKA_CFG_CLUSTER_ID" -c /tmp/server.properties --ignore-formatted |
| 35 | else |
| 36 | # remove KRaft-only options from server.properties |
| 37 | sed -e '/process.roles/d' \ |
| 38 | -e '/controller.quorum.voters/d' \ |
| 39 | -e '/controller.listener.names/d' \ |
| 40 | -e '/cluster.id/d' \ |
| 41 | -i /tmp/server.properties |
| 42 | fi |
| 43 | |
| 44 | sort /tmp/server.properties |
| 45 | |
| 46 | exec bin/kafka-server-start.sh /tmp/server.properties |