Skip to content

Commit

Permalink
Introduce AbstractHelmMojo class
Browse files Browse the repository at this point in the history
When new Helm goals will be written, this class will serve as a base
class. Previously, the HelmMojo class served as both a Helm goal and a
base class for other Helm goals (like helm-push and helm-lint).

Signed-off-by: Jurrie Overgoor <eclipse.org@jurr.org>
  • Loading branch information
Jurrie committed Feb 21, 2024
1 parent fddd604 commit 9d76dfc
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class KubernetesHelmPushTask extends AbstractJKubeTask {
@Inject
public KubernetesHelmPushTask(Class<? extends KubernetesExtension> extensionClass) {
super(extensionClass);
setDescription("Upload a helm chart to specified helm repository.");
setDescription("Upload a Helm chart to specified Helm repository.");
}

@Override
Expand All @@ -37,10 +37,10 @@ public void run() {
HelmConfig helm = initHelmConfig(kubernetesExtension.getDefaultHelmType(), kubernetesExtension.javaProject,
kubernetesExtension.getKubernetesTemplateOrDefault(),
kubernetesExtension.helm).build();
helm = initHelmPushConfig(helm, kubernetesExtension.javaProject);
initHelmPushConfig(helm, kubernetesExtension.javaProject);
jKubeServiceHub.getHelmService().uploadHelmChart(helm);
} catch (Exception exp) {
kitLogger.error("Error performing helm push", exp);
kitLogger.error("Error performing Helm push", exp);
throw new IllegalStateException(exp.getMessage(), exp);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.jkube.kit.common.Maintainer;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -67,7 +68,8 @@ public class HelmConfig {
private String outputDir;
private String tarballOutputDir;
private String tarFileClassifier;
private List<GeneratedChartListener> generatedChartListeners;
@Builder.Default
private List<GeneratedChartListener> generatedChartListeners = new ArrayList<>();
private HelmRepository stableRepository;
private HelmRepository snapshotRepository;
private String security;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,11 @@ public static HelmConfig.HelmConfigBuilder initHelmConfig(
return helmConfig.toBuilder();
}

public static HelmConfig initHelmPushConfig(HelmConfig helmConfig, JavaProject project) {
if (helmConfig == null) {
helmConfig = new HelmConfig();
}

public static void initHelmPushConfig(HelmConfig helmConfig, JavaProject project) {
helmConfig.setStableRepository(initHelmRepository(helmConfig.getStableRepository(), project, STABLE_REPOSITORY));
helmConfig.setSnapshotRepository(initHelmRepository(helmConfig.getSnapshotRepository(), project, SNAPSHOT_REPOSITORY));

helmConfig.setSecurity(resolveFromPropertyOrDefault(PROPERTY_SECURITY, project, helmConfig::getSecurity, () -> DEFAULT_SECURITY));
return helmConfig;
}

static HelmRepository initHelmRepository(HelmRepository helmRepository, JavaProject project, String repositoryType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.maven.plugin.mojo.build;

import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmConfig;

import java.io.File;
import java.io.IOException;

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.jkube.kit.resource.helm.HelmConfig;

public abstract class AbstractHelmMojo extends AbstractJKubeMojo {

/**
* One of:
* <ul>
* <li>A directory containing OpenShift Templates to use as Helm parameters.</li>
* <li>A file containing a Kubernetes List with OpenShift Template entries to be used as Helm parameters.</li>
* </ul>
*/
@Parameter(property = "jkube.kubernetesTemplate", defaultValue = "${basedir}/target/classes/META-INF/jkube/kubernetes")
File kubernetesTemplate;

@Parameter
HelmConfig helm;

@Override
public void init() throws MojoFailureException {
super.init();

try {
helm = initHelmConfig(getDefaultHelmType(), javaProject, getKubernetesTemplate(), helm).build();
} catch (IOException e) {
throw new MojoFailureException(e.getMessage(), e);

Check warning on line 47 in kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractHelmMojo.java

View check run for this annotation

Codecov / codecov/patch

kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractHelmMojo.java#L46-L47

Added lines #L46 - L47 were not covered by tests
}
}

protected File getKubernetesTemplate() {
return kubernetesTemplate;
}

protected HelmConfig.HelmType getDefaultHelmType() {
return HelmConfig.HelmType.KUBERNETES;
}

protected HelmConfig getHelm() {
return helm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,22 @@ public abstract class AbstractJKubeMojo extends AbstractMojo implements KitLogge

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
init();
if (shouldSkip()) {
log.info("`%s` goal is skipped.", mojoExecution.getMojoDescriptor().getFullGoalName());
return;
}
executeInternal();
} catch (DependencyResolutionRequiredException e) {
throw new MojoFailureException(e.getMessage());
init();
if (shouldSkip()) {
log.info("`%s` goal is skipped.", mojoExecution.getMojoDescriptor().getFullGoalName());
return;
}
executeInternal();
}

protected void init() throws DependencyResolutionRequiredException {
protected void init() throws MojoFailureException {
log = createLogger(null);
clusterAccess = new ClusterAccess(initClusterConfiguration());
javaProject = MavenUtil.convertMavenProjectToJKubeProject(project, session);
try {
javaProject = MavenUtil.convertMavenProjectToJKubeProject(project, session);
} catch (DependencyResolutionRequiredException e) {
throw new MojoFailureException(e.getMessage());

Check warning on line 171 in kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractJKubeMojo.java

View check run for this annotation

Codecov / codecov/patch

kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractJKubeMojo.java#L170-L171

Added lines #L170 - L171 were not covered by tests
}
jkubeServiceHub = initJKubeServiceHubBuilder(javaProject).build();
resources = updateResourceConfigNamespace(namespace, resources);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,11 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

import java.io.IOException;

import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmConfig;

@Mojo(name = "helm-lint", defaultPhase = LifecyclePhase.INTEGRATION_TEST, requiresDependencyResolution = ResolutionScope.COMPILE)
public class HelmLintMojo extends HelmMojo {
public class HelmLintMojo extends AbstractHelmMojo {

@Override
public void executeInternal() throws MojoExecutionException {
try {
helm = initHelmConfig(getDefaultHelmType(), javaProject, getKubernetesTemplate(), helm)
.build();
jkubeServiceHub.getHelmService().lint(helm);
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}

jkubeServiceHub.getHelmService().lint(getHelm());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,81 +15,64 @@

import java.io.File;
import java.io.IOException;
import java.util.Collections;

import org.eclipse.jkube.kit.resource.helm.HelmConfig;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProjectHelper;

import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmConfig;
import org.eclipse.jkube.kit.resource.helm.GeneratedChartListener;
import org.eclipse.jkube.kit.resource.helm.HelmConfig;

/**
* Generates a Helm chart for the kubernetes resources
* Generates a Helm chart for the Kubernetes resources
*/
@Mojo(name = "helm", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST)
public class HelmMojo extends AbstractJKubeMojo {

@Component
MavenProjectHelper projectHelper;
public class HelmMojo extends AbstractHelmMojo {

/**
* The generated kubernetes YAML file
* The generated Kubernetes YAML file
*/
@Parameter(property = "jkube.kubernetesManifest", defaultValue = "${basedir}/target/classes/META-INF/jkube/kubernetes.yml")
File kubernetesManifest;
private File kubernetesManifest;

/**
* One of:
* <ul>
* <li>A directory containing OpenShift Templates to use as Helm parameters.</li>
* <li>A file containing a Kubernetes List with OpenShift Template entries to be used as Helm parameters.</li>
* </ul>
*/
@Parameter(property = "jkube.kubernetesTemplate", defaultValue = "${basedir}/target/classes/META-INF/jkube/kubernetes")
File kubernetesTemplate;
@Component
MavenProjectHelper projectHelper;

@Parameter
HelmConfig helm;
@Override
public void init() throws MojoFailureException {
super.init();

final File manifest = getKubernetesManifest();
if (manifest == null || !manifest.isFile()) {
logManifestNotFoundWarning(manifest);
}

final GeneratedChartListener generatedChartListener = (helmConfig, type, chartFile) -> projectHelper.attachArtifact(project, helmConfig.getChartExtension(), type.getClassifier(), chartFile);
getHelm().getGeneratedChartListeners().add(generatedChartListener);
}

@Override
public void executeInternal() throws MojoExecutionException {
try {
final File manifest = getKubernetesManifest();
if (manifest == null || !manifest.isFile()) {
logManifestNotFoundWarning(manifest);
}
helm = initHelmConfig(getDefaultHelmType(), javaProject, getKubernetesTemplate(), helm)
.generatedChartListeners(Collections.singletonList((helmConfig, type, chartFile) -> projectHelper
.attachArtifact(project, helmConfig.getChartExtension(), type.getClassifier(), chartFile)))
.build();
jkubeServiceHub.getHelmService().generateHelmCharts(helm);
jkubeServiceHub.getHelmService().generateHelmCharts(getHelm());
} catch (IOException exception) {
throw new MojoExecutionException(exception.getMessage());
}
}

protected void logManifestNotFoundWarning(File manifest) {
getKitLogger().warn("No kubernetes manifest file has been generated yet by the k8s:resource goal at: " + manifest);
getKitLogger().warn("No Kubernetes manifest file has been generated yet by the k8s:resource goal at: " + manifest);
}

protected File getKubernetesManifest() {
return kubernetesManifest;
}

protected File getKubernetesTemplate() {
return kubernetesTemplate;
}

@Override
protected HelmConfig.HelmType getDefaultHelmType() {
return HelmConfig.HelmType.KUBERNETES;
}

HelmConfig getHelm() {
return helm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jkube.maven.plugin.mojo.build;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
Expand All @@ -22,24 +23,25 @@
import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmPushConfig;

@Mojo(name = "helm-push", defaultPhase = LifecyclePhase.INSTALL, requiresDependencyResolution = ResolutionScope.COMPILE)
public class HelmPushMojo extends HelmMojo {
public class HelmPushMojo extends AbstractHelmMojo {

@Override
public void init() throws MojoFailureException {
super.init();

initHelmPushConfig(helm, javaProject);
}

@Override
public void executeInternal() throws MojoExecutionException {
if (skip) {
return;
}
try {
super.executeInternal();
helm = initHelmPushConfig(helm, javaProject);
if (securityDispatcher instanceof DefaultSecDispatcher) {
((DefaultSecDispatcher) securityDispatcher).setConfigurationFile(helm.getSecurity());
((DefaultSecDispatcher) securityDispatcher).setConfigurationFile(getHelm().getSecurity());

Check warning on line 39 in kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmPushMojo.java

View check run for this annotation

Codecov / codecov/patch

kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmPushMojo.java#L39

Added line #L39 was not covered by tests
}
jkubeServiceHub.getHelmService().uploadHelmChart(helm);
jkubeServiceHub.getHelmService().uploadHelmChart(getHelm());
} catch (Exception exp) {
getKitLogger().error("Error performing helm push", exp);
getKitLogger().error("Error performing Helm push", exp);
throw new MojoExecutionException(exp.getMessage(), exp);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,22 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.eclipse.jkube.kit.resource.helm.HelmConfig;
import org.eclipse.jkube.maven.plugin.mojo.OpenShift;

import java.io.File;

@Mojo(name = "helm-lint", defaultPhase = LifecyclePhase.INTEGRATION_TEST, requiresDependencyResolution = ResolutionScope.COMPILE)
public class OpenshiftHelmLintMojo extends HelmLintMojo {

/**
* The generated kubernetes YAML file
* One of:
* <ul>
* <li>A directory containing OpenShift Templates to use as Helm parameters.</li>
* <li>A file containing a Kubernetes List with OpenShift Template entries to be used as Helm parameters.</li>
* </ul>
*/
@Parameter(property = "jkube.kubernetesManifest", 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.openshiftTemplate", defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift")
private File openShiftTemplate;

@Override
protected File getKubernetesManifest() {
return openShiftManifest;
}

@Override
protected File getKubernetesTemplate() {
Expand All @@ -51,9 +44,4 @@ protected File getKubernetesTemplate() {
protected HelmConfig.HelmType getDefaultHelmType() {
return HelmConfig.HelmType.OPENSHIFT;
}

@Override
protected String getLogPrefix() {
return OpenShift.DEFAULT_LOG_PREFIX;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class OpenshiftHelmMojo extends HelmMojo {

/**
* The generated kubernetes YAML file
* The generated Kubernetes YAML file
*/
@Parameter(property = "jkube.openshiftManifest", defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift.yml")
private File openShiftManifest;
Expand Down Expand Up @@ -62,6 +62,6 @@ protected String getLogPrefix() {

@Override
protected void logManifestNotFoundWarning(File manifest) {
getKitLogger().warn("No openshift manifest file has been generated yet by the oc:resource goal at: " + manifest);
getKitLogger().warn("No OpenShift manifest file has been generated yet by the oc:resource goal at: " + manifest);

Check warning on line 65 in openshift-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/OpenshiftHelmMojo.java

View check run for this annotation

Codecov / codecov/patch

openshift-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/OpenshiftHelmMojo.java#L65

Added line #L65 was not covered by tests
}
}
Loading

0 comments on commit 9d76dfc

Please sign in to comment.