forked from eclipse-jkube/jkube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helm): Helm Uninstall functionality exposed via Maven Mojos / Gr…
…adle Tasks Signed-off-by: Rohan Kumar <[email protected]>
- Loading branch information
1 parent
1d2ca4a
commit 729dc8a
Showing
14 changed files
with
611 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...netes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesHelmUninstallTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.gradle.plugin.task; | ||
|
||
import org.eclipse.jkube.gradle.plugin.KubernetesExtension; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class KubernetesHelmUninstallTask extends AbstractHelmTask { | ||
@Inject | ||
public KubernetesHelmUninstallTask(Class<? extends KubernetesExtension> extensionClass) { | ||
super(extensionClass); | ||
setDescription("Uninstalls the Helm release from Kubernetes cluster"); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
jKubeServiceHub.getHelmService().uninstall(kubernetesExtension.helm); | ||
} catch (Exception exp) { | ||
kitLogger.error("Error performing helm uninstall", exp); | ||
throw new IllegalStateException(exp.getMessage(), exp); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...s/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesHelmUninstallTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.gradle.plugin.task; | ||
|
||
import com.marcnuri.helm.Helm; | ||
import io.fabric8.kubernetes.api.model.Secret; | ||
import io.fabric8.kubernetes.api.model.SecretListBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; | ||
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; | ||
import org.apache.commons.io.FileUtils; | ||
import org.eclipse.jkube.gradle.plugin.KubernetesExtension; | ||
import org.eclipse.jkube.gradle.plugin.TestKubernetesExtension; | ||
import org.eclipse.jkube.kit.common.access.ClusterConfiguration; | ||
import org.eclipse.jkube.kit.resource.helm.HelmConfig; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; | ||
import static org.eclipse.jkube.kit.common.util.KubernetesMockServerUtil.prepareMockWebServerExpectationsForAggregatedDiscoveryEndpoints; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@EnableKubernetesMockClient(crud = true) | ||
class KubernetesHelmUninstallTaskTest { | ||
@RegisterExtension | ||
private final TaskEnvironmentExtension taskEnvironment = new TaskEnvironmentExtension(); | ||
private KubernetesClient kubernetesClient; | ||
private KubernetesMockServer server; | ||
private TestKubernetesExtension extension; | ||
|
||
@BeforeEach | ||
void setUp() throws IOException { | ||
extension = new TestKubernetesExtension(); | ||
// Remove after https://github.com/fabric8io/kubernetes-client/issues/6062 is fixed | ||
prepareMockWebServerExpectationsForAggregatedDiscoveryEndpoints(server); | ||
Helm.create().withDir(taskEnvironment.getRoot().toPath()).withName("empty-project").call(); | ||
Path helmChartOutputDir = taskEnvironment.getRoot().toPath().resolve("build").resolve("jkube").resolve("helm"); | ||
Files.createDirectories(helmChartOutputDir.resolve("kubernetes")); | ||
FileUtils.copyDirectory(taskEnvironment.getRoot().toPath().resolve("empty-project").toFile(), helmChartOutputDir.resolve("kubernetes").toFile()); | ||
System.setProperty("jkube.kubernetesTemplate", taskEnvironment.getRoot().getAbsolutePath()); | ||
extension.helm = HelmConfig.builder() | ||
.chartExtension("tgz") | ||
.disableOpenAPIValidation(true) | ||
.outputDir(helmChartOutputDir.toString()).build(); | ||
extension.access = ClusterConfiguration.from(kubernetesClient.getConfiguration()).build(); | ||
extension.isUseColor = false; | ||
when(taskEnvironment.project.getName()).thenReturn("empty-project"); | ||
when(taskEnvironment.project.getVersion()).thenReturn("0.1.0"); | ||
when(taskEnvironment.project.getExtensions().getByType(KubernetesExtension.class)).thenReturn(extension); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
System.clearProperty("jkube.kubernetesTemplate"); | ||
} | ||
|
||
@Test | ||
@DisplayName("when Helm Release Installed on Kubernetes Cluster, then uninstall Helm Release") | ||
void runTask_withHelmReleasePresentInKubernetesCluster_shouldSucceed() { | ||
// Given | ||
KubernetesHelmUninstallTask kubernetesHelmUninstallTask = new KubernetesHelmUninstallTask(KubernetesExtension.class); | ||
kubernetesHelmUninstallTask.init(); | ||
kubernetesHelmUninstallTask.jKubeServiceHub.getHelmService().install(extension.helm); | ||
// Should be removed once https://github.com/fabric8io/kubernetes-client/issues/6220 gets fixed | ||
Secret secret = kubernetesClient.secrets().withName("sh.helm.release.v1.empty-project.v1").get(); | ||
server.expect().get().withPath("/api/v1/namespaces/test/secrets?labelSelector=name%3Dempty-project%2Cowner%3Dhelm") | ||
.andReturn(200, new SecretListBuilder() | ||
.addToItems(secret) | ||
.build()) | ||
.once(); | ||
|
||
// When | ||
kubernetesHelmUninstallTask.runTask(); | ||
// Then | ||
verify(taskEnvironment.logger).lifecycle("k8s: release \"empty-project\" uninstalled"); | ||
} | ||
|
||
@Test | ||
@DisplayName("Helm Release not installed on Kubernetes cluster, then throw exception") | ||
void execute_whenReleaseNotPresent_thenThrowException() { | ||
// Given | ||
KubernetesHelmUninstallTask kubernetesHelmUninstallTask = new KubernetesHelmUninstallTask(KubernetesExtension.class); | ||
|
||
// When + Then | ||
assertThatIllegalStateException() | ||
.isThrownBy(kubernetesHelmUninstallTask::runTask) | ||
.withMessageContaining(" not found"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...nshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftHelmUninstallTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.gradle.plugin.task; | ||
|
||
import org.eclipse.jkube.gradle.plugin.OpenShiftExtension; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class OpenShiftHelmUninstallTask extends KubernetesHelmUninstallTask implements OpenShiftJKubeTask { | ||
@Inject | ||
public OpenShiftHelmUninstallTask(Class<? extends OpenShiftExtension> extensionClass) { | ||
super(extensionClass); | ||
setDescription("Uninstalls the Helm release from OpenShift cluster"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...ft/src/test/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftHelmUninstallTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.gradle.plugin.task; | ||
|
||
import com.marcnuri.helm.Helm; | ||
import io.fabric8.kubernetes.api.model.Secret; | ||
import io.fabric8.kubernetes.api.model.SecretListBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; | ||
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; | ||
import org.apache.commons.io.FileUtils; | ||
import org.eclipse.jkube.gradle.plugin.OpenShiftExtension; | ||
import org.eclipse.jkube.gradle.plugin.TestOpenShiftExtension; | ||
import org.eclipse.jkube.kit.common.access.ClusterConfiguration; | ||
import org.eclipse.jkube.kit.resource.helm.HelmConfig; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; | ||
import static org.eclipse.jkube.kit.common.util.KubernetesMockServerUtil.prepareMockWebServerExpectationsForAggregatedDiscoveryEndpoints; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@EnableKubernetesMockClient(crud = true) | ||
class OpenShiftHelmUninstallTaskTest { | ||
@RegisterExtension | ||
private final TaskEnvironmentExtension taskEnvironment = new TaskEnvironmentExtension(); | ||
private KubernetesClient kubernetesClient; | ||
private KubernetesMockServer server; | ||
private TestOpenShiftExtension extension; | ||
|
||
@BeforeEach | ||
void setUp() throws IOException { | ||
extension = new TestOpenShiftExtension(); | ||
// Remove after https://github.com/fabric8io/kubernetes-client/issues/6062 is fixed | ||
prepareMockWebServerExpectationsForAggregatedDiscoveryEndpoints(server); | ||
Helm.create().withDir(taskEnvironment.getRoot().toPath()).withName("empty-project").call(); | ||
Path helmChartOutputDir = taskEnvironment.getRoot().toPath().resolve("build").resolve("jkube").resolve("helm"); | ||
Files.createDirectories(helmChartOutputDir.resolve("openshift")); | ||
FileUtils.copyDirectory(taskEnvironment.getRoot().toPath().resolve("empty-project").toFile(), helmChartOutputDir.resolve("openshift").toFile()); | ||
System.setProperty("jkube.kubernetesTemplate", taskEnvironment.getRoot().getAbsolutePath()); | ||
extension.helm = HelmConfig.builder() | ||
.chartExtension("tgz") | ||
.disableOpenAPIValidation(true) | ||
.outputDir(helmChartOutputDir.toString()).build(); | ||
extension.access = ClusterConfiguration.from(kubernetesClient.getConfiguration()).build(); | ||
extension.isUseColor = false; | ||
when(taskEnvironment.project.getName()).thenReturn("empty-project"); | ||
when(taskEnvironment.project.getVersion()).thenReturn("0.1.0"); | ||
when(taskEnvironment.project.getExtensions().getByType(OpenShiftExtension.class)).thenReturn(extension); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
System.clearProperty("jkube.kubernetesTemplate"); | ||
} | ||
|
||
@Test | ||
@DisplayName("when Helm Release Installed on Kubernetes Cluster, then uninstall Helm Release") | ||
void runTask_withHelmReleasePresentInKubernetesCluster_shouldSucceed() { | ||
// Given | ||
OpenShiftHelmUninstallTask openShiftHelmUninstallTask = new OpenShiftHelmUninstallTask(OpenShiftExtension.class); | ||
openShiftHelmUninstallTask.init(); | ||
openShiftHelmUninstallTask.jKubeServiceHub.getHelmService().install(extension.helm); | ||
// Should be removed once https://github.com/fabric8io/kubernetes-client/issues/6220 gets fixed | ||
Secret secret = kubernetesClient.secrets().withName("sh.helm.release.v1.empty-project.v1").get(); | ||
server.expect().get().withPath("/api/v1/namespaces/test/secrets?labelSelector=name%3Dempty-project%2Cowner%3Dhelm") | ||
.andReturn(200, new SecretListBuilder() | ||
.addToItems(secret) | ||
.build()) | ||
.once(); | ||
|
||
// When | ||
openShiftHelmUninstallTask.runTask(); | ||
// Then | ||
verify(taskEnvironment.logger).lifecycle("oc: release \"empty-project\" uninstalled"); | ||
} | ||
|
||
@Test | ||
@DisplayName("Helm Release not installed on Kubernetes cluster, then throw exception") | ||
void execute_whenReleaseNotPresent_thenThrowException() { | ||
// Given | ||
OpenShiftHelmUninstallTask openShiftHelmUninstallTask = new OpenShiftHelmUninstallTask(OpenShiftExtension.class); | ||
|
||
// When + Then | ||
assertThatIllegalStateException() | ||
.isThrownBy(openShiftHelmUninstallTask::runTask) | ||
.withMessageContaining(" not found"); | ||
} | ||
} |
Oops, something went wrong.