-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #1052: Add k8sHelm task to Kubernetes Gradle Plugin
Add `k8s:helm` maven equivalent `k8sHelm` task to Kubernetes Gradle Plugin. Signed-off-by: Rohan Kumar <rohaan@redhat.com>
- Loading branch information
1 parent
d951c23
commit 78ca19d
Showing
6 changed files
with
140 additions
and
5 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
47 changes: 47 additions & 0 deletions
47
...gin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesHelmTask.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,47 @@ | ||
/** | ||
* 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 org.eclipse.jkube.kit.resource.helm.HelmConfig; | ||
|
||
import javax.inject.Inject; | ||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmConfig; | ||
|
||
public class KubernetesHelmTask extends AbstractJKubeTask { | ||
@Inject | ||
public KubernetesHelmTask(Class<? extends KubernetesExtension> extensionClass) { | ||
super(extensionClass); | ||
setDescription("Generates a Helm chart for the kubernetes resources."); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
File manifest = kubernetesExtension.getKubernetesManifestOrDefault(); | ||
if (manifest == null || !manifest.isFile()) { | ||
kitLogger.warn("No kubernetes manifest file has been generated yet by the k8sResource task at: " + manifest); | ||
} | ||
HelmConfig helm = initHelmConfig(kubernetesExtension.getDefaultHelmType(), kubernetesExtension.javaProject, | ||
kubernetesExtension.getKubernetesManifestOrDefault(), kubernetesExtension.getKubernetesTemplateOrDefault(), | ||
kubernetesExtension.helm).build(); | ||
jKubeServiceHub.getHelmService().generateHelmCharts(helm); | ||
} catch (IOException exception) { | ||
throw new IllegalStateException(exception.getMessage()); | ||
} | ||
} | ||
} |
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
80 changes: 80 additions & 0 deletions
80
...kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesHelmTaskTest.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,80 @@ | ||
/** | ||
* 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 org.eclipse.jkube.gradle.plugin.TestKubernetesExtension; | ||
import org.eclipse.jkube.kit.resource.helm.HelmService; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.MockedConstruction; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mockConstruction; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class KubernetesHelmTaskTest { | ||
@Rule | ||
public TaskEnvironment taskEnvironment = new TaskEnvironment(); | ||
|
||
private TestKubernetesExtension extension; | ||
private MockedConstruction<HelmService> helmServiceMockedConstruction; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
extension = new TestKubernetesExtension(); | ||
extension.isUseColor = false; | ||
helmServiceMockedConstruction = mockConstruction(HelmService.class); | ||
when(taskEnvironment.project.getExtensions().getByType(KubernetesExtension.class)).thenReturn(extension); | ||
} | ||
|
||
@Test | ||
public void runTask_withNoTemplateDir_shouldThrowException() { | ||
// Given | ||
KubernetesHelmTask kubernetesHelmTask = new KubernetesHelmTask(KubernetesExtension.class); | ||
|
||
// When | ||
IllegalStateException illegalStateException = assertThrows(IllegalStateException.class, kubernetesHelmTask::runTask); | ||
|
||
// Then | ||
assertThat(illegalStateException) | ||
.hasMessageContaining("META-INF/jkube/kubernetes (No such file or directory)"); | ||
} | ||
|
||
@Test | ||
public void runTask_withTemplateDir_shouldCallHelmService() throws IOException { | ||
// Given | ||
taskEnvironment.withKubernetesTemplate(); | ||
KubernetesHelmTask kubernetesHelmTask = new KubernetesHelmTask(KubernetesExtension.class); | ||
|
||
// When | ||
kubernetesHelmTask.runTask(); | ||
|
||
// Then | ||
verify((helmServiceMockedConstruction.constructed().iterator().next()), times(1)).generateHelmCharts(any()); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
helmServiceMockedConstruction.close(); | ||
} | ||
} |
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