Skip to content

add EXTRA_ADVERTISED_LISTENERS#3649

Open
MistaTwista wants to merge 1 commit intotestcontainers:mainfrom
MistaTwista:feature/customize_advertised_listeners
Open

add EXTRA_ADVERTISED_LISTENERS#3649
MistaTwista wants to merge 1 commit intotestcontainers:mainfrom
MistaTwista:feature/customize_advertised_listeners

Conversation

@MistaTwista
Copy link
Copy Markdown

@MistaTwista MistaTwista commented Apr 13, 2026

What does this PR do?

Adds ability to use EXTRA_KAFKA_ADVERTISED_LISTENERS variable declared for kafka module to patch KAFKA_ADVERTISED_LISTENERS in a modules/kafka starterScriptContent.

Why is it important?

modules/kafka got const starterScriptContent with exported KAFKA_ADVERTISED_LISTENERS that is currently hidden for modifications from outside of the module. This add few problems with testing.

Because, as you know kafka wants to know what hosts will be used to connect to it before it was started. For my usecase I want to connect from kafka ui to debug messages in topics in a local test environment. Current script have no patch points to KAFKA_ADVERTISED_LISTENERS.

Related issues

How to test this PR

Add echo $KAFKA_ADVERTISED_LISTENERS and see how it works after bash use it. I can see how to test it automatically, i just want to know what do you think about that way of fixing it.

@MistaTwista MistaTwista requested a review from a team as a code owner April 13, 2026 16:16
@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 13, 2026

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 6e2bc9f
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-go/deploys/69dd16d315f4560008e3a2e3
😎 Deploy Preview https://deploy-preview-3649--testcontainers-go.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 13, 2026

Walkthrough

The Kafka container initialization script in modules/kafka/kafka.go has been modified to properly quote the KAFKA_ADVERTISED_LISTENERS configuration value and add conditional support for appending extra advertised listeners with proper comma formatting.

Changes

Cohort / File(s) Summary
Kafka Configuration
modules/kafka/kafka.go
Updated KAFKA_ADVERTISED_LISTENERS to use quoted formatting and conditionally append EXTRA_ADVERTISED_LISTENERS when the variable is non-empty.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐰 A listener's tale in quoted grace,
KAFKA_ADVERTISED finds its place,
With commas dancing when needed true,
Extra listeners whisper through,
Configuration now robust and clean! 🎵

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'add EXTRA_ADVERTISED_LISTENERS' directly corresponds to the main change: adding support for an EXTRA_KAFKA_ADVERTISED_LISTENERS variable in the Kafka module.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description check ✅ Passed The pull request description clearly explains what it does (adds ability to use EXTRA_KAFKA_ADVERTISED_LISTENERS), why it matters (allows testing with Kafka UI), and references a related issue (#2630).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
modules/kafka/kafka.go (1)

24-24: Add explicit tests for extra listener append behavior

The current advertised-listeners test (modules/kafka/kafka_test.go:77-91) validates BROKER://<hostname>:9092, but not the new ${...:+,...} append logic. Please add cases for (1) unset extra var, (2) set extra var, to lock formatting and comma behavior.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@modules/kafka/kafka.go` at line 24, Update the advertised-listeners unit test
in modules/kafka/kafka_test.go to explicitly cover the new append behavior for
the environment expansion in the KAFKA_ADVERTISED_LISTENERS export line: add two
subcases—one where EXTRA_ADVERTISED_LISTENERS is unset (expect exactly
"BROKER://<hostname>:9092" with no trailing comma/extra), and one where
EXTRA_ADVERTISED_LISTENERS is set (expect
"BROKER://<hostname>:9092,<extra-value>" i.e., comma plus extra); ensure the
test asserts both the full exported string and that comma placement is correct
so the "${EXTRA_ADVERTISED_LISTENERS:+,${EXTRA_ADVERTISED_LISTENERS}}" behavior
is locked down.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@modules/kafka/kafka.go`:
- Line 24: Mismatch between the exported env var name and the documented
feature: replace the incorrect env var name used in the export string inside
modules/kafka/kafka.go (the export KAFKA_ADVERTISED_LISTENERS line that
currently references EXTRA_ADVERTISED_LISTENERS) with the documented environment
variable name used by the feature (ensure the token exactly matches the name
used in docs/other code), and then extend kafka_test.go to set that env var and
assert that its value is appended to the computed KAFKA_ADVERTISED_LISTENERS
(add a test case that sets the env var and verifies the EXTRA listeners are
included after BROKER://%s:9092).

---

Nitpick comments:
In `@modules/kafka/kafka.go`:
- Line 24: Update the advertised-listeners unit test in
modules/kafka/kafka_test.go to explicitly cover the new append behavior for the
environment expansion in the KAFKA_ADVERTISED_LISTENERS export line: add two
subcases—one where EXTRA_ADVERTISED_LISTENERS is unset (expect exactly
"BROKER://<hostname>:9092" with no trailing comma/extra), and one where
EXTRA_ADVERTISED_LISTENERS is set (expect
"BROKER://<hostname>:9092,<extra-value>" i.e., comma plus extra); ensure the
test asserts both the full exported string and that comma placement is correct
so the "${EXTRA_ADVERTISED_LISTENERS:+,${EXTRA_ADVERTISED_LISTENERS}}" behavior
is locked down.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 03fccecb-3438-4967-862b-8e40593d09c9

📥 Commits

Reviewing files that changed from the base of the PR and between c251392 and 6e2bc9f.

📒 Files selected for processing (1)
  • modules/kafka/kafka.go

Comment thread modules/kafka/kafka.go
starterScriptContent = `#!/bin/bash
source /etc/confluent/docker/bash-config
export KAFKA_ADVERTISED_LISTENERS=%s,BROKER://%s:9092
export KAFKA_ADVERTISED_LISTENERS="%s,BROKER://%s:9092${EXTRA_ADVERTISED_LISTENERS:+,${EXTRA_ADVERTISED_LISTENERS}}"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify which variable name is used by docs/tests/code.
rg -n -C2 'EXTRA_KAFKA_ADVERTISED_LISTENERS|EXTRA_ADVERTISED_LISTENERS'

Repository: testcontainers/testcontainers-go

Length of output: 499


🏁 Script executed:

# Search across entire codebase for any mention of EXTRA_KAFKA_ADVERTISED_LISTENERS
rg -i 'EXTRA_KAFKA' --type-list | head -20
rg 'EXTRA_KAFKA_ADVERTISED_LISTENERS' -t go -t md -t txt --type-list
find . -type f \( -name "*.md" -o -name "*.txt" -o -name "*README*" \) | head -20

Repository: testcontainers/testcontainers-go

Length of output: 1447


🏁 Script executed:

# Check kafka_test.go for any test related to extra listeners
rg -A5 -B5 'EXTRA.*ADVERTISED|extra.*listener' modules/kafka/kafka_test.go

Repository: testcontainers/testcontainers-go

Length of output: 58


🏁 Script executed:

# Look for any documentation or comments mentioning the feature name
rg -C3 'EXTRA_ADVERTISED_LISTENERS' modules/kafka/

Repository: testcontainers/testcontainers-go

Length of output: 697


🏁 Script executed:

# Explicit search for EXTRA_KAFKA_ADVERTISED_LISTENERS
rg 'EXTRA_KAFKA_ADVERTISED_LISTENERS' --max-count 10

Repository: testcontainers/testcontainers-go

Length of output: 58


🏁 Script executed:

# Read the kafka_test.go file to see test coverage
cat modules/kafka/kafka_test.go

Repository: testcontainers/testcontainers-go

Length of output: 2915


🏁 Script executed:

# Search for any docs mentioning kafka advertised listeners
rg -l 'advertised.*listener|KAFKA_ADVERTISED' --type md --type txt

Repository: testcontainers/testcontainers-go

Length of output: 58


Fix environment variable name mismatch in starter script

Line 24 uses EXTRA_ADVERTISED_LISTENERS, but this inconsistency with the documented feature name means users following the intended API contract will not have their settings applied. The code currently references an environment variable that does not appear elsewhere in the codebase or documentation.

🔧 Proposed fix
-export KAFKA_ADVERTISED_LISTENERS="%s,BROKER://%s:9092${EXTRA_ADVERTISED_LISTENERS:+,${EXTRA_ADVERTISED_LISTENERS}}"
+export KAFKA_ADVERTISED_LISTENERS="%s,BROKER://%s:9092${EXTRA_KAFKA_ADVERTISED_LISTENERS:+,${EXTRA_KAFKA_ADVERTISED_LISTENERS}}"

Additionally, the existing test in kafka_test.go only validates the BROKER:// URL format and does not cover the extra advertised listeners append behavior. Consider adding a test to verify that extra listeners are properly applied when the environment variable is set.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export KAFKA_ADVERTISED_LISTENERS="%s,BROKER://%s:9092${EXTRA_ADVERTISED_LISTENERS:+,${EXTRA_ADVERTISED_LISTENERS}}"
export KAFKA_ADVERTISED_LISTENERS="%s,BROKER://%s:9092${EXTRA_KAFKA_ADVERTISED_LISTENERS:+,${EXTRA_KAFKA_ADVERTISED_LISTENERS}}"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@modules/kafka/kafka.go` at line 24, Mismatch between the exported env var
name and the documented feature: replace the incorrect env var name used in the
export string inside modules/kafka/kafka.go (the export
KAFKA_ADVERTISED_LISTENERS line that currently references
EXTRA_ADVERTISED_LISTENERS) with the documented environment variable name used
by the feature (ensure the token exactly matches the name used in docs/other
code), and then extend kafka_test.go to set that env var and assert that its
value is appended to the computed KAFKA_ADVERTISED_LISTENERS (add a test case
that sets the env var and verifies the EXTRA listeners are included after
BROKER://%s:9092).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Kafka (KRaft) - customizable startup script/advertised listeners

1 participant