Skip to content

Commit

Permalink
feat: add skipIfExists option for Gradle Helm install task (#406)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeromy Cannon <[email protected]>
  • Loading branch information
jeromy-cannon authored Oct 17, 2023
1 parent 23d7c9a commit ccfbabf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions fullstack-examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ tasks.register<HelmInstallChartTask>("helmInstallNginxChart") {
namespace.set("nginx-ns")
release.set("nginx-release")
chart.set("oci://ghcr.io/nginxinc/charts/nginx-ingress")
skipIfExists.set(true)
}

tasks.register<HelmUninstallChartTask>("helmUninstallNginxChart") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,13 +69,21 @@ 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<String> getValues();

@Input
@Optional
@Option(
option = "skipIfExists",
description = "Skip installation if the release is already installed, default false")
public abstract Property<Boolean> getSkipIfExists();

@TaskAction
void installChart() {
HelmClientBuilder helmClientBuilder = HelmClient.builder();
if (getNamespace().isPresent()) {
helmClientBuilder.defaultNamespace(getNamespace().get());
}
HelmClient helmClient = helmClientBuilder.build();

InstallChartOptionsBuilder optionsBuilder = InstallChartOptionsBuilder.builder();
if (getCreateNamespace().isPresent()) {
optionsBuilder.createNamespace(getCreateNamespace().get());
Expand All @@ -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<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(
"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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ccfbabf

Please sign in to comment.