diff --git a/gradle-plugin/doc/src/main/asciidoc/inc/tasks/build/_jkube-helm.adoc b/gradle-plugin/doc/src/main/asciidoc/inc/tasks/build/_jkube-helm.adoc index 1a50b04334..b5f850a7cf 100644 --- a/gradle-plugin/doc/src/main/asciidoc/inc/tasks/build/_jkube-helm.adoc +++ b/gradle-plugin/doc/src/main/asciidoc/inc/tasks/build/_jkube-helm.adoc @@ -126,11 +126,13 @@ In a next step you can install this via the https://github.com/helm/helm/release [source, sh, subs="+attributes"] ---- -helm install nameForChartInRepository build/jkube/helm/${chartName}/kubernetes +helm install nameForChartInRepository build/jkube/helm/${chartName}/{cluster} ---- or + +[source, sh, subs="+attributes"] ---- -helm install build/jkube/helm/${chartName}/kubernetes --generate-name +helm install build/jkube/helm/${chartName}/{cluster} --generate-name ---- In addition, this task will also create a tar-archive below `${basedir}/build` which contains the chart with its template. diff --git a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesHelmTask.java b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesHelmTask.java index 9c0c8f6f46..18f8997620 100644 --- a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesHelmTask.java +++ b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesHelmTask.java @@ -32,16 +32,28 @@ public KubernetesHelmTask(Class extensionClass) { @Override public void run() { try { - File manifest = kubernetesExtension.getKubernetesManifestOrDefault(); + File manifest = resolveManifest(); if (manifest == null || !manifest.isFile()) { - kitLogger.warn("No kubernetes manifest file has been generated yet by the k8sResource task at: " + manifest); + logManifestNotFoundWarning(manifest); } HelmConfig helm = initHelmConfig(kubernetesExtension.getDefaultHelmType(), kubernetesExtension.javaProject, - kubernetesExtension.getKubernetesManifestOrDefault(), kubernetesExtension.getKubernetesTemplateOrDefault(), + resolveManifest(), resolveTemplate(), kubernetesExtension.helm).build(); jKubeServiceHub.getHelmService().generateHelmCharts(helm); } catch (IOException exception) { throw new IllegalStateException(exception.getMessage()); } } + + protected File resolveManifest() { + return kubernetesExtension.getKubernetesManifestOrDefault(); + } + + protected File resolveTemplate() { + return kubernetesExtension.getKubernetesTemplateOrDefault(); + } + + protected void logManifestNotFoundWarning(File manifest) { + kitLogger.warn("No kubernetes manifest file has been generated yet by the k8sResource task at: " + manifest); + } } diff --git a/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/OpenShiftExtension.java b/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/OpenShiftExtension.java index 351490d220..e29f20ab9c 100644 --- a/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/OpenShiftExtension.java +++ b/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/OpenShiftExtension.java @@ -25,11 +25,13 @@ import org.eclipse.jkube.kit.config.resource.RuntimeMode; import io.fabric8.kubernetes.client.KubernetesClient; +import org.eclipse.jkube.kit.resource.helm.HelmConfig; import org.gradle.api.provider.Property; public abstract class OpenShiftExtension extends KubernetesExtension { public static final Path DEFAULT_OPENSHIFT_MANIFEST = Paths.get("META-INF","jkube","openshift.yml"); + private static final Path DEFAULT_OPENSHIFT_TEMPLATE = Paths.get("META-INF", "jkube", "openshift"); private static final String DEFAULT_OPENSHIFT_PULLSECRET = "pullsecret-jkube"; private static final String DEFAULT_S2I_BUILDNAME_SUFFIX = "-s2i"; public static final String DEFAULT_BUILD_OUTPUT_KIND = "ImageStreamTag"; @@ -108,6 +110,15 @@ public boolean isSupportOAuthClients() { return true; } + @Override + public HelmConfig.HelmType getDefaultHelmType() { + return HelmConfig.HelmType.OPENSHIFT; + } + + public File getOpenShiftTemplateOrDefault() { + return getOrDefaultFile("jkube.kubernetesTemplate", this::getKubernetesTemplate, javaProject.getOutputDirectory().toPath().resolve(DEFAULT_OPENSHIFT_TEMPLATE).toFile()); + } + public File getOpenShiftManifestOrDefault() { return getOrDefaultFile("jkube.openshiftManifest", this::getOpenShiftManifest, javaProject.getOutputDirectory().toPath().resolve(DEFAULT_OPENSHIFT_MANIFEST).toFile()); } diff --git a/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/OpenShiftPlugin.java b/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/OpenShiftPlugin.java index 1898b2e435..aa82ab8ccd 100644 --- a/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/OpenShiftPlugin.java +++ b/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/OpenShiftPlugin.java @@ -27,6 +27,7 @@ import org.eclipse.jkube.gradle.plugin.task.OpenShiftApplyTask; import org.eclipse.jkube.gradle.plugin.task.OpenShiftBuildTask; import org.eclipse.jkube.gradle.plugin.task.OpenShiftDebugTask; +import org.eclipse.jkube.gradle.plugin.task.OpenShiftHelmTask; import org.eclipse.jkube.gradle.plugin.task.OpenShiftPushTask; import org.eclipse.jkube.gradle.plugin.task.OpenShiftResourceTask; import org.eclipse.jkube.gradle.plugin.task.OpenShiftUndeployTask; @@ -47,6 +48,7 @@ public Map>> getTaskPrecedence() { ret.put("ocDebug", Arrays.asList(KubernetesBuildTask.class, OpenShiftBuildTask.class, KubernetesResourceTask.class, OpenShiftResourceTask.class, KubernetesApplyTask.class, OpenShiftApplyTask.class)); ret.put("ocPush", Arrays.asList(KubernetesBuildTask.class, OpenShiftBuildTask.class)); + ret.put("ocHelm", Arrays.asList(KubernetesResourceTask.class, OpenShiftResourceTask.class)); return ret; } @@ -60,6 +62,7 @@ protected void jKubeApply(Project project) { register(project, "ocPush", OpenShiftPushTask.class); register(project, "ocResource", OpenShiftResourceTask.class); register(project, "ocUndeploy", OpenShiftUndeployTask.class); + register(project, "ocHelm", OpenShiftHelmTask.class); } } diff --git a/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftHelmTask.java b/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftHelmTask.java new file mode 100644 index 0000000000..e305d4e6c4 --- /dev/null +++ b/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftHelmTask.java @@ -0,0 +1,44 @@ +/** + * 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; +import java.io.File; + +public class OpenShiftHelmTask extends KubernetesHelmTask implements OpenShiftJKubeTask { + + @Inject + public OpenShiftHelmTask(Class extensionClass) { + super(extensionClass); + setDescription( + "Generates a Helm chart for the OpenShift resources."); + } + + @Override + protected File resolveManifest() { + return getOpenShiftExtension().getOpenShiftManifestOrDefault(); + } + + @Override + protected File resolveTemplate() { + return getOpenShiftExtension().getOpenShiftTemplateOrDefault(); + } + + @Override + protected void logManifestNotFoundWarning(File manifest) { + kitLogger.warn("No OpenShift manifest file has been generated yet by the ocResource task at: " + manifest); + } +} diff --git a/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftPluginRegisterTaskTest.java b/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftPluginRegisterTaskTest.java index b612f1c588..f72ffa4a00 100644 --- a/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftPluginRegisterTaskTest.java +++ b/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftPluginRegisterTaskTest.java @@ -21,6 +21,7 @@ import org.eclipse.jkube.gradle.plugin.task.OpenShiftApplyTask; import org.eclipse.jkube.gradle.plugin.task.OpenShiftBuildTask; import org.eclipse.jkube.gradle.plugin.task.OpenShiftDebugTask; +import org.eclipse.jkube.gradle.plugin.task.OpenShiftHelmTask; import org.eclipse.jkube.gradle.plugin.task.OpenShiftPushTask; import org.eclipse.jkube.gradle.plugin.task.OpenShiftResourceTask; import org.eclipse.jkube.gradle.plugin.task.OpenShiftUndeployTask; @@ -50,7 +51,8 @@ public static Collection data() { new Object[] { "ocLog", KubernetesLogTask.class }, new Object[] { "ocPush", OpenShiftPushTask.class }, new Object[] { "ocResource", OpenShiftResourceTask.class }, - new Object[] { "ocUndeploy", OpenShiftUndeployTask.class }); + new Object[] { "ocUndeploy", OpenShiftUndeployTask.class }, + new Object[] { "ocHelm", OpenShiftHelmTask.class}); } @Parameterized.Parameter diff --git a/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftPluginTest.java b/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftPluginTest.java index ced8609f20..60358559ad 100644 --- a/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftPluginTest.java +++ b/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftPluginTest.java @@ -37,10 +37,11 @@ public void configurePrecedence_withValidProject_shouldReturnTaskPrecedence() { final Map>> result = new OpenShiftPlugin().getTaskPrecedence(); // Then assertThat(result) - .hasSize(3) + .hasSize(4) .containsEntry("ocApply", Arrays.asList(KubernetesResourceTask.class, OpenShiftResourceTask.class)) .containsEntry("ocDebug", Arrays.asList(KubernetesBuildTask.class, OpenShiftBuildTask.class, KubernetesResourceTask.class, OpenShiftResourceTask.class, KubernetesApplyTask.class, OpenShiftApplyTask.class)) - .containsEntry("ocPush", Arrays.asList(KubernetesBuildTask.class, OpenShiftBuildTask.class)); + .containsEntry("ocPush", Arrays.asList(KubernetesBuildTask.class, OpenShiftBuildTask.class)) + .containsEntry("ocHelm", Arrays.asList(KubernetesResourceTask.class, OpenShiftResourceTask.class)); } } diff --git a/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftHelmTaskTest.java b/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftHelmTaskTest.java new file mode 100644 index 0000000000..ce7f81780c --- /dev/null +++ b/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftHelmTaskTest.java @@ -0,0 +1,79 @@ +/** + * 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 org.eclipse.jkube.gradle.plugin.TestOpenShiftExtension; +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 OpenShiftHelmTaskTest { + @Rule + public TaskEnvironment taskEnvironment = new TaskEnvironment(); + + private TestOpenShiftExtension extension; + private MockedConstruction helmServiceMockedConstruction; + + @Before + public void setUp() { + extension = new TestOpenShiftExtension(); + helmServiceMockedConstruction = mockConstruction(HelmService.class); + when(taskEnvironment.project.getExtensions().getByType(OpenShiftExtension.class)).thenReturn(extension); + } + + @Test + public void runTask_withNoTemplateDir_shouldThrowException() { + // Given + OpenShiftHelmTask kubernetesHelmTask = new OpenShiftHelmTask(OpenShiftExtension.class); + + // When + IllegalStateException illegalStateException = assertThrows(IllegalStateException.class, kubernetesHelmTask::runTask); + + // Then + assertThat(illegalStateException) + .hasMessageContaining("META-INF/jkube/openshift (No such file or directory)"); + } + + @Test + public void runTask_withTemplateDir_shouldCallHelmService() throws IOException { + // Given + taskEnvironment.withOpenShiftTemplate(); + OpenShiftHelmTask kubernetesHelmTask = new OpenShiftHelmTask(OpenShiftExtension.class); + + // When + kubernetesHelmTask.runTask(); + + // Then + verify((helmServiceMockedConstruction.constructed().iterator().next()), times(1)).generateHelmCharts(any()); + } + + @After + public void tearDown() { + helmServiceMockedConstruction.close(); + } +} diff --git a/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/task/TaskEnvironment.java b/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/task/TaskEnvironment.java index a7e976d7fc..3b85516c65 100644 --- a/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/task/TaskEnvironment.java +++ b/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/task/TaskEnvironment.java @@ -62,4 +62,8 @@ public void withOpenShiftManifest() throws IOException { final File manifestsDir = newFolder("build", "classes", "java", "main", "META-INF", "jkube"); FileUtils.touch(new File(manifestsDir, "openshift.yml").toPath()); } + + public void withOpenShiftTemplate() throws IOException { + newFolder("build", "classes", "java", "main", "META-INF", "jkube", "openshift"); + } } diff --git a/quickstarts/gradle/spring-boot-helm/build.gradle b/quickstarts/gradle/spring-boot-helm/build.gradle index e95728ac10..d53202f84b 100644 --- a/quickstarts/gradle/spring-boot-helm/build.gradle +++ b/quickstarts/gradle/spring-boot-helm/build.gradle @@ -41,3 +41,26 @@ kubernetes { }] } } + +openshift { + resources { + labels { + all { + testProject = 'spring-boot-with-yaml-label-for-all' + } + } + } + generator { + config { + 'spring-boot' { + color = 'always' + } + } + } + helm { + maintainers = [{ + name = 'John' + email = 'john.doe@example.com' + }] + } +}