Skip to content

Commit c1e60d8

Browse files
Jurriearsenalzp
authored andcommitted
refactor: introduce AbstractHelmMojo class (eclipse-jkube#2714)
* Introduce AbstractHelmMojo class 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). * Implement pull request comments * Add a quick note on the separate integration tests repository
1 parent e6a8b7f commit c1e60d8

File tree

12 files changed

+136
-113
lines changed

12 files changed

+136
-113
lines changed

BUILDING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ The affected profiles will ["delombok"](http://anthonywhitford.com/lombok.maven/
1616
the source code before generating the documentation in order to spread the
1717
"condensed" javadoc declared in the Lombok affected fields.
1818

19+
## Integration tests
20+
Apart from tests that are available in this repository, there is a separate repository with intergration tests [available here](https://github.com/jkubeio/jkube-integration-tests).
21+
1922
## Release process
2023
Release process has to be performed in a project fork.
2124

gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesHelmPushTask.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class KubernetesHelmPushTask extends AbstractJKubeTask {
2525
@Inject
2626
public KubernetesHelmPushTask(Class<? extends KubernetesExtension> extensionClass) {
2727
super(extensionClass);
28-
setDescription("Upload a helm chart to specified helm repository.");
28+
setDescription("Upload a Helm chart to specified Helm repository.");
2929
}
3030

3131
@Override
@@ -37,10 +37,10 @@ public void run() {
3737
HelmConfig helm = initHelmConfig(kubernetesExtension.getDefaultHelmType(), kubernetesExtension.javaProject,
3838
kubernetesExtension.getKubernetesTemplateOrDefault(),
3939
kubernetesExtension.helm).build();
40-
helm = initHelmPushConfig(helm, kubernetesExtension.javaProject);
40+
initHelmPushConfig(helm, kubernetesExtension.javaProject);
4141
jKubeServiceHub.getHelmService().uploadHelmChart(helm);
4242
} catch (Exception exp) {
43-
kitLogger.error("Error performing helm push", exp);
43+
kitLogger.error("Error performing Helm push", exp);
4444
throw new IllegalStateException(exp.getMessage(), exp);
4545
}
4646
}

jkube-kit/helm/src/main/java/org/eclipse/jkube/kit/resource/helm/HelmConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.eclipse.jkube.kit.common.Maintainer;
2626

2727
import java.io.File;
28+
import java.util.ArrayList;
2829
import java.util.Collections;
2930
import java.util.List;
3031
import java.util.Optional;
@@ -67,7 +68,8 @@ public class HelmConfig {
6768
private String outputDir;
6869
private String tarballOutputDir;
6970
private String tarFileClassifier;
70-
private List<GeneratedChartListener> generatedChartListeners;
71+
@Builder.Default
72+
private List<GeneratedChartListener> generatedChartListeners = new ArrayList<>();
7173
private HelmRepository stableRepository;
7274
private HelmRepository snapshotRepository;
7375
private String security;

jkube-kit/helm/src/main/java/org/eclipse/jkube/kit/resource/helm/HelmServiceUtil.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,11 @@ public static HelmConfig.HelmConfigBuilder initHelmConfig(
120120
return helmConfig.toBuilder();
121121
}
122122

123-
public static HelmConfig initHelmPushConfig(HelmConfig helmConfig, JavaProject project) {
124-
if (helmConfig == null) {
125-
helmConfig = new HelmConfig();
126-
}
127-
123+
public static void initHelmPushConfig(HelmConfig helmConfig, JavaProject project) {
128124
helmConfig.setStableRepository(initHelmRepository(helmConfig.getStableRepository(), project, STABLE_REPOSITORY));
129125
helmConfig.setSnapshotRepository(initHelmRepository(helmConfig.getSnapshotRepository(), project, SNAPSHOT_REPOSITORY));
130126

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

135130
static HelmRepository initHelmRepository(HelmRepository helmRepository, JavaProject project, String repositoryType) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2019 Red Hat, Inc.
3+
* This program and the accompanying materials are made
4+
* available under the terms of the Eclipse Public License 2.0
5+
* which is available at:
6+
*
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Red Hat, Inc. - initial API and implementation
13+
*/
14+
package org.eclipse.jkube.maven.plugin.mojo.build;
15+
16+
import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmConfig;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
21+
import org.apache.maven.plugin.MojoFailureException;
22+
import org.apache.maven.plugins.annotations.Parameter;
23+
import org.eclipse.jkube.kit.resource.helm.HelmConfig;
24+
25+
public abstract class AbstractHelmMojo extends AbstractJKubeMojo {
26+
27+
/**
28+
* One of:
29+
* <ul>
30+
* <li>A directory containing OpenShift Templates to use as Helm parameters.</li>
31+
* <li>A file containing a Kubernetes List with OpenShift Template entries to be used as Helm parameters.</li>
32+
* </ul>
33+
*/
34+
@Parameter(property = "jkube.kubernetesTemplate", defaultValue = "${basedir}/target/classes/META-INF/jkube/kubernetes")
35+
File kubernetesTemplate;
36+
37+
@Parameter
38+
HelmConfig helm;
39+
40+
@Override
41+
public void init() throws MojoFailureException {
42+
super.init();
43+
44+
try {
45+
helm = initHelmConfig(getDefaultHelmType(), javaProject, getKubernetesTemplate(), helm).build();
46+
} catch (IOException e) {
47+
throw new MojoFailureException(e.getMessage(), e);
48+
}
49+
}
50+
51+
protected File getKubernetesTemplate() {
52+
return kubernetesTemplate;
53+
}
54+
55+
protected HelmConfig.HelmType getDefaultHelmType() {
56+
return HelmConfig.HelmType.KUBERNETES;
57+
}
58+
59+
protected HelmConfig getHelm() {
60+
return helm;
61+
}
62+
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,22 @@ public abstract class AbstractJKubeMojo extends AbstractMojo implements KitLogge
154154

155155
@Override
156156
public void execute() throws MojoExecutionException, MojoFailureException {
157-
try {
158-
init();
159-
if (shouldSkip()) {
160-
log.info("`%s` goal is skipped.", mojoExecution.getMojoDescriptor().getFullGoalName());
161-
return;
162-
}
163-
executeInternal();
164-
} catch (DependencyResolutionRequiredException e) {
165-
throw new MojoFailureException(e.getMessage());
157+
init();
158+
if (shouldSkip()) {
159+
log.info("`%s` goal is skipped.", mojoExecution.getMojoDescriptor().getFullGoalName());
160+
return;
166161
}
162+
executeInternal();
167163
}
168164

169-
protected void init() throws DependencyResolutionRequiredException {
165+
protected void init() throws MojoFailureException {
170166
log = createLogger(null);
171167
clusterAccess = new ClusterAccess(initClusterConfiguration());
172-
javaProject = MavenUtil.convertMavenProjectToJKubeProject(project, session);
168+
try {
169+
javaProject = MavenUtil.convertMavenProjectToJKubeProject(project, session);
170+
} catch (DependencyResolutionRequiredException e) {
171+
throw new MojoFailureException(e.getMessage());
172+
}
173173
jkubeServiceHub = initJKubeServiceHubBuilder(javaProject).build();
174174
resources = updateResourceConfigNamespace(namespace, resources);
175175
}

kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmLintMojo.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,11 @@
1818
import org.apache.maven.plugins.annotations.Mojo;
1919
import org.apache.maven.plugins.annotations.ResolutionScope;
2020

21-
import java.io.IOException;
22-
23-
import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmConfig;
24-
2521
@Mojo(name = "helm-lint", defaultPhase = LifecyclePhase.INTEGRATION_TEST, requiresDependencyResolution = ResolutionScope.COMPILE)
26-
public class HelmLintMojo extends HelmMojo {
22+
public class HelmLintMojo extends AbstractHelmMojo {
2723

2824
@Override
2925
public void executeInternal() throws MojoExecutionException {
30-
try {
31-
helm = initHelmConfig(getDefaultHelmType(), javaProject, getKubernetesTemplate(), helm)
32-
.build();
33-
jkubeServiceHub.getHelmService().lint(helm);
34-
} catch (IOException e) {
35-
throw new MojoExecutionException(e.getMessage(), e);
36-
}
37-
26+
jkubeServiceHub.getHelmService().lint(getHelm());
3827
}
3928
}

kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmMojo.java

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,81 +15,64 @@
1515

1616
import java.io.File;
1717
import java.io.IOException;
18-
import java.util.Collections;
19-
20-
import org.eclipse.jkube.kit.resource.helm.HelmConfig;
2118

2219
import org.apache.maven.plugin.MojoExecutionException;
20+
import org.apache.maven.plugin.MojoFailureException;
2321
import org.apache.maven.plugins.annotations.Component;
2422
import org.apache.maven.plugins.annotations.LifecyclePhase;
2523
import org.apache.maven.plugins.annotations.Mojo;
2624
import org.apache.maven.plugins.annotations.Parameter;
2725
import org.apache.maven.project.MavenProjectHelper;
28-
29-
import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmConfig;
26+
import org.eclipse.jkube.kit.resource.helm.GeneratedChartListener;
27+
import org.eclipse.jkube.kit.resource.helm.HelmConfig;
3028

3129
/**
32-
* Generates a Helm chart for the kubernetes resources
30+
* Generates a Helm chart for the Kubernetes resources
3331
*/
3432
@Mojo(name = "helm", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST)
35-
public class HelmMojo extends AbstractJKubeMojo {
36-
37-
@Component
38-
MavenProjectHelper projectHelper;
33+
public class HelmMojo extends AbstractHelmMojo {
3934

4035
/**
41-
* The generated kubernetes YAML file
36+
* The generated Kubernetes YAML file
4237
*/
4338
@Parameter(property = "jkube.kubernetesManifest", defaultValue = "${basedir}/target/classes/META-INF/jkube/kubernetes.yml")
44-
File kubernetesManifest;
39+
private File kubernetesManifest;
4540

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

56-
@Parameter
57-
HelmConfig helm;
44+
@Override
45+
public void init() throws MojoFailureException {
46+
super.init();
47+
48+
final File manifest = getKubernetesManifest();
49+
if (manifest == null || !manifest.isFile()) {
50+
logManifestNotFoundWarning(manifest);
51+
}
52+
53+
final GeneratedChartListener generatedChartListener = (helmConfig, type, chartFile) -> projectHelper.attachArtifact(project, helmConfig.getChartExtension(), type.getClassifier(), chartFile);
54+
getHelm().getGeneratedChartListeners().add(generatedChartListener);
55+
}
5856

5957
@Override
6058
public void executeInternal() throws MojoExecutionException {
6159
try {
62-
final File manifest = getKubernetesManifest();
63-
if (manifest == null || !manifest.isFile()) {
64-
logManifestNotFoundWarning(manifest);
65-
}
66-
helm = initHelmConfig(getDefaultHelmType(), javaProject, getKubernetesTemplate(), helm)
67-
.generatedChartListeners(Collections.singletonList((helmConfig, type, chartFile) -> projectHelper
68-
.attachArtifact(project, helmConfig.getChartExtension(), type.getClassifier(), chartFile)))
69-
.build();
70-
jkubeServiceHub.getHelmService().generateHelmCharts(helm);
60+
jkubeServiceHub.getHelmService().generateHelmCharts(getHelm());
7161
} catch (IOException exception) {
7262
throw new MojoExecutionException(exception.getMessage());
7363
}
7464
}
7565

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

8070
protected File getKubernetesManifest() {
8171
return kubernetesManifest;
8272
}
8373

84-
protected File getKubernetesTemplate() {
85-
return kubernetesTemplate;
86-
}
87-
74+
@Override
8875
protected HelmConfig.HelmType getDefaultHelmType() {
8976
return HelmConfig.HelmType.KUBERNETES;
9077
}
91-
92-
HelmConfig getHelm() {
93-
return helm;
94-
}
9578
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.jkube.maven.plugin.mojo.build;
1515

1616
import org.apache.maven.plugin.MojoExecutionException;
17+
import org.apache.maven.plugin.MojoFailureException;
1718
import org.apache.maven.plugins.annotations.LifecyclePhase;
1819
import org.apache.maven.plugins.annotations.Mojo;
1920
import org.apache.maven.plugins.annotations.ResolutionScope;
@@ -22,24 +23,25 @@
2223
import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmPushConfig;
2324

2425
@Mojo(name = "helm-push", defaultPhase = LifecyclePhase.INSTALL, requiresDependencyResolution = ResolutionScope.COMPILE)
25-
public class HelmPushMojo extends HelmMojo {
26+
public class HelmPushMojo extends AbstractHelmMojo {
27+
28+
@Override
29+
public void init() throws MojoFailureException {
30+
super.init();
31+
32+
initHelmPushConfig(helm, javaProject);
33+
}
2634

2735
@Override
2836
public void executeInternal() throws MojoExecutionException {
29-
if (skip) {
30-
return;
31-
}
3237
try {
33-
super.executeInternal();
34-
helm = initHelmPushConfig(helm, javaProject);
3538
if (securityDispatcher instanceof DefaultSecDispatcher) {
36-
((DefaultSecDispatcher) securityDispatcher).setConfigurationFile(helm.getSecurity());
39+
((DefaultSecDispatcher) securityDispatcher).setConfigurationFile(getHelm().getSecurity());
3740
}
38-
jkubeServiceHub.getHelmService().uploadHelmChart(helm);
41+
jkubeServiceHub.getHelmService().uploadHelmChart(getHelm());
3942
} catch (Exception exp) {
40-
getKitLogger().error("Error performing helm push", exp);
43+
getKitLogger().error("Error performing Helm push", exp);
4144
throw new MojoExecutionException(exp.getMessage(), exp);
4245
}
4346
}
44-
4547
}

openshift-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/OpenshiftHelmLintMojo.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,15 @@
2626
public class OpenshiftHelmLintMojo extends HelmLintMojo {
2727

2828
/**
29-
* The generated kubernetes YAML file
29+
* One of:
30+
* <ul>
31+
* <li>A directory containing OpenShift Templates to use as Helm parameters.</li>
32+
* <li>A file containing a Kubernetes List with OpenShift Template entries to be used as Helm parameters.</li>
33+
* </ul>
3034
*/
31-
@Parameter(property = "jkube.kubernetesManifest", defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift.yml")
32-
private File openShiftManifest;
33-
34-
/**
35-
* The generated kubernetes YAML file
36-
*/
37-
@Parameter(property = "jkube.kubernetesManifest", defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift")
35+
@Parameter(property = "jkube.openshiftTemplate", defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift")
3836
private File openShiftTemplate;
3937

40-
@Override
41-
protected File getKubernetesManifest() {
42-
return openShiftManifest;
43-
}
4438

4539
@Override
4640
protected File getKubernetesTemplate() {

0 commit comments

Comments
 (0)