Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure always pickup the same port #1210

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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