Skip to content

Commit 7ae3814

Browse files
mdelapenyaclaude
andauthored
chore(redpanda)!: use Run function (#3430)
* chore(redpanda)!: use Run function BREAKING CHANGE: Option type now returns error - Changed Option type to return error for proper error handling - Migrated from GenericContainer to testcontainers.Run() - Process custom options before building moduleOpts - Listener network validation happens after user options (as final step) - WithListener now returns errors for invalid host:port format - All option functions return error (nil for success) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * fix(redpanda): wait strategy was using incorrect path+port * chore: use octal for file mode * fix(redpanda): prevent nil on networkAliases --------- Co-authored-by: Claude <[email protected]>
1 parent b890b0c commit 7ae3814

File tree

2 files changed

+123
-103
lines changed

2 files changed

+123
-103
lines changed

modules/redpanda/options.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redpanda
22

33
import (
4+
"fmt"
45
"net"
56
"strconv"
67

@@ -84,7 +85,7 @@ func defaultOptions() options {
8485
var _ testcontainers.ContainerCustomizer = (Option)(nil)
8586

8687
// Option is an option for the Redpanda container.
87-
type Option func(*options)
88+
type Option func(*options) error
8889

8990
// Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.
9091
func (o Option) Customize(*testcontainers.GenericContainerRequest) error {
@@ -96,16 +97,18 @@ func (o Option) Customize(*testcontainers.GenericContainerRequest) error {
9697
// that shall be created, so that you can use these to authenticate against
9798
// Redpanda (either for the Kafka API or Schema Registry HTTP access).
9899
func WithNewServiceAccount(username, password string) Option {
99-
return func(o *options) {
100+
return func(o *options) error {
100101
o.ServiceAccounts[username] = password
102+
return nil
101103
}
102104
}
103105

104106
// WithSuperusers defines the superusers added to the redpanda config.
105107
// By default, there are no superusers.
106108
func WithSuperusers(superusers ...string) Option {
107-
return func(o *options) {
109+
return func(o *options) error {
108110
o.Superusers = superusers
111+
return nil
109112
}
110113
}
111114

@@ -114,31 +117,35 @@ func WithSuperusers(superusers ...string) Option {
114117
// When setting an authentication method, make sure to add users
115118
// as well as authorize them using the WithSuperusers() option.
116119
func WithEnableSASL() Option {
117-
return func(o *options) {
120+
return func(o *options) error {
118121
o.KafkaAuthenticationMethod = "sasl"
122+
return nil
119123
}
120124
}
121125

122126
// WithEnableKafkaAuthorization enables authorization for connections on the Kafka API.
123127
func WithEnableKafkaAuthorization() Option {
124-
return func(o *options) {
128+
return func(o *options) error {
125129
o.KafkaEnableAuthorization = true
130+
return nil
126131
}
127132
}
128133

129134
// WithEnableWasmTransform enables wasm transform.
130135
// Should not be used with RP versions before 23.3
131136
func WithEnableWasmTransform() Option {
132-
return func(o *options) {
137+
return func(o *options) error {
133138
o.EnableWasmTransform = true
139+
return nil
134140
}
135141
}
136142

137143
// WithEnableSchemaRegistryHTTPBasicAuth enables HTTP basic authentication for
138144
// Schema Registry.
139145
func WithEnableSchemaRegistryHTTPBasicAuth() Option {
140-
return func(o *options) {
146+
return func(o *options) error {
141147
o.SchemaRegistryAuthenticationMethod = "http_basic"
148+
return nil
142149
}
143150
}
144151

@@ -147,29 +154,33 @@ func WithEnableSchemaRegistryHTTPBasicAuth() Option {
147154
func WithHTTPProxyAuthMethod(method HTTPProxyAuthMethod) Option {
148155
switch method {
149156
case HTTPProxyAuthMethodNone, HTTPProxyAuthMethodHTTPBasic, HTTPProxyAuthMethodOIDC:
150-
return func(o *options) {
157+
return func(o *options) error {
151158
o.HTTPProxyAuthenticationMethod = method
159+
return nil
152160
}
153161
default:
154-
return func(o *options) {
162+
return func(o *options) error {
155163
// Invalid method, default to "none"
156164
o.HTTPProxyAuthenticationMethod = HTTPProxyAuthMethodNone
165+
return nil
157166
}
158167
}
159168
}
160169

161170
// WithAutoCreateTopics enables topic auto creation.
162171
func WithAutoCreateTopics() Option {
163-
return func(o *options) {
172+
return func(o *options) error {
164173
o.AutoCreateTopics = true
174+
return nil
165175
}
166176
}
167177

168178
func WithTLS(cert, key []byte) Option {
169-
return func(o *options) {
179+
return func(o *options) error {
170180
o.EnableTLS = true
171181
o.cert = cert
172182
o.key = key
183+
return nil
173184
}
174185
}
175186

@@ -178,22 +189,23 @@ func WithTLS(cert, key []byte) Option {
178189
// networks. At least one network must be attached to the container, if not an
179190
// error will be thrown when starting the container.
180191
func WithListener(lis string) Option {
181-
host, port, err := net.SplitHostPort(lis)
182-
if err != nil {
183-
return func(_ *options) {}
184-
}
192+
return func(o *options) error {
193+
host, port, err := net.SplitHostPort(lis)
194+
if err != nil {
195+
return fmt.Errorf("split host port: %w", err)
196+
}
185197

186-
portInt, err := strconv.Atoi(port)
187-
if err != nil {
188-
return func(_ *options) {}
189-
}
198+
portInt, err := strconv.Atoi(port)
199+
if err != nil {
200+
return fmt.Errorf("parse port: %w", err)
201+
}
190202

191-
return func(o *options) {
192203
o.Listeners = append(o.Listeners, listener{
193204
Address: host,
194205
Port: portInt,
195206
AuthenticationMethod: o.KafkaAuthenticationMethod,
196207
})
208+
return nil
197209
}
198210
}
199211

@@ -202,16 +214,18 @@ func WithListener(lis string) Option {
202214
// config file, which is particularly useful for configs requiring a restart
203215
// when otherwise applied to a running Redpanda instance.
204216
func WithBootstrapConfig(cfg string, val any) Option {
205-
return func(o *options) {
217+
return func(o *options) error {
206218
o.ExtraBootstrapConfig[cfg] = val
219+
return nil
207220
}
208221
}
209222

210223
// WithAdminAPIAuthentication enables Admin API Authentication.
211224
// It sets `admin_api_require_auth` configuration to true and configures a bootstrap user account.
212225
// See https://docs.redpanda.com/current/deploy/deployment-option/self-hosted/manual/production/production-deployment/#bootstrap-a-user-account
213226
func WithAdminAPIAuthentication() Option {
214-
return func(o *options) {
227+
return func(o *options) error {
215228
o.enableAdminAPIAuthentication = true
229+
return nil
216230
}
217231
}

0 commit comments

Comments
 (0)