Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for kafka 3 with zookeeper support (adding images for 3.0.1, 3.1.1 and 3.2.0) #709

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ac64f78
Jack12816 initial 3.0 approach
frank-vissing-se May 11, 2022
07b56eb
Merge remote-tracking branch 'origin/master' into going_to_3
frank-vissing-se May 11, 2022
a576cbf
make test independent on how sort works
frank-vissing-se May 12, 2022
f267754
add support for kafka 3.1.0
frank-vissing-se May 12, 2022
5d7a593
add newer and better versions
frank-vissing-se May 18, 2022
8be81b0
make sure user removes obsolete config
frank-vissing-se May 18, 2022
ecef794
support 3.x in compose
frank-vissing-se May 18, 2022
c527cb5
fix typo
frank-vissing-se May 18, 2022
289836a
fix missing $ (typo)
frank-vissing-se May 18, 2022
426638f
re add BROKER_LIST, propper default KAFKA_VERSION
frank-vissing-se May 19, 2022
823e024
make shell test happy
frank-vissing-se May 19, 2022
c3c455f
propper default
frank-vissing-se May 19, 2022
57af850
do not require BROKER_LIST
frank-vissing-se May 19, 2022
750ebb1
refactor removed arguments for kafka
frank-vissing-se May 19, 2022
01fa192
start kafka with propper options
frank-vissing-se May 20, 2022
fe19311
supply entire list of bootstrap servers
frank-vissing-se May 20, 2022
8a8daf4
various test cleanup to support removal of KAFKA_ADVERTISED_HOST_NAME…
frank-vissing-se May 20, 2022
8914a83
fix shell check
frank-vissing-se May 20, 2022
724866b
make MAJOR_VERSION accessible for test scripts
frank-vissing-se May 20, 2022
a97e332
propper naming of versions.sh
frank-vissing-se May 20, 2022
8d80440
fix selection of kafka args
frank-vissing-se May 20, 2022
13933fd
revievers comment about wrong hostname, changing to propper name kafka
frank-vissing-se May 20, 2022
14f089d
reviewers comment about not needing seperate test setup for previous …
frank-vissing-se May 23, 2022
b3e54b0
trigger build
frank-vissing-se May 23, 2022
704e9ab
no need to specify compose file
frank-vissing-se May 23, 2022
0698081
totally revert this file
frank-vissing-se May 23, 2022
58dfb8f
trigger build
frank-vissing-se Jun 8, 2022
8922faa
typo in readme, we are in the test folder when executing this, this i…
frank-vissing-se Jun 30, 2022
2abb7db
examine KAFKA_LISTNERS to determine the port we are runing on, also r…
frank-vissing-se Jun 30, 2022
001df7d
Double quote to prevent globbing
frank-vissing-se Jun 30, 2022
722fc5c
make create topics work both for KAFKA_LISTNERS and BROKER_LIST
frank-vissing-se Jun 30, 2022
19d8c52
changed image
sharninder Apr 17, 2023
3b705db
Merge pull request #3 from sharninder/upgrade-image
lunarfs Apr 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
# This version will be also tagged as 'latest'
env:
global:
- LATEST="2.13-2.8.1"
- LATEST="2.13-3.2.0"

# Build recommended versions based on: http://kafka.apache.org/downloads
matrix:
Expand All @@ -29,6 +29,12 @@ matrix:
env: KAFKA_VERSION=2.7.2
- scala: 2.13
env: KAFKA_VERSION=2.8.1
- scala: 2.13
env: KAFKA_VERSION=3.0.1
- scala: 2.13
env: KAFKA_VERSION=3.1.1
- scala: 2.13
env: KAFKA_VERSION=3.2.0

# Upgrade Docker Engine so we can use buildx
before_install:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ Changelog
=========

Kafka features are not tied to a specific kafka-docker version (ideally all changes will be merged into all branches). Therefore, this changelog will track changes to the image by date.
12-May-2022
-----------

- Add support for Kafka `3.1.0`
09-Apr-2022
----------

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM openjdk:11-jre-slim

ARG kafka_version=2.8.1
ARG kafka_version=3.1.0
ARG scala_version=2.13
ARG vcs_ref=unspecified
ARG build_date=unspecified
Expand Down
3 changes: 2 additions & 1 deletion broker-list.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

CONTAINERS=$(docker ps | grep 9092 | awk '{print $1}')
BROKERS=$(for CONTAINER in ${CONTAINERS}; do docker port "$CONTAINER" 9092 | sed -e "s/0.0.0.0:/$HOST_IP:/g"; done)
# We are just interested in ipv4 addresses, so ignore ipv6 addresses
BROKERS=$(for CONTAINER in ${CONTAINERS}; do docker port "$CONTAINER" 9092 | grep '0.0.0.0' | sed -e "s/0.0.0.0:/$HOST_IP:/g"; done)
echo "${BROKERS//$'\n'/,}"
10 changes: 9 additions & 1 deletion create-topics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ if [[ "$MAJOR_VERSION" == "0" && "$MINOR_VERSION" -gt "9" ]] || [[ "$MAJOR_VERSI
KAFKA_0_10_OPTS="--if-not-exists"
fi

# since 3.0.0 there is no --zookeeper option anymore, so we have to use the
# --bootstrap-server option with a random broker
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
CONNECT_OPTS="--bootstrap-server $(echo "${BROKER_LIST}" | cut -d ',' -f1)"
lunarfs marked this conversation as resolved.
Show resolved Hide resolved
else
CONNECT_OPTS="--zookeeper ${KAFKA_ZOOKEEPER_CONNECT}"
fi

# Expected format:
# name:partitions:replicas:cleanup.policy
IFS="${KAFKA_CREATE_TOPICS_SEPARATOR-,}"; for topicToCreate in $KAFKA_CREATE_TOPICS; do
Expand All @@ -45,7 +53,7 @@ IFS="${KAFKA_CREATE_TOPICS_SEPARATOR-,}"; for topicToCreate in $KAFKA_CREATE_TOP

COMMAND="JMX_PORT='' ${KAFKA_HOME}/bin/kafka-topics.sh \\
--create \\
--zookeeper ${KAFKA_ZOOKEEPER_CONNECT} \\
${CONNECT_OPTS} \\
--topic ${topicConfig[0]} \\
--partitions ${topicConfig[1]} \\
--replication-factor ${topicConfig[2]} \\
Expand Down
69 changes: 53 additions & 16 deletions start-kafka.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash -e

# shellcheck disable=SC1091
source /usr/bin/versions.sh

# Allow specific kafka versions to perform any unique bootstrap operations
OVERRIDE_FILE="/opt/overrides/${KAFKA_VERSION}.sh"
if [[ -x "$OVERRIDE_FILE" ]]; then
Expand All @@ -10,9 +13,32 @@ fi
# Store original IFS config, so we can restore it at various stages
ORIG_IFS=$IFS

if [[ -z "$KAFKA_ZOOKEEPER_CONNECT" ]]; then
echo "ERROR: missing mandatory config: KAFKA_ZOOKEEPER_CONNECT"
exit 1
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
if [[ -z "BROKER_LIST" ]]; then
lunarfs marked this conversation as resolved.
Show resolved Hide resolved
echo "ERROR: missing mandatory config: BROKER_LIST"
exit 1
fi
if [[ -v KAFKA_ADVERTISED_HOST_NAME ]]; then
echo "ERROR: KAFKA_ADVERTISED_HOST_NAME is removed as of kafka 3, remove KAFKA_ADVERTISED_HOST_NAME=$KAFKA_ADVERTISED_HOST_NAME from your config"
exit 1
fi
if [[ -v KAFKA_ADVERTISED_PORT ]]; then
echo "ERROR: KAFKA_ADVERTISED_PORT is removed as of kafka 3, remove KAFKA_ADVERTISED_PORT=$KAFKA_ADVERTISED_PORT from your config"
exit 1
fi
if [[ -v KAFKA_PORT ]]; then
echo "ERROR: KAFKA_PORT is removed as of kafka 3, remove KAFKA_PORT=$KAFKA_PORT from your config"
exit 1
fi
if [[ -v KAFKA_HOST_NAME ]]; then
echo "ERROR: KAFKA_HOST_NAME is removed as of kafka 3, remove KAFKA_HOST_NAME=$KAFKA_HOST_NAME from your config"
exit 1
fi
else
if [[ -z "$KAFKA_ZOOKEEPER_CONNECT" ]]; then
echo "ERROR: missing mandatory config: KAFKA_ZOOKEEPER_CONNECT"
exit 1
fi
fi

if [[ -z "$KAFKA_PORT" ]]; then
Expand All @@ -26,7 +52,7 @@ if [[ -z "$KAFKA_ADVERTISED_PORT" && \
-z "$KAFKA_LISTENERS" && \
-z "$KAFKA_ADVERTISED_LISTENERS" && \
-S /var/run/docker.sock ]]; then
KAFKA_ADVERTISED_PORT=$(docker port "$(hostname)" $KAFKA_PORT | sed -r 's/.*:(.*)/\1/g' | head -n1)
KAFKA_ADVERTISED_PORT=$(docker port "$(hostname)" $KAFKA_PORT | sed -r 's/.*:(.*)/\1/g' | head -n1)
export KAFKA_ADVERTISED_PORT
fi

Expand Down Expand Up @@ -81,19 +107,30 @@ if [[ -n "$RACK_COMMAND" && -z "$KAFKA_BROKER_RACK" ]]; then
fi

# Try and configure minimal settings or exit with error if there isn't enough information
if [[ -z "$KAFKA_ADVERTISED_HOST_NAME$KAFKA_LISTENERS" ]]; then
if [[ -n "$KAFKA_ADVERTISED_LISTENERS" ]]; then
echo "ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS"
exit 1
elif [[ -z "$HOSTNAME_VALUE" ]]; then
echo "ERROR: No listener or advertised hostname configuration provided in environment."
echo " Please define KAFKA_LISTENERS / (deprecated) KAFKA_ADVERTISED_HOST_NAME"
exit 1
fi
if [[ "$MAJOR_VERSION" -lt "3" ]]; then
if [[ -z "$KAFKA_ADVERTISED_HOST_NAME$KAFKA_LISTENERS" ]]; then
if [[ -n "$KAFKA_ADVERTISED_LISTENERS" ]]; then
echo "ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS"
exit 1
elif [[ -z "$HOSTNAME_VALUE" ]]; then
echo "ERROR: No listener or advertised hostname configuration provided in environment."
echo " Please define KAFKA_LISTENERS / (deprecated) KAFKA_ADVERTISED_HOST_NAME"
exit 1
fi

# Maintain existing behaviour
# If HOSTNAME_COMMAND is provided, set that to the advertised.host.name value if listeners are not defined.
export KAFKA_ADVERTISED_HOST_NAME="$HOSTNAME_VALUE"
# Maintain existing behaviour
# If HOSTNAME_COMMAND is provided, set that to the advertised.host.name value if listeners are not defined.
export KAFKA_ADVERTISED_HOST_NAME="$HOSTNAME_VALUE"
fi
fi
# `advertised.port` and `advertised.host.name` are removed with Kafka 3.0.0
# See: https://github.com/apache/kafka/pull/10872
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
if [ -z "$KAFKA_ADVERTISED_LISTENERS" ]; then
if [ -n "${KAFKA_ADVERTISED_HOST_NAME}" ]; then
export KAFKA_ADVERTISED_LISTENERS="PLAINTEXT://${KAFKA_ADVERTISED_HOST_NAME}:${KAFKA_PORT}"
fi
fi
fi

#Issue newline to config file in case there is not one already
Expand Down
14 changes: 13 additions & 1 deletion test/0.0/test.create-topics-custom-separator.kafka.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,25 @@ testCreateTopicsCustomSeparator() {

create-topics.sh

# shellcheck disable=SC1091
source "/usr/bin/versions.sh"

# since 3.0.0 there is no --zookeeper option anymore, so we have to use the
# --bootstrap-server option with a random broker
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
CONNECT_OPTS="--bootstrap-server $(echo "${BROKER_LIST}" | cut -d ',' -f1)"
else
CONNECT_OPTS="--zookeeper ${KAFKA_ZOOKEEPER_CONNECT}"
fi

# Loop through each array, validate that topic exists
for i in "${!TOPICS[@]}"; do
TOPIC=${TOPICS[i]}

echo "Validating topic '$TOPIC'"

EXISTS=$(/opt/kafka/bin/kafka-topics.sh --zookeeper "$KAFKA_ZOOKEEPER_CONNECT" --list --topic "$TOPIC")
# shellcheck disable=SC2086
EXISTS=$(/opt/kafka/bin/kafka-topics.sh ${CONNECT_OPTS} --list --topic "$TOPIC")
if [[ "$EXISTS" != "$TOPIC" ]]; then
echo "$TOPIC topic not created"
return 1
Expand Down
10 changes: 9 additions & 1 deletion test/0.0/test.start-kafka-advertised-host.kafka.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ testAdvertisedHost() {
assertAbsent 'advertised.listeners'
assertAbsent 'listeners'
}
source "/usr/bin/versions.sh"

testAdvertisedHost
# since 3.0.0 there is no --zookeeper option anymore, so we have to use the
# --bootstrap-server option with a random broker
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
echo "with kafka from version 3.0.0 'advertised.port' and 'advertised.host.name' are removed with Kafka 3.0.0"
echo "See: https://github.com/apache/kafka/pull/10872"
else
testAdvertisedHost
fi
10 changes: 9 additions & 1 deletion test/0.0/test.start-kafka-bug-312-kafka-env.kafka.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@ testKafkaEnv() {

echo " > Set KAFKA_OPTS=$KAFKA_OPTS"
}
source "/usr/bin/versions.sh"

testKafkaEnv
# since 3.0.0 there is no --zookeeper option anymore, so we have to use the
# --bootstrap-server option with a random broker
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
echo "this thes is obsolete with kafka from version 3.0.0 'advertised.host.name' are removed with Kafka 3.0.0"
lunarfs marked this conversation as resolved.
Show resolved Hide resolved
echo "See: https://github.com/apache/kafka/pull/10872"
else
testKafkaEnv
fi
10 changes: 9 additions & 1 deletion test/0.0/test.start-kafka-bug-313-kafka-opts.kafka.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@ testKafkaOpts() {

echo " > Set KAFKA_OPTS=$KAFKA_OPTS"
}
source "/usr/bin/versions.sh"

testKafkaOpts
# since 3.0.0 there is no --zookeeper option anymore, so we have to use the
# --bootstrap-server option with a random broker
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
echo "this thes is obsolete with kafka from version 3.0.0 'advertised.host.name' are removed with Kafka 3.0.0"
lunarfs marked this conversation as resolved.
Show resolved Hide resolved
echo "See: https://github.com/apache/kafka/pull/10872"
else
testKafkaOpts
fi
10 changes: 9 additions & 1 deletion test/0.0/test.start-kafka-host-name.kafka.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ testHostnameCommand() {
assertAbsent 'advertised.listeners'
assertAbsent 'listeners'
}
source "/usr/bin/versions.sh"

testHostnameCommand
# since 3.0.0 there is no --zookeeper option anymore, so we have to use the
# --bootstrap-server option with a random broker
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
echo "this thes is obsolete with kafka from version 3.0.0 'advertised.host.name' are removed with Kafka 3.0.0"
echo "See: https://github.com/apache/kafka/pull/10872"
else
testHostnameCommand
fi
12 changes: 9 additions & 3 deletions test/0.0/test.start-kafka-log4j-config.kafka.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ source test.functions

testLog4jConfig() {
# Given Log4j overrides are provided
export KAFKA_ADVERTISED_HOST_NAME="testhost"
source "/usr/bin/versions.sh"

# since 3.0.0 there is no --zookeeper option anymore, so we have to use the
# --bootstrap-server option with a random broker
if [[ "$MAJOR_VERSION" -lt "3" ]]; then
export KAFKA_ADVERTISED_HOST_NAME="testhost"
fi

export LOG4J_LOGGER_KAFKA=DEBUG

# When the script is invoked
Expand All @@ -13,5 +20,4 @@ testLog4jConfig() {
# Then the configuration file is correct
assertExpectedLog4jConfig "log4j.logger.kafka=DEBUG"
}

testLog4jConfig
testLog4jConfig
10 changes: 9 additions & 1 deletion test/0.0/test.start-kafka-restart.kafka.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ testRestart() {
assertExpectedConfig 'advertised.host.name=testhost'
assertAbsent 'listeners'
}
source "/usr/bin/versions.sh"

testRestart
# since 3.0.0 there is no --zookeeper option anymore, so we have to use the
# --bootstrap-server option with a random broker
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
echo "this thes is obsolete with kafka from version 3.0.0 'advertised.host.name' are removed with Kafka 3.0.0"
echo "See: https://github.com/apache/kafka/pull/10872"
else
testRestart
fi
40 changes: 31 additions & 9 deletions test/0.10/test.create-topics.kafka.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,51 @@ testCreateTopics() {
NOW=$(date +%s)

# TOPICS array contains the topic name to create / validate
# CLEANUP array contains the expected cleanup policy configuration for the topic
# CONFIG array contains the expected cleanup policy configuration for the topic
TOPICS[0]="default-$NOW"
CLEANUP[0]=""
CONFIG[0]=""

TOPICS[1]="compact-$NOW"
CLEANUP[1]="compression.type=snappy,cleanup.policy=compact"
CONFIG[1]=$(echo -e "compression.type=snappy\ncleanup.policy=compact" | sort \
| tr '\n' ',' \
| sed 's/,$//')

KAFKA_CREATE_TOPICS="${TOPICS[0]}:1:1,${TOPICS[1]}:2:1:compact --config=compression.type=snappy" create-topics.sh

# Loop through each array, validate that topic exists, and correct cleanup policy is set
# shellcheck disable=SC1091
source "/usr/bin/versions.sh"

# since 3.0.0 there is no --zookeeper option anymore, so we have to use the
# --bootstrap-server option with a random broker
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
CONNECT_OPTS="--bootstrap-server $(echo "${BROKER_LIST}" | cut -d ',' -f1)"
else
CONNECT_OPTS="--zookeeper ${KAFKA_ZOOKEEPER_CONNECT}"
fi

# Loop through each array, validate that topic exists, and correct configuration is set
for i in "${!TOPICS[@]}"; do
TOPIC=${TOPICS[i]}

echo "Validating topic '$TOPIC'"

EXISTS=$(/opt/kafka/bin/kafka-topics.sh --zookeeper "$KAFKA_ZOOKEEPER_CONNECT" --list --topic "$TOPIC")
POLICY=$(/opt/kafka/bin/kafka-topics.sh --zookeeper "$KAFKA_ZOOKEEPER_CONNECT" --describe --topic "$TOPIC" | awk -F'Configs:' '{print $2}' | xargs)
# shellcheck disable=SC2086
EXISTS=$(/opt/kafka/bin/kafka-topics.sh ${CONNECT_OPTS} --list --topic "$TOPIC")
# shellcheck disable=SC2086
ACTUAL_CONFIG=$(/opt/kafka/bin/kafka-configs.sh ${CONNECT_OPTS} --entity-type topics --entity-name "$TOPIC" --describe \
| cut -d'{' -f1 \
| grep -oE '(compression.type|cleanup.policy)=([^ ,]+)' \
| sort \
| tr '\n' ',' \
| sed 's/,$//')

RESULT="$EXISTS:$POLICY"
EXPECTED="$TOPIC:${CLEANUP[i]}"
RESULT="$EXISTS:$ACTUAL_CONFIG"
EXPECTED="$TOPIC:${CONFIG[i]}"

if [[ "$RESULT" != "$EXPECTED" ]]; then
echo "$TOPIC topic not configured correctly: '$RESULT'"
echo "$TOPIC topic not configured correctly:"
echo " Actual: '$RESULT'"
echo " Expected: '$EXPECTED'"
return 1
fi
done
Expand Down
2 changes: 1 addition & 1 deletion test/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Run selected tests
### Kafka

```
docker-compose run --rm kafkatest <testname pattern>
docker-compose run -e BROKER_LIST=$(../internal-broker-list.sh) --rm kafkatest <testname pattern>
```

### Kafkacat
Expand Down
3 changes: 1 addition & 2 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ services:
kafkacattest:
image: confluentinc/cp-kafkacat:5.0.0
environment:
- BROKER_LIST
- KAFKA_VERSION=${KAFKA_VERSION-2.8.1}
- KAFKA_VERSION=${KAFKA_VERSION-3.1.0}
volumes:
- .:/tests
working_dir: /tests
Expand Down
4 changes: 2 additions & 2 deletions test/runAllTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ echo "BROKER_LIST=$BROKER_LIST"

runAll() {
# Tests that require kafka
docker-compose run --rm kafkatest
docker-compose run -e BROKER_LIST="${BROKER_LIST}" --rm kafkatest

RESULT=$?
if [[ $RESULT -eq 0 ]]; then
# Tests that require kafkacat
docker-compose run --rm kafkacattest
docker-compose run -e BROKER_LIST="${BROKER_LIST}" --rm kafkacattest
RESULT=$?
fi

Expand Down