Skip to content

Commit

Permalink
feat: add ifExists option to Gradle Helm uninstall task (#405)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeromy Cannon <jeromy@swirldslabs.com>
  • Loading branch information
jeromy-cannon authored Oct 17, 2023
1 parent ccfbabf commit 0726725
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
7 changes: 7 additions & 0 deletions fullstack-examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@ tasks.register<HelmReleaseExistsTask>("helmNginxExists") {
release.set("nginx-release")
}

// This task will succeed because it only uninstalls if the release exists
tasks.register<HelmUninstallChartTask>("helmUninstallNotAChart") {
release.set("not-a-release")
ifExists.set(true)
}

tasks.check {
dependsOn("helmInstallNginxChart")
dependsOn("helmNginxExists")
dependsOn("helmUninstallNginxChart")
dependsOn("helmUninstallNotAChart")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> 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<Boolean> getIfExists();

@Input
@Option(option = "release", description = "The name of the release to uninstall")
public abstract Property<String> getRelease();

@TaskAction
Expand All @@ -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<ReleaseItem> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ void testHelmInstallChartTaskSimple() {
.create("helmUninstallChart", HelmUninstallChartTask.class, task -> {
task.getNamespace().set(namespace);
task.getRelease().set(RELEASE_NAME);
task.getIfExists().set(true);
});
helmUninstallChartTask.uninstallChart();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 0726725

Please sign in to comment.