-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKafkaConnectContainer.java
326 lines (294 loc) · 13.2 KB
/
KafkaConnectContainer.java
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package com.findinpath.testcontainers;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.MountableFile;
import org.testcontainers.utility.TestcontainersConfiguration;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import static com.findinpath.testcontainers.Utils.CONFLUENT_PLATFORM_VERSION;
import static com.findinpath.testcontainers.Utils.containerLogsConsumer;
import static java.lang.String.format;
/**
* This file is a slight adapatation of the KafkaConnectContainer code available on ydespreaux's Github account:
*
* @see <a href="https://github.com/ydespreaux/testcontainers/blob/master/testcontainers-kafka/src/main/java/com/github/ydespreaux/testcontainers/kafka/containers/KafkaConnectContainer.java">KafkaConnectContainer.java</a>
*/
public class KafkaConnectContainer extends GenericContainer<KafkaConnectContainer> {
public static final int CONNECT_REST_PORT_INTERNAL = 28082;
/**
* Key / value for Configuration
*/
public static final String GROUP_ID_CONFIG = "CONNECT_GROUP_ID";
public static final String OFFSET_STORAGE_FILE_FILENAME_CONFIG = "CONNECT_OFFSET_STORAGE_FILE_FILENAME";
public static final String OFFSET_STORAGE_TOPIC_CONFIG = "CONNECT_OFFSET_STORAGE_TOPIC";
public static final String OFFSET_STORAGE_PARTITIONS_CONFIG = "CONNECT_OFFSET_STORAGE_PARTITIONS";
public static final String CONFIG_STORAGE_TOPIC_CONFIG = "CONNECT_CONFIG_STORAGE_TOPIC";
public static final String STATUS_STORAGE_TOPIC_CONFIG = "CONNECT_STATUS_STORAGE_TOPIC";
public static final String STATUS_STORAGE_PARTITIONS_CONFIG = "CONNECT_STATUS_STORAGE_PARTITIONS";
public static final String KEY_CONVERTER_CONFIG = "CONNECT_KEY_CONVERTER";
public static final String KEY_CONVERTER_SCHEMA_REGISTRY_URL_CONFIG = "CONNECT_KEY_CONVERTER_SCHEMA_REGISTRY_URL";
public static final String VALUE_CONVERTER_CONFIG = "CONNECT_VALUE_CONVERTER";
public static final String VALUE_CONVERTER_SCHEMA_REGISTRY_URL_CONFIG = "CONNECT_VALUE_CONVERTER_SCHEMA_REGISTRY_URL";
private static final String AVRO_CONVERTER_PATTERN = "AvroConverter";
private static final String PLUGIN_PATH_CONTAINER = "/usr/share/java";
private static final String GROUP_ID_DEFAULT_VALUE = "kafka-connect-group";
private static final String OFFSET_STORAGE_FILE_FILENAME_DEFAULT_VALUE = "connect-offsets-file.txt";
private static final String OFFSET_STORAGE_TOPIC_DEFAULT_VALUE = "connect-offsets";
private static final Integer OFFSET_STORAGE_PARTITIONS_DEFAULT_VALUE = 3;
private static final String OFFSET_STORAGE_REPLICATION_FACTOR_CONFIG = "CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR";
private static final Integer OFFSET_STORAGE_REPLICATION_FACTOR_DEFAULT_VALUE = 1;
private static final String CONFIG_STORAGE_TOPIC_DEFAULT_VALUE = "connect-config";
private static final String CONFIG_STORAGE_REPLICATION_FACTOR_CONFIG = "CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR";
private static final Integer CONFIG_STORAGE_REPLICATION_FACTOR_DEFAULT_VALUE = 1;
private static final String STATUS_STORAGE_TOPIC_DEFAULT_VALUE = "connect-status";
private static final Integer STATUS_STORAGE_PARTITIONS_DEFAULT_VALUE = 3;
private static final String STATUS_STORAGE_REPLICATION_FACTOR_CONFIG = "CONNECT_STATUS_STORAGE_REPLICATION_FACTOR";
private static final Integer STATUS_STORAGE_REPLICATION_FACTOR_DEFAULT_VALUE = 1;
private static final String KEY_CONVERTER_DEFAULT_VALUE = "org.apache.kafka.connect.json.JsonConverter";
private static final String KEY_CONVERTER_SCHEMAS_ENABLE_CONFIG = "CONNECT_KEY_CONVERTER_SCHEMAS_ENABLE";
private static final String VALUE_CONVERTER_DEFAULT_VALUE = "org.apache.kafka.connect.json.JsonConverter";
private static final String VALUE_CONVERTER_SCHEMAS_ENABLE_CONFIG = "CONNECT_VALUE_CONVERTER_SCHEMAS_ENABLE";
private static final String INTERNAL_KEY_CONVERTER_CONFIG = "CONNECT_INTERNAL_KEY_CONVERTER";
private static final String INTERNAL_KEY_CONVERTER_DEFAULT_VALUE = "org.apache.kafka.connect.json.JsonConverter";
private static final String INTERNAL_VALUE_CONVERTER_CONFIG = "CONNECT_INTERNAL_VALUE_CONVERTER";
private static final String INTERNAL_VALUE_CONVERTER_DEFAULT_VALUE = "org.apache.kafka.connect.json.JsonConverter";
private final String bootstrapServers;
private final String networkAlias = "kafka-connect";
/**
* Schema registry url
*/
private String schemaRegistryUrl;
private boolean hasKeyAvroConverter = false;
private boolean hasValueAvroConverter = false;
public KafkaConnectContainer(String bootstrapServersConnect) {
this(CONFLUENT_PLATFORM_VERSION, bootstrapServersConnect);
}
public KafkaConnectContainer(String confluentPlatformVersion,
String bootstrapServers) {
super(getKafkaConnectContainerImage(confluentPlatformVersion));
this.bootstrapServers = bootstrapServers;
this.withLogConsumer(containerLogsConsumer(logger()))
.withEnv(GROUP_ID_CONFIG, GROUP_ID_DEFAULT_VALUE)
.withEnv(KEY_CONVERTER_CONFIG, KEY_CONVERTER_DEFAULT_VALUE)
.withEnv(VALUE_CONVERTER_CONFIG, VALUE_CONVERTER_DEFAULT_VALUE)
.withEnv(OFFSET_STORAGE_FILE_FILENAME_CONFIG, OFFSET_STORAGE_FILE_FILENAME_DEFAULT_VALUE)
.withEnv(OFFSET_STORAGE_TOPIC_CONFIG, OFFSET_STORAGE_TOPIC_DEFAULT_VALUE)
.withEnv(OFFSET_STORAGE_PARTITIONS_CONFIG, String.valueOf(OFFSET_STORAGE_PARTITIONS_DEFAULT_VALUE))
.withEnv(OFFSET_STORAGE_REPLICATION_FACTOR_CONFIG, String.valueOf(OFFSET_STORAGE_REPLICATION_FACTOR_DEFAULT_VALUE))
.withEnv(CONFIG_STORAGE_TOPIC_CONFIG, CONFIG_STORAGE_TOPIC_DEFAULT_VALUE)
.withEnv(CONFIG_STORAGE_REPLICATION_FACTOR_CONFIG, String.valueOf(CONFIG_STORAGE_REPLICATION_FACTOR_DEFAULT_VALUE))
.withEnv(STATUS_STORAGE_TOPIC_CONFIG, STATUS_STORAGE_TOPIC_DEFAULT_VALUE)
.withEnv(STATUS_STORAGE_PARTITIONS_CONFIG, String.valueOf(STATUS_STORAGE_PARTITIONS_DEFAULT_VALUE))
.withEnv(STATUS_STORAGE_REPLICATION_FACTOR_CONFIG, String.valueOf(STATUS_STORAGE_REPLICATION_FACTOR_DEFAULT_VALUE))
.withEnv(INTERNAL_KEY_CONVERTER_CONFIG, INTERNAL_KEY_CONVERTER_DEFAULT_VALUE)
.withEnv(INTERNAL_VALUE_CONVERTER_CONFIG, INTERNAL_VALUE_CONVERTER_DEFAULT_VALUE);
withExposedPorts(CONNECT_REST_PORT_INTERNAL);
withNetworkAliases(networkAlias);
waitingFor(Wait.forHttp("/").withStartupTimeout(Duration.ofSeconds(120L)));
}
private static String getKafkaConnectContainerImage(String confluentPlatformVersion) {
return (String) TestcontainersConfiguration
.getInstance().getProperties().getOrDefault(
"kafkaconnect.container.image",
"confluentinc/cp-kafka-connect:" + confluentPlatformVersion
);
}
/**
* Configure the container
*/
@Override
protected void configure() {
super.configure();
this.withEnv("CONNECT_BOOTSTRAP_SERVERS", this.bootstrapServers)
.withEnv("CONNECT_REST_ADVERTISED_HOST_NAME", "kafka-connect")
.withEnv("CONNECT_PLUGIN_PATH", PLUGIN_PATH_CONTAINER)
.withEnv("CONNECT_LOG4J_LOGGERS", "org.reflections=ERROR")
.withEnv("CONNECT_REST_PORT", String.valueOf(CONNECT_REST_PORT_INTERNAL))
.withCreateContainerCmdModifier(createContainerCmd -> createContainerCmd.withName("testcontainsers-kafka-connect-" + UUID.randomUUID()));
if (hasKeyAvroConverter) {
Objects.requireNonNull(this.schemaRegistryUrl, "Schema registry URL not defined !!");
this.withEnv(KEY_CONVERTER_SCHEMA_REGISTRY_URL_CONFIG, this.schemaRegistryUrl);
this.withEnv(KEY_CONVERTER_SCHEMAS_ENABLE_CONFIG, "true");
}
if (hasValueAvroConverter) {
Objects.requireNonNull(this.schemaRegistryUrl, "Schema registry URL not defined !!");
this.withEnv(VALUE_CONVERTER_SCHEMA_REGISTRY_URL_CONFIG, this.schemaRegistryUrl);
this.withEnv(VALUE_CONVERTER_SCHEMAS_ENABLE_CONFIG, "true");
}
}
public KafkaConnectContainer withSchemaRegistryUrl(String schemaRegistryUrl) {
this.schemaRegistryUrl = schemaRegistryUrl;
return this;
}
/**
* Set the group id
*
* @param groupId
* @return
*/
public KafkaConnectContainer withGroupId(String groupId) {
if (groupId != null) {
withEnv(GROUP_ID_CONFIG, groupId);
}
return this;
}
/**
* Set the config storage topic.
*
* @param topic
* @return
*/
public KafkaConnectContainer withConfigStorageTopic(String topic) {
if (topic != null) {
withEnv(CONFIG_STORAGE_TOPIC_CONFIG, topic);
}
return this;
}
/**
* Set the topic name of the storage offsets topic.
*
* @param topic
* @return
*/
public KafkaConnectContainer withOffsetStorageTopic(String topic) {
if (topic != null) {
withEnv(OFFSET_STORAGE_TOPIC_CONFIG, topic);
}
return this;
}
/**
* Set the offsets storage partition.
*
* @param partitions
* @return
*/
public KafkaConnectContainer withOffsetStoragePartition(Integer partitions) {
if (partitions != null) {
withEnv(OFFSET_STORAGE_PARTITIONS_CONFIG, String.valueOf(partitions));
}
return this;
}
/**
* Set the status storage topic's name.
*
* @param topic
* @return
*/
public KafkaConnectContainer withStatusStorageTopic(String topic) {
if (topic != null) {
withEnv(STATUS_STORAGE_TOPIC_CONFIG, topic);
}
return this;
}
/**
* Set the status storage partition.
*
* @param partitions
* @return
*/
public KafkaConnectContainer withStatusStoragePartition(Integer partitions) {
if (partitions != null) {
withEnv(STATUS_STORAGE_PARTITIONS_CONFIG, String.valueOf(partitions));
}
return this;
}
/**
* Set the offsets storage file name.
*
* @param storageFilename
* @return
*/
public KafkaConnectContainer withOffsetStorageFilename(String storageFilename) {
if (storageFilename != null) {
withEnv(OFFSET_STORAGE_FILE_FILENAME_CONFIG, storageFilename);
}
return this;
}
/**
* Set the key converter.
*
* @param keyConverter
* @return
*/
public KafkaConnectContainer withKeyConverter(String keyConverter) {
if (keyConverter != null) {
withEnv(KEY_CONVERTER_CONFIG, keyConverter);
this.hasKeyAvroConverter = keyConverter.contains(AVRO_CONVERTER_PATTERN);
}
return this;
}
/**
* Set the value converter.
*
* @param valueConverter
* @return
*/
public KafkaConnectContainer withValueConverter(String valueConverter) {
if (valueConverter != null) {
withEnv(VALUE_CONVERTER_CONFIG, valueConverter);
this.hasValueAvroConverter = valueConverter.contains(AVRO_CONVERTER_PATTERN);
}
return this;
}
/**
* Set the list of plugins directory.
*
* @param plugins
* @return
*/
public KafkaConnectContainer withPlugins(Set<String> plugins) {
if (plugins == null) {
return this;
}
plugins.forEach(this::withPlugins);
return this;
}
/**
* Set the plugins directory.
*
* @param plugins
* @return
*/
public KafkaConnectContainer withPlugins(String plugins) {
if (plugins == null) {
return this;
}
MountableFile mountableFile = MountableFile.forClasspathResource(plugins);
Path pluginsPath = Paths.get(mountableFile.getResolvedPath());
File pluginsFile = pluginsPath.toFile();
if (!pluginsFile.exists()) {
throw new IllegalArgumentException(format("Resource with path %s could not be found", pluginsPath.toString()));
}
String containerPath = PLUGIN_PATH_CONTAINER;
if (pluginsFile.isDirectory()) {
containerPath += "/" + pluginsPath.getFileName();
} else {
containerPath += "/" + pluginsPath.getParent().getFileName() + "/" + pluginsPath.getFileName();
}
// Create the volume that will be need for scripts
this.addFileSystemBind(mountableFile.getResolvedPath(), containerPath, BindMode.READ_ONLY);
return this;
}
/**
* Get the url.
*
* @return
*/
public String getUrl() {
return format("http://%s:%d", this.getContainerIpAddress(), getMappedPort(CONNECT_REST_PORT_INTERNAL));
}
/**
* Get the local url
*
* @return
*/
public String getInternalUrl() {
return format("http://%s:%d", this.getNetworkAliases().get(0), CONNECT_REST_PORT_INTERNAL);
}
}