/
kafka.go
190 lines (160 loc) · 6.14 KB
/
kafka.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package kafka
import (
"context"
"fmt"
"math"
"strings"
"github.com/docker/go-connections/nat"
"golang.org/x/mod/semver"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
const publicPort = nat.Port("9093/tcp")
const (
starterScript = "/usr/sbin/testcontainers_start.sh"
// starterScript {
starterScriptContent = `#!/bin/bash
source /etc/confluent/docker/bash-config
export KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://%s:%d,BROKER://%s:9092
echo Starting Kafka KRaft mode
sed -i '/KAFKA_ZOOKEEPER_CONNECT/d' /etc/confluent/docker/configure
echo 'kafka-storage format --ignore-formatted -t "$(kafka-storage random-uuid)" -c /etc/kafka/kafka.properties' >> /etc/confluent/docker/configure
echo '' > /etc/confluent/docker/ensure
/etc/confluent/docker/configure
/etc/confluent/docker/launch`
// }
)
// KafkaContainer represents the Kafka container type used in the module
type KafkaContainer struct {
testcontainers.Container
ClusterID string
}
// RunContainer creates an instance of the Kafka container type
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*KafkaContainer, error) {
req := testcontainers.ContainerRequest{
Image: "confluentinc/confluent-local:7.5.0",
ExposedPorts: []string{string(publicPort)},
Env: map[string]string{
// envVars {
"KAFKA_LISTENERS": "PLAINTEXT://0.0.0.0:9093,BROKER://0.0.0.0:9092,CONTROLLER://0.0.0.0:9094",
"KAFKA_REST_BOOTSTRAP_SERVERS": "PLAINTEXT://0.0.0.0:9093,BROKER://0.0.0.0:9092,CONTROLLER://0.0.0.0:9094",
"KAFKA_LISTENER_SECURITY_PROTOCOL_MAP": "BROKER:PLAINTEXT,PLAINTEXT:PLAINTEXT,CONTROLLER:PLAINTEXT",
"KAFKA_INTER_BROKER_LISTENER_NAME": "BROKER",
"KAFKA_BROKER_ID": "1",
"KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR": "1",
"KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS": "1",
"KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR": "1",
"KAFKA_TRANSACTION_STATE_LOG_MIN_ISR": "1",
"KAFKA_LOG_FLUSH_INTERVAL_MESSAGES": fmt.Sprintf("%d", math.MaxInt),
"KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS": "0",
"KAFKA_NODE_ID": "1",
"KAFKA_PROCESS_ROLES": "broker,controller",
"KAFKA_CONTROLLER_LISTENER_NAMES": "CONTROLLER",
// }
},
Entrypoint: []string{"sh"},
// this CMD will wait for the starter script to be copied into the container and then execute it
Cmd: []string{"-c", "while [ ! -f " + starterScript + " ]; do sleep 0.1; done; bash " + starterScript},
LifecycleHooks: []testcontainers.ContainerLifecycleHooks{
{
PostStarts: []testcontainers.ContainerHook{
// 1. copy the starter script into the container
func(ctx context.Context, c testcontainers.Container) error {
host, err := c.Host(ctx)
if err != nil {
return err
}
port, err := c.MappedPort(ctx, publicPort)
if err != nil {
return err
}
scriptContent := fmt.Sprintf(starterScriptContent, host, port.Int(), host)
return c.CopyToContainer(ctx, []byte(scriptContent), starterScript, 0o755)
},
// 2. wait for the Kafka server to be ready
func(ctx context.Context, c testcontainers.Container) error {
return wait.ForLog(".*Transitioning from RECOVERY to RUNNING.*").AsRegexp().WaitUntilReady(ctx, c)
},
},
},
},
}
genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
}
for _, opt := range opts {
opt.Customize(&genericContainerReq)
}
err := validateKRaftVersion(genericContainerReq.Image)
if err != nil {
return nil, err
}
clusterID := genericContainerReq.Env["CLUSTER_ID"]
configureControllerQuorumVoters(&genericContainerReq)
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
if err != nil {
return nil, err
}
return &KafkaContainer{Container: container, ClusterID: clusterID}, nil
}
func WithClusterID(clusterID string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
req.Env["CLUSTER_ID"] = clusterID
}
}
// Brokers retrieves the broker connection strings from Kafka with only one entry,
// defined by the exposed public port.
func (kc *KafkaContainer) Brokers(ctx context.Context) ([]string, error) {
host, err := kc.Host(ctx)
if err != nil {
return nil, err
}
port, err := kc.MappedPort(ctx, publicPort)
if err != nil {
return nil, err
}
return []string{fmt.Sprintf("%s:%d", host, port.Int())}, nil
}
// configureControllerQuorumVoters sets the quorum voters for the controller. For that, it will
// check if there are any network aliases defined for the container and use the first alias in the
// first network. Else, it will use localhost.
func configureControllerQuorumVoters(req *testcontainers.GenericContainerRequest) {
if req.Env == nil {
req.Env = map[string]string{}
}
if req.Env["KAFKA_CONTROLLER_QUORUM_VOTERS"] == "" {
host := "localhost"
if len(req.Networks) > 0 {
nw := req.Networks[0]
if len(req.NetworkAliases[nw]) > 0 {
host = req.NetworkAliases[nw][0]
}
}
req.Env["KAFKA_CONTROLLER_QUORUM_VOTERS"] = fmt.Sprintf("1@%s:9094", host)
}
// }
}
// validateKRaftVersion validates if the image version is compatible with KRaft mode,
// which is available since version 7.0.0.
func validateKRaftVersion(fqName string) error {
if fqName == "" {
return fmt.Errorf("image cannot be empty")
}
image := fqName[:strings.LastIndex(fqName, ":")]
version := fqName[strings.LastIndex(fqName, ":")+1:]
if !strings.EqualFold(image, "confluentinc/confluent-local") {
// do not validate if the image is not the official one.
// not raising an error here, letting the image to start and
// eventually evaluate an error if it exists.
return nil
}
// semver requires the version to start with a "v"
if !strings.HasPrefix(version, "v") {
version = fmt.Sprintf("v%s", version)
}
if semver.Compare(version, "v7.4.0") < 0 { // version < v7.4.0
return fmt.Errorf("version=%s. KRaft mode is only available since version 7.4.0", version)
}
return nil
}