Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithClusterID ¶
func WithClusterID(clusterID string) testcontainers.CustomizeRequestOption
WithClusterID sets the CLUSTER_ID environment variable, used to format the KRaft storage.
Types ¶
type KafkaContainer ¶
type KafkaContainer struct {
testcontainers.Container
ClusterID string
// contains filtered or unexported fields
}
KafkaContainer represents the Kafka container type used in the module
func Run ¶ added in v0.32.0
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*KafkaContainer, error)
Run creates an instance of the Kafka container type
Example ¶
package main
import (
"context"
"fmt"
"log"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/kafka"
)
func main() {
// runKafkaContainer {
ctx := context.Background()
kafkaContainer, err := kafka.Run(ctx,
"confluentinc/confluent-local:7.5.0",
kafka.WithClusterID("test-cluster"),
)
defer func() {
if err := testcontainers.TerminateContainer(kafkaContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
// }
state, err := kafkaContainer.State(ctx)
if err != nil {
log.Printf("failed to get container state: %s", err)
return
}
fmt.Println(kafkaContainer.ClusterID)
fmt.Println(state.Running)
}
Output: test-cluster true
func RunContainer
deprecated
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*KafkaContainer, error)
Deprecated: use Run instead RunContainer creates an instance of the Kafka container type
func (*KafkaContainer) Brokers ¶
func (kc *KafkaContainer) Brokers(ctx context.Context) ([]string, error)
Brokers retrieves the broker connection strings from Kafka with only one entry, defined by the exposed public port.
func (*KafkaContainer) BrokersTLS ¶ added in v0.45.0
func (kc *KafkaContainer) BrokersTLS(ctx context.Context) ([]string, error)
BrokersTLS retrieves the broker connection strings for the SSL listener, defined by the exposed SSL port. Returns an error if TLS is not enabled.
type Option ¶ added in v0.45.0
type Option func(*options) error
Option is an option for the Kafka container.
func WithTLS ¶ added in v0.45.0
func WithTLS() Option
WithTLS enables an additional SSL listener on the Kafka container. When enabled, the container will:
- Generate a self-signed CA and a server certificate signed by it, valid for the host the container is reachable at
- Configure Kafka with a PKCS12 keystore holding the server certificate
- Expose the SSL port (9095), while keeping the PLAINTEXT one
The SSL listener is added to KAFKA_LISTENERS and KAFKA_LISTENER_SECURITY_PROTOCOL_MAP after all the options have been applied, so it is kept even if they are overridden. If they already define an SSL listener, it must listen on port 9095 and use the SSL protocol, otherwise the container fails to start with an error.
Use BrokersTLS() to get the SSL endpoint and TLSConfig() to get the *tls.Config for client connections.
func (Option) Customize ¶ added in v0.45.0
func (o Option) Customize(*testcontainers.GenericContainerRequest) error
Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.