Skip to content

Commit

Permalink
Fix eclipse-jkube#1053: Add ocHelm task to OpenShift Gradle Plugin
Browse files Browse the repository at this point in the history
Implement `oc:helm` equivalent functionality `ocHelm` for OpenShift
Gradle Plugin.

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Oct 27, 2021
1 parent 93d556d commit e1030d3
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 8 deletions.
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 @@ -32,16 +32,28 @@ public KubernetesHelmTask(Class<? extends KubernetesExtension> 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);
}
}
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 @@ -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());
}
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,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<? extends OpenShiftExtension> 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);
}
}
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");
}
}
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]'
}]
}
}

0 comments on commit e1030d3

Please sign in to comment.