diff --git a/fullstack-examples/build.gradle.kts b/fullstack-examples/build.gradle.kts index fa565a645..0967d940d 100644 --- a/fullstack-examples/build.gradle.kts +++ b/fullstack-examples/build.gradle.kts @@ -59,8 +59,15 @@ tasks.register("helmNginxExists") { release.set("nginx-release") } +// This task will succeed because it only uninstalls if the release exists +tasks.register("helmUninstallNotAChart") { + release.set("not-a-release") + ifExists.set(true) +} + tasks.check { dependsOn("helmInstallNginxChart") dependsOn("helmNginxExists") dependsOn("helmUninstallNginxChart") + dependsOn("helmUninstallNotAChart") } diff --git a/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTask.java b/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTask.java index 860d0e8a9..a4f75b1d0 100644 --- a/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTask.java +++ b/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTask.java @@ -18,6 +18,9 @@ import com.hedera.fullstack.helm.client.HelmClient; import com.hedera.fullstack.helm.client.HelmClientBuilder; +import com.hedera.fullstack.helm.client.model.release.ReleaseItem; +import java.util.List; +import java.util.Objects; import org.gradle.api.DefaultTask; import org.gradle.api.provider.Property; import org.gradle.api.tasks.Input; @@ -28,11 +31,18 @@ public abstract class HelmUninstallChartTask extends DefaultTask { @Input @Optional - @Option(option = "namespace", description = "The namespace to use when installing the chart") + @Option(option = "namespace", description = "The namespace to use when uninstalling the chart") public abstract Property getNamespace(); @Input - @Option(option = "release", description = "The name of the release to install") + @Optional + @Option( + option = "ifExists", + description = "True if we should only uninstall the chart if it exists, default is false") + public abstract Property getIfExists(); + + @Input + @Option(option = "release", description = "The name of the release to uninstall") public abstract Property getRelease(); @TaskAction @@ -42,8 +52,27 @@ void uninstallChart() { helmClientBuilder.defaultNamespace(getNamespace().get()); } HelmClient helmClient = helmClientBuilder.build(); + try { - helmClient.uninstallChart(getRelease().getOrNull()); + final String release = getRelease().getOrNull(); + Objects.requireNonNull(release, "release must not be null"); + + if (getIfExists().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( + "HelmUninstallChartTask.uninstallChart() The release {} does not exist, skipping uninstall", + release); + return; + } + } + helmClient.uninstallChart(release); } 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..05dd3b185 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 @@ -109,6 +109,7 @@ void testHelmInstallChartTaskSimple() { .create("helmUninstallChart", HelmUninstallChartTask.class, task -> { task.getNamespace().set(namespace); task.getRelease().set(RELEASE_NAME); + task.getIfExists().set(true); }); helmUninstallChartTask.uninstallChart(); } finally { diff --git a/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTaskTest.java b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTaskTest.java index 4fe613124..2211d6158 100644 --- a/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTaskTest.java +++ b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTaskTest.java @@ -38,11 +38,23 @@ static void beforeAll() { void testErrorThrownWhenChartNotFound() { assertThrows(HelmExecutionException.class, () -> { HelmUninstallChartTask helmUninstallChartTask = project.getTasks() - .create("helmUninstallNonExistingChartChart", HelmUninstallChartTask.class, task -> { + .create("helmUninstallNonExistingChart", HelmUninstallChartTask.class, task -> { task.getNamespace().set("test-failure"); task.getRelease().set("not-a-release"); }); helmUninstallChartTask.uninstallChart(); }); } + + @Test + @DisplayName("test that an uninstall will pass without error if the ifExists flag is set") + void testUninstallIfExists() { + HelmUninstallChartTask helmUninstallChartTask = project.getTasks() + .create("helmUninstallIfExists", HelmUninstallChartTask.class, task -> { + task.getNamespace().set("test-failure"); + task.getRelease().set("not-a-release"); + task.getIfExists().set(true); + }); + helmUninstallChartTask.uninstallChart(); + } }