From adfc528f92d088530639e6eab8838ba71cc91300 Mon Sep 17 00:00:00 2001 From: Steven Hartland Date: Wed, 31 Jul 2024 23:45:28 +0100 Subject: [PATCH] fix(redpanda): wait for Wait for the admin interface to response to HTTP to avoid failures in configuring the instance when its not fully ready. --- modules/redpanda/redpanda.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/modules/redpanda/redpanda.go b/modules/redpanda/redpanda.go index a7387c9b90a..120281ebdb2 100644 --- a/modules/redpanda/redpanda.go +++ b/modules/redpanda/redpanda.go @@ -212,9 +212,28 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom } // 8. Wait until Redpanda is ready to serve requests. + waitHTTP := wait.ForHTTP(defaultAdminAPIPort). + WithStatusCodeMatcher(func(status int) bool { + return status == http.StatusNotFound + }) + + var tlsConfig *tls.Config + if settings.EnableTLS { + cert, err := tls.X509KeyPair(settings.cert, settings.key) + if err != nil { + return c, fmt.Errorf("failed to create admin client with cert: %w", err) + } + caCertPool := x509.NewCertPool() + caCertPool.AppendCertsFromPEM(settings.cert) + tlsConfig = &tls.Config{ + Certificates: []tls.Certificate{cert}, + RootCAs: caCertPool, + } + waitHTTP = waitHTTP.WithTLS(true, tlsConfig) + } err = wait.ForAll( wait.NewHostPortStrategy(defaultKafkaAPIPort), - wait.NewHostPortStrategy(defaultAdminAPIPort), + waitHTTP, wait.NewHostPortStrategy(defaultSchemaRegistryPort), wait.ForLog("Successfully started Redpanda!"), ).WaitUntilReady(ctx, container) @@ -237,21 +256,12 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom adminAPIUrl := fmt.Sprintf("%s://%v:%d", c.urlScheme, hostIP, adminAPIPort.Int()) adminCl := NewAdminAPIClient(adminAPIUrl) if settings.EnableTLS { - cert, err := tls.X509KeyPair(settings.cert, settings.key) - if err != nil { - return c, fmt.Errorf("failed to create admin client with cert: %w", err) - } - caCertPool := x509.NewCertPool() - caCertPool.AppendCertsFromPEM(settings.cert) adminCl = adminCl.WithHTTPClient(&http.Client{ Timeout: 5 * time.Second, Transport: &http.Transport{ ForceAttemptHTTP2: true, TLSHandshakeTimeout: 10 * time.Second, - TLSClientConfig: &tls.Config{ - Certificates: []tls.Certificate{cert}, - RootCAs: caCertPool, - }, + TLSClientConfig: tlsConfig, }, }) }