Skip to content

Commit

Permalink
Allow disabling the manifests generation for K8s/OpenShift/Knative
Browse files Browse the repository at this point in the history
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 #34094
  • Loading branch information
Sgitario committed Jun 30, 2023
1 parent 8632e3e commit f58be1f
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public class KindProcessor {
public void checkKind(ApplicationInfoBuildItem applicationInfo, KubernetesConfig config,
BuildProducer<KubernetesDeploymentTargetBuildItem> deploymentTargets,
BuildProducer<KubernetesResourceMetadataBuildItem> resourceMeta) {
if (!config.enabled) {
return;
}

deploymentTargets.produce(
new KubernetesDeploymentTargetBuildItem(KIND, DEPLOYMENT, DEPLOYMENT_GROUP, DEPLOYMENT_VERSION,
KIND_PRIORITY, true, config.getDeployStrategy()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public class MinikubeProcessor {
public void checkMinikube(ApplicationInfoBuildItem applicationInfo, KubernetesConfig config,
BuildProducer<KubernetesDeploymentTargetBuildItem> deploymentTargets,
BuildProducer<KubernetesResourceMetadataBuildItem> resourceMeta) {
if (!config.enabled) {
return;
}

deploymentTargets.produce(
new KubernetesDeploymentTargetBuildItem(MINIKUBE, DEPLOYMENT, DEPLOYMENT_GROUP, DEPLOYMENT_VERSION,
MINIKUBE_PRIORITY, true, config.getDeployStrategy()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class OpenshiftProcessor {
public void checkOpenshift(ApplicationInfoBuildItem applicationInfo, Capabilities capabilities, OpenshiftConfig config,
BuildProducer<KubernetesDeploymentTargetBuildItem> deploymentTargets,
BuildProducer<KubernetesResourceMetadataBuildItem> resourceMeta) {
if (!config.enabled) {
return;
}

DeploymentResourceKind deploymentResourceKind = config.getDeploymentResourceKind(capabilities);
deploymentTargets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,13 @@ public class KnativeProcessor {

@BuildStep
public void checkKnative(ApplicationInfoBuildItem applicationInfo, KnativeConfig config,
KubernetesConfig kubernetesConfig,
BuildProducer<KubernetesDeploymentTargetBuildItem> deploymentTargets,
BuildProducer<KubernetesResourceMetadataBuildItem> resourceMeta) {
if (!kubernetesConfig.enabled) {
return;
}

List<String> targets = KubernetesConfigUtil.getConfiguredDeploymentTargets();
boolean knativeEnabled = targets.contains(KNATIVE);
deploymentTargets.produce(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ public class KubernetesDeployer {

@BuildStep(onlyIf = IsNormalNotRemoteDev.class)
public void selectDeploymentTarget(ContainerImageInfoBuildItem containerImageInfo,
EnabledKubernetesDeploymentTargetsBuildItem targets,
Optional<EnabledKubernetesDeploymentTargetsBuildItem> targets,
Capabilities capabilities,
ContainerImageConfig containerImageConfig,
BuildProducer<SelectedKubernetesDeploymentTargetBuildItem> selectedDeploymentTarget,
BuildProducer<PreventImplicitContainerImagePushBuildItem> preventImplicitContainerImagePush) {
if (targets.isEmpty()) {
return;
}

Optional<String> activeContainerImageCapability = ContainerImageCapabilitiesUtil
.getActiveContainerImageCapability(capabilities);
Expand All @@ -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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ FeatureBuildItem produceFeature() {
}

@BuildStep
public EnabledKubernetesDeploymentTargetsBuildItem enabledKubernetesDeploymentTargets(
List<KubernetesDeploymentTargetBuildItem> allDeploymentTargets) {
public void enabledKubernetesDeploymentTargets(List<KubernetesDeploymentTargetBuildItem> allDeploymentTargets,
BuildProducer<EnabledKubernetesDeploymentTargetsBuildItem> enabledKubernetesDeploymentTargets) {
List<KubernetesDeploymentTargetBuildItem> mergedDeploymentTargets = mergeList(allDeploymentTargets);
Collections.sort(mergedDeploymentTargets);

Expand All @@ -83,7 +83,10 @@ public EnabledKubernetesDeploymentTargetsBuildItem enabledKubernetesDeploymentTa
deploymentTarget.getDeployStrategy()));
}
}
return new EnabledKubernetesDeploymentTargetsBuildItem(entries);

if (!entries.isEmpty()) {
enabledKubernetesDeploymentTargets.produce(new EnabledKubernetesDeploymentTargetsBuildItem(entries));
}
}

@BuildStep
Expand All @@ -106,14 +109,17 @@ public void build(ApplicationInfoBuildItem applicationInfo,
Capabilities capabilities,
LaunchModeBuildItem launchMode,
List<KubernetesPortBuildItem> kubernetesPorts,
EnabledKubernetesDeploymentTargetsBuildItem kubernetesDeploymentTargets,
Optional<EnabledKubernetesDeploymentTargetsBuildItem> kubernetesDeploymentTargets,
List<ConfiguratorBuildItem> configurators,
List<ConfigurationSupplierBuildItem> configurationSuppliers,
List<DecoratorBuildItem> decorators,
BuildProducer<DekorateOutputBuildItem> dekorateSessionProducer,
Optional<CustomProjectRootBuildItem> customProjectRoot,
BuildProducer<GeneratedFileSystemResourceBuildItem> generatedResourceProducer,
BuildProducer<GeneratedKubernetesResourceBuildItem> generatedKubernetesResourceProducer) {
if (kubernetesDeploymentTargets.isEmpty()) {
return;
}

List<ConfiguratorBuildItem> allConfigurators = new ArrayList<>(configurators);
List<ConfigurationSupplierBuildItem> allConfigurationSuppliers = new ArrayList<>(configurationSuppliers);
Expand All @@ -127,7 +133,7 @@ public void build(ApplicationInfoBuildItem applicationInfo,
}

Map<String, Object> config = KubernetesConfigUtil.toMap(kubernetesConfig, openshiftConfig, knativeConfig);
Set<String> deploymentTargets = kubernetesDeploymentTargets.getEntriesSortedByPriority().stream()
Set<String> deploymentTargets = kubernetesDeploymentTargets.get().getEntriesSortedByPriority().stream()
.map(DeploymentTargetEntry::getName)
.collect(Collectors.toSet());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ public class OpenshiftProcessor {

@BuildStep
public void checkOpenshift(ApplicationInfoBuildItem applicationInfo, Capabilities capabilities, OpenshiftConfig config,
KubernetesConfig kubernetesConfig,
BuildProducer<KubernetesDeploymentTargetBuildItem> deploymentTargets,
BuildProducer<KubernetesResourceMetadataBuildItem> resourceMeta) {
if (!kubernetesConfig.enabled || !config.enabled) {
return;
}

List<String> targets = KubernetesConfigUtil.getConfiguredDeploymentTargets();
boolean openshiftEnabled = targets.contains(OPENSHIFT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public void checkVanillaKubernetes(ApplicationInfoBuildItem applicationInfo, Cap
KubernetesConfig config,
BuildProducer<KubernetesDeploymentTargetBuildItem> deploymentTargets,
BuildProducer<KubernetesResourceMetadataBuildItem> resourceMeta) {
if (!config.enabled) {
return;
}

String kind = config.getDeploymentResourceKind(capabilities).kind;

List<String> userSpecifiedDeploymentTargets = KubernetesConfigUtil.getConfiguredDeploymentTargets();
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}

}
Original file line number Diff line number Diff line change
@@ -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"));
}

}
Original file line number Diff line number Diff line change
@@ -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"));
}

}

0 comments on commit f58be1f

Please sign in to comment.