Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions fullstack-examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,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 @@ -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 {
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();
}
}