Skip to content

Commit

Permalink
review:refactor: HelidonGenerator
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Jun 19, 2023
1 parent 91364de commit 44c1e8a
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ protected enum JDK {
}

private static final String WEB_PORT_DEFAULT = "8080";
private static final String JOLOKIA_PORT_DEFAULT = "8778";
private static final String PROMETHEUS_PORT_DEFAULT = "9779";
public static final String JOLOKIA_PORT_DEFAULT = "8778";
public static final String PROMETHEUS_PORT_DEFAULT = "9779";
// Environment variable used for specifying a main class
static final String JAVA_MAIN_CLASS_ENV_VAR = "JAVA_MAIN_CLASS";
protected static final String JAVA_OPTIONS = "JAVA_OPTIONS";
Expand Down
14 changes: 14 additions & 0 deletions jkube-kit/jkube-kit-helidon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,18 @@
</dependency>
</dependencies>

<build>
<resources>
<!-- Copy over default images versions defined above -->
<resource>
<directory>src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@

import static org.eclipse.jkube.kit.common.util.FileUtil.getRelativePath;

public class HelidonAssemblies {
public static final HelidonAssembly NATIVE = helidonGenerator -> {

@FunctionalInterface
public interface HelidonAssembly {

AssemblyConfiguration createAssemblyConfiguration(HelidonGenerator helidonGenerator);

HelidonAssembly NATIVE = helidonGenerator -> {
final JavaProject project = helidonGenerator.getContext().getProject();
final AssemblyFileSet.AssemblyFileSetBuilder artifactFileSetBuilder = AssemblyFileSet.builder()
.outputDirectory(new File("."))
Expand All @@ -34,12 +39,14 @@ public class HelidonAssemblies {
if (nativeBinary.exists()) {
artifactFileSetBuilder.include(nativeBinary.getName());
}
return createAssemblyConfiguration(helidonGenerator.getBuildWorkdir())
.layer(Assembly.builder().fileSet(artifactFileSetBuilder.build()).build())
.build();
return AssemblyConfiguration.builder()
.targetDir(helidonGenerator.getBuildWorkdir())
.excludeFinalOutputArtifact(true)
.layer(Assembly.builder().fileSet(artifactFileSetBuilder.build()).build())
.build();
};

public static final HelidonAssembly STANDARD = helidonGenerator -> {
HelidonAssembly STANDARD = helidonGenerator -> {
final JavaProject project = helidonGenerator.getContext().getProject();
AssemblyFileSet.AssemblyFileSetBuilder libFileSet = AssemblyFileSet.builder()
.outputDirectory(new File("."))
Expand All @@ -54,20 +61,11 @@ public class HelidonAssemblies {
if (defaultArtifactFile != null) {
artifactFileSet.include(getRelativePath(project.getBuildPackageDirectory(), defaultArtifactFile).getPath());
}
return createAssemblyConfiguration(helidonGenerator.getBuildWorkdir())
return AssemblyConfiguration.builder()
.targetDir(helidonGenerator.getBuildWorkdir())
.excludeFinalOutputArtifact(true)
.layer(Assembly.builder().id("libs").fileSet(libFileSet.build()).build())
.layer(Assembly.builder().id("artifact").fileSet(artifactFileSet.build()).build())
.build();
};

private static AssemblyConfiguration.AssemblyConfigurationBuilder createAssemblyConfiguration(String targetDir) {
return AssemblyConfiguration.builder()
.targetDir(targetDir)
.excludeFinalOutputArtifact(true);
}

@FunctionalInterface
public interface HelidonAssembly {
AssemblyConfiguration createAssemblyConfiguration(HelidonGenerator quarkusGenerator);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.helidon.generator;


import org.eclipse.jkube.generator.api.FromSelector;

@FunctionalInterface
public interface HelidonFromSelector {

FromSelector fromSelector(HelidonGenerator helidonGenerator);

HelidonFromSelector NATIVE = helidonGenerator -> new FromSelector.Default(helidonGenerator.getContext(), "helidon-native");
HelidonFromSelector STANDARD = helidonGenerator -> new FromSelector.Default(helidonGenerator.getContext(), "java");
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.jkube.generator.javaexec.JavaExecGenerator;
import org.eclipse.jkube.kit.common.Arguments;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.Configs;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;

import java.util.List;
Expand All @@ -25,11 +26,9 @@
import static org.eclipse.jkube.helidon.HelidonUtils.extractPort;
import static org.eclipse.jkube.helidon.HelidonUtils.getHelidonConfiguration;
import static org.eclipse.jkube.helidon.HelidonUtils.hasHelidonDependencies;
import static org.eclipse.jkube.helidon.HelidonUtils.hasHelidonGraalNativeImageExtension;

public class HelidonGenerator extends JavaExecGenerator {
public static final String HELIDON = "helidon";
public static final String DEFAULT_NATIVE_BASE_IMAGE = "registry.access.redhat.com/ubi8/ubi-minimal:8.7-1107";

public HelidonGenerator(GeneratorContext context) {
super(context, HELIDON);
Expand All @@ -42,37 +41,23 @@ public boolean isApplicable(List<ImageConfiguration> configs) {

@Override
protected AssemblyConfiguration createAssembly() {
if (isNativeImage()) {
return HelidonAssemblies.NATIVE.createAssemblyConfiguration(this);
}
return HelidonAssemblies.STANDARD.createAssemblyConfiguration(this);
return HelidonGenerators.from(getProject()).createAssemblyConfiguration(this);
}

@Override
protected String getBuildWorkdir() {
if (isNativeImage()) {
return "/";
}
return getConfig(JavaExecGenerator.Config.TARGET_DIR);
return HelidonGenerators.from(getProject()).getBuildWorkdir(this);
}

@Override
protected String getFromAsConfigured() {
if (isNativeImage()) {
return Optional.ofNullable(super.getFromAsConfigured()).orElse(getNativeFrom());
}
return super.getFromAsConfigured();
return Optional.ofNullable(super.getFromAsConfigured())
.orElse(HelidonGenerators.from(getProject()).fromSelector(this).getFrom());
}

@Override
protected Arguments getBuildEntryPoint() {
if (isNativeImage()) {
final Arguments.ArgumentsBuilder ab = Arguments.builder();
ab.execArgument("./" + getProject().getArtifactId());
getExtraJavaOptions().forEach(ab::execArgument);
return ab.build();
}
return null;
return HelidonGenerators.from(getProject()).getBuildEntryPoint(this);
}

@Override
Expand All @@ -82,19 +67,17 @@ protected String getDefaultWebPort() {

@Override
protected String getDefaultJolokiaPort() {
return isNativeImage() ? "0" : super.getDefaultJolokiaPort();
return HelidonGenerators.from(getProject()).getDefaultJolokiaPort();
}

@Override
protected String getDefaultPrometheusPort() {
return isNativeImage() ? "0" : super.getDefaultPrometheusPort();
return HelidonGenerators.from(getProject()).getDefaultPrometheusPort();
}

private String getNativeFrom() {
return DEFAULT_NATIVE_BASE_IMAGE;
}

private boolean isNativeImage() {
return hasHelidonGraalNativeImageExtension(getProject());
@SuppressWarnings("java:S1185") // Expose super method to HelidonGenerators
@Override
protected String getConfig(Configs.Config key) {
return super.getConfig(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.helidon.generator;

import org.eclipse.jkube.generator.api.FromSelector;
import org.eclipse.jkube.generator.javaexec.JavaExecGenerator;
import org.eclipse.jkube.kit.common.Arguments;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.JavaProject;

import java.util.function.Function;

import static org.eclipse.jkube.generator.javaexec.JavaExecGenerator.JOLOKIA_PORT_DEFAULT;
import static org.eclipse.jkube.generator.javaexec.JavaExecGenerator.PROMETHEUS_PORT_DEFAULT;
import static org.eclipse.jkube.helidon.HelidonUtils.hasHelidonGraalNativeImageExtension;

public enum HelidonGenerators {

NATIVE(
HelidonFromSelector.NATIVE,
HelidonAssembly.NATIVE,
helidonGenerator -> "/",
helidonGenerator -> Arguments.builder()
.execArgument("./" + helidonGenerator.getContext().getProject().getArtifactId())
.build(),
"0",
"0"),
STANDARD(
HelidonFromSelector.STANDARD,
HelidonAssembly.STANDARD,
helidonGenerator -> helidonGenerator.getConfig(JavaExecGenerator.Config.TARGET_DIR),
helidonGenerator -> null,
JOLOKIA_PORT_DEFAULT,
PROMETHEUS_PORT_DEFAULT);

private final HelidonFromSelector fromSelector;
private final HelidonAssembly assembly;
private final Function<HelidonGenerator, String> buildWorkdir;
private final Function<HelidonGenerator, Arguments> buildEntryPoint;
private final String jolokiaPort;
private final String prometheusPort;

HelidonGenerators(
HelidonFromSelector fromSelector, HelidonAssembly assembly, Function<HelidonGenerator, String> buildWorkdir,
Function<HelidonGenerator, Arguments> buildEntryPoint, String jolokiaPort, String prometheusPort) {
this.fromSelector = fromSelector;
this.assembly = assembly;
this.buildWorkdir = buildWorkdir;
this.buildEntryPoint = buildEntryPoint;
this.jolokiaPort = jolokiaPort;
this.prometheusPort = prometheusPort;
}


public FromSelector fromSelector(HelidonGenerator helidonGenerator) {
return fromSelector.fromSelector(helidonGenerator);
}

public AssemblyConfiguration createAssemblyConfiguration(HelidonGenerator helidonGenerator) {
return assembly.createAssemblyConfiguration(helidonGenerator);
}

public String getBuildWorkdir(HelidonGenerator helidonGenerator) {
return buildWorkdir.apply(helidonGenerator);
}

public Arguments getBuildEntryPoint(HelidonGenerator helidonGenerator) {
return buildEntryPoint.apply(helidonGenerator);
}

public String getDefaultJolokiaPort() {
return jolokiaPort;
}

public String getDefaultPrometheusPort() {
return prometheusPort;
}

public static HelidonGenerators from(JavaProject project) {
if (hasHelidonGraalNativeImageExtension(project)) {
return NATIVE;
}
return STANDARD;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# 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
#

# Properties for specifying the default images version to use
# The replacement values are defined in the parent pom.xml as properties

# Upstream images
helidon-native.upstream.s2i=${image.helidon-native.upstream.s2i}
helidon-native.upstream.docker=${image.helidon-native.upstream.docker}
helidon-native.upstream.istag=${image.helidon-native.upstream.istag}
Loading

0 comments on commit 44c1e8a

Please sign in to comment.