Scripting Topic Management

Scripting Topic Management for BookNest

Hand-made topics drift between clusters. Keep the desired topics under version control and let a script converge the cluster to them.

listings/l0647_ensure.sh: create, grow and configure topics idempotentlyShell
# Converge the cluster to BookNest's topic list; safe to run again and again
K="docker exec l3-kafka /opt/kafka/bin"                       # no -i: keep the loop's stdin
B="--bootstrap-server l3-kafka:9092"
while read -r name parts configs; do
  have=$($K/kafka-topics.sh $B --describe --topic "$name" 2>/dev/null |
         grep -o 'PartitionCount: [0-9]*' | tr -dc 0-9)
  if [ -z "$have" ]; then
    $K/kafka-topics.sh $B --create --topic "$name" --partitions "$parts" >/dev/null 2>&1
  elif [ "$have" -lt "$parts" ]; then                        # never shrink: Kafka cannot
    $K/kafka-topics.sh $B --alter --topic "$name" --partitions "$parts"
  fi
  $K/kafka-configs.sh $B --alter --entity-type topics --entity-name "$name" \
    --add-config "$configs" >/dev/null && echo "$name: ${have:-created} -> $parts; $configs"
done <<'TOPICS'
booknest.order-events  3  retention.ms=604800000
booknest.customers     6  cleanup.policy=compact,min.compaction.lag.ms=60000
booknest.catalog       3  cleanup.policy=compact
TOPICS
Output
booknest.order-events: 3 -> 3; retention.ms=604800000
booknest.customers: 6 -> 6; cleanup.policy=compact,min.compaction.lag.ms=60000
booknest.catalog: created -> 3; cleanup.policy=compact

A second run changed nothing. The same idea comes packaged as Strimzi 326,081 's KafkaTopic resources or the Terraform 4,548 Kafka 129 provider (https://github.com/Mongey/terraform-provider-kafka 617 ) (MIT, v0.13.1). Keep deletion a reviewed manual step: a script that deletes what its list lacks will one day delete a forgotten topic.