Skip to content

Commit

Permalink
#2847 OpenShift route generated targetPort doesn't use the service "t…
Browse files Browse the repository at this point in the history
…argetPort"
  • Loading branch information
luigidemasi committed Mar 25, 2024
1 parent cfa80fc commit 9e901fd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -611,22 +611,38 @@ private String ensureProtocol(ServicePort port) {
return protocol;
}

public static Integer getPortToExpose(ServiceBuilder serviceBuilder) {
private static ServicePort getServicePortToExpose(ServiceBuilder serviceBuilder){
ServiceSpec spec = serviceBuilder.buildSpec();
if (spec != null) {
if (spec != null) {
final List<ServicePort> ports = spec.getPorts();
if (ports != null && !ports.isEmpty()) {
for (ServicePort port : ports) {
if (Objects.equals(port.getName(), "http") || Objects.equals(port.getProtocol(), "http") ) {
return port.getPort();
return port;
}
}
ServicePort servicePort = ports.iterator().next();
if (servicePort.getPort() != null) {
return servicePort.getPort();
return servicePort;
}
}
}
return null;
}

public static Integer getPortToExpose(ServiceBuilder serviceBuilder) {
ServicePort servicePort = getServicePortToExpose(serviceBuilder);
if (servicePort == null){
return null;
}
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 @@ -38,6 +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.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 @@ -122,7 +123,7 @@ private void addRoute(KubernetesListBuilder listBuilder, ServiceBuilder serviceB

private static RoutePort createRoutePort(ServiceBuilder serviceBuilder) {
RoutePort routePort = null;
final Integer servicePort = getPortToExpose(serviceBuilder);
final Integer servicePort = getTargetPortToExpose(serviceBuilder);
if (servicePort != null) {
routePort = new RoutePort();
routePort.setTargetPort(new IntOrString(servicePort));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void withFragmentWithMatchingName_shouldMergeRoute() {
.last()
.hasFieldOrPropertyWithValue("apiVersion", "route.openshift.io/v1")
.hasFieldOrPropertyWithValue("metadata.name", "http")
.hasFieldOrPropertyWithValue("spec.port.targetPort.value", 80)
.hasFieldOrPropertyWithValue("spec.port.targetPort.value", null)
.hasFieldOrPropertyWithValue("spec.to.kind", "Service")
.hasFieldOrPropertyWithValue("spec.to.name", "http")
.extracting("metadata.annotations")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void routeTargetPortFromServicePort() {
// Then
assertThat(route).isNotNull()
.extracting("metadata.name", "spec.host", "spec.to.kind", "spec.to.name", "spec.port.targetPort.intVal")
.contains("test-svc", "example.com", "Service", "test-svc", 80);
.contains("test-svc", "example.com", "Service", "test-svc", 8080);
}

@Test
Expand Down

0 comments on commit 9e901fd

Please sign in to comment.