Skip to content

Commit

Permalink
Ensure Kubernetes manifests are generated in the project location
Browse files Browse the repository at this point in the history
... when using the `quarkus.kubernetes.output-directory`.

In gradle, when users set the output-directory property, the K8s resources were generated at `$HOME/.gradle/workers`.

I tried these changes in Maven and Gradle and now, it works. 

Fix #34827
  • Loading branch information
Sgitario committed Jul 25, 2023
1 parent 7597447 commit 20766e1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.kubernetes.spi;

import java.nio.file.Path;

import io.quarkus.builder.item.SimpleBuildItem;

/**
* Contains the effective output directory where to find the generated kubernetes resources.
*/
public final class KubernetesOutputDirectoryBuildItem extends SimpleBuildItem {
private final Path outputDirectory;

public KubernetesOutputDirectoryBuildItem(Path outputDirectory) {
this.outputDirectory = outputDirectory;
}

public Path getOutputDirectory() {
return outputDirectory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import static io.quarkus.kubernetes.deployment.Constants.CRONJOB;
import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT;
import static io.quarkus.kubernetes.deployment.Constants.JOB;
import static io.quarkus.kubernetes.deployment.Constants.KUBERNETES;
import static io.quarkus.kubernetes.deployment.Constants.STATEFULSET;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -617,17 +614,4 @@ public KubernetesConfig.DeploymentResourceKind getDeploymentResourceKind(Capabil

return DeploymentResourceKind.Deployment;
}

/**
* Resolve the effective output directory where to generate the Kubernetes manifests.
* If the `quarkus.kubernetes.output-directory` property is not provided, then the default project output directory will be
* used.
*
* @param projectOutputDirectory The project output target.
* @return the effective output directory.
*/
public Path getEffectiveOutputDirectory(Path projectOutputDirectory) {
return outputDirectory.map(d -> Paths.get("").toAbsolutePath().resolve(d))
.orElse(projectOutputDirectory.resolve(KUBERNETES));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import io.quarkus.kubernetes.spi.GeneratedKubernetesResourceBuildItem;
import io.quarkus.kubernetes.spi.KubernetesDeploymentClusterBuildItem;
import io.quarkus.kubernetes.spi.KubernetesOptionalResourceDefinitionBuildItem;
import io.quarkus.kubernetes.spi.KubernetesOutputDirectoryBuildItem;

public class KubernetesDeployer {

Expand Down Expand Up @@ -110,7 +111,7 @@ public void deploy(KubernetesClientBuildItem kubernetesClientBuilder,
List<KubernetesDeploymentClusterBuildItem> deploymentClusters,
Optional<SelectedKubernetesDeploymentTargetBuildItem> selectedDeploymentTarget,
OutputTargetBuildItem outputTarget,
KubernetesConfig kubernetesConfig,
KubernetesOutputDirectoryBuildItem outputDirectoryBuildItem,
OpenshiftConfig openshiftConfig,
ContainerImageConfig containerImageConfig,
ApplicationInfoBuildItem applicationInfo,
Expand Down Expand Up @@ -139,7 +140,7 @@ public void deploy(KubernetesClientBuildItem kubernetesClientBuilder,
try (final KubernetesClient client = kubernetesClientBuilder.buildClient()) {
deploymentResult
.produce(deploy(selectedDeploymentTarget.get().getEntry(), client,
kubernetesConfig.getEffectiveOutputDirectory(outputTarget.getOutputDirectory()),
outputDirectoryBuildItem.getOutputDirectory(),
openshiftConfig, applicationInfo, optionalResourceDefinitions));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import io.quarkus.kubernetes.spi.DekorateOutputBuildItem;
import io.quarkus.kubernetes.spi.GeneratedKubernetesResourceBuildItem;
import io.quarkus.kubernetes.spi.KubernetesDeploymentTargetBuildItem;
import io.quarkus.kubernetes.spi.KubernetesOutputDirectoryBuildItem;
import io.quarkus.kubernetes.spi.KubernetesPortBuildItem;
import io.quarkus.runtime.LaunchMode;

Expand Down Expand Up @@ -113,7 +114,8 @@ public void build(ApplicationInfoBuildItem applicationInfo,
BuildProducer<DekorateOutputBuildItem> dekorateSessionProducer,
Optional<CustomProjectRootBuildItem> customProjectRoot,
BuildProducer<GeneratedFileSystemResourceBuildItem> generatedResourceProducer,
BuildProducer<GeneratedKubernetesResourceBuildItem> generatedKubernetesResourceProducer) {
BuildProducer<GeneratedKubernetesResourceBuildItem> generatedKubernetesResourceProducer,
BuildProducer<KubernetesOutputDirectoryBuildItem> outputDirectoryBuildItemBuildProducer) {

List<ConfiguratorBuildItem> allConfigurators = new ArrayList<>(configurators);
List<ConfigurationSupplierBuildItem> allConfigurationSuppliers = new ArrayList<>(configurationSuppliers);
Expand Down Expand Up @@ -183,7 +185,10 @@ public void build(ApplicationInfoBuildItem applicationInfo,
}
});

Path targetDirectory = kubernetesConfig.getEffectiveOutputDirectory(outputTarget.getOutputDirectory());
Path targetDirectory = getEffectiveOutputDirectory(kubernetesConfig, project.getRoot(),
outputTarget.getOutputDirectory());

outputDirectoryBuildItemBuildProducer.produce(new KubernetesOutputDirectoryBuildItem(targetDirectory));

// write the generated resources to the filesystem
generatedResourcesMap = session.close();
Expand Down Expand Up @@ -277,4 +282,19 @@ private Path getRunner(OutputTargetBuildItem outputTarget,

return buildDir.resolve(QUARKUS_RUN_JAR);
}

/**
* Resolve the effective output directory where to generate the Kubernetes manifests.
* If the `quarkus.kubernetes.output-directory` property is not provided, then the default project output directory will be
* used.
*
* @param config The Kubernetes configuration.
* @param projectLocation The project location.
* @param projectOutputDirectory The project output target.
* @return the effective output directory.
*/
private Path getEffectiveOutputDirectory(KubernetesConfig config, Path projectLocation, Path projectOutputDirectory) {
return config.outputDirectory.map(d -> projectLocation.resolve(d))
.orElse(projectOutputDirectory.resolve(KUBERNETES));
}
}

0 comments on commit 20766e1

Please sign in to comment.