Skip to content

Commit

Permalink
OpenShift route generator: prefer port name over port number for targ…
Browse files Browse the repository at this point in the history
…etPort when it's possible
  • Loading branch information
luigidemasi authored Aug 12, 2024
1 parent 21d0ece commit ad717ae
Show file tree
Hide file tree
Showing 43 changed files with 135 additions and 67 deletions.
2 changes: 1 addition & 1 deletion gradle-plugin/it/src/it/autotls/expected/openshift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ items:
name: autotls
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: autotls
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ items:
name: dockerfile-simple
spec:
port:
targetPort: 9080
targetPort: glrpc
to:
kind: Service
name: dockerfile-simple
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ items:
name: expose
spec:
port:
targetPort: 21
targetPort: ftp
to:
kind: Service
name: expose
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ items:
name: expose
spec:
port:
targetPort: 21
targetPort: ftp
to:
kind: Service
name: expose
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ items:
name: expose
spec:
port:
targetPort: 21
targetPort: ftp
to:
kind: Service
name: expose
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ items:
name: expose
spec:
port:
targetPort: 80
targetPort: http
to:
kind: Service
name: expose
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ items:
name: expose
spec:
port:
targetPort: 80
targetPort: http
to:
kind: Service
name: expose
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ items:
name: expose
spec:
port:
targetPort: 443
targetPort: https
to:
kind: Service
name: expose
Expand All @@ -256,7 +256,7 @@ items:
name: ssh
spec:
port:
targetPort: 22
targetPort: ssh
to:
kind: Service
name: ssh
2 changes: 1 addition & 1 deletion gradle-plugin/it/src/it/helidon/expected/openshift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ items:
name: helidon
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: helidon
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ items:
name: image-pull-secrets
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: image-pull-secrets
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ items:
name: initcontainers
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: initcontainers
2 changes: 1 addition & 1 deletion gradle-plugin/it/src/it/metadata/expected/openshift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ items:
name: metadata
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: metadata
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ items:
name: multiple-services
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: multiple-services
Expand Down
2 changes: 1 addition & 1 deletion gradle-plugin/it/src/it/openliberty/expected/openshift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ items:
name: openliberty
spec:
port:
targetPort: 9080
targetPort: glrpc
to:
kind: Service
name: openliberty
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ items:
name: probes-groovy-dsl-config
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: probes-groovy-dsl-config
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ items:
name: probes-groovy-dsl-config
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: probes-groovy-dsl-config
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ items:
name: probes-groovy-dsl-config
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: probes-groovy-dsl-config
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ items:
name: project-label
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: project-label
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ items:
name: project-label
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: project-label
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ items:
name: project-label
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: project-label
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ items:
name: project-label
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: project-label
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ items:
name: project-label
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: project-label
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ items:
name: customized-name
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: customized-name
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ items:
name: route
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: route
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ items:
name: customized-name
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: customized-name
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ items:
name: customized-name
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: customized-name
2 changes: 1 addition & 1 deletion gradle-plugin/it/src/it/service/expected/openshift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ items:
name: svc1
spec:
port:
targetPort: 8080
targetPort: port1
to:
kind: Service
name: svc1
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ items:
name: smallrye-health
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: smallrye-health
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ items:
name: spring-boot-managementhealthprobes
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: spring-boot-managementhealthprobes
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ items:
name: well-known-labels
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: well-known-labels
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ items:
name: well-known-labels
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: well-known-labels
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ items:
name: well-known-labels
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: well-known-labels
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ items:
name: well-known-labels
spec:
port:
targetPort: 8080
targetPort: http
to:
kind: Service
name: well-known-labels
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ private String ensureProtocol(ServicePort port) {
return protocol;
}

private static ServicePort getServicePortToExpose(ServiceBuilder serviceBuilder){
public static ServicePort getServicePortToExpose(ServiceBuilder serviceBuilder){
ServiceSpec spec = serviceBuilder.buildSpec();
if (spec != null) {
final List<ServicePort> ports = spec.getPorts();
Expand All @@ -637,12 +637,4 @@ public static Integer getPortToExpose(ServiceBuilder serviceBuilder) {
}
return servicePort.getPort();
}

public static Integer getTargetPortToExpose(ServiceBuilder serviceBuilder) {
ServicePort servicePort = getServicePortToExpose(serviceBuilder);
if (servicePort == null || servicePort.getTargetPort() == null){
return null;
}
return servicePort.getTargetPort().getIntVal();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jkube.enricher.generic.openshift;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.ServicePort;
import io.fabric8.openshift.api.model.RouteSpec;
import org.eclipse.jkube.kit.common.Configs;
import org.eclipse.jkube.kit.common.util.FileUtil;
Expand All @@ -37,8 +38,7 @@

import java.util.Objects;

import static org.eclipse.jkube.enricher.generic.DefaultServiceEnricher.getPortToExpose;
import static org.eclipse.jkube.enricher.generic.DefaultServiceEnricher.getTargetPortToExpose;
import static org.eclipse.jkube.enricher.generic.DefaultServiceEnricher.getServicePortToExpose;
import static org.eclipse.jkube.kit.enricher.api.util.KubernetesResourceUtil.mergeMetadata;
import static org.eclipse.jkube.kit.enricher.api.util.KubernetesResourceUtil.mergeSimpleFields;
import static org.eclipse.jkube.kit.enricher.api.util.KubernetesResourceUtil.removeItemFromKubernetesBuilder;
Expand Down Expand Up @@ -121,14 +121,23 @@ private void addRoute(KubernetesListBuilder listBuilder, ServiceBuilder serviceB
}
}

private static RoutePort createRoutePort(ServiceBuilder serviceBuilder) {
RoutePort routePort = null;
final Integer serviceTargetPort = getTargetPortToExpose(serviceBuilder);
final Integer servicePort = serviceTargetPort != null ? serviceTargetPort : getPortToExpose(serviceBuilder);
if (servicePort != null) {
routePort = new RoutePort();
routePort.setTargetPort(new IntOrString(servicePort));
private static RoutePort createRoutePort(ServicePort servicePortToExpose) {
RoutePort routePort = new RoutePort();
final String servicePortName = servicePortToExpose.getName();

if(servicePortName != null){
routePort.setTargetPort(new IntOrString(servicePortName));
return routePort;
}

final IntOrString serviceTargetPort = servicePortToExpose.getTargetPort();
if( serviceTargetPort != null && serviceTargetPort.getValue() != null){
routePort.setTargetPort(serviceTargetPort);
return routePort;
}

final Integer servicePort = servicePortToExpose.getPort();
routePort.setTargetPort(new IntOrString(servicePort));
return routePort;
}

Expand Down Expand Up @@ -224,8 +233,17 @@ private static void handleTlsTermination(RouteBuilder routeBuilder, String tlsTe
static Route createOpinionatedRouteFromService(ServiceBuilder serviceBuilder, String routeDomainPostfix, String tlsTermination, String edgeTerminationPolicy, boolean isRouteWithTls) {
ObjectMeta serviceMetadata = serviceBuilder.buildMetadata();
if (serviceMetadata != null) {
RoutePort routePort = null;
String name = serviceMetadata.getName();
RoutePort routePort = createRoutePort(serviceBuilder);
final ServicePort servicePortToExpose = getServicePortToExpose(serviceBuilder);

if(servicePortToExpose != null && (
servicePortToExpose.getName() != null ||
servicePortToExpose.getPort() != null ||
servicePortToExpose.getTargetPort() != null)){
routePort = createRoutePort(servicePortToExpose);
}

if (routePort != null) {
RouteBuilder routeBuilder = new RouteBuilder().
withMetadata(serviceMetadata).
Expand Down
Loading

0 comments on commit ad717ae

Please sign in to comment.