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 Install functionality exposed via Maven Mojos / Grad…
…le Tasks Signed-off-by: Rohan Kumar <[email protected]>
- Loading branch information
1 parent
3db16fd
commit 7f45544
Showing
18 changed files
with
698 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
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
...ernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesHelmInstallTask.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 KubernetesHelmInstallTask extends AbstractHelmTask { | ||
@Inject | ||
public KubernetesHelmInstallTask(Class<? extends KubernetesExtension> extensionClass) { | ||
super(extensionClass); | ||
setDescription("Installs a helm chart onto Kubernetes cluster"); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
jKubeServiceHub.getHelmService().install(kubernetesExtension.helm); | ||
} catch (Exception exp) { | ||
kitLogger.error("Error performing helm install", 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
123 changes: 123 additions & 0 deletions
123
...tes/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesHelmInstallTaskTest.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,123 @@ | ||
/* | ||
* 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.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.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
|
||
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 KubernetesHelmInstallTaskTest { | ||
@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()); | ||
Files.write(helmChartOutputDir.resolve("kubernetes").resolve("Chart.yaml"), | ||
("\ndependencies:\n" + | ||
" - name: the-dependency\n" + | ||
" version: 0.1.0\n" + | ||
" repository: file://../../../../the-dependency\n").getBytes(StandardCharsets.UTF_8), | ||
StandardOpenOption.APPEND); | ||
System.setProperty("jkube.kubernetesTemplate", taskEnvironment.getRoot().getAbsolutePath()); | ||
extension.helm = HelmConfig.builder() | ||
.chartExtension("tgz") | ||
.installDependencyUpdate(true) | ||
.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 | ||
void runTask_withHelmDependencyPresent_shouldSucceed() { | ||
// Given | ||
KubernetesHelmInstallTask kubernetesHelmInstallTask = new KubernetesHelmInstallTask(KubernetesExtension.class); | ||
Helm.create().withName("the-dependency").withDir(taskEnvironment.getRoot().toPath()).call(); | ||
|
||
// When | ||
kubernetesHelmInstallTask.runTask(); | ||
// Then | ||
verify(taskEnvironment.logger).lifecycle("k8s: NAME : empty-project"); | ||
verify(taskEnvironment.logger).lifecycle("k8s: NAMESPACE : "); | ||
verify(taskEnvironment.logger).lifecycle("k8s: STATUS : deployed"); | ||
verify(taskEnvironment.logger).lifecycle("k8s: REVISION : 1"); | ||
verify(taskEnvironment.logger).lifecycle("k8s: Saving 1 charts"); | ||
verify(taskEnvironment.logger).lifecycle("k8s: Deleting outdated charts"); | ||
} | ||
|
||
@Test | ||
void runTask_withHelmDependencyAbsent_shouldThrowException() { | ||
// Given | ||
KubernetesHelmInstallTask kubernetesHelmInstallTask = new KubernetesHelmInstallTask(KubernetesExtension.class); | ||
// When + Then | ||
assertThatIllegalStateException() | ||
.isThrownBy(kubernetesHelmInstallTask::runTask) | ||
.withMessageContaining("the-dependency not found"); | ||
} | ||
|
||
@Test | ||
void runTask_withInstallDependencyUpdateDisabled_shouldThrowException() { | ||
// Given | ||
extension.helm = extension.helm.toBuilder() | ||
.installDependencyUpdate(false) | ||
.build(); | ||
when(taskEnvironment.project.getExtensions().getByType(KubernetesExtension.class)).thenReturn(extension); | ||
KubernetesHelmInstallTask kubernetesHelmInstallTask = new KubernetesHelmInstallTask(KubernetesExtension.class); | ||
// When + Then | ||
assertThatIllegalStateException() | ||
.isThrownBy(kubernetesHelmInstallTask::runTask) | ||
.withMessage("An error occurred while checking for chart dependencies. " + | ||
"You may need to run `helm dependency build` to fetch missing dependencies: found in Chart.yaml, but missing in charts/ directory: the-dependency"); | ||
} | ||
} |
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
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
...penshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftHelmInstallTask.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 OpenShiftHelmInstallTask extends KubernetesHelmInstallTask implements OpenShiftJKubeTask { | ||
@Inject | ||
public OpenShiftHelmInstallTask(Class<? extends OpenShiftExtension> extensionClass) { | ||
super(extensionClass); | ||
setDescription("Installs a helm chart onto 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
Oops, something went wrong.