From 1da5a0bfde1fd1eebda4395112a394abec8b87a6 Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 8 Jun 2023 13:06:51 +0200 Subject: [PATCH 1/2] Print messages about ports that can't change at runtime for K8s Related to https://github.com/quarkusio/quarkus/issues/33307, task 3. Fix https://github.com/quarkusio/quarkus/issues/32882 --- .../deployment/KubernetesCommonHelper.java | 29 ++++--- .../deployment/OpenshiftProcessor.java | 4 + .../deployment/RuntimePropertiesUtil.java | 84 +++++++++++++++++++ .../VanillaKubernetesProcessor.java | 5 +- 4 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RuntimePropertiesUtil.java diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesCommonHelper.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesCommonHelper.java index dadabe7723868..aa827427b2566 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesCommonHelper.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesCommonHelper.java @@ -25,8 +25,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; @@ -103,7 +101,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", @@ -187,13 +184,14 @@ public static Map combinePorts(List 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); @@ -201,6 +199,17 @@ public static Map combinePorts(List ports return allPorts; } + /** + * Creates the configurator build items. + */ + public static void printMessageAboutPortsThatCantChange(String target, List ports, + PlatformConfiguration config) { + Collection allPorts = combinePorts(ports, config).values(); + for (Port port : allPorts) { + RuntimePropertiesUtil.printTraceIfRuntimePropertyIsSet(target, port); + } + } + /** * Creates the common decorator build items. */ diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java index ab3f4f5a3fb7d..386ef5302527a 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java @@ -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; @@ -375,6 +376,9 @@ public List 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; } diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RuntimePropertiesUtil.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RuntimePropertiesUtil.java new file mode 100644 index 0000000000000..a559a2492e76d --- /dev/null +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RuntimePropertiesUtil.java @@ -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 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 { + private final String propertyName; + private final Class clazzOfValue; + private final T defaultValue; + private final String usage; + + public RuntimeProperty(String propertyName, Class clazzOfValue, T defaultValue, String usage) { + this.propertyName = propertyName; + this.clazzOfValue = clazzOfValue; + this.defaultValue = defaultValue; + this.usage = usage; + } + + public Optional getValue() { + return ConfigProvider.getConfig().getOptionalValue(propertyName, clazzOfValue); + } + } +} diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/VanillaKubernetesProcessor.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/VanillaKubernetesProcessor.java index fa1f8e9e38dc6..74e197ac406f0 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/VanillaKubernetesProcessor.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/VanillaKubernetesProcessor.java @@ -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; @@ -298,7 +299,7 @@ public List createDecorators(ApplicationInfoBuildItem applic .withMaxUnavailable(config.rollingUpdate.maxUnavailable) .build()))); } - + printMessageAboutPortsThatCantChange(KUBERNETES, ports, config); return result; } @@ -320,4 +321,4 @@ void externalizeInitTasks( jobs, initContainers, env, roles, roleBindings, decorators); } } -} \ No newline at end of file +} From de116aad7adc21bc2a450a68bf0d69b07ca45535 Mon Sep 17 00:00:00 2001 From: Ioannis Canellos Date: Fri, 16 Jun 2023 11:08:37 +0300 Subject: [PATCH 2/2] refactor: port mgmt from kube to ext publishing the build items --- .../grpc/deployment/GrpcServerProcessor.java | 4 +- .../spi/KubernetesPortBuildItem.java | 38 ++++++++- .../io/quarkus/kubernetes/spi/Property.java | 46 ++++++++++ .../deployment/KubernetesCommonHelper.java | 56 ++++++++----- .../deployment/OpenshiftProcessor.java | 1 - .../kubernetes/deployment/PropertyUtil.java | 50 +++++++++++ .../deployment/RuntimePropertiesUtil.java | 84 ------------------- .../http/deployment/VertxHttpProcessor.java | 20 ++--- 8 files changed, 173 insertions(+), 126 deletions(-) create mode 100644 extensions/kubernetes/spi/src/main/java/io/quarkus/kubernetes/spi/Property.java create mode 100644 extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/PropertyUtil.java delete mode 100644 extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RuntimePropertiesUtil.java diff --git a/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcServerProcessor.java b/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcServerProcessor.java index 2b7888e326cc7..213d6fd096359 100644 --- a/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcServerProcessor.java +++ b/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcServerProcessor.java @@ -505,9 +505,7 @@ KubernetesPortBuildItem registerGrpcServiceInKubernetes(List> source; public KubernetesPortBuildItem(int port, Feature feature) { - this(port, feature.getName()); + this(port, feature.getName(), true, Optional.empty()); } public KubernetesPortBuildItem(int port, String name) { + this(port, name, true, Optional.empty()); + } + + public KubernetesPortBuildItem(int port, String name, boolean enabled, Optional> source) { this.port = port; this.name = name; + this.source = source; + this.enabled = enabled; + } + + public static KubernetesPortBuildItem fromRuntimeConfiguration(String name, String propertyName, Integer defaultValue, + boolean enabled) { + Property origin = Property.fromRuntimeConfiguration(propertyName, Integer.class, defaultValue); + Integer port = origin.getValue().orElse(defaultValue); + return new KubernetesPortBuildItem(port, name, enabled, Optional.of(origin)); + } + + public static KubernetesPortBuildItem fromRuntimeConfiguration(String name, String propertyName, Integer defaultValue) { + Property origin = Property.fromRuntimeConfiguration(propertyName, Integer.class, defaultValue); + Integer port = origin.getValue().orElse(defaultValue); + return new KubernetesPortBuildItem(port, name, origin.getValue().isPresent(), Optional.of(origin)); } public int getPort() { @@ -24,4 +52,12 @@ public int getPort() { public String getName() { return name; } + + public boolean isEnabled() { + return enabled; + } + + public Optional> getSource() { + return source; + } } diff --git a/extensions/kubernetes/spi/src/main/java/io/quarkus/kubernetes/spi/Property.java b/extensions/kubernetes/spi/src/main/java/io/quarkus/kubernetes/spi/Property.java new file mode 100644 index 0000000000000..f9418bd21736e --- /dev/null +++ b/extensions/kubernetes/spi/src/main/java/io/quarkus/kubernetes/spi/Property.java @@ -0,0 +1,46 @@ +package io.quarkus.kubernetes.spi; + +import java.util.Optional; + +import org.eclipse.microprofile.config.ConfigProvider; + +public class Property { + + private final String name; + private final Class type; + private final Optional value; + private final T defaultValue; + private final boolean runtime; + + public Property(String name, Class type, Optional value, T defaultValue, boolean runtime) { + this.name = name; + this.type = type; + this.value = value; + this.defaultValue = defaultValue; + this.runtime = runtime; + } + + public static Property fromRuntimeConfiguration(String name, Class type, T defaultValue) { + return new Property(name, type, ConfigProvider.getConfig().getOptionalValue(name, type), defaultValue, true); + } + + public String getName() { + return name; + } + + public Class getType() { + return type; + } + + public Optional getValue() { + return value; + } + + public T getDefaultValue() { + return defaultValue; + } + + public boolean isRuntime() { + return runtime; + } +} diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesCommonHelper.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesCommonHelper.java index aa827427b2566..5e3204620405d 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesCommonHelper.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesCommonHelper.java @@ -22,7 +22,9 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.OptionalInt; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import io.dekorate.kubernetes.config.Annotation; @@ -97,6 +99,7 @@ import io.quarkus.kubernetes.spi.KubernetesRoleBindingBuildItem; import io.quarkus.kubernetes.spi.KubernetesRoleBuildItem; import io.quarkus.kubernetes.spi.KubernetesServiceAccountBuildItem; +import io.quarkus.kubernetes.spi.Property; import io.quarkus.kubernetes.spi.RoleRef; import io.quarkus.kubernetes.spi.Subject; @@ -161,9 +164,16 @@ public static Optional getPort(List ports, Platfo public static Map combinePorts(List ports, PlatformConfiguration config) { Map allPorts = new HashMap<>(); - allPorts.putAll(verifyPorts(ports).entrySet().stream() + Map activePorts = new HashMap<>(); + + allPorts.putAll(ports.stream() + .map(p -> new PortBuilder().withName(p.getName()).withContainerPort(p.getPort()).build()) + .collect(Collectors.toMap(Port::getName, Function.identity(), (first, second) -> first))); //prevent dublicate keys + + activePorts.putAll(verifyPorts(ports) + .entrySet().stream() .map(e -> new PortBuilder().withName(e.getKey()).withContainerPort(e.getValue()).build()) - .collect(Collectors.toMap(Port::getName, p -> p))); + .collect(Collectors.toMap(Port::getName, Function.identity(), (first, second) -> first))); //prevent dublicate keys config.getPorts().entrySet().forEach(e -> { String name = e.getKey(); @@ -183,31 +193,30 @@ public static Map combinePorts(List ports .withPath(Strings.isNotNullOrEmpty(configuredPort.getPath()) ? configuredPort.getPath() : buildItemPort.getPath()) .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); + activePorts.put(name, combinedPort); }); - return allPorts; + return activePorts; } /** * Creates the configurator build items. */ public static void printMessageAboutPortsThatCantChange(String target, List ports, - PlatformConfiguration config) { - Collection allPorts = combinePorts(ports, config).values(); - for (Port port : allPorts) { - RuntimePropertiesUtil.printTraceIfRuntimePropertyIsSet(target, port); - } + PlatformConfiguration configuration) { + ports.stream().forEach(port -> { + boolean enabled = port.isEnabled() || configuration.getPorts().containsKey(port.getName()); + if (enabled) { + String name = "quarkus." + target + ".ports." + port.getName() + ".container-port"; + Optional value = Optional.ofNullable(configuration.getPorts().get(port.getName())) + .map(p -> p.containerPort) + .filter(OptionalInt::isPresent) + .map(OptionalInt::getAsInt); + Property kubernetesPortProperty = new Property(name, Integer.class, value, null, false); + PropertyUtil.printMessages(String.format("The container port %s", port.getName()), target, + kubernetesPortProperty, + port.getSource()); + } + }); } /** @@ -1078,15 +1087,18 @@ private static Map verifyPorts(List ku final Map result = new HashMap<>(); final Set usedPorts = new HashSet<>(); for (KubernetesPortBuildItem entry : kubernetesPortBuildItems) { + if (!entry.isEnabled()) { + continue; + } final String name = entry.getName(); if (result.containsKey(name)) { throw new IllegalArgumentException( - "All Kubernetes ports must have unique names - " + name + "has been used multiple times"); + "All Kubernetes ports must have unique names - " + name + " has been used multiple times"); } final Integer port = entry.getPort(); if (usedPorts.contains(port)) { throw new IllegalArgumentException( - "All Kubernetes ports must be unique - " + port + "has been used multiple times"); + "All Kubernetes ports must be unique - " + port + " has been used multiple times"); } result.put(name, port); usedPorts.add(port); diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java index 386ef5302527a..2f46a5ddb652f 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java @@ -378,7 +378,6 @@ public List createDecorators(ApplicationInfoBuildItem applic } printMessageAboutPortsThatCantChange(OPENSHIFT, ports, config); - return result; } diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/PropertyUtil.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/PropertyUtil.java new file mode 100644 index 0000000000000..699a5dbc08d02 --- /dev/null +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/PropertyUtil.java @@ -0,0 +1,50 @@ +package io.quarkus.kubernetes.deployment; + +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; + +import org.jboss.logging.Logger; + +import io.quarkus.kubernetes.spi.Property; + +public class PropertyUtil { + + private static final Set VISITED_EXTENSION_PROPERTIES = new HashSet<>(); + private static final Logger LOG = Logger.getLogger(PropertyUtil.class); + + public static void printMessages(String usage, String platform, Property kubernetesProperty, + Optional> extensionProperty) { + extensionProperty.ifPresent(p -> { + printMessages(usage, platform, kubernetesProperty, p); + }); + } + + public static void printMessages(String usage, String platform, Property kubernetesProperty, + Property extensionProperty) { + if (!VISITED_EXTENSION_PROPERTIES.add(extensionProperty.getName())) { + return; + } + + String platformCapitalized = platform.substring(0, 1).toUpperCase() + platform.substring(1); + T kubernetesValue = kubernetesProperty.getValue().orElse(null); + if (kubernetesValue == null) { + // If no kubernetes property is provided, this will be used instead. + String defaultOrProvided = extensionProperty.getValue().isPresent() ? "provided" : "default"; + String stringValue = String.valueOf(extensionProperty.getValue().orElse(extensionProperty.getDefaultValue())); + LOG.infof("%s manifests are generated with '%s' having %s value '%s'. " + + "The app and manifests will get out of sync if the property '%s' is changed at runtime.", + platformCapitalized, usage, defaultOrProvided, stringValue, extensionProperty.getName()); + + } else if (extensionProperty.getValue().filter(v -> !v.equals(kubernetesValue)).isPresent()) { + // We have conflicting properties that need to be aligned. Maybe warn? + String runtimeOrBuildTime = extensionProperty.isRuntime() ? "runtime" : "buildtime"; + LOG.debugf( + "%s property '%s' has been set with value '%s' while %s property '%s' is set with '%s'. %s will be set using the former.", + platformCapitalized, kubernetesProperty.getName(), kubernetesProperty.getValue().get(), runtimeOrBuildTime, + extensionProperty.getName(), extensionProperty.getValue().get(), usage); + } else { + // Both proeprties are present and aligned. + } + } +} diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RuntimePropertiesUtil.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RuntimePropertiesUtil.java deleted file mode 100644 index a559a2492e76d..0000000000000 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RuntimePropertiesUtil.java +++ /dev/null @@ -1,84 +0,0 @@ -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 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 { - private final String propertyName; - private final Class clazzOfValue; - private final T defaultValue; - private final String usage; - - public RuntimeProperty(String propertyName, Class clazzOfValue, T defaultValue, String usage) { - this.propertyName = propertyName; - this.clazzOfValue = clazzOfValue; - this.defaultValue = defaultValue; - this.usage = usage; - } - - public Optional getValue() { - return ConfigProvider.getConfig().getOptionalValue(propertyName, clazzOfValue); - } - } -} diff --git a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/VertxHttpProcessor.java b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/VertxHttpProcessor.java index 8f7cdb6ce1495..fd8ec2b3ce36e 100644 --- a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/VertxHttpProcessor.java +++ b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/VertxHttpProcessor.java @@ -174,26 +174,16 @@ void filterMultipleVertxInstancesWarning(LaunchModeBuildItem launchModeBuildItem @BuildStep public void kubernetes(BuildProducer kubernetesPorts) { - if (isSslConfigured()) { - // ssl is not disabled - int sslPort = ConfigProvider.getConfig() - .getOptionalValue("quarkus.http.ssl-port", Integer.class) - .orElse(8443); - kubernetesPorts.produce(new KubernetesPortBuildItem(sslPort, "https")); - } - - int port = ConfigProvider.getConfig().getOptionalValue("quarkus.http.port", Integer.class).orElse(8080); - kubernetesPorts.produce(new KubernetesPortBuildItem(port, "http")); + kubernetesPorts.produce(KubernetesPortBuildItem.fromRuntimeConfiguration("http", "quarkus.http.port", 8080, true)); + kubernetesPorts.produce( + KubernetesPortBuildItem.fromRuntimeConfiguration("https", "quarkus.http.ssl-port", 8443, isSslConfigured())); } @BuildStep public KubernetesPortBuildItem kubernetesForManagement( ManagementInterfaceBuildTimeConfig managementInterfaceBuildTimeConfig) { - if (managementInterfaceBuildTimeConfig.enabled) { - int port = ConfigProvider.getConfig().getOptionalValue("quarkus.management.port", Integer.class).orElse(9000); - return new KubernetesPortBuildItem(port, "management"); - } - return null; + return KubernetesPortBuildItem.fromRuntimeConfiguration("management", "quarkus.management.port", 9000, + managementInterfaceBuildTimeConfig.enabled); } @BuildStep