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 8, 2023
1 parent 0db1710 commit fed4bc4
Showing 4 changed files with 110 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -24,8 +24,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.microprofile.config.ConfigProvider;

import io.dekorate.kubernetes.config.Annotation;
import io.dekorate.kubernetes.config.ConfigMapVolumeBuilder;
import io.dekorate.kubernetes.config.EnvBuilder;
@@ -102,7 +100,6 @@
import io.quarkus.kubernetes.spi.Subject;

public class KubernetesCommonHelper {

private static final String ANY = null;
private static final String OUTPUT_ARTIFACT_FORMAT = "%s%s.jar";
private static final String[] PROMETHEUS_ANNOTATION_TARGETS = { "Service",
@@ -186,20 +183,32 @@ 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) {
Integer containerPort = RuntimePropertiesUtil.getPortNumberFromRuntime(name);
if (containerPort != null) {
combinedPort = new PortBuilder(combinedPort)
.withContainerPort(containerPort)
.build();
}
}

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) {
RuntimePropertiesUtil.printTraceIfRuntimePropertyIsSet(target, port);
}
}

/**
* Creates the common decorator build items.
*/
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.quarkus.kubernetes.deployment;

import java.util.Map;
import java.util.Optional;

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

import io.dekorate.kubernetes.config.Port;

/**
* Utility to handling runtime properties.
*/
public final class RuntimePropertiesUtil {

private static final Logger LOG = Logger.getLogger(KubernetesDeployer.class);

private static final String CONTAINER_PORT = "the container port";
/**
* List of ports that are linked to a runtime property.
*/
private static final Map<String, RuntimeProperty> PORTS = Map.of(
"http", new RuntimeProperty<>("quarkus.http.port", Integer.class, 8080, CONTAINER_PORT),
"https", new RuntimeProperty<>("quarkus.http.ssl-port", Integer.class, 8443, CONTAINER_PORT),
"management", new RuntimeProperty<>("quarkus.management.port", Integer.class, 9000, CONTAINER_PORT));

private RuntimePropertiesUtil() {

}

/**
* This method will return the port number that is configured from the runtime property. If the runtime property does not
* return any value, it will return the default value that is defined in the {@link RuntimePropertiesUtil#PORTS} map.
*/
public static Integer getPortNumberFromRuntime(String name) {
RuntimeProperty runtimeProperty = PORTS.get(name);
if (runtimeProperty != null) {
return (Integer) runtimeProperty.getValue().orElse(runtimeProperty.defaultValue);
}

return null;
}

/**
* This method will trace an informative message to let users know that runtime properties that are used in the generated
* resources by Kubernetes can't change again at runtime.
*
* For example, for users that set a runtime property "quarkus.http.port=9000" at build time, Kubernetes will use this value
* in the generated resources. Then, when running the application in Kubernetes, if users try to modify again the
* runtime property "quarkus.http.port" to a different value, this won't work because the generated resources already took
* the 9000 value.
*
* Note that this message won't be printed if the users didn't provide the runtime property at build time.
*/
public static void printTraceIfRuntimePropertyIsSet(String target, Port port) {
RuntimeProperty runtimeProperty = PORTS.get(port.getName());
if (runtimeProperty != null) {
var runtimePropertyValue = runtimeProperty.getValue();
if (runtimePropertyValue.isPresent()) {
LOG.info(String.format("The '%s' manifests are generated with %s '%s' having value '%d'. "
+ "The app and manifests will get out of sync if the property '%s' is changed at runtime.",
target, runtimeProperty.usage, port.getName(), port.getContainerPort(), runtimeProperty.propertyName));
}
}
}

private static class RuntimeProperty<T> {
private final String propertyName;
private final Class<T> clazzOfValue;
private final T defaultValue;
private final String usage;

public RuntimeProperty(String propertyName, Class<T> clazzOfValue, T defaultValue, String usage) {
this.propertyName = propertyName;
this.clazzOfValue = clazzOfValue;
this.defaultValue = defaultValue;
this.usage = usage;
}

public Optional<T> getValue() {
return ConfigProvider.getConfig().getOptionalValue(propertyName, clazzOfValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
}

0 comments on commit fed4bc4

Please sign in to comment.