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 20, 2024
1 parent 0860e8a commit bfa28f5
Show file tree
Hide file tree
Showing 25 changed files with 175 additions and 165 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 @@ -14,17 +14,26 @@
package org.eclipse.jkube.kit.common.util;

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;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Supplier;

import static org.eclipse.jkube.kit.common.util.JKubeProjectUtil.getClassLoader;
import static org.eclipse.jkube.kit.common.util.YamlUtil.getPropertiesFromYamlResource;

public class PropertiesUtil {

Expand Down Expand Up @@ -92,4 +101,40 @@ public static Map<String, String> toMap(Properties properties) {
}
return map;
}

public static List<Supplier<Properties>> getPropertySuppliersList(JavaProject javaProject, List<String> propertySources) {
final URLClassLoader urlClassLoader = getClassLoader(javaProject);
List<Supplier<Properties>> sources = new ArrayList<>();
for (String source : propertySources) {
if (source.endsWith(".properties")) {
sources.add(() -> getPropertiesFromResource(urlClassLoader.findResource(source)));
} else {
sources.add(() -> getPropertiesFromYamlResource(urlClassLoader.findResource(source)));
}
}

return sources;
}

public static Properties createPropertiesFromApplicationConfig(JavaProject project, List<String> appConfigSources) {
final List<Supplier<Properties>> sources = getPropertySuppliersList(project, appConfigSources);
for (Supplier<Properties> source : sources) {
final Properties props = source.get();
if (!props.isEmpty()) {
props.putAll(toMap(project.getProperties()));
return props;
}
}
return project.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.debug("The following properties file is used for application configuration: %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 @@ -43,6 +44,7 @@ public class SpringBootUtil {
private static final String PLACEHOLDER_PREFIX = "${";
private static final String PLACEHOLDER_SUFFIX = "}";
private static final String VALUE_SEPARATOR = ":";
public static final List<String> SPRING_BOOT_APP_CONFIG_FILES_LIST = Arrays.asList("application.properties", "application.yml");

private SpringBootUtil() {}

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
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
*/
package org.eclipse.jkube.kit.common.util;

import java.net.URL;
import java.net.URLClassLoader;
import org.eclipse.jkube.kit.common.JavaProject;

import java.util.Collections;
import java.util.List;
import java.util.Properties;

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

public class ThorntailUtil {
public static final List<String> THORNTAIL_APP_CONFIG_FILES_LIST = Collections.singletonList("project-defaults.yml");

private ThorntailUtil() {}

Expand All @@ -28,8 +33,7 @@ private ThorntailUtil() {}
* @param compileClassLoader URLClassLoader for resource access
* @return thorntail configuration properties
*/
public static Properties getThorntailProperties(URLClassLoader compileClassLoader) {
URL ymlResource = compileClassLoader.findResource("project-defaults.yml");
return YamlUtil.getPropertiesFromYamlResource(ymlResource);
public static Properties getThorntailProperties(JavaProject javaProject) {
return createPropertiesFromApplicationConfig(javaProject, 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,19 +16,15 @@
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;

import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.function.Supplier;

import static org.eclipse.jkube.kit.common.util.JKubeProjectUtil.getClassLoader;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.getPropertiesFromResource;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.toMap;
import static org.eclipse.jkube.kit.common.util.YamlUtil.getPropertiesFromYamlResource;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.createPropertiesFromApplicationConfig;

public class HelidonUtils {
private static final String HELIDON_HTTP_PORT = "server.port";
public static final List<String> HELIDON_APP_CONFIG_FILES_LIST = Arrays.asList("META-INF/microprofile-config.properties", "application.yaml", "application.yml");

private HelidonUtils() { }

Expand All @@ -46,20 +42,7 @@ public static boolean hasHelidonHealthDependency(JavaProject javaProject) {
}

public static Properties getHelidonConfiguration(JavaProject javaProject) {
final URLClassLoader urlClassLoader = getClassLoader(javaProject);
final List<Supplier<Properties>> sources = Arrays.asList(
() -> getPropertiesFromResource(urlClassLoader.findResource("META-INF/microprofile-config.properties")),
() -> getPropertiesFromYamlResource(urlClassLoader.findResource("application.yaml")),
() -> getPropertiesFromYamlResource(urlClassLoader.findResource("application.yml"))
);
for (Supplier<Properties> source : sources) {
final Properties props = source.get();
if (!props.isEmpty()) {
props.putAll(toMap(javaProject.getProperties()));
return props;
}
}
return javaProject.getProperties();
return createPropertiesFromApplicationConfig(javaProject, HELIDON_APP_CONFIG_FILES_LIST);
}

public static String extractPort(Properties properties, String defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext;
import org.eclipse.jkube.microprofile.enricher.AbstractMicroprofileHealthCheckEnricher;

import static org.eclipse.jkube.helidon.HelidonUtils.HELIDON_APP_CONFIG_FILES_LIST;
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;
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(), HELIDON_APP_CONFIG_FILES_LIST);
return asInteger(extractPort(getHelidonConfiguration(getContext().getProject()), DEFAULT_HELIDON_PORT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
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.util.JKubeProjectUtil;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;

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

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

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

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

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

import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.function.Supplier;

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

public class MicronautUtils {
public static final List<String> MICRONAUT_APP_CONFIG_FILES_LIST = Arrays.asList("application.properties", "application.yml", "application.json");

private MicronautUtils() {}

Expand All @@ -35,20 +35,8 @@ public static boolean isHealthEnabled(Properties properties) {
return properties.getProperty("endpoints.health.enabled", "false").equalsIgnoreCase("true");
}

@SuppressWarnings("unchecked")
public static Properties getMicronautConfiguration(URLClassLoader urlClassLoader) {
final Supplier<Properties>[] sources = new Supplier[]{
() -> getPropertiesFromResource(urlClassLoader.findResource("application.properties")),
() -> getPropertiesFromYamlResource(urlClassLoader.findResource("application.yml")),
() -> getPropertiesFromYamlResource(urlClassLoader.findResource("application.json"))
};
for (Supplier<Properties> source : sources) {
final Properties props = source.get();
if (!props.isEmpty()) {
return props;
}
}
return new Properties();
public static Properties getMicronautConfiguration(JavaProject javaProject) {
return createPropertiesFromApplicationConfig(javaProject, MICRONAUT_APP_CONFIG_FILES_LIST);
}

public static boolean hasMicronautPlugin(JavaProject javaProject) {
Expand Down
Loading

0 comments on commit bfa28f5

Please sign in to comment.