From f58be1f55ca67ed27000117f5bf1ed881310eadb Mon Sep 17 00:00:00 2001 From: Jose Date: Fri, 30 Jun 2023 07:23:03 +0200 Subject: [PATCH] Allow disabling the manifests generation for K8s/OpenShift/Knative To me, it makes sense to separately enable/disable the generated resources by extension (kubernetes, knative, openshift, ...). We could disable the manifests generation for Kubernetes, but not for OpenShift for example. Fix https://github.com/quarkusio/quarkus/issues/34094 --- .../kind/deployment/KindProcessor.java | 4 ++ .../deployment/MinikubeProcessor.java | 4 ++ .../deployment/OpenshiftProcessor.java | 3 ++ .../deployment/KnativeProcessor.java | 5 +++ .../deployment/KubernetesConfig.java | 6 +++ .../deployment/KubernetesDeployer.java | 7 +++- .../deployment/KubernetesProcessor.java | 16 ++++--- .../deployment/OpenshiftConfig.java | 6 +++ .../deployment/OpenshiftProcessor.java | 5 +++ .../VanillaKubernetesProcessor.java | 4 ++ ...rnetesDisabledAndOpenshiftEnabledTest.java | 42 +++++++++++++++++++ .../it/kubernetes/KubernetesDisabledTest.java | 32 ++++++++++++++ .../it/kubernetes/OpenshiftDisabledTest.java | 33 +++++++++++++++ 13 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesDisabledAndOpenshiftEnabledTest.java create mode 100644 integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesDisabledTest.java create mode 100644 integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftDisabledTest.java 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..0d7454e264263 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 @@ -58,6 +58,10 @@ public class KindProcessor { public void checkKind(ApplicationInfoBuildItem applicationInfo, KubernetesConfig config, BuildProducer deploymentTargets, BuildProducer resourceMeta) { + if (!config.enabled) { + return; + } + deploymentTargets.produce( new KubernetesDeploymentTargetBuildItem(KIND, DEPLOYMENT, DEPLOYMENT_GROUP, DEPLOYMENT_VERSION, KIND_PRIORITY, true, config.getDeployStrategy())); 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..d9d5d94d14391 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 @@ -55,6 +55,10 @@ public class MinikubeProcessor { public void checkMinikube(ApplicationInfoBuildItem applicationInfo, KubernetesConfig config, BuildProducer deploymentTargets, BuildProducer resourceMeta) { + if (!config.enabled) { + return; + } + deploymentTargets.produce( new KubernetesDeploymentTargetBuildItem(MINIKUBE, DEPLOYMENT, DEPLOYMENT_GROUP, DEPLOYMENT_VERSION, MINIKUBE_PRIORITY, true, config.getDeployStrategy())); diff --git a/extensions/kubernetes/openshift/deployment/src/main/java/io/quarkus/openshift/deployment/OpenshiftProcessor.java b/extensions/kubernetes/openshift/deployment/src/main/java/io/quarkus/openshift/deployment/OpenshiftProcessor.java index 6460ca8c0feef..a3b2aaf59c8c8 100644 --- a/extensions/kubernetes/openshift/deployment/src/main/java/io/quarkus/openshift/deployment/OpenshiftProcessor.java +++ b/extensions/kubernetes/openshift/deployment/src/main/java/io/quarkus/openshift/deployment/OpenshiftProcessor.java @@ -18,6 +18,9 @@ public class OpenshiftProcessor { public void checkOpenshift(ApplicationInfoBuildItem applicationInfo, Capabilities capabilities, OpenshiftConfig config, BuildProducer deploymentTargets, BuildProducer resourceMeta) { + if (!config.enabled) { + return; + } DeploymentResourceKind deploymentResourceKind = config.getDeploymentResourceKind(capabilities); deploymentTargets 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..a1f14b46b8763 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 @@ -87,8 +87,13 @@ public class KnativeProcessor { @BuildStep public void checkKnative(ApplicationInfoBuildItem applicationInfo, KnativeConfig config, + KubernetesConfig kubernetesConfig, BuildProducer deploymentTargets, BuildProducer resourceMeta) { + if (!kubernetesConfig.enabled) { + return; + } + List targets = KubernetesConfigUtil.getConfiguredDeploymentTargets(); boolean knativeEnabled = targets.contains(KNATIVE); deploymentTargets.produce( diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java index 3a4a79888d640..353ec476be6ae 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java @@ -36,6 +36,12 @@ public enum DeploymentResourceKind { } } + /** + * Whether the Kubernetes extension is enabled and hence the Kubernetes manifests will be generated or not. + */ + @ConfigItem(defaultValue = "true") + public boolean enabled; + /** * The name of the group this component belongs too */ diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesDeployer.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesDeployer.java index f450cea0321aa..2040e08bdf552 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesDeployer.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesDeployer.java @@ -65,11 +65,14 @@ public class KubernetesDeployer { @BuildStep(onlyIf = IsNormalNotRemoteDev.class) public void selectDeploymentTarget(ContainerImageInfoBuildItem containerImageInfo, - EnabledKubernetesDeploymentTargetsBuildItem targets, + Optional targets, Capabilities capabilities, ContainerImageConfig containerImageConfig, BuildProducer selectedDeploymentTarget, BuildProducer preventImplicitContainerImagePush) { + if (targets.isEmpty()) { + return; + } Optional activeContainerImageCapability = ContainerImageCapabilitiesUtil .getActiveContainerImageCapability(capabilities); @@ -81,7 +84,7 @@ public void selectDeploymentTarget(ContainerImageInfoBuildItem containerImageInf return; } - final DeploymentTargetEntry selectedTarget = determineDeploymentTarget(containerImageInfo, targets, + final DeploymentTargetEntry selectedTarget = determineDeploymentTarget(containerImageInfo, targets.get(), containerImageConfig); selectedDeploymentTarget.produce(new SelectedKubernetesDeploymentTargetBuildItem(selectedTarget)); if (MINIKUBE.equals(selectedTarget.getName()) || KIND.equals(selectedTarget.getName())) { diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesProcessor.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesProcessor.java index 657bac8b41bb5..0d4e51d7ed92c 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesProcessor.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesProcessor.java @@ -70,8 +70,8 @@ FeatureBuildItem produceFeature() { } @BuildStep - public EnabledKubernetesDeploymentTargetsBuildItem enabledKubernetesDeploymentTargets( - List allDeploymentTargets) { + public void enabledKubernetesDeploymentTargets(List allDeploymentTargets, + BuildProducer enabledKubernetesDeploymentTargets) { List mergedDeploymentTargets = mergeList(allDeploymentTargets); Collections.sort(mergedDeploymentTargets); @@ -83,7 +83,10 @@ public EnabledKubernetesDeploymentTargetsBuildItem enabledKubernetesDeploymentTa deploymentTarget.getDeployStrategy())); } } - return new EnabledKubernetesDeploymentTargetsBuildItem(entries); + + if (!entries.isEmpty()) { + enabledKubernetesDeploymentTargets.produce(new EnabledKubernetesDeploymentTargetsBuildItem(entries)); + } } @BuildStep @@ -106,7 +109,7 @@ public void build(ApplicationInfoBuildItem applicationInfo, Capabilities capabilities, LaunchModeBuildItem launchMode, List kubernetesPorts, - EnabledKubernetesDeploymentTargetsBuildItem kubernetesDeploymentTargets, + Optional kubernetesDeploymentTargets, List configurators, List configurationSuppliers, List decorators, @@ -114,6 +117,9 @@ public void build(ApplicationInfoBuildItem applicationInfo, Optional customProjectRoot, BuildProducer generatedResourceProducer, BuildProducer generatedKubernetesResourceProducer) { + if (kubernetesDeploymentTargets.isEmpty()) { + return; + } List allConfigurators = new ArrayList<>(configurators); List allConfigurationSuppliers = new ArrayList<>(configurationSuppliers); @@ -127,7 +133,7 @@ public void build(ApplicationInfoBuildItem applicationInfo, } Map config = KubernetesConfigUtil.toMap(kubernetesConfig, openshiftConfig, knativeConfig); - Set deploymentTargets = kubernetesDeploymentTargets.getEntriesSortedByPriority().stream() + Set deploymentTargets = kubernetesDeploymentTargets.get().getEntriesSortedByPriority().stream() .map(DeploymentTargetEntry::getName) .collect(Collectors.toSet()); 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..d14d9ac05e5e7 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 @@ -57,6 +57,12 @@ public static enum DeploymentResourceKind { } } + /** + * Whether the OpenShift extension is enabled and hence the OpenShift manifests are generated or not. + */ + @ConfigItem(defaultValue = "true") + public boolean enabled; + /** * The OpenShift flavor / version to use. * Older versions of OpenShift have minor differences in the labels and fields they support. 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..a71500eeb9058 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 @@ -84,8 +84,13 @@ public class OpenshiftProcessor { @BuildStep public void checkOpenshift(ApplicationInfoBuildItem applicationInfo, Capabilities capabilities, OpenshiftConfig config, + KubernetesConfig kubernetesConfig, BuildProducer deploymentTargets, BuildProducer resourceMeta) { + if (!kubernetesConfig.enabled || !config.enabled) { + return; + } + List targets = KubernetesConfigUtil.getConfiguredDeploymentTargets(); boolean openshiftEnabled = targets.contains(OPENSHIFT); 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..a6a30a0782fb5 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 @@ -75,6 +75,10 @@ public void checkVanillaKubernetes(ApplicationInfoBuildItem applicationInfo, Cap KubernetesConfig config, BuildProducer deploymentTargets, BuildProducer resourceMeta) { + if (!config.enabled) { + return; + } + String kind = config.getDeploymentResourceKind(capabilities).kind; List userSpecifiedDeploymentTargets = KubernetesConfigUtil.getConfiguredDeploymentTargets(); diff --git a/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesDisabledAndOpenshiftEnabledTest.java b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesDisabledAndOpenshiftEnabledTest.java new file mode 100644 index 0000000000000..024ec9cdec3d5 --- /dev/null +++ b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesDisabledAndOpenshiftEnabledTest.java @@ -0,0 +1,42 @@ +package io.quarkus.it.kubernetes; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.builder.Version; +import io.quarkus.maven.dependency.Dependency; +import io.quarkus.test.ProdBuildResults; +import io.quarkus.test.ProdModeTestResults; +import io.quarkus.test.QuarkusProdModeTest; + +public class KubernetesDisabledAndOpenshiftEnabledTest { + + @RegisterExtension + static final QuarkusProdModeTest config = new QuarkusProdModeTest() + .withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class)) + .setApplicationName("kubernetes-disabled-openshift-enabled") + .setApplicationVersion("0.1-SNAPSHOT") + .overrideConfigKey("quarkus.kubernetes.enabled", "false") + .overrideConfigKey("quarkus.openshift.enabled", "true") + .setForcedDependencies(List.of(Dependency.of("io.quarkus", "quarkus-openshift", Version.getVersion()))); + + @ProdBuildResults + private ProdModeTestResults prodModeTestResults; + + @Test + public void assertGeneratedResources() throws IOException { + Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes"); + assertThat(kubernetesDir) + .isDirectoryNotContaining(p -> p.getFileName().endsWith("kubernetes.json")) + .isDirectoryNotContaining(p -> p.getFileName().endsWith("kubernetes.yml")) + .isDirectoryContaining(p -> p.getFileName().endsWith("openshift.json")) + .isDirectoryContaining(p -> p.getFileName().endsWith("openshift.yml")); + } + +} diff --git a/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesDisabledTest.java b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesDisabledTest.java new file mode 100644 index 0000000000000..e346058e08e4c --- /dev/null +++ b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesDisabledTest.java @@ -0,0 +1,32 @@ +package io.quarkus.it.kubernetes; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.ProdBuildResults; +import io.quarkus.test.ProdModeTestResults; +import io.quarkus.test.QuarkusProdModeTest; + +public class KubernetesDisabledTest { + + @RegisterExtension + static final QuarkusProdModeTest config = new QuarkusProdModeTest() + .withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class)) + .setApplicationName("kubernetes-disabled") + .setApplicationVersion("0.1-SNAPSHOT") + .overrideConfigKey("quarkus.kubernetes.enabled", "false"); + + @ProdBuildResults + private ProdModeTestResults prodModeTestResults; + + @Test + public void assertGeneratedResources() throws IOException { + assertThat(prodModeTestResults.getBuildDir()) + .isDirectoryNotContaining(p -> p.toString().endsWith("kubernetes")); + } + +} diff --git a/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftDisabledTest.java b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftDisabledTest.java new file mode 100644 index 0000000000000..fa21620944b9f --- /dev/null +++ b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftDisabledTest.java @@ -0,0 +1,33 @@ +package io.quarkus.it.kubernetes; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.ProdBuildResults; +import io.quarkus.test.ProdModeTestResults; +import io.quarkus.test.QuarkusProdModeTest; + +public class OpenshiftDisabledTest { + + @RegisterExtension + static final QuarkusProdModeTest config = new QuarkusProdModeTest() + .withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class)) + .setApplicationName("openshift-disabled") + .setApplicationVersion("0.1-SNAPSHOT") + .overrideConfigKey("quarkus.kubernetes.deployment-target", "openshift") + .overrideConfigKey("quarkus.kubernetes.enabled", "false"); + + @ProdBuildResults + private ProdModeTestResults prodModeTestResults; + + @Test + public void assertGeneratedResources() throws IOException { + assertThat(prodModeTestResults.getBuildDir()) + .isDirectoryNotContaining(p -> p.toString().endsWith("kubernetes")); + } + +}