Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add skipIfExists option for Gradle Helm install task #406

Merged
merged 1 commit into from
Oct 17, 2023
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
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