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 helm execution gradle task: HelmUninstallChartTask #375

Merged
merged 1 commit into from
Oct 5, 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
8 changes: 6 additions & 2 deletions fullstack-examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import com.hedera.fullstack.gradle.plugin.HelmInstallChartTask
import com.hedera.fullstack.gradle.plugin.HelmUninstallChartTask

plugins {
id("java")
Expand Down Expand Up @@ -46,9 +47,12 @@ tasks.register<HelmInstallChartTask>("helmInstallNginxChart") {
chart.set("oci://ghcr.io/nginxinc/charts/nginx-ingress")
}

// TODO: task register helmUninstallNginxChart
tasks.register<HelmUninstallChartTask>("helmUninstallNginxChart") {
namespace.set("nginx-ns")
release.set("nginx-release")
}

tasks.check {
dependsOn("helmInstallNginxChart")
// TODO: depends on helmUninstallNginxChart
dependsOn("helmUninstallNginxChart")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.fullstack.gradle.plugin;

import com.hedera.fullstack.helm.client.HelmClient;
import com.hedera.fullstack.helm.client.HelmClientBuilder;
import org.gradle.api.DefaultTask;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

public abstract class HelmUninstallChartTask extends DefaultTask {
@Input
@Optional
@Option(option = "namespace", description = "The namespace to use when installing the chart")
public abstract Property<String> getNamespace();

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

@TaskAction
void uninstallChart() {
HelmClientBuilder helmClientBuilder = HelmClient.builder();
if (getNamespace().isPresent()) {
helmClientBuilder.defaultNamespace(getNamespace().get());
}
HelmClient helmClient = helmClientBuilder.build();
try {
helmClient.uninstallChart(getRelease().getOrNull());
} catch (Exception e) {
this.getProject()
.getLogger()
.error(
"HelmUninstallChartTask.uninstallChart() An ERROR occurred while uninstalling the chart: ",
e);
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ void testHelmInstallChartTaskSimple() {
});
assertEquals(RELEASE_NAME, helmInstallChartTask.getRelease().get());
helmInstallChartTask.installChart();
HelmUninstallChartTask helmUninstallChartTask = project.getTasks()
.create("helmUninstallChart", HelmUninstallChartTask.class, task -> {
task.getNamespace().set("simple-test");
task.getRelease().set(RELEASE_NAME);
});
helmUninstallChartTask.uninstallChart();
} finally {
suppressExceptions(() -> helmClient.uninstallChart(RELEASE_NAME));
suppressExceptions(() -> helmClient.removeRepository(REPOSITORY));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.fullstack.gradle.plugin;

import static org.junit.jupiter.api.Assertions.assertThrows;

import com.hedera.fullstack.helm.client.HelmExecutionException;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class HelmUninstallChartTaskTest {
private static Project project;

@BeforeAll
static void beforeAll() {
project = ProjectBuilder.builder().build();
}

@Test
@DisplayName("test an error is thrown when the chart is not found")
void testErrorThrownWhenChartNotFound() {
assertThrows(HelmExecutionException.class, () -> {
HelmUninstallChartTask helmUninstallChartTask = project.getTasks()
.create("helmUninstallNonExistingChartChart", HelmUninstallChartTask.class, task -> {
task.getNamespace().set("test-failure");
task.getRelease().set("not-a-release");
});
helmUninstallChartTask.uninstallChart();
});
}
}