diff --git a/extensions/kubernetes/kind/deployment/src/main/java/io/quarkus/kind/deployment/KindProcessor.java b/extensions/kubernetes/kind/deployment/src/main/java/io/quarkus/kind/deployment/KindProcessor.java index 94a42ab4c9c5a..6d55a769a0b27 100644 --- a/extensions/kubernetes/kind/deployment/src/main/java/io/quarkus/kind/deployment/KindProcessor.java +++ b/extensions/kubernetes/kind/deployment/src/main/java/io/quarkus/kind/deployment/KindProcessor.java @@ -89,7 +89,7 @@ public void createLabels(KubernetesConfig config, BuildProducer createConfigurators(KubernetesConfig config, List ports) { List result = new ArrayList<>(); - KubernetesCommonHelper.combinePorts(ports, config).values() + KubernetesCommonHelper.combinePorts(ports, config.getPorts()).values() .forEach(value -> result.add(new ConfiguratorBuildItem(new AddPortToKubernetesConfig(value)))); return result; } diff --git a/extensions/kubernetes/minikube/deployment/src/main/java/io/quarkus/minikube/deployment/MinikubeProcessor.java b/extensions/kubernetes/minikube/deployment/src/main/java/io/quarkus/minikube/deployment/MinikubeProcessor.java index 96343e7f69757..99ff976ce04b7 100644 --- a/extensions/kubernetes/minikube/deployment/src/main/java/io/quarkus/minikube/deployment/MinikubeProcessor.java +++ b/extensions/kubernetes/minikube/deployment/src/main/java/io/quarkus/minikube/deployment/MinikubeProcessor.java @@ -84,7 +84,7 @@ public void createLabels(KubernetesConfig config, BuildProducer createConfigurators(KubernetesConfig config, List ports) { List result = new ArrayList<>(); - KubernetesCommonHelper.combinePorts(ports, config).values().forEach(value -> { + KubernetesCommonHelper.combinePorts(ports, config.getPorts()).values().forEach(value -> { result.add(new ConfiguratorBuildItem(new AddPortToKubernetesConfig(value))); }); return result; diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KnativeProcessor.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KnativeProcessor.java index 190aa39d4aab2..03f56005eb4ea 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KnativeProcessor.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KnativeProcessor.java @@ -4,10 +4,12 @@ import static io.quarkus.kubernetes.deployment.Constants.KNATIVE_SERVICE; import static io.quarkus.kubernetes.deployment.Constants.KNATIVE_SERVICE_GROUP; import static io.quarkus.kubernetes.deployment.Constants.KNATIVE_SERVICE_VERSION; +import static io.quarkus.kubernetes.deployment.KubernetesConfigUtil.defaultMapIfEmpty; import static io.quarkus.kubernetes.spi.KubernetesDeploymentTargetBuildItem.DEFAULT_PRIORITY; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Stream; @@ -102,25 +104,39 @@ public void checkKnative(ApplicationInfoBuildItem applicationInfo, KnativeConfig } @BuildStep - public void createAnnotations(KnativeConfig config, BuildProducer annotations) { - config.getAnnotations().forEach((k, v) -> { + public void createAnnotations(KnativeConfig config, + KubernetesConfig kubernetesConfig, + BuildProducer annotations) { + Map annotationsFromConfig = config.getAnnotations(); + if (annotationsFromConfig.isEmpty()) { + annotationsFromConfig = kubernetesConfig.getAnnotations(); + } + annotationsFromConfig.forEach((k, v) -> { annotations.produce(new KubernetesAnnotationBuildItem(k, v, KNATIVE)); }); } @BuildStep - public void createLabels(KnativeConfig config, BuildProducer labels, + public void createLabels(KnativeConfig config, + KubernetesConfig kubernetesConfig, + BuildProducer labels, BuildProducer imageLabels) { - config.getLabels().forEach((k, v) -> { + Map labelsFromConfig = config.getLabels(); + if (labelsFromConfig.isEmpty()) { + labelsFromConfig = kubernetesConfig.getLabels(); + } + labelsFromConfig.forEach((k, v) -> { labels.produce(new KubernetesLabelBuildItem(k, v, KNATIVE)); imageLabels.produce(new ContainerImageLabelBuildItem(k, v)); }); } @BuildStep - public List createConfigurators(KnativeConfig config, List ports) { + public List createConfigurators(KnativeConfig config, + KubernetesConfig kubernetesConfig, + List ports) { List result = new ArrayList<>(); - KubernetesCommonHelper.combinePorts(ports, config).values() + KubernetesCommonHelper.combinePorts(ports, defaultMapIfEmpty(config.getPorts(), kubernetesConfig.getPorts())).values() .stream() // At the moment, Knative only supports single port binding: https://github.com/knative/serving/issues/8471 .filter(p -> p.getName().equals("http")) @@ -133,6 +149,7 @@ public List createConfigurators(KnativeConfig config, Lis public List createDecorators(ApplicationInfoBuildItem applicationInfo, OutputTargetBuildItem outputTarget, KnativeConfig config, + KubernetesConfig kubernetesConfig, PackageConfig packageConfig, Optional metricsConfiguration, Optional kubernetesClientConfiguration, @@ -162,7 +179,9 @@ public List createDecorators(ApplicationInfoBuildItem applic String name = ResourceNameUtil.getResourceName(config, applicationInfo); Optional project = KubernetesCommonHelper.createProject(applicationInfo, customProjectRoot, outputTarget, packageConfig); - Optional port = KubernetesCommonHelper.getPort(ports, config, "http"); + Optional port = KubernetesCommonHelper.getPort(ports, + defaultMapIfEmpty(config.getPorts(), kubernetesConfig.getPorts()), + "http"); result.addAll(KubernetesCommonHelper.createDecorators(project, KNATIVE, name, config, metricsConfiguration, kubernetesClientConfiguration, annotations, labels, command, port, livenessPath, readinessPath, startupProbePath, 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..764ce2629227d 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 @@ -145,14 +145,15 @@ public static Optional createProject(ApplicationInfoBuildItem app, * Creates the configurator build items. */ public static Optional getPort(List ports, KubernetesConfig config) { - return getPort(ports, config, config.ingress.targetPort); + return getPort(ports, config.getPorts(), config.ingress.targetPort); } /** * Creates the configurator build items. */ - public static Optional getPort(List ports, PlatformConfiguration config, String targetPort) { - return combinePorts(ports, config).values().stream() + public static Optional getPort(List ports, Map portsFromConfig, + String targetPort) { + return combinePorts(ports, portsFromConfig).values().stream() .filter(distinct(p -> p.getName())) .filter(p -> p.getName().equals(targetPort)) .findFirst(); @@ -162,13 +163,13 @@ public static Optional getPort(List ports, Platfo * Creates the configurator build items. */ public static Map combinePorts(List ports, - PlatformConfiguration config) { + Map portsFromConfig) { Map allPorts = new HashMap<>(); allPorts.putAll(verifyPorts(ports).entrySet().stream() .map(e -> new PortBuilder().withName(e.getKey()).withContainerPort(e.getValue()).build()) .collect(Collectors.toMap(Port::getName, p -> p))); - config.getPorts().entrySet().forEach(e -> { + portsFromConfig.entrySet().forEach(e -> { String name = e.getKey(); Port configuredPort = PortConverter.convert(e); Port buildItemPort = allPorts.get(name); diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfigUtil.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfigUtil.java index 876d5bea25aef..424e9a18b1346 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfigUtil.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfigUtil.java @@ -124,6 +124,10 @@ public static Map toMap(PlatformConfiguration... platformConfigu return result; } + public static Map defaultMapIfEmpty(Map map, Map defaultMap) { + return map == null || map.isEmpty() ? defaultMap : map; + } + public static boolean managementPortIsEnabled() { return ConfigProvider.getConfig().getOptionalValue("quarkus.management.enabled", Boolean.class).orElse(false); } diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java index b4855fcaa652f..f0639fb4b9433 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java @@ -75,20 +75,20 @@ public static enum DeploymentResourceKind { /** * The name of the group this component belongs too */ - @ConfigItem + @ConfigItem(defaultValue = "${quarkus.kubernetes.part-of}") Optional partOf; /** * The name of the application. This value will be used for naming Kubernetes * resources like: 'Deployment', 'Service' and so on... */ - @ConfigItem + @ConfigItem(defaultValue = "${quarkus.kubernetes.name}") Optional name; /** * The version of the application. */ - @ConfigItem + @ConfigItem(defaultValue = "${quarkus.kubernetes.version}") Optional version; /** @@ -100,7 +100,7 @@ public static enum DeploymentResourceKind { * (see https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#context * for more details). */ - @ConfigItem + @ConfigItem(defaultValue = "${quarkus.kubernetes.namespace}") Optional namespace; /** @@ -120,31 +120,31 @@ public static enum DeploymentResourceKind { * This is a very useful way to have manifests of successive builds of the same * application differ - thus ensuring that Kubernetes will apply the updated resources */ - @ConfigItem(defaultValue = "true") + @ConfigItem(defaultValue = "${quarkus.kubernetes.add-build-timestamp}") boolean addBuildTimestamp; /** * Working directory */ - @ConfigItem + @ConfigItem(defaultValue = "${quarkus.kubernetes.working-dir}") Optional workingDir; /** * The commands */ - @ConfigItem + @ConfigItem(defaultValue = "${quarkus.kubernetes.command}") Optional> command; /** * The arguments */ - @ConfigItem + @ConfigItem(defaultValue = "${quarkus.kubernetes.arguments}") Optional> arguments; /** * The service account */ - @ConfigItem + @ConfigItem(defaultValue = "${quarkus.kubernetes.service-account}") Optional serviceAccount; /** @@ -156,31 +156,31 @@ public static enum DeploymentResourceKind { /** * The number of desired pods */ - @ConfigItem(defaultValue = "1") + @ConfigItem(defaultValue = "${quarkus.kubernetes.replicas}") Integer replicas; /** * The type of service that will be generated for the application */ - @ConfigItem(defaultValue = "ClusterIP") + @ConfigItem(defaultValue = "${quarkus.kubernetes.service-type}") ServiceType serviceType; /** * The nodePort to set when serviceType is set to nodePort */ - @ConfigItem + @ConfigItem(defaultValue = "${quarkus.kubernetes.node-port}") OptionalInt nodePort; /** * Image pull policy */ - @ConfigItem(defaultValue = "Always") + @ConfigItem(defaultValue = "${quarkus.kubernetes.image-pull-policy}") ImagePullPolicy imagePullPolicy; /** * The image pull secret */ - @ConfigItem + @ConfigItem(defaultValue = "${quarkus.kubernetes.image-pull-secrets}") Optional> imagePullSecrets; /** 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 d0ca1f7d10e4b..313d3cbc11760 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 @@ -10,6 +10,7 @@ import static io.quarkus.kubernetes.deployment.Constants.ROUTE; import static io.quarkus.kubernetes.deployment.Constants.STARTUP_PROBE; import static io.quarkus.kubernetes.deployment.KubernetesConfigUtil.MANAGEMENT_PORT_NAME; +import static io.quarkus.kubernetes.deployment.KubernetesConfigUtil.defaultMapIfEmpty; import static io.quarkus.kubernetes.deployment.KubernetesConfigUtil.managementPortIsEnabled; import static io.quarkus.kubernetes.deployment.OpenshiftConfig.OpenshiftFlavor.v3; import static io.quarkus.kubernetes.spi.KubernetesDeploymentTargetBuildItem.DEFAULT_PRIORITY; @@ -124,16 +125,24 @@ public void populateInternalRegistry(OpenshiftConfig openshiftConfig, ContainerI } @BuildStep - public void createAnnotations(OpenshiftConfig config, BuildProducer annotations) { - config.getAnnotations().forEach((k, v) -> { + public void createAnnotations(OpenshiftConfig config, + KubernetesConfig kubernetesConfig, + BuildProducer annotations) { + Map annotationsFromConfig = config.getAnnotations(); + if (annotationsFromConfig.isEmpty()) { + annotationsFromConfig = kubernetesConfig.getAnnotations(); + } + annotationsFromConfig.forEach((k, v) -> { annotations.produce(new KubernetesAnnotationBuildItem(k, v, OPENSHIFT)); }); } @BuildStep - public void createLabels(OpenshiftConfig config, BuildProducer labels, + public void createLabels(OpenshiftConfig config, + KubernetesConfig kubernetesConfig, + BuildProducer labels, BuildProducer imageLabels) { - config.getLabels().forEach((k, v) -> { + defaultMapIfEmpty(config.getLabels(), kubernetesConfig.getLabels()).forEach((k, v) -> { labels.produce(new KubernetesLabelBuildItem(k, v, OPENSHIFT)); imageLabels.produce(new ContainerImageLabelBuildItem(k, v)); }); @@ -142,14 +151,17 @@ public void createLabels(OpenshiftConfig config, BuildProducer createConfigurators(ApplicationInfoBuildItem applicationInfo, - OpenshiftConfig config, Capabilities capabilities, Optional image, + OpenshiftConfig config, + KubernetesConfig kubernetesConfig, + Capabilities capabilities, + Optional image, List ports) { List result = new ArrayList<>(); - KubernetesCommonHelper.combinePorts(ports, config).values().forEach(value -> { - result.add(new ConfiguratorBuildItem(new AddPortToOpenshiftConfig(value))); - }); + KubernetesCommonHelper.combinePorts(ports, defaultMapIfEmpty(config.getPorts(), kubernetesConfig.getPorts())) + .values() + .forEach(value -> result.add(new ConfiguratorBuildItem(new AddPortToOpenshiftConfig(value)))); result.add(new ConfiguratorBuildItem(new ApplyOpenshiftRouteConfigurator(config.route))); @@ -179,6 +191,7 @@ public List createConfigurators(ApplicationInfoBuildItem public List createDecorators(ApplicationInfoBuildItem applicationInfo, OutputTargetBuildItem outputTarget, OpenshiftConfig config, + KubernetesConfig kubernetesConfig, ContainerImageConfig containerImageConfig, Optional fallbackRegistry, PackageConfig packageConfig, @@ -215,7 +228,9 @@ public List createDecorators(ApplicationInfoBuildItem applic Optional project = KubernetesCommonHelper.createProject(applicationInfo, customProjectRoot, outputTarget, packageConfig); - Optional port = KubernetesCommonHelper.getPort(ports, config, config.route.targetPort); + Optional port = KubernetesCommonHelper.getPort(ports, + defaultMapIfEmpty(config.getPorts(), kubernetesConfig.getPorts()), + config.route.targetPort); result.addAll(KubernetesCommonHelper.createDecorators(project, OPENSHIFT, name, config, metricsConfiguration, kubernetesClientConfiguration, annotations, labels, command, @@ -322,18 +337,18 @@ public List createDecorators(ApplicationInfoBuildItem applic } // Probe port handling - result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, LIVENESS_PROBE, config.livenessProbe, + result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, LIVENESS_PROBE, config.getLivenessProbe(), portName, ports, - config.ports)); - result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, READINESS_PROBE, config.readinessProbe, + config.getPorts())); + result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, READINESS_PROBE, config.getReadinessProbe(), portName, ports, - config.ports)); - result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, STARTUP_PROBE, config.startupProbe, + config.getPorts())); + result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, STARTUP_PROBE, config.getStartupProbe(), portName, ports, - config.ports)); + config.getPorts())); // Handle non-openshift builds if (deploymentKind == DeploymentResourceKind.DeploymentConfig 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..233f8cbb2e00c 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 @@ -114,7 +114,7 @@ public void createLabels(KubernetesConfig config, BuildProducer createConfigurators(KubernetesConfig config, List ports) { List result = new ArrayList<>(); - KubernetesCommonHelper.combinePorts(ports, config).values().forEach(value -> { + KubernetesCommonHelper.combinePorts(ports, config.getPorts()).values().forEach(value -> { result.add(new ConfiguratorBuildItem(new AddPortToKubernetesConfig(value))); }); if (config.ingress != null) { diff --git a/integration-tests/kubernetes/quarkus-standard-way/src/test/resources/openshift-v4.properties b/integration-tests/kubernetes/quarkus-standard-way/src/test/resources/openshift-v4.properties index e83c675f79905..fb9e69b315358 100644 --- a/integration-tests/kubernetes/quarkus-standard-way/src/test/resources/openshift-v4.properties +++ b/integration-tests/kubernetes/quarkus-standard-way/src/test/resources/openshift-v4.properties @@ -1,3 +1,3 @@ quarkus.kubernetes.deployment-target=openshift quarkus.openshift.flavor=v4 -quarkus.openshift.labels."app.openshift.io/runtime"=test +quarkus.kubernetes.labels."app.openshift.io/runtime"=test