From ccfbabf57514cf48460952ba3300b1656df43cc3 Mon Sep 17 00:00:00 2001 From: Jeromy Cannon Date: Tue, 17 Oct 2023 06:42:48 -0500 Subject: [PATCH] feat: add skipIfExists option for Gradle Helm install task (#406) Signed-off-by: Jeromy Cannon --- fullstack-examples/build.gradle.kts | 1 + .../gradle/plugin/HelmInstallChartTask.java | 35 +++++++++++++++++-- .../plugin/HelmInstallChartTaskTest.java | 5 +++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/fullstack-examples/build.gradle.kts b/fullstack-examples/build.gradle.kts index fa565a645..a3100efaa 100644 --- a/fullstack-examples/build.gradle.kts +++ b/fullstack-examples/build.gradle.kts @@ -46,6 +46,7 @@ tasks.register("helmInstallNginxChart") { namespace.set("nginx-ns") release.set("nginx-release") chart.set("oci://ghcr.io/nginxinc/charts/nginx-ingress") + skipIfExists.set(true) } tasks.register("helmUninstallNginxChart") { diff --git a/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTask.java b/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTask.java index e915e9d09..58314e365 100644 --- a/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTask.java +++ b/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTask.java @@ -20,7 +20,10 @@ import com.hedera.fullstack.helm.client.HelmClientBuilder; import com.hedera.fullstack.helm.client.model.Chart; import com.hedera.fullstack.helm.client.model.install.InstallChartOptionsBuilder; +import com.hedera.fullstack.helm.client.model.release.ReleaseItem; import java.util.ArrayList; +import java.util.List; +import java.util.Objects; import org.gradle.api.DefaultTask; import org.gradle.api.provider.Property; import org.gradle.api.provider.SetProperty; @@ -66,6 +69,13 @@ public abstract class HelmInstallChartTask extends DefaultTask { @Option(option = "values", description = "Specify values in a YAML file or a URL (can specify multiple)") public abstract SetProperty getValues(); + @Input + @Optional + @Option( + option = "skipIfExists", + description = "Skip installation if the release is already installed, default false") + public abstract Property getSkipIfExists(); + @TaskAction void installChart() { HelmClientBuilder helmClientBuilder = HelmClient.builder(); @@ -73,6 +83,7 @@ void installChart() { helmClientBuilder.defaultNamespace(getNamespace().get()); } HelmClient helmClient = helmClientBuilder.build(); + InstallChartOptionsBuilder optionsBuilder = InstallChartOptionsBuilder.builder(); if (getCreateNamespace().isPresent()) { optionsBuilder.createNamespace(getCreateNamespace().get()); @@ -83,11 +94,29 @@ void installChart() { if (getValues().isPresent()) { optionsBuilder.values(new ArrayList<>(getValues().get())); } + try { + final String release = getRelease().getOrNull(); + Objects.requireNonNull(release, "release must not be null"); + + if (getSkipIfExists().getOrElse(false)) { + List releaseItems = helmClient.listReleases(false); + ReleaseItem releaseItem = releaseItems.stream() + .filter(item -> item.name().equals(release)) + .findFirst() + .orElse(null); + if (releaseItem != null) { + this.getProject() + .getLogger() + .warn( + "HelmInstallChartTask.installChart() The release {} already exists, skipping install", + release); + return; + } + } + helmClient.installChart( - getRelease().getOrNull(), - new Chart(getChart().getOrNull(), getRepo().getOrNull()), - optionsBuilder.build()); + release, new Chart(getChart().getOrNull(), getRepo().getOrNull()), optionsBuilder.build()); } catch (Exception e) { this.getProject() .getLogger() diff --git a/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java index 358874ab0..a5a844d30 100644 --- a/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java +++ b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java @@ -96,9 +96,14 @@ void testHelmInstallChartTaskSimple() { task.getNamespace().set(namespace); task.getRelease().set(RELEASE_NAME); task.getRepo().set(CHART.repoName()); + task.getSkipIfExists().set(true); }); assertThat(helmInstallChartTask.getRelease().get()).isEqualTo(RELEASE_NAME); helmInstallChartTask.installChart(); + + // call a second time to test skipIfExists + helmInstallChartTask.installChart(); + HelmReleaseExistsTask helmReleaseExistsTask = project.getTasks() .create("helmReleaseExists", HelmReleaseExistsTask.class, task -> { task.getNamespace().set(namespace);