Skip to content

Commit

Permalink
chore : Add a debug log statement for application config file location
Browse files Browse the repository at this point in the history
Add a debug log statement in generators of these frameworks:
- Spring Boot
- Thorntail
- Helidon
- Quarkus
- Micronaut

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Sep 3, 2024
1 parent 5f8bdab commit 36f49ae
Show file tree
Hide file tree
Showing 35 changed files with 333 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.io.File;
import java.io.Serializable;
import java.net.URL;
import java.time.LocalDate;
import java.util.List;
import java.util.Locale;
Expand All @@ -38,14 +37,7 @@
public class JavaProject implements Serializable {

private static final long serialVersionUID = 6438404976521622633L;

/**
* Properties file for the project
*
* @param propertiesFile for the project.
* @return Properties file for the project.
*/
private URL propertiesFile;

/**
* Project's name.
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.eclipse.jkube.kit.common.util.YamlUtil.getPropertiesFromYamlResource;

public class PropertiesUtil {
private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path";

private PropertiesUtil() {}

Expand Down Expand Up @@ -102,14 +103,16 @@ public static Properties fromApplicationConfig(JavaProject javaProject, String[]
final URLClassLoader urlClassLoader = getClassLoader(javaProject);
for (String source : appConfigSources) {
final Properties properties;
URL applicationConfigSource = urlClassLoader.findResource(source);
if (source.endsWith(".properties")) {
properties = getPropertiesFromResource(urlClassLoader.findResource(source));
properties = getPropertiesFromResource(applicationConfigSource);
} else {
properties = getPropertiesFromYamlResource(urlClassLoader.findResource(source));
properties = getPropertiesFromYamlResource(applicationConfigSource);
}
// Consider only the first non-empty application config source
if (!properties.isEmpty()) {
properties.putAll(toMap(javaProject.getProperties()));
properties.put(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION, applicationConfigSource);
return properties;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import lombok.Builder;
import lombok.Getter;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.PropertiesExtender;

import java.util.Optional;
import java.util.Properties;
Expand All @@ -40,11 +39,9 @@ public class SpringBootConfiguration {
private boolean managementHealthProbesEnabled;

public static SpringBootConfiguration from(JavaProject project) {
final PropertiesExtender properties = SpringBootUtil.getSpringBootApplicationProperties(
final Properties properties = SpringBootUtil.getSpringBootApplicationProperties(
SpringBootUtil.getSpringBootActiveProfile(project),
JKubeProjectUtil.getClassLoader(project));
project.setPropertiesFile(properties.getPropertiesFile());

final int majorVersion = SpringBootUtil.getSpringBootVersion(project)
.map(semVer -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.Plugin;
import org.eclipse.jkube.kit.common.PropertiesExtender;

import static org.eclipse.jkube.kit.common.util.PropertiesUtil.getPropertiesFromResource;

Expand All @@ -44,6 +43,7 @@ public class SpringBootUtil {
private static final String PLACEHOLDER_PREFIX = "${";
private static final String PLACEHOLDER_SUFFIX = "}";
private static final String VALUE_SEPARATOR = ":";
private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path";

private SpringBootUtil() {}

Expand All @@ -66,22 +66,19 @@ public static Properties getSpringBootApplicationProperties(URLClassLoader compi
* @param compileClassLoader compile class loader
* @return properties object
*/
public static PropertiesExtender getSpringBootApplicationProperties(String springActiveProfile, URLClassLoader compileClassLoader) {
public static Properties getSpringBootApplicationProperties(String springActiveProfile, URLClassLoader compileClassLoader) {
URL ymlResource = compileClassLoader.findResource("application.yml");
URL propertiesResource = compileClassLoader.findResource("application.properties");

Properties props = YamlUtil.getPropertiesFromYamlResource(springActiveProfile, ymlResource);
props.putAll(getPropertiesFromResource(propertiesResource));
props = new SpringBootPropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true)
if (ymlResource != null) {
props.put(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION, ymlResource.toString());
} else if (propertiesResource != null) {
props.put(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION, propertiesResource.toString());
}
return new SpringBootPropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true)
.replaceAllPlaceholders(props);

// Extend Properties object with resources file path
PropertiesExtender propsExtender = new PropertiesExtender();
URL propertiesFile = ymlResource != null ? ymlResource : propertiesResource;
propsExtender.setPropertiesFile(propertiesFile);
propsExtender.putAll(props);

return propsExtender;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ void yml() {
// When
Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"application.yml"});
// Then
assertThat(properties).containsExactly(
entry("application.name", "name-via-yaml"));
assertThat(properties)
.containsOnly(
entry("jkube.internal.application-config-file.path", PropertiesUtilTest.class.getResource("/util/properties-util/yaml/application.yml")),
entry("application.name", "name-via-yaml"));
}

@Test
Expand All @@ -164,8 +166,10 @@ void properties() {
// When
Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"application.properties"});
// Then
assertThat(properties).containsExactly(
entry("application.name", "name-via-properties"));
assertThat(properties)
.containsOnly(
entry("jkube.internal.application-config-file.path", PropertiesUtilTest.class.getResource("/util/properties-util/properties/application.properties")),
entry("application.name", "name-via-properties"));
}

@Test
Expand All @@ -174,8 +178,10 @@ void multipleSources_thenFirstOneTakesPrecedence() {
// When
Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"application.properties", "application.yml"});
// Then
assertThat(properties).containsExactly(
entry("application.name", "name-via-properties"));
assertThat(properties)
.containsOnly(
entry("jkube.internal.application-config-file.path", PropertiesUtilTest.class.getResource("/util/properties-util/properties/application.properties")),
entry("application.name", "name-via-properties"));
}

@Test
Expand All @@ -184,8 +190,10 @@ void multipleSourcesWithEmpty_thenFirstNonEmptyTakesPrecedence() {
// When
Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"not-there", "application.yml", "application.properties"});
// Then
assertThat(properties).containsExactly(
entry("application.name", "name-via-yaml"));
assertThat(properties)
.containsOnly(
entry("jkube.internal.application-config-file.path", PropertiesUtilTest.class.getResource("/util/properties-util/yaml/application.yml")),
entry("application.name", "name-via-yaml"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ void setUp(@TempDir Path target) throws IOException {
project = JavaProject.builder()
.properties(properties)
.outputDirectory(target.toFile())
.baseDirectory(target.toFile())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void getSpringBootApplicationPropertiesLoadsStandardProperties(@TempDir File tem
Properties result = SpringBootUtil.getSpringBootApplicationProperties(cl);
//Then
assertThat(result).containsOnly(
entry("jkube.internal.application-config-file.path", tempDir.toPath().resolve("target/classes/application.properties").toUri().toURL().toString()),
entry("spring.application.name", "demoservice"),
entry("server.port", "9090")
);
Expand Down Expand Up @@ -99,7 +100,6 @@ void getSpringBootApplicationProperties_withCompileClassloader_shouldLoadPropert
JavaProject javaProject = JavaProject.builder()
.compileClassPathElement(Objects.requireNonNull(getClass().getResource("/util/springboot/resources")).getPath())
.outputDirectory(new File("target"))
.baseDirectory(new File("target"))
.build();
URLClassLoader compileClassLoader = JKubeProjectUtil.getClassLoader(javaProject);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -103,12 +102,6 @@ private List<ImageConfiguration> generateImages(List<ImageConfiguration> imageCo
if (generator.isApplicable(ret)) {
log.info("Running generator %s", generator.getName());
ret = generator.customize(ret, genCtx.isPrePackagePhase());
URL propFileURL = genCtx.getProject().getPropertiesFile();
if (propFileURL != null) {
log.info("The following properties file is used %s", propFileURL);
} else {
log.info("No properties file found");
}
}
}
return ret;
Expand Down
4 changes: 4 additions & 0 deletions jkube-kit/jkube-kit-helidon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@
import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext;
import org.eclipse.jkube.microprofile.enricher.AbstractMicroprofileHealthCheckEnricher;

import java.util.Properties;

import static org.eclipse.jkube.helidon.HelidonUtils.extractPort;
import static org.eclipse.jkube.helidon.HelidonUtils.getHelidonConfiguration;
import static org.eclipse.jkube.helidon.HelidonUtils.hasHelidonHealthDependency;
import static org.eclipse.jkube.kit.common.Configs.asInteger;

public class HelidonHealthCheckEnricher extends AbstractMicroprofileHealthCheckEnricher {
private static final String DEFAULT_HELIDON_PORT = "8080";
private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path";
private final Properties helidonApplicationConfiguration;

public HelidonHealthCheckEnricher(JKubeEnricherContext buildContext) {
super(buildContext, "jkube-healthcheck-helidon");
helidonApplicationConfiguration = getHelidonConfiguration(getContext().getProject());
log.debug("Helidon Application Config loaded from : %s", helidonApplicationConfiguration.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION));
}

@Override
Expand All @@ -34,6 +41,6 @@ protected boolean shouldAddProbe() {

@Override
protected int getPort() {
return asInteger(extractPort(getHelidonConfiguration(getContext().getProject()), DEFAULT_HELIDON_PORT));
return asInteger(extractPort(helidonApplicationConfiguration, DEFAULT_HELIDON_PORT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.List;
import java.util.Optional;
import java.util.Properties;

import static org.eclipse.jkube.helidon.HelidonUtils.extractPort;
import static org.eclipse.jkube.helidon.HelidonUtils.getHelidonConfiguration;
Expand All @@ -29,10 +30,14 @@
public class HelidonGenerator extends JavaExecGenerator {
public static final String HELIDON = "helidon";
private final HelidonNestedGenerator nestedGenerator;
private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path";
private final Properties helidonApplicationConfiguration;

public HelidonGenerator(GeneratorContext context) {
super(context, HELIDON);
nestedGenerator = HelidonNestedGenerator.from(context, getGeneratorConfig());
helidonApplicationConfiguration = getHelidonConfiguration(getContext().getProject());
log.debug("Helidon Application Config loaded from : %s", helidonApplicationConfiguration.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION));
}

@Override
Expand Down Expand Up @@ -62,7 +67,7 @@ protected Arguments getBuildEntryPoint() {

@Override
protected String getDefaultWebPort() {
return extractPort(getHelidonConfiguration(getProject()), super.getDefaultWebPort());
return extractPort(helidonApplicationConfiguration, super.getDefaultWebPort());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ void getHelidonConfiguration_whenApplicationYamlProvided_thenShouldExtractConfig
final Properties props = HelidonUtils.getHelidonConfiguration(javaProject);
// Then
assertThat(props).containsOnly(
entry("jkube.internal.application-config-file.path", getClass().getResource("/utils-test/config/yaml/application.yml")),
entry("app.greeting", "Hello"),
entry("server.port", "8080"),
entry("server.host", "0.0.0.0"));
Expand All @@ -109,6 +110,7 @@ void getHelidonConfiguration_whenMicroprofilePropertiesProvided_thenShouldExtrac
final Properties props = HelidonUtils.getHelidonConfiguration(javaProject);
// Then
assertThat(props).containsOnly(
entry("jkube.internal.application-config-file.path", getClass().getResource("/utils-test/config/properties/META-INF/microprofile-config.properties")),
entry("app.greeting", "Hello"),
entry("server.port", "8080"),
entry("server.host", "0.0.0.0"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,22 @@
import java.util.function.Supplier;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

class HelidonHealthCheckEnricherTest {
private JKubeEnricherContext context;
private JavaProject javaProject;
private Properties properties;
private KubernetesListBuilder klb;
private KitLogger logger;

@BeforeEach
void setup() {
properties = new Properties();
klb = new KubernetesListBuilder();
logger = spy(new KitLogger.SilentLogger());
klb.addToItems(new DeploymentBuilder()
.editOrNewSpec()
.editOrNewTemplate()
Expand All @@ -67,12 +72,27 @@ void setup() {
.dependenciesWithTransitive(new ArrayList<>())
.build();
context = JKubeEnricherContext.builder()
.log(new KitLogger.SilentLogger())
.log(logger)
.project(javaProject)
.processorConfig(new ProcessorConfig())
.build();
}

@Test
void constructorShouldLogHelidonApplicationConfigPath() {
// Given
context = context.toBuilder()
.project(javaProject.toBuilder()
.compileClassPathElement(Objects.requireNonNull(getClass().getResource("/custom-port-microprofile-configuration")).getPath())
.build())
.build();
// When
HelidonHealthCheckEnricher helidonHealthCheckEnricher = new HelidonHealthCheckEnricher(context);
// Then
assertThat(helidonHealthCheckEnricher).isNotNull();
verify(logger, times(1)).debug("jkube-healthcheck-helidon: Helidon Application Config loaded from : %s", getClass().getResource("/custom-port-microprofile-configuration/META-INF/microprofile-config.properties"));
}

@Test
void create_withNoMicroprofileDependency_shouldNotAddProbes() {
// Given
Expand Down
Loading

0 comments on commit 36f49ae

Please sign in to comment.