Skip to content

Commit

Permalink
Print messages about ports that can't change at runtime for K8s
Browse files Browse the repository at this point in the history
Related to #33307, task 3.
Fix #32882
  • Loading branch information
Sgitario committed Jun 2, 2023
1 parent 164d080 commit 78285bf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.stream.Collectors;

import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;

import io.dekorate.kubernetes.config.Annotation;
import io.dekorate.kubernetes.config.ConfigMapVolumeBuilder;
Expand Down Expand Up @@ -103,6 +104,10 @@

public class KubernetesCommonHelper {

private static final Map<String, PortProperty> PORTS_WITH_CONFIG = Map.of("http",
new PortProperty("quarkus.http.port", 8080),
"https", new PortProperty("quarkus.http.ssl-port", 8443),
"management", new PortProperty("quarkus.management.port", 9000));
private static final String ANY = null;
private static final String OUTPUT_ARTIFACT_FORMAT = "%s%s.jar";
private static final String[] PROMETHEUS_ANNOTATION_TARGETS = { "Service",
Expand All @@ -111,6 +116,7 @@ public class KubernetesCommonHelper {
private static final List<String> LIST_WITH_EMPTY = List.of("");
private static final String SCHEME_HTTP = "HTTP";
private static final String SCHEME_HTTPS = "HTTPS";
private static final Logger LOG = Logger.getLogger(KubernetesDeployer.class);

public static Optional<Project> createProject(ApplicationInfoBuildItem app,
Optional<CustomProjectRootBuildItem> customProjectRoot, OutputTargetBuildItem outputTarget,
Expand Down Expand Up @@ -186,20 +192,40 @@ public static Map<String, Port> combinePorts(List<KubernetesPortBuildItem> ports
: buildItemPort.getPath())
.build();

// Special handling for ports with name "https". We look up the container port from the Quarkus configuration.
if ("https".equals(name) && combinedPort.getContainerPort() == null) {
int containerPort = ConfigProvider.getConfig()
.getOptionalValue("quarkus.http.ssl-port", Integer.class)
.orElse(8443);

combinedPort = new PortBuilder(combinedPort).withContainerPort(containerPort).build();
// Special handling for ports with mapped configuration. We look up the container port from the Quarkus configuration.
if (combinedPort.getContainerPort() == null) {
for (Map.Entry<String, PortProperty> portWithConfig : PORTS_WITH_CONFIG.entrySet()) {
if (portWithConfig.getKey().equals(name)) {
combinedPort = new PortBuilder(combinedPort)
.withContainerPort(portWithConfig.getValue().getPortFromConfig())
.build();
break;
}
}
}

allPorts.put(name, combinedPort);
});
return allPorts;
}

/**
* Creates the configurator build items.
*/
public static void printMessageAboutPortsThatCantChange(String target, List<KubernetesPortBuildItem> ports,
PlatformConfiguration config) {
Collection<Port> allPorts = combinePorts(ports, config).values();
for (Port port : allPorts) {
for (Map.Entry<String, PortProperty> portWithConfig : PORTS_WITH_CONFIG.entrySet()) {
if (port.getName().equals(portWithConfig.getKey())) {
LOG.info(String.format("The target '%s' is mapping the container to the port '%s' which value is '%d'. "
+ "You won't be able to change it at runtime using the property '%s'.",
target, port.getName(), port.getContainerPort(), portWithConfig.getValue().propertyName));
}
}
}
}

/**
* Creates the common decorator build items.
*/
Expand Down Expand Up @@ -1085,4 +1111,18 @@ private static List<PolicyRule> toPolicyRulesList(Map<String, PolicyRuleConfig>
.build())
.collect(Collectors.toList());
}

private static class PortProperty {
public String propertyName;
public int defaultPort;

public PortProperty(String propertyName, int defaultPort) {
this.propertyName = propertyName;
this.defaultPort = defaultPort;
}

public int getPortFromConfig() {
return ConfigProvider.getConfig().getOptionalValue(propertyName, Integer.class).orElse(defaultPort);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static io.quarkus.kubernetes.deployment.Constants.READINESS_PROBE;
import static io.quarkus.kubernetes.deployment.Constants.ROUTE;
import static io.quarkus.kubernetes.deployment.Constants.STARTUP_PROBE;
import static io.quarkus.kubernetes.deployment.KubernetesCommonHelper.printMessageAboutPortsThatCantChange;
import static io.quarkus.kubernetes.deployment.KubernetesConfigUtil.MANAGEMENT_PORT_NAME;
import static io.quarkus.kubernetes.deployment.KubernetesConfigUtil.managementPortIsEnabled;
import static io.quarkus.kubernetes.deployment.OpenshiftConfig.OpenshiftFlavor.v3;
Expand Down Expand Up @@ -378,6 +379,9 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
|| !config.route.targetPort.equals(MANAGEMENT_PORT_NAME))) {
result.add(new DecoratorBuildItem(OPENSHIFT, new RemovePortFromServiceDecorator(name, MANAGEMENT_PORT_NAME)));
}

printMessageAboutPortsThatCantChange(OPENSHIFT, ports, config);

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static io.quarkus.kubernetes.deployment.Constants.LIVENESS_PROBE;
import static io.quarkus.kubernetes.deployment.Constants.READINESS_PROBE;
import static io.quarkus.kubernetes.deployment.Constants.STARTUP_PROBE;
import static io.quarkus.kubernetes.deployment.KubernetesCommonHelper.printMessageAboutPortsThatCantChange;
import static io.quarkus.kubernetes.deployment.KubernetesConfigUtil.MANAGEMENT_PORT_NAME;
import static io.quarkus.kubernetes.deployment.KubernetesConfigUtil.managementPortIsEnabled;
import static io.quarkus.kubernetes.spi.KubernetesDeploymentTargetBuildItem.VANILLA_KUBERNETES_PRIORITY;
Expand Down Expand Up @@ -290,6 +291,8 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
result.add(new DecoratorBuildItem(KUBERNETES, new RemovePortFromServiceDecorator(name, MANAGEMENT_PORT_NAME)));
}

printMessageAboutPortsThatCantChange(KUBERNETES, ports, config);

return result;
}

Expand Down

0 comments on commit 78285bf

Please sign in to comment.