Skip to content

Commit 5477e39

Browse files
authored
fix(docker): make rpk create topic idempotent (#581)
When the redpandacreatetopics container is run it invokes the `rpk create topics` command, which is not idempotent, see: redpanda-data/redpanda#6651 This patch replace the command with a script checking which topic has been created and piping the missing one to the create command.
1 parent c80a7b5 commit 5477e39

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

docker-compose.dev.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,19 @@ services:
305305
depends_on:
306306
redpanda:
307307
condition: service_healthy
308-
entrypoint: >
309-
rpk topic create events-raw events_enriched events_charged_in_advance events_dead_letter activity_logs api_logs --brokers redpanda:9092
308+
# https://github.com/redpanda-data/redpanda/issues/6651
309+
entrypoint: create-topics
310+
command:
311+
- events-raw
312+
- events_enriched
313+
- events_charged_in_advance
314+
- events_dead_letter
315+
- activity_logs
316+
- api_logs
317+
volumes:
318+
- ./scripts/create-topics.sh:/usr/local/bin/create-topics
319+
environment:
320+
- RPK_BROKERS=redpanda:9092
310321

311322
redpanda-console:
312323
image: docker.redpanda.com/redpandadata/console:v2.3.1

scripts/create-topics.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
# due to https://github.com/redpanda-data/redpanda/issues/6651
4+
# we need to create the topics only if they don't exist
5+
# this script gets the list of topics from the arguments
6+
# and creates them if they don't exist, creation is not invoked if the topics already exist
7+
rpk topic list | awk -v expected="$*" '
8+
BEGIN {
9+
n = split(expected, exp_array, " ")
10+
for (i = 1; i <= n; i++) expected_topics[exp_array[i]] = 1
11+
} NR > 1 {
12+
actual_topics[$1] = 1
13+
} END {
14+
for (topic in expected_topics) {
15+
if (!(topic in actual_topics)) printf "%s ", topic
16+
}
17+
}' | xargs -r rpk topic create

0 commit comments

Comments
 (0)