-
Notifications
You must be signed in to change notification settings - Fork 445
/
DefaultServiceEnricher.java
618 lines (529 loc) · 23.5 KB
/
DefaultServiceEnricher.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.enricher.generic;
import io.fabric8.ianaservicehelper.Helper;
import io.fabric8.kubernetes.api.builder.TypedVisitor;
import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
import io.fabric8.kubernetes.api.model.ServiceFluent;
import io.fabric8.kubernetes.api.model.ServicePort;
import io.fabric8.kubernetes.api.model.ServicePortBuilder;
import io.fabric8.kubernetes.api.model.ServiceSpec;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.common.Configs;
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;
import org.eclipse.jkube.kit.common.util.SpringBootUtil;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.config.resource.PlatformMode;
import org.eclipse.jkube.kit.config.resource.ResourceConfig;
import org.eclipse.jkube.kit.config.resource.ServiceConfig;
import org.eclipse.jkube.kit.enricher.api.BaseEnricher;
import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext;
import org.eclipse.jkube.kit.common.util.KubernetesHelper;
import org.eclipse.jkube.kit.enricher.handler.HandlerHub;
import org.eclipse.jkube.kit.enricher.handler.ServiceHandler;
import java.util.Optional;
import java.util.Properties;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* An enricher for creating default services when not present.
*
* @author roland
*/
public class DefaultServiceEnricher extends BaseEnricher {
private static final Pattern PORT_PROTOCOL_PATTERN =
Pattern.compile("^(\\d+)(?:/(tcp|udp))?$", Pattern.CASE_INSENSITIVE);
private static final Pattern PORT_MAPPING_PATTERN =
Pattern.compile("^\\s*(?<port>\\d+)(\\s*:\\s*(?<targetPort>\\d+))?(\\s*/\\s*(?<protocol>(tcp|udp)))?\\s*$",
Pattern.CASE_INSENSITIVE);
private static final String PORT_IMAGE_LABEL_PREFIX = "jkube.generator.service.port";
private static final String PORTS_IMAGE_LABEL_PREFIX = "jkube.generator.service.ports";
/**
* Port mapping to refer for normalization of port numbering.
* Key -> TargetPort
* Value -> Port
*/
private static final Map<Integer, Integer> PORT_NORMALIZATION_MAPPING = new HashMap<>();
static {
PORT_NORMALIZATION_MAPPING.put(8080, 80);
PORT_NORMALIZATION_MAPPING.put(8081, 80);
PORT_NORMALIZATION_MAPPING.put(8181, 80);
PORT_NORMALIZATION_MAPPING.put(8180, 80);
PORT_NORMALIZATION_MAPPING.put(8443, 443);
PORT_NORMALIZATION_MAPPING.put(443, 443);
}
@AllArgsConstructor
private enum Config implements Configs.Config {
// Default name to use instead of a calculated one
NAME("name", null),
// Whether allow headless services.
HEADLESS("headless", Boolean.FALSE.toString()),
// Whether expose the service as ingress. Needs an 'exposeController'
// running
EXPOSE("expose", Boolean.FALSE.toString()),
// Type of the service (LoadBalancer, NodePort, ClusterIP, ...)
TYPE("type", null),
// Service port to use (only for the first exposed pod port)
PORT("port", null),
// Whether to expose multiple ports or only the first one
MULTI_PORT("multiPort", Boolean.FALSE.toString()),
// protocol to use
PROTOCOL("protocol", "tcp"),
// Whether to normalize services port numbers according to PORT_NORMALISATION_MAPPING
NORMALIZE_PORT("normalizePort", Boolean.FALSE.toString());
@Getter
protected String key;
@Getter
protected String defaultValue;
}
public DefaultServiceEnricher(JKubeEnricherContext buildContext) {
super(buildContext, "jkube-service");
}
@Override
public void create(PlatformMode platformMode, KubernetesListBuilder builder) {
final ResourceConfig xmlConfig = getConfiguration().getResource();
if (Optional.ofNullable(xmlConfig).map(ResourceConfig::getServices).map(c -> !c.isEmpty()).orElse(false)) {
// Add Services configured via XML
addServices(builder, xmlConfig.getServices());
} else {
final Service defaultService = getDefaultService();
if (hasServices(builder)) {
mergeInDefaultServiceParameters(builder, defaultService);
} else {
addDefaultService(builder, defaultService);
}
if (Configs.asBoolean(getConfig(Config.NORMALIZE_PORT))) {
normalizeServicePorts(builder);
}
}
}
private void normalizeServicePorts(KubernetesListBuilder builder) {
builder.accept(new TypedVisitor<ServicePortBuilder>() {
@Override
public void visit(ServicePortBuilder portBuilder) {
PORT_NORMALIZATION_MAPPING.keySet().forEach(key -> {
if (key.intValue() == portBuilder.buildTargetPort().getIntVal()) {
portBuilder.withPort(PORT_NORMALIZATION_MAPPING.get(key));
}
});
}
});
}
private void addServices(KubernetesListBuilder builder, List<ServiceConfig> services) {
HandlerHub handlerHub = new HandlerHub(getContext().getGav(), getContext().getProperties());
ServiceHandler serviceHandler = handlerHub.getServiceHandler();
builder.addToServiceItems(toArray(serviceHandler.getServices(services)));
}
// convert list to array, never returns null.
private Service[] toArray(List<Service> services) {
if (services == null) {
return new Service[0];
}
if (services instanceof ArrayList) {
return services.toArray(new Service[0]);
} else {
Service[] ret = new Service[services.size()];
for (int i = 0; i < services.size(); i++) {
ret[i] = services.get(i);
}
return ret;
}
}
private String getAppName() {
try {
if (getContext().getProjectClassLoaders().isClassInCompileClasspath(true)) {
Properties
properties = SpringBootUtil.getSpringBootApplicationProperties(getContext().getProjectClassLoaders().getCompileClassLoader());
return properties.getProperty("spring.application.name");
}
} catch (Exception ex) {
log.error("Error while reading the spring-boot configuration", ex);
}
return null;
}
private String getServiceName() {
String appName = getAppName();
if (appName != null) {
return appName;
} else {
return getConfig(Config.NAME, JKubeProjectUtil.createDefaultResourceName(getContext().getGav().getSanitizedArtifactId()));
}
}
private Service getDefaultService() {
// No image config, no service
if (!hasImageConfiguration()) {
return null;
}
String serviceName = getServiceName();
// Create service only for all images which are supposed to live in a single pod
List<ServicePort> ports = extractPorts(getImages());
ServiceBuilder builder = new ServiceBuilder()
.withNewMetadata()
.withName(serviceName)
.withLabels(extractLabels())
.endMetadata();
ServiceFluent.SpecNested<ServiceBuilder> specBuilder = builder.withNewSpec();
if (!ports.isEmpty()) {
specBuilder.withPorts(ports);
} else if (Configs.asBoolean(getConfig(Config.HEADLESS))) {
specBuilder.withClusterIP("None");
} else {
// No ports, no headless --> no service
return null;
}
specBuilder.withType(getConfig(Config.TYPE));
specBuilder.endSpec();
return builder.build();
}
private boolean hasServices(KubernetesListBuilder builder) {
final AtomicBoolean hasService = new AtomicBoolean(false);
builder.accept(new TypedVisitor<ServiceBuilder>() {
@Override
public void visit(ServiceBuilder element) {
hasService.set(true);
}
});
return hasService.get();
}
private void mergeInDefaultServiceParameters(KubernetesListBuilder builder, final Service defaultService) {
builder.accept(new TypedVisitor<ServiceBuilder>() {
@Override
public void visit(ServiceBuilder service) {
// Only update single service matching the default service's name
String defaultServiceName = getDefaultServiceName(defaultService);
ObjectMeta serviceMetadata = ensureServiceMetadata(service, defaultService);
String serviceName = ensureServiceName(serviceMetadata, service, defaultServiceName);
if (defaultService != null && defaultServiceName != null && defaultServiceName.equals(serviceName)) {
addMissingServiceParts(service, defaultService);
}
}
});
}
private void addDefaultService(KubernetesListBuilder builder, Service defaultService) {
if (defaultService == null) {
return;
}
ServiceSpec spec = defaultService.getSpec();
List<ServicePort> ports = spec.getPorts();
if (!ports.isEmpty()) {
log.info("Adding a default service '%s' with ports [%s]",
defaultService.getMetadata().getName(), formatPortsAsList(ports));
} else {
log.info("Adding headless default service '%s'",
defaultService.getMetadata().getName());
}
builder.addToServiceItems(defaultService);
}
// ....................................................................................
private Map<String, String> extractLabels() {
Map<String, String> labels = new HashMap<>();
if (Configs.asBoolean(getConfig(Config.EXPOSE))) {
labels.put("expose", "true");
}
return labels;
}
// ========================================================================================================
// Port handling
private List<ServicePort> extractPorts(List<ImageConfiguration> images) {
List<ServicePort> ret = new ArrayList<>();
boolean isMultiPort = Boolean.parseBoolean(getConfig(Config.MULTI_PORT));
List<ServicePort> configuredPorts = extractPortsFromConfig();
for (ImageConfiguration image : images) {
Map<String, String> labels = extractLabelsFromConfig(image);
List<String> podPorts = getPortsFromBuildConfiguration(image);
List<String> portsFromImageLabels = getLabelWithService(labels);
if (podPorts.isEmpty()) {
continue;
}
// Extract first port and remove first element
if(portsFromImageLabels == null || portsFromImageLabels.isEmpty()) {
addPortIfNotNull(ret, extractPortsFromImageSpec(image.getName(), podPorts.remove(0), shiftOrNull(configuredPorts), null));
} else {
for (String imageLabelPort : portsFromImageLabels) {
addPortIfNotNull(ret, extractPortsFromImageSpec(image.getName(), podPorts.remove(0), shiftOrNull(configuredPorts), imageLabelPort));
}
}
// Remaining port specs if multi-port is selected
if (isMultiPort) {
for (String port : podPorts) {
addPortIfNotNull(ret, extractPortsFromImageSpec(image.getName(), port, shiftOrNull(configuredPorts), null));
}
}
}
// If there are still ports configured add them directly
if (isMultiPort) {
ret.addAll(mirrorMissingTargetPorts(configuredPorts));
} else if (ret.isEmpty() && !configuredPorts.isEmpty()) {
ret.addAll(mirrorMissingTargetPorts(Collections.singletonList(configuredPorts.get(0))));
}
return ret;
}
private List<ServicePort> mirrorMissingTargetPorts(List<ServicePort> ports) {
List<ServicePort> ret = new ArrayList<>();
for (ServicePort port : ports) {
ret.add(updateMissingTargetPort(port, port.getPort()));
}
return ret;
}
// Examine images for build configuration and extract all ports
private List<String> getPortsFromBuildConfiguration(ImageConfiguration image) {
// No build, no default service (doesn't make much sense to have no build config, though)
BuildConfiguration buildConfig = image.getBuildConfiguration();
if (buildConfig == null) {
return Collections.emptyList();
}
return buildConfig.getPorts();
}
// Config can override ports
private List<ServicePort> extractPortsFromConfig() {
List<ServicePort> ret = new LinkedList<>();
String ports = getConfig(Config.PORT);
if (ports != null) {
for (String port : ports.split(",")) {
ret.add(parsePortMapping(port));
}
}
return ret;
}
private Map<String, String> extractLabelsFromConfig(ImageConfiguration imageConfiguration) {
Map<String, String> labels = new HashMap<>();
if(imageConfiguration.getBuildConfiguration() != null && imageConfiguration.getBuildConfiguration().getLabels() != null) {
labels.putAll(imageConfiguration.getBuildConfiguration().getLabels());
}
return labels;
}
private List<String> getLabelWithService(Map<String, String> labels) {
List<String> portsList = new ArrayList<>();
for(Map.Entry<String, String> entry : labels.entrySet()) {
if(entry.getKey().equals(PORT_IMAGE_LABEL_PREFIX)) {
portsList.add(entry.getValue());
} if(entry.getKey().equals(PORTS_IMAGE_LABEL_PREFIX)) {
portsList.addAll(Arrays.asList(entry.getValue().split(",")));
}
}
return portsList;
}
// parse config specified ports
private ServicePort parsePortMapping(String port) {
Matcher matcher = PORT_MAPPING_PATTERN.matcher(port);
if (!matcher.matches()) {
log.error("Invalid 'port' configuration '%s'. Must match <port>(:<targetPort>)?,<port2>?,...", port);
throw new IllegalArgumentException("Invalid port mapping specification " + port);
}
int servicePort = Integer.parseInt(matcher.group("port"));
String optionalTargetPort = matcher.group("targetPort");
String protocol = getProtocol(matcher.group("protocol"));
ServicePortBuilder builder = new ServicePortBuilder()
.withPort(servicePort)
.withProtocol(protocol)
.withName(getDefaultPortName(servicePort, protocol));
// leave empty if not set. will be filled up with the port from the image config
if (optionalTargetPort != null) {
builder.withNewTargetPort(Integer.parseInt(optionalTargetPort));
}
return builder.build();
}
// null ports can happen for ignored mappings
private void addPortIfNotNull(List<ServicePort> ret, ServicePort port) {
if (port != null) {
ret.add(port);
}
}
private ServicePort extractPortsFromImageSpec(String imageName, String portSpec, ServicePort portOverride, String targetPortFromImageLabel) {
Matcher portMatcher = PORT_PROTOCOL_PATTERN.matcher(portSpec);
if (!portMatcher.matches()) {
log.warn("Invalid port specification '%s' for image %s. Must match \\d+(/(tcp|udp))?. Ignoring for now for service generation",
portSpec, imageName);
return null;
}
Integer targetPort = Integer.parseInt(targetPortFromImageLabel != null ? targetPortFromImageLabel : portMatcher.group(1));
String protocol = getProtocol(portMatcher.group(2));
Integer port = checkForLegacyMapping(targetPort);
// With a port override you can override the detected ports
if (portOverride != null) {
return updateMissingTargetPort(portOverride, targetPort);
}
return new ServicePortBuilder()
.withPort(port)
.withNewTargetPort(targetPort)
.withProtocol(protocol)
.withName(getDefaultPortName(port, protocol))
.build();
}
private ServicePort updateMissingTargetPort(ServicePort port, Integer targetPort) {
if (port.getTargetPort() == null) {
return new ServicePortBuilder(port).withNewTargetPort(targetPort).build();
}
return port;
}
private int checkForLegacyMapping(int port) {
return port;
}
private String getProtocol(String imageProtocol) {
String protocol = imageProtocol != null ? imageProtocol : getConfig(Config.PROTOCOL);
if ("tcp".equalsIgnoreCase(protocol) || "udp".equalsIgnoreCase(protocol)) {
return protocol.toUpperCase();
} else {
throw new IllegalArgumentException(
String.format("Invalid service protocol %s specified for enricher '%s'. Must be 'tcp' or 'udp'",
protocol, getName()));
}
}
private String formatPortsAsList(List<ServicePort> ports) {
List<String> p = new ArrayList<>();
for (ServicePort port : ports) {
String targetPort = getPortValue(port.getTargetPort());
String servicePort= port.getPort() != null ? Integer.toString(port.getPort()) : targetPort;
p.add(targetPort.equals(servicePort) ? targetPort : servicePort + ":" + targetPort);
}
return String.join(",", p);
}
private String getPortValue(IntOrString port) {
String val = port.getStrVal();
if (val == null) {
val = Integer.toString(port.getIntVal());
}
return val;
}
private String getDefaultPortName(int port, String serviceProtocol) {
if ("TCP".equals(serviceProtocol)) {
switch (port) {
case 80:
case 8080:
case 9090:
return "http";
case 443:
case 8443:
return "https";
case 8778:
return "jolokia";
case 9779:
return "prometheus";
}
}
try {
Set<String> serviceNames = Helper.serviceNames(port, serviceProtocol.toLowerCase());
if (serviceNames != null && !serviceNames.isEmpty()) {
return serviceNames.iterator().next();
} else {
return null;
}
} catch (IOException e) {
log.warn("Cannot lookup port %d/%s in IANA database: %s", port, serviceProtocol.toLowerCase(), e.getMessage());
return null;
}
}
// remove first element of list or null if list is empty
private ServicePort shiftOrNull(List<ServicePort> ports) {
if (!ports.isEmpty()) {
return ports.remove(0);
}
return null;
}
// ==============================================================================================================
// Enhance existing services
// -------------------------
private String getDefaultServiceName(Service defaultService) {
String defaultServiceName = KubernetesHelper.getName(defaultService);
if (defaultServiceName != null && !defaultServiceName.isEmpty()) {
defaultServiceName = getContext().getGav().getSanitizedArtifactId();
}
return defaultServiceName;
}
// Merge services of same name with the default service
private void addMissingServiceParts(ServiceBuilder service, Service defaultService) {
final ServiceSpec originalSpec = service.buildSpec();
// If service has no spec -> take over the complete spec from default service
if (originalSpec == null) {
service.withNewSpecLike(defaultService.getSpec()).endSpec();
return;
}
final List<ServicePort> ports = service.buildSpec().getPorts();
if (ports == null || ports.isEmpty()) {
service.editSpec().withPorts(defaultService.getSpec().getPorts()).endSpec();
} else {
service.editSpec()
.withPorts(addMissingDefaultPorts(ports, defaultService))
.endSpec();
}
if (originalSpec.getType() == null) {
service.editSpec().withType(defaultService.getSpec().getType()).endSpec();
}
if (originalSpec.getClusterIP() == null) {
service.editSpec().withClusterIP(defaultService.getSpec().getClusterIP()).endSpec();
}
}
private String ensureServiceName(ObjectMeta serviceMetadata, ServiceBuilder service, String defaultServiceName) {
String serviceName = KubernetesHelper.getName(serviceMetadata);
if (serviceName != null && !serviceName.isEmpty()) {
service.buildMetadata().setName(defaultServiceName);
serviceName = KubernetesHelper.getName(service.buildMetadata());
}
return serviceName;
}
private ObjectMeta ensureServiceMetadata(ServiceBuilder service, Service defaultService) {
if (!service.hasMetadata() && defaultService != null) {
service.withNewMetadataLike(defaultService.getMetadata()).endMetadata();
}
return service.buildMetadata();
}
private List<ServicePort> addMissingDefaultPorts(List<ServicePort> ports, Service defaultService) {
// Ensure protocol and port names are set on the given ports
ensurePortProtocolAndName(ports);
// lets add at least one default port
return tryToFindAtLeastOnePort(ports, defaultService);
}
private void ensurePortProtocolAndName(List<ServicePort> ports) {
for (ServicePort port : ports) {
String protocol = ensureProtocol(port);
ensurePortName(port, protocol);
}
}
private List<ServicePort> tryToFindAtLeastOnePort(List<ServicePort> ports, Service defaultService) {
List<ServicePort> defaultPorts = defaultService.getSpec().getPorts();
if (!ports.isEmpty() || defaultPorts == null || defaultPorts.isEmpty()) {
return ports;
}
return Collections.singletonList(defaultPorts.get(0));
}
private void ensurePortName(ServicePort port, String protocol) {
if (port.getName() != null && !port.getName().isEmpty()) {
port.setName(getDefaultPortName(port.getPort(), getProtocol(protocol)));
}
}
private String ensureProtocol(ServicePort port) {
String protocol = port.getProtocol();
if (protocol != null && !protocol.isEmpty()) {
port.setProtocol("TCP");
return "TCP";
}
return protocol;
}
}