-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathGlobalKafkaConfiguration.java
564 lines (494 loc) · 23.8 KB
/
GlobalKafkaConfiguration.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package io.jenkins.plugins.remotingkafka;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardCertificateCredentials;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.SchemeRequirement;
import com.cloudbees.plugins.credentials.matchers.IdMatcher;
import hudson.Extension;
import hudson.Util;
import hudson.model.Item;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.apps.StatefulSet;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.jenkins.plugins.remotingkafka.exception.RemotingKafkaConfigurationException;
import io.jenkins.plugins.remotingkafka.exception.RemotingKafkaException;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.kubernetes.credentials.TokenProducer;
import org.jenkinsci.plugins.plaincredentials.FileCredentials;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.interceptor.RequirePOST;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
@Extension
@Symbol("kafka")
public class GlobalKafkaConfiguration extends GlobalConfiguration {
private static final Logger LOGGER = Logger.getLogger(GlobalKafkaConfiguration.class.getName());
public static final SchemeRequirement KAFKA_SCHEME = new SchemeRequirement("kafka");
private String brokerURL;
private String zookeeperURL;
private boolean enableSSL;
private String kafkaCredentialsId;
private String sslTruststoreCredentialsId;
private String sslKeystoreCredentialsId;
private String sslKeyCredentialsId;
private boolean useKubernetes;
private String kubernetesIp;
private String kubernetesApiPort;
private String kubernetesCertificate;
private String kubernetesCredentialsId;
private boolean kubernetesSkipTlsVerify;
private String kubernetesNamespace;
public GlobalKafkaConfiguration() {
load();
}
public static GlobalKafkaConfiguration get() {
return GlobalConfiguration.all().getInstance(GlobalKafkaConfiguration.class);
}
public String getBrokerURL() {
return brokerURL;
}
public void setBrokerURL(String brokerURL) {
this.brokerURL = brokerURL;
}
public String getZookeeperURL() {
return zookeeperURL;
}
public void setZookeeperURL(String zookeeperURL) {
this.zookeeperURL = zookeeperURL;
}
public boolean getEnableSSL() {
return enableSSL;
}
public void setEnableSSL(boolean enableSSL) {
this.enableSSL = enableSSL;
}
public String getKafkaCredentialsId() {
return kafkaCredentialsId;
}
public void setKafkaCredentialsId(String kafkaCredentialsId) {
this.kafkaCredentialsId = kafkaCredentialsId;
}
public String getSslTruststoreCredentialsId() {
return sslTruststoreCredentialsId;
}
public void setSslTruststoreCredentialsId(String sslTruststoreCredentialsId) {
this.sslTruststoreCredentialsId = sslTruststoreCredentialsId;
}
public String getSslKeystoreCredentialsId() {
return sslKeystoreCredentialsId;
}
public void setSslKeystoreCredentialsId(String sslKeystoreCredentialsId) {
this.sslKeystoreCredentialsId = sslKeystoreCredentialsId;
}
public String getSslKeyCredentialsId() {
return sslKeyCredentialsId;
}
public void setSslKeyCredentialsId(String sslKeyCredentialsId) {
this.sslKeyCredentialsId = sslKeyCredentialsId;
}
public boolean getUseKubernetes() {
return useKubernetes;
}
public void setUseKubernetes(boolean useKubernetes) {
this.useKubernetes = useKubernetes;
}
public String getKubernetesIp() {
return kubernetesIp;
}
public void setKubernetesIp(String kubernetesIp) {
this.kubernetesIp = kubernetesIp;
}
public String getKubernetesApiPort() {
return kubernetesApiPort;
}
public void setKubernetesApiPort(String kubernetesApiPort) {
this.kubernetesApiPort = kubernetesApiPort;
}
public String getKubernetesCertificate() {
return kubernetesCertificate;
}
public void setKubernetesCertificate(String kubernetesCertificate) {
this.kubernetesCertificate = kubernetesCertificate;
}
public String getKubernetesCredentialsId() {
return kubernetesCredentialsId;
}
public void setKubernetesCredentialsId(String kubernetesCredentialsId) {
this.kubernetesCredentialsId = kubernetesCredentialsId;
}
public boolean getKubernetesSkipTlsVerify() {
return kubernetesSkipTlsVerify;
}
public void setKubernetesSkipTlsVerify(boolean kubernetesSkipTlsVerify) {
this.kubernetesSkipTlsVerify = kubernetesSkipTlsVerify;
}
public String getKubernetesNamespace() {
return kubernetesNamespace;
}
public void setKubernetesNamespace(String kubernetesNamespace) {
this.kubernetesNamespace = kubernetesNamespace;
}
public String getKafkaUsername() throws RemotingKafkaConfigurationException {
StandardUsernamePasswordCredentials credential = getCredential(kafkaCredentialsId);
if (credential == null) {
throw new RemotingKafkaConfigurationException("No Kafka credential provided");
}
return credential.getUsername();
}
public String getKafkaPassword() throws RemotingKafkaConfigurationException {
StandardUsernamePasswordCredentials credential = getCredential(kafkaCredentialsId);
if (credential == null) {
throw new RemotingKafkaConfigurationException("No Kafka credential provided");
}
return credential.getPassword().getPlainText();
}
public String getSSLTruststoreLocation() throws RemotingKafkaConfigurationException {
StandardUsernamePasswordCredentials credential = getCredential(sslTruststoreCredentialsId);
if (credential == null) {
throw new RemotingKafkaConfigurationException("No SSL truststore credential provided");
}
return credential.getUsername();
}
public String getSSLTruststorePassword() throws RemotingKafkaConfigurationException {
StandardUsernamePasswordCredentials credential = getCredential(sslTruststoreCredentialsId);
if (credential == null) {
throw new RemotingKafkaConfigurationException("No SSL truststore credential provided");
}
return credential.getPassword().getPlainText();
}
public String getSSLKeystoreLocation() throws RemotingKafkaConfigurationException {
StandardUsernamePasswordCredentials credential = getCredential(sslKeystoreCredentialsId);
if (credential == null) {
throw new RemotingKafkaConfigurationException("No SSL keystore credential provided");
}
return credential.getUsername();
}
public String getSSLKeystorePassword() throws RemotingKafkaConfigurationException {
StandardUsernamePasswordCredentials credential = getCredential(sslKeystoreCredentialsId);
if (credential == null) {
throw new RemotingKafkaConfigurationException("No SSL keystore credential provided");
}
return credential.getPassword().getPlainText();
}
public String getSSLKeyPassword() throws RemotingKafkaConfigurationException {
StandardUsernamePasswordCredentials credential = getCredential(sslKeyCredentialsId);
if (credential == null) {
throw new RemotingKafkaConfigurationException("No SSL key credential provided");
}
return credential.getPassword().getPlainText();
}
public FormValidation doCheckBrokerURL(@QueryParameter("brokerURL") final String brokerURL) {
if (StringUtils.isBlank(brokerURL)) {
return FormValidation.error(Messages.GlobalKafkaConfiguration_KafkaConnectionURLWarning());
}
return FormValidation.ok();
}
public FormValidation doCheckZookeeperURL(@QueryParameter("zookeeperURL") String zookeeperURL) {
if (StringUtils.isBlank(zookeeperURL)) {
return FormValidation.error(Messages.GlobalKafkaConfiguration_ZookeeperURLWarning());
}
return FormValidation.ok();
}
@RequirePOST
public FormValidation doTestZookeeperConnection(@QueryParameter("zookeeperURL") final String zookeeperURL)
throws IOException, ServletException {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return FormValidation.error("Need admin permission to perform this action");
}
try {
String[] hostport = zookeeperURL.split(":");
String host = hostport[0];
int port = Integer.parseInt(hostport[1]);
testConnection(host, port);
return FormValidation.ok("Success");
} catch (Exception e) {
return FormValidation.error("Connection error : " + e.getMessage());
}
}
@RequirePOST
public FormValidation doTestBrokerConnection(@QueryParameter("brokerURL") final String brokerURL)
throws IOException, ServletException {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return FormValidation.error("Need admin permission to perform this action");
}
try {
String[] hostport = brokerURL.split(":");
String host = hostport[0];
int port = Integer.parseInt(hostport[1]);
testConnection(host, port);
return FormValidation.ok("Success");
} catch (Exception e) {
return FormValidation.error("Connection error : " + e.getMessage());
}
}
@RequirePOST
public FormValidation doTestKubernetesConnection(
@QueryParameter("kubernetesIp") String serverIp,
@QueryParameter("kubernetesApiPort") String serverPort,
@QueryParameter("kubernetesCredentialsId") String credentialsId,
@QueryParameter("kubernetesCertificate") String serverCertificate,
@QueryParameter("kubernetesSkipTlsVerify") boolean skipTlsVerify,
@QueryParameter("kubernetesNamespace") String namespace
) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
try {
String serverUrl = getURL(serverIp, Integer.parseInt(serverPort));
KubernetesClient client = new KubernetesFactoryAdapter(serverUrl, namespace,
Util.fixEmpty(serverCertificate), Util.fixEmpty(credentialsId), skipTlsVerify
).createClient();
// Call Pod list API to ensure functionality
client.pods().list();
return FormValidation.ok("Success");
} catch (KubernetesClientException e) {
LOGGER.log(Level.FINE, "Error testing Kubernetes connection", e);
return FormValidation.error("Error: %s", e.getCause() == null
? e.getMessage()
: String.format("%s: %s", e.getCause().getClass().getName(), e.getCause().getMessage()));
} catch (Exception e) {
LOGGER.log(Level.FINE, "Error testing Kubernetes connection", e);
return FormValidation.error("Error: %s", e.getMessage());
}
}
@RequirePOST
public FormValidation doStartKafkaOnKubernetes(
@QueryParameter("kubernetesIp") String serverIp,
@QueryParameter("kubernetesApiPort") String serverPort,
@QueryParameter("kubernetesCredentialsId") String credentialsId,
@QueryParameter("kubernetesCertificate") String serverCertificate,
@QueryParameter("kubernetesSkipTlsVerify") boolean skipTlsVerify,
@QueryParameter("kubernetesNamespace") String namespace
) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
Class clazz = GlobalKafkaConfiguration.class;
try (InputStream zookeeperFile = clazz.getResourceAsStream(Paths.get("kubernetes", "zookeeper.yaml").toString());
InputStream kafkaServiceFile = clazz.getResourceAsStream(Paths.get("kubernetes", "kafka-service.yaml").toString());
InputStream kafkaStatefulSetFile = clazz.getResourceAsStream(Paths.get("kubernetes", "kafka-statefulset.yaml").toString())) {
String serverUrl = getURL(serverIp, Integer.parseInt(serverPort));
KubernetesClient client = new KubernetesFactoryAdapter(serverUrl, namespace,
Util.fixEmpty(serverCertificate), Util.fixEmpty(credentialsId), skipTlsVerify
).createClient();
KubernetesQuery kubernetesQuery = new KubernetesQuery(client);
client.load(zookeeperFile).createOrReplace();
LOGGER.info("Starting Zookeeper");
client.load(kafkaServiceFile).createOrReplace();
LOGGER.info("Starting Kafka Services");
Integer zookeeperPort = kubernetesQuery.getFirstNodePortByServiceName("zookeeper-svc");
Integer kafkaPort = kubernetesQuery.getFirstNodePortByServiceName("kafka-svc");
if (zookeeperPort == null || kafkaPort == null)
throw new RemotingKafkaConfigurationException("Zookeeper NodePort or Kafka NodePort not found");
// Set Kafka advertised.listeners property
List<HasMetadata> kafkaStatefulSetResources = client.load(kafkaStatefulSetFile).get();
StatefulSet kafkaStatefulSet = (StatefulSet) kafkaStatefulSetResources
.stream()
.filter(res -> res.getKind().equals("StatefulSet") && res.getMetadata().getName().equals("kafka"))
.findFirst()
.orElseThrow(() -> new RemotingKafkaConfigurationException("Couldn't find StatefulSet named kafka in YAML configuration"));
Container kafkaContainer = kafkaStatefulSet
.getSpec()
.getTemplate()
.getSpec()
.getContainers()
.stream()
.filter(con -> con.getName().equals("kafka"))
.findFirst()
.orElseThrow(() -> new RemotingKafkaConfigurationException("Couldn't find Container named kafka in template specification"));
kafkaContainer.getEnv().add(new EnvVar(
"KAFKA_ADVERTISED_LISTENERS",
String.format("EXTERNAL://%s:%s", serverIp, kafkaPort),
null
));
client.resourceList(kafkaStatefulSetResources).createOrReplace();
LOGGER.info("Starting Kafka StatefulSet");
// Probe for Kafka readiness
while (true) {
try {
testConnection(serverIp, kafkaPort);
break;
} catch (IOException e) {
LOGGER.fine(String.format("Waiting for Kafka connection at %s:%s", serverIp, kafkaPort));
}
TimeUnit.SECONDS.sleep(1);
}
LOGGER.info("Zookeeper and Kafka started");
// Set Zookeeper and Broker URL
GlobalKafkaConfiguration.get().setZookeeperURL(getURL(serverIp, zookeeperPort));
GlobalKafkaConfiguration.get().setBrokerURL(getURL(serverIp, kafkaPort));
return FormValidation.ok(String.format("Success. Zookeeper: %s:%s and Kafka: %s:%s", serverIp, zookeeperPort, serverIp, kafkaPort));
} catch (RuntimeException | InterruptedException | RemotingKafkaException | IOException | GeneralSecurityException e) {
LOGGER.log(Level.SEVERE, "Error", e);
return FormValidation.error("Error: %s", e.getCause() == null
? e.getMessage()
: String.format("%s: %s", e.getCause().getClass().getName(), e.getCause().getMessage()));
}
}
@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
this.brokerURL = formData.getString("brokerURL");
this.zookeeperURL = formData.getString("zookeeperURL");
this.enableSSL = Boolean.valueOf(formData.getString("enableSSL"));
this.useKubernetes = Boolean.valueOf(formData.getString("useKubernetes"));
this.kubernetesIp = formData.getString("kubernetesIp");
this.kubernetesApiPort = formData.getString("kubernetesApiPort");
this.kubernetesCertificate = formData.getString("kubernetesCertificate");
this.kubernetesSkipTlsVerify = Boolean.valueOf(formData.getString("kubernetesSkipTlsVerify"));
this.kubernetesNamespace = formData.getString("kubernetesNamespace");
save();
return true;
}
private void testConnection(String host, int port) throws IOException {
new Socket(host, port);
}
@CheckForNull
private StandardUsernamePasswordCredentials getCredential(@Nonnull String id) {
StandardUsernamePasswordCredentials credential = null;
List<StandardUsernamePasswordCredentials> credentials = CredentialsProvider.lookupCredentials(
StandardUsernamePasswordCredentials.class, Jenkins.get(), ACL.SYSTEM, Collections.emptyList());
IdMatcher matcher = new IdMatcher(id);
for (StandardUsernamePasswordCredentials c : credentials) {
if (matcher.matches(c)) {
credential = c;
}
}
return credential;
}
@RequirePOST
public ListBoxModel doFillKafkaCredentialsIdItems(@AncestorInPath Item item, @QueryParameter String credentialsId) {
return fillCredentialsIdItems(item, credentialsId);
}
public FormValidation doCheckKafkaCredentialsId(@AncestorInPath Item item, @QueryParameter String value) {
FormValidation checkedResult = checkCredentialsId(item, value);
boolean isAdminOrNullItem = (item != null || Jenkins.get().hasPermission(Jenkins.ADMINISTER));
if (isAdminOrNullItem && checkedResult.equals(FormValidation.ok())) {
this.kafkaCredentialsId = value;
}
return checkedResult;
}
@RequirePOST
public ListBoxModel doFillSslTruststoreCredentialsIdItems(@AncestorInPath Item item, @QueryParameter String credentialsId) {
return fillCredentialsIdItems(item, credentialsId);
}
public FormValidation doCheckSslTruststoreCredentialsId(@AncestorInPath Item item, @QueryParameter String value) {
FormValidation checkedResult = checkCredentialsId(item, value);
boolean isAdminOrNullItem = (item != null || Jenkins.get().hasPermission(Jenkins.ADMINISTER));
if (isAdminOrNullItem && checkedResult.equals(FormValidation.ok())) {
this.sslTruststoreCredentialsId = value;
}
return checkedResult;
}
@RequirePOST
public ListBoxModel doFillSslKeystoreCredentialsIdItems(@AncestorInPath Item item, @QueryParameter String credentialsId) {
return fillCredentialsIdItems(item, credentialsId);
}
public FormValidation doCheckSslKeystoreCredentialsId(@AncestorInPath Item item, @QueryParameter String value) {
FormValidation checkedResult = checkCredentialsId(item, value);
boolean isAdminOrNullItem = (item != null || Jenkins.get().hasPermission(Jenkins.ADMINISTER));
if (isAdminOrNullItem && checkedResult.equals(FormValidation.ok())) {
this.sslKeystoreCredentialsId = value;
}
return checkedResult;
}
@RequirePOST
public ListBoxModel doFillSslKeyCredentialsIdItems(@AncestorInPath Item item, @QueryParameter String credentialsId) {
return fillCredentialsIdItems(item, credentialsId);
}
public FormValidation doCheckSslKeyCredentialsId(@AncestorInPath Item item, @QueryParameter String value) {
FormValidation checkedResult = checkCredentialsId(item, value);
boolean isAdminOrNullItem = (item != null || Jenkins.get().hasPermission(Jenkins.ADMINISTER));
if (isAdminOrNullItem && checkedResult.equals(FormValidation.ok())) {
this.sslKeyCredentialsId = value;
}
return checkedResult;
}
@RequirePOST
public ListBoxModel doFillKubernetesCredentialsIdItems() {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
return new StandardListBoxModel().withEmptySelection()
.withMatching(
CredentialsMatchers.anyOf(
CredentialsMatchers.instanceOf(StandardUsernamePasswordCredentials.class),
CredentialsMatchers.instanceOf(FileCredentials.class),
CredentialsMatchers.instanceOf(TokenProducer.class),
CredentialsMatchers.instanceOf(StandardCertificateCredentials.class),
CredentialsMatchers.instanceOf(StringCredentials.class)),
CredentialsProvider.lookupCredentials(StandardCredentials.class,
Jenkins.get(),
ACL.SYSTEM,
Collections.EMPTY_LIST
));
}
private FormValidation checkCredentialsId(@AncestorInPath Item item, @QueryParameter String value) {
if (item == null) {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return FormValidation.ok();
}
}
if (value.startsWith("${") && value.endsWith("}")) {
return FormValidation.warning("Cannot validate expression based credentials");
}
if (CredentialsProvider.listCredentials(
StandardUsernamePasswordCredentials.class,
Jenkins.get(),
ACL.SYSTEM,
Collections.singletonList(KAFKA_SCHEME),
CredentialsMatchers.withId(value)
).isEmpty()) {
return FormValidation.error("Cannot find currently selected credentials");
}
return FormValidation.ok();
}
private ListBoxModel fillCredentialsIdItems(@AncestorInPath Item item, @QueryParameter String credentialsId) {
StandardListBoxModel result = new StandardListBoxModel();
if (item == null) {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return result.includeCurrentValue(credentialsId);
}
}
return result
.includeMatchingAs(
ACL.SYSTEM,
Jenkins.get(),
StandardUsernamePasswordCredentials.class,
Collections.singletonList(KAFKA_SCHEME),
CredentialsMatchers.always()
)
.includeCurrentValue(credentialsId);
}
private static String getURL(String host, int port) {
String url = new URIBuilder()
.setHost(host)
.setPort(port)
.toString();
if (url.startsWith("//")) {
url = url.substring("//".length());
}
return url;
}
}