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..2eb8b139ab 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 @@ -34,14 +34,18 @@ 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); + logManifestNotFoundWarning(manifest); } HelmConfig helm = initHelmConfig(kubernetesExtension.getDefaultHelmType(), kubernetesExtension.javaProject, - kubernetesExtension.getKubernetesManifestOrDefault(), kubernetesExtension.getKubernetesTemplateOrDefault(), + kubernetesExtension.getKubernetesManifestOrDefault(), kubernetesExtension.getKubernetesTemplateOrDefault(), kubernetesExtension.helm).build(); jKubeServiceHub.getHelmService().generateHelmCharts(helm); } catch (IOException exception) { throw new IllegalStateException(exception.getMessage()); } } + + 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/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesUndeployTask.java b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesUndeployTask.java index fde499cfe2..7ff7c50a75 100644 --- a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesUndeployTask.java +++ b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesUndeployTask.java @@ -52,13 +52,9 @@ public void run() { } } - protected File findResources() { - return kubernetesExtension.getKubernetesManifestOrDefault(); - } - protected List findManifestsToUndeploy() { final List ret = new ArrayList<>(); - ret.add(findResources()); + ret.add(kubernetesExtension.getKubernetesManifestOrDefault()); return ret; } } \ No newline at end of file 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..9c09167199 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"; @@ -71,9 +73,9 @@ public ResourceClassifier getResourceClassifier() { @Override public File getManifest(KitLogger kitLogger, KubernetesClient kubernetesClient) { if (OpenshiftHelper.isOpenShift(kubernetesClient)) { - return getOpenShiftManifestOrDefault(); + return getKubernetesManifestOrDefault(); } - return getKubernetesManifestOrDefault(); + return super.getKubernetesManifestOrDefault(); } @Override @@ -108,7 +110,18 @@ public boolean isSupportOAuthClients() { return true; } - public File getOpenShiftManifestOrDefault() { + @Override + public HelmConfig.HelmType getDefaultHelmType() { + return HelmConfig.HelmType.OPENSHIFT; + } + + @Override + public File getKubernetesTemplateOrDefault() { + return getOrDefaultFile("jkube.kubernetesTemplate", this::getKubernetesTemplate, javaProject.getOutputDirectory().toPath().resolve(DEFAULT_OPENSHIFT_TEMPLATE).toFile()); + } + + @Override + public File getKubernetesManifestOrDefault() { 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..afbc005780 --- /dev/null +++ b/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftHelmTask.java @@ -0,0 +1,34 @@ +/** + * 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 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/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftUndeployTask.java b/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftUndeployTask.java index 555167c200..c5a8760268 100644 --- a/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftUndeployTask.java +++ b/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftUndeployTask.java @@ -27,11 +27,6 @@ public OpenShiftUndeployTask(Class extensionClass) "Undeploys (deletes) the OpenShift resources generated by the current project."); } - @Override - protected File findResources() { - return getOpenShiftExtension().getOpenShiftManifestOrDefault(); - } - @Override protected List findManifestsToUndeploy() { final List ret = super.findManifestsToUndeploy(); diff --git a/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftExtensionPropertyTest.java b/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftExtensionPropertyTest.java index 9c3fefca0f..93656dbd24 100644 --- a/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftExtensionPropertyTest.java +++ b/gradle-plugin/openshift/src/test/java/org/eclipse/jkube/gradle/plugin/OpenShiftExtensionPropertyTest.java @@ -16,10 +16,7 @@ import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -50,7 +47,7 @@ public static Collection data() { new Object[] { "getBuildOutputKindOrDefault", "jkube.build.buildOutput.kind", "DockerImage", "DockerImage", "ImageStreamTag" }, new Object[] { "getProcessTemplatesLocallyOrDefault", "jkube.deploy.processTemplatesLocally", "true", true, false }, - new Object[] { "getOpenShiftManifestOrDefault", "jkube.openshiftManifest", + new Object[] { "getKubernetesManifestOrDefault", "jkube.openshiftManifest", Paths.get("META-INF", "jkube", "other.yml").toString(), Paths.get("META-INF", "jkube", "other.yml").toFile(), new File(BASE, "build").toPath().resolve(Paths.get("META-INF", "jkube", "openshift.yml")).toFile() }, @@ -58,6 +55,11 @@ public static Collection data() { Paths.get("test-project-is.yml").toString(), Paths.get("test-project-is.yml").toFile(), new File(BASE, "build").toPath().resolve("artifact-id-is.yml").toFile() + }, + new Object[] { "getKubernetesTemplateOrDefault", "jkube.kubernetesTemplate", + Paths.get("META-INF", "jkube", "other").toString(), + Paths.get("META-INF", "jkube", "other").toFile(), + new File(BASE, "build").toPath().resolve(Paths.get("META-INF", "jkube", "openshift")).toFile() } ); } 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/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmMojo.java index 711f3ada3c..d4c3883674 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmMojo.java @@ -57,7 +57,7 @@ public void executeInternal() throws MojoExecutionException { try { File manifest = getKubernetesManifest(); if (manifest == null || !manifest.isFile()) { - getKitLogger().warn("No kubernetes manifest file has been generated yet by the kubernetes:resource goal at: " + manifest); + logManifestNotFoundWarning(manifest); } helm = initHelmConfig(getDefaultHelmType(), javaProject, getKubernetesManifest(), getKubernetesTemplate(), helm) .generatedChartListeners(Collections.singletonList((helmConfig, type, chartFile) -> projectHelper @@ -69,6 +69,10 @@ public void executeInternal() throws MojoExecutionException { } } + protected void logManifestNotFoundWarning(File manifest) { + getKitLogger().warn("No kubernetes manifest file has been generated yet by the k8s:resource goal at: " + manifest); + } + protected File getKubernetesManifest() { return kubernetesManifest; } diff --git a/openshift-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/OpenshiftHelmMojo.java b/openshift-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/OpenshiftHelmMojo.java index 755cdcff92..f0bd0bff74 100644 --- a/openshift-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/OpenshiftHelmMojo.java +++ b/openshift-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/OpenshiftHelmMojo.java @@ -27,13 +27,13 @@ public class OpenshiftHelmMojo extends HelmMojo { /** * The generated kubernetes YAML file */ - @Parameter(property = "jkube.kubernetesManifest", defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift.yml") + @Parameter(property = "jkube.openshiftManifest", defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift.yml") private File openShiftManifest; /** * The generated kubernetes YAML file */ - @Parameter(property = "jkube.kubernetesManifest", defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift") + @Parameter(property = "jkube.kubernetesTemplate", defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift") private File openShiftTemplate; @Override @@ -55,4 +55,9 @@ protected HelmConfig.HelmType getDefaultHelmType() { protected String getLogPrefix() { return OpenShift.DEFAULT_LOG_PREFIX; } + + @Override + protected void logManifestNotFoundWarning(File manifest) { + getKitLogger().warn("No openshift manifest file has been generated yet by the oc:resource goal at: " + manifest); + } } 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' + }] + } +}