Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1053: Add ocHelm task to OpenShift Gradle Plugin #1072

Merged
merged 5 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,9 @@ public void run() {
}
}

protected File findResources() {
return kubernetesExtension.getKubernetesManifestOrDefault();
}

protected List<File> findManifestsToUndeploy() {
final List<File> ret = new ArrayList<>();
ret.add(findResources());
ret.add(kubernetesExtension.getKubernetesManifestOrDefault());
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
Comment on lines +119 to +121
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for this in KubernetesExtensionPropertyTest + OpenShiftExtensionPropertyTest

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a test already in KubernetesExtensionPropertyTest, I'll add the test in OpenShiftExtensionPropertyTest

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I was checking on an outdated branch


@Override
public File getKubernetesManifestOrDefault() {
return getOrDefaultFile("jkube.openshiftManifest", this::getOpenShiftManifest, javaProject.getOutputDirectory().toPath().resolve(DEFAULT_OPENSHIFT_MANIFEST).toFile());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,6 +48,7 @@ public Map<String, Collection<Class<? extends Task>>> 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;
}

Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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<? extends OpenShiftExtension> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public OpenShiftUndeployTask(Class<? extends OpenShiftExtension> extensionClass)
"Undeploys (deletes) the OpenShift resources generated by the current project.");
}

@Override
protected File findResources() {
return getOpenShiftExtension().getOpenShiftManifestOrDefault();
}

@Override
protected List<File> findManifestsToUndeploy() {
final List<File> ret = super.findManifestsToUndeploy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -50,14 +47,19 @@ public static Collection<Object[]> 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() },
new Object[] { "getImageStreamManifestOrDefault", "jkube.openshiftImageStreamManifest",
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()
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +51,8 @@ public static Collection<Object[]> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ public void configurePrecedence_withValidProject_shouldReturnTaskPrecedence() {
final Map<String, Collection<Class<? extends Task>>> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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<HelmService> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
23 changes: 23 additions & 0 deletions quickstarts/gradle/spring-boot-helm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]'
}]
}
}