Skip to content

Commit

Permalink
chore : Add a debug log statement for application config file locatio…
Browse files Browse the repository at this point in the history
…n while inferring ports

Add a debug log statement in generators of these frameworks while
resolving default web ports:
- Spring Boot
- Thorntail
- Helidon
- Quarkus
- Micronaut

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Aug 29, 2024
1 parent ef3be23 commit 319bd03
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 64 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 @@ -15,6 +15,7 @@

import io.fabric8.kubernetes.client.utils.Utils;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.KitLogger;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -24,6 +25,7 @@
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
Expand Down Expand Up @@ -115,4 +117,14 @@ public static Properties fromApplicationConfig(JavaProject javaProject, String[]
}
return javaProject.getProperties();
}

public static void logApplicationConfigurationFileSource(KitLogger logger, JavaProject javaProject, List<String> appConfigSources) {
final URLClassLoader urlClassLoader = getClassLoader(javaProject);
appConfigSources.forEach(s -> {
URL ymlResource = urlClassLoader.findResource(s);
if (ymlResource != null) {
logger.info("Loading application configuration from : %s", ymlResource);
}
});
}
}
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 @@ -39,11 +38,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 @@ -16,14 +16,15 @@
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

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 @@ -39,6 +40,7 @@ public class SpringBootUtil {
public static final String SPRING_BOOT_GRADLE_PLUGIN_ARTIFACT_ID = "org.springframework.boot.gradle.plugin";
public static final String DEV_TOOLS_REMOTE_SECRET = "spring.devtools.remote.secret";
public static final String DEV_TOOLS_REMOTE_SECRET_ENV = "SPRING_DEVTOOLS_REMOTE_SECRET";
private static final String[] SPRING_BOOT_APP_CONFIG_FILES_LIST = new String[] {"application.properties", "application.yml"};

private static final String PLACEHOLDER_PREFIX = "${";
private static final String PLACEHOLDER_SUFFIX = "}";
Expand All @@ -65,22 +67,14 @@ 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)
.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;
return new SpringBootPropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true)
.replaceAllPlaceholders(props);
}

/**
Expand Down Expand Up @@ -158,5 +152,9 @@ public static File findNativeArtifactFile(JavaProject project) {
}
return null;
}

public static List<String> getSpringBootAppConfigFiles() {
return Arrays.asList(SPRING_BOOT_APP_CONFIG_FILES_LIST);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

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

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import static org.eclipse.jkube.kit.common.util.PropertiesUtil.fromApplicationConfig;
Expand All @@ -34,4 +36,8 @@ private ThorntailUtil() {}
public static Properties getThorntailProperties(JavaProject javaProject) {
return fromApplicationConfig(javaProject, THORNTAIL_APP_CONFIG_FILES_LIST);
}

public static List<String> getThorntailAppConfigFiles() {
return Arrays.asList(THORNTAIL_APP_CONFIG_FILES_LIST);
}
}
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 @@ -99,7 +99,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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import static org.eclipse.jkube.kit.common.util.PropertiesUtil.fromApplicationConfig;
Expand Down Expand Up @@ -46,4 +48,8 @@ public static Properties getHelidonConfiguration(JavaProject javaProject) {
public static String extractPort(Properties properties, String defaultValue) {
return properties.getProperty(HELIDON_HTTP_PORT, defaultValue);
}

public static List<String> getHelidonAppConfigFiles() {
return Arrays.asList(HELIDON_APP_CONFIG_FILES_LIST);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.eclipse.jkube.microprofile.enricher.AbstractMicroprofileHealthCheckEnricher;

import static org.eclipse.jkube.helidon.HelidonUtils.extractPort;
import static org.eclipse.jkube.helidon.HelidonUtils.getHelidonAppConfigFiles;
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;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.logApplicationConfigurationFileSource;

public class HelidonHealthCheckEnricher extends AbstractMicroprofileHealthCheckEnricher {
private static final String DEFAULT_HELIDON_PORT = "8080";
Expand All @@ -34,6 +36,7 @@ protected boolean shouldAddProbe() {

@Override
protected int getPort() {
logApplicationConfigurationFileSource(log, getContext().getProject(), getHelidonAppConfigFiles());
return asInteger(extractPort(getHelidonConfiguration(getContext().getProject()), DEFAULT_HELIDON_PORT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.util.Optional;

import static org.eclipse.jkube.helidon.HelidonUtils.extractPort;
import static org.eclipse.jkube.helidon.HelidonUtils.getHelidonAppConfigFiles;
import static org.eclipse.jkube.helidon.HelidonUtils.getHelidonConfiguration;
import static org.eclipse.jkube.helidon.HelidonUtils.hasHelidonDependencies;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.logApplicationConfigurationFileSource;

public class HelidonGenerator extends JavaExecGenerator {
public static final String HELIDON = "helidon";
Expand Down Expand Up @@ -62,6 +64,7 @@ protected Arguments getBuildEntryPoint() {

@Override
protected String getDefaultWebPort() {
logApplicationConfigurationFileSource(log, getContext().getProject(), getHelidonAppConfigFiles());
return extractPort(getHelidonConfiguration(getProject()), super.getDefaultWebPort());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import static org.eclipse.jkube.kit.common.util.PropertiesUtil.fromApplicationConfig;
Expand Down Expand Up @@ -53,4 +55,8 @@ public static boolean hasNativeImagePackaging(JavaProject javaProject) {
}
return false;
}

public static List<String> getMicronautAppConfigFiles() {
return Arrays.asList(MICRONAUT_APP_CONFIG_FILES_LIST);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;

import static org.eclipse.jkube.kit.common.util.PropertiesUtil.logApplicationConfigurationFileSource;
import static org.eclipse.jkube.micronaut.MicronautUtils.extractPort;
import static org.eclipse.jkube.micronaut.MicronautUtils.getMicronautAppConfigFiles;
import static org.eclipse.jkube.micronaut.MicronautUtils.getMicronautConfiguration;
import static org.eclipse.jkube.micronaut.MicronautUtils.hasMicronautPlugin;

Expand Down Expand Up @@ -79,6 +81,7 @@ protected AssemblyConfiguration createAssembly() {

@Override
protected String getDefaultWebPort() {
logApplicationConfigurationFileSource(log, getContext().getProject(), getMicronautAppConfigFiles());
return extractPort(
getMicronautConfiguration(getProject()), super.getDefaultWebPort()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.eclipse.jkube.quarkus;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -198,6 +200,10 @@ public static boolean isStartupEndpointSupported(JavaProject javaProject) {
return isVersionAtLeast(QUARKUS_MAJOR_VERSION_SINCE_STARTUP_CHANGE, QUARKUS_MINOR_VERSION_SINCE_STARTUP_CHANGE, findQuarkusVersion(javaProject));
}

public static List<String> getQuarkusAppConfigFiles() {
return Arrays.asList(QUARKUS_APP_CONFIG_FILES_LIST);
}

private static String resolveQuarkusNonApplicationRootPath(String quarkusVersion, Properties quarkusProperties) {
final String defaultValue = isVersionAtLeast(QUARKUS2_MAJOR_VERSION, QUARKUS2_MINOR_VERSION, quarkusVersion) ?
DEFAULT_NON_APPLICATION_ROOT_AFTER_2_0 : DEFAULT_NON_APPLICATION_ROOT_BEFORE_2_0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import org.apache.commons.lang3.StringUtils;

import static org.eclipse.jkube.kit.common.Configs.asInteger;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.logApplicationConfigurationFileSource;
import static org.eclipse.jkube.quarkus.QuarkusUtils.QUARKUS_GROUP_ID;
import static org.eclipse.jkube.quarkus.QuarkusUtils.concatPath;
import static org.eclipse.jkube.quarkus.QuarkusUtils.getQuarkusAppConfigFiles;
import static org.eclipse.jkube.quarkus.QuarkusUtils.isStartupEndpointSupported;
import static org.eclipse.jkube.quarkus.QuarkusUtils.resolveCompleteQuarkusHealthRootPath;
import static org.eclipse.jkube.quarkus.QuarkusUtils.extractPort;
Expand Down Expand Up @@ -87,6 +89,7 @@ private Probe discoverQuarkusHealthCheck(int initialDelay, Function<JavaProject,
if (!getContext().hasDependency(QUARKUS_GROUP_ID, "quarkus-smallrye-health")) {
return null;
}
logApplicationConfigurationFileSource(log, getContext().getProject(), getQuarkusAppConfigFiles());
return new ProbeBuilder()
.withNewHttpGet()
.withNewPort(asInteger(extractPort(getContext().getProject(), getQuarkusConfiguration(getContext().getProject()), getConfig(Config.PORT))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import static org.eclipse.jkube.kit.common.util.PropertiesUtil.logApplicationConfigurationFileSource;
import static org.eclipse.jkube.quarkus.QuarkusUtils.extractPort;
import static org.eclipse.jkube.quarkus.QuarkusUtils.getQuarkusAppConfigFiles;
import static org.eclipse.jkube.quarkus.QuarkusUtils.getQuarkusConfiguration;
import static org.eclipse.jkube.quarkus.QuarkusUtils.hasQuarkusPlugin;

Expand Down Expand Up @@ -68,6 +70,7 @@ public boolean isApplicable(List<ImageConfiguration> configs) {

@Override
protected String getDefaultWebPort() {
logApplicationConfigurationFileSource(log, getContext().getProject(), getQuarkusAppConfigFiles());
return extractPort(getProject(), getQuarkusConfiguration(getProject()), super.getDefaultWebPort());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import static org.eclipse.jkube.kit.common.util.PropertiesUtil.logApplicationConfigurationFileSource;
import static org.eclipse.jkube.kit.common.util.SpringBootUtil.DEV_TOOLS_REMOTE_SECRET;
import static org.eclipse.jkube.kit.common.util.SpringBootUtil.getSpringBootAppConfigFiles;
import static org.eclipse.jkube.kit.common.util.SpringBootUtil.isSpringBootRepackage;
import static org.eclipse.jkube.springboot.SpringBootDevtoolsUtils.addDevToolsFilesToFatJar;
import static org.eclipse.jkube.springboot.SpringBootDevtoolsUtils.ensureSpringDevToolSecretToken;
Expand Down Expand Up @@ -108,6 +110,7 @@ protected boolean isFatJar() {

@Override
protected String getDefaultWebPort() {
logApplicationConfigurationFileSource(log, getProject(), getSpringBootAppConfigFiles());
return "" + SpringBootConfiguration.from(getProject()).getServerPort();
}

Expand Down
Loading

0 comments on commit 319bd03

Please sign in to comment.