Skip to content

Commit

Permalink
Ensure always pickup the same port
Browse files Browse the repository at this point in the history
If the container ports are generated in a different order, the port is pickup in a different order. 

With this change, the logic to pick up the http to use is consistent.
  • Loading branch information
Sgitario authored and iocanel committed Jun 13, 2023
1 parent bcbdcb6 commit a57ed11
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions core/src/main/java/io/dekorate/utils/Ports.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -37,7 +37,7 @@

public class Ports {

private static final Map<String, Integer> HTTP_PORT_NAMES = Collections.unmodifiableMap(new HashMap<String, Integer>() {
private static final Map<String, Integer> HTTP_PORT_NAMES = Collections.unmodifiableMap(new LinkedHashMap<String, Integer>() {
{
put("http", 80);
put("https", 443);
Expand All @@ -46,14 +46,15 @@ public class Ports {
}
});

private static final Map<Integer, Integer> HTTP_PORT_NUMBERS = Collections.unmodifiableMap(new HashMap<Integer, Integer>() {
{
put(80, 80);
put(8080, 80);
put(443, 443);
put(8443, 443);
}
});
private static final Map<Integer, Integer> HTTP_PORT_NUMBERS = Collections
.unmodifiableMap(new LinkedHashMap<Integer, Integer>() {
{
put(80, 80);
put(8080, 80);
put(443, 443);
put(8443, 443);
}
});

public static final String DEFAULT_HTTP_PORT_PATH = "/";
public static final int MIN_PORT_NUMBER = 1;
Expand All @@ -62,7 +63,6 @@ public class Ports {
public static final int MAX_NODE_PORT_VALUE = 31999;

public static final Predicate<PortBuilder> PORT_PREDICATE = p -> HTTP_PORT_NAMES.containsKey(p.getName())
|| HTTP_PORT_NAMES.containsKey(p.getName())
|| HTTP_PORT_NUMBERS.containsKey(p.getContainerPort());

public static final Map<String, Integer> webPortNameMappings() {
Expand Down Expand Up @@ -134,17 +134,22 @@ public static Optional<ContainerPort> getHttpPort(ContainerFluent<?> container)
return Optional.of(container.getPorts().get(0));
}

//Check the service name
Optional<ContainerPort> port = container.getPorts().stream().filter(p -> HTTP_PORT_NAMES.containsKey(p.getName()))
.findFirst();
if (port.isPresent()) {
return port;
for (String portName : HTTP_PORT_NAMES.keySet()) {
Optional<ContainerPort> port = container.getPorts().stream().filter(p -> portName.equals(p.getName()))
.findFirst();
if (port.isPresent()) {
return port;
}
}

port = container.getPorts().stream().filter(p -> HTTP_PORT_NUMBERS.containsKey(p.getHostPort())).findFirst();
if (port.isPresent()) {
return port;
for (Integer portNumber : HTTP_PORT_NUMBERS.keySet()) {
Optional<ContainerPort> port = container.getPorts().stream().filter(p -> portNumber.equals(p.getHostPort()))
.findFirst();
if (port.isPresent()) {
return port;
}
}

return Optional.empty();
}

Expand Down Expand Up @@ -205,8 +210,6 @@ public static Integer calculateNodePort(String name, Port port) {
// If not hostPort exists, then we will return the containerPort to follow
// the same convention as kubernetes suggests
return getStablePortNumberInRange(name, MIN_NODE_PORT_VALUE, MAX_NODE_PORT_VALUE);
// return new PortBuilder(port).withHostPort(calculateHostPort(port))
// .withNodePort(stablePortNumberInRange).build();
}

/**
Expand Down

0 comments on commit a57ed11

Please sign in to comment.