Skip to content

Commit

Permalink
feat: log application configuration file used in generators and healt…
Browse files Browse the repository at this point in the history
…h check enrichers (2880)

fix wrong resources directory path

Signed-off-by: Oleksandr Krutko <[email protected]>

fix typo of resourcesDirectory javadoc in JavaProject class

Signed-off-by: Oleksandr Krutko <[email protected]>
---
add INFO message to show which properties file is being used

Signed-off-by: Oleksandr Krutko <[email protected]>
---
add missed PropertiesExtender class file

Signed-off-by: Oleksandr Krutko <[email protected]>
---
fix typos, fix DefaultGeneratorManager test, add licence license header

Signed-off-by: Oleksandr Krutko <[email protected]>
---
remove additional path for properties file, add INFO message for properties file

Signed-off-by: Oleksandr Krutko <[email protected]>
---
chore : Add a debug log statement for application config file location

Add a debug log statement in generators of these frameworks:
- Spring Boot
- Thorntail
- Helidon
- Quarkus
- Micronaut

Signed-off-by: Rohan Kumar <[email protected]>
---
review: log application configuration location for generator and enrichers

Signed-off-by: Marc Nuri <[email protected]>

Co-authored-by: Rohan Kumar <[email protected]>
Co-authored-by: Marc Nuri <[email protected]>
  • Loading branch information
3 people authored Oct 21, 2024
1 parent a0a3369 commit e2c44f4
Show file tree
Hide file tree
Showing 30 changed files with 353 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

public class PropertiesUtil {

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

private PropertiesUtil() {}

/**
Expand Down Expand Up @@ -102,14 +104,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 @@ -24,6 +24,7 @@
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.Plugin;

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

/**
Expand Down Expand Up @@ -71,8 +72,13 @@ public static Properties getSpringBootApplicationProperties(String springActiveP

Properties props = YamlUtil.getPropertiesFromYamlResource(springActiveProfile, ymlResource);
props.putAll(getPropertiesFromResource(propertiesResource));
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);
.replaceAllPlaceholders(props);
}

/**
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 @@ -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
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,23 @@
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;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION;

public class HelidonHealthCheckEnricher extends AbstractMicroprofileHealthCheckEnricher {
private static final String DEFAULT_HELIDON_PORT = "8080";
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 +42,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,18 +21,24 @@

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

public class HelidonGenerator extends JavaExecGenerator {
public static final String HELIDON = "helidon";
private final HelidonNestedGenerator nestedGenerator;
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 +68,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", HelidonUtilsTest.class.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", HelidonUtilsTest.class.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 @@ -18,6 +18,7 @@
import io.fabric8.kubernetes.api.model.Probe;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.AssertionsForInterfaceTypes;
import org.eclipse.jkube.kit.common.Dependency;
import org.eclipse.jkube.kit.common.JavaProject;
Expand All @@ -28,7 +29,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -42,11 +45,13 @@ class HelidonHealthCheckEnricherTest {
private JavaProject javaProject;
private Properties properties;
private KubernetesListBuilder klb;
private ByteArrayOutputStream out;

@BeforeEach
void setup() {
properties = new Properties();
klb = new KubernetesListBuilder();
out = new ByteArrayOutputStream();
klb.addToItems(new DeploymentBuilder()
.editOrNewSpec()
.editOrNewTemplate()
Expand All @@ -67,12 +72,29 @@ void setup() {
.dependenciesWithTransitive(new ArrayList<>())
.build();
context = JKubeEnricherContext.builder()
.log(new KitLogger.SilentLogger())
.log(new KitLogger.PrintStreamLogger(new PrintStream(out)))
.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();
Assertions.assertThat(out.toString())
.contains("jkube-healthcheck-helidon: Helidon Application Config loaded from: " +
HelidonHealthCheckEnricherTest.class.getResource("/custom-port-microprofile-configuration/META-INF/microprofile-config.properties"));
}

@Test
void create_withNoMicroprofileDependency_shouldNotAddProbes() {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Stream;

Expand All @@ -54,11 +57,13 @@ class HelidonGeneratorTest {
private Properties projectProps;
private JavaProject project;
private GeneratorContext ctx;
private ByteArrayOutputStream out;

@BeforeEach
public void setUp() throws IOException {
ProcessorConfig config = new ProcessorConfig();
projectProps = new Properties();
out = new ByteArrayOutputStream();
projectProps.put("jkube.generator.name", "helidon");
targetDir = Files.createDirectory(temporaryFolder.resolve("target")).toFile();
project = JavaProject.builder()
Expand All @@ -73,13 +78,30 @@ public void setUp() throws IOException {
.packaging("jar")
.build();
ctx = GeneratorContext.builder()
.logger(new KitLogger.SilentLogger())
.logger(new KitLogger.PrintStreamLogger(new PrintStream(out)))
.project(project)
.config(config)
.strategy(JKubeBuildStrategy.s2i)
.build();
}

@Test
void constructorShouldLogHelidonApplicationConfigPath() {
// Given
ctx = ctx.toBuilder()
.project(project.toBuilder()
.compileClassPathElement(Objects.requireNonNull(getClass().getResource("/custom-port-microprofile-configuration")).getPath())
.build())
.build();
// When
HelidonGenerator helidonGenerator = new HelidonGenerator(ctx);
// Then
assertThat(helidonGenerator).isNotNull();
assertThat(out.toString())
.contains("helidon: Helidon Application Config loaded from: " +
HelidonGeneratorTest.class.getResource("/custom-port-microprofile-configuration/META-INF/microprofile-config.properties"));
}

@Test
@DisplayName("isApplicable, when valid ImageConfiguration present, then returns false")
void isApplicable_whenImageConfigurationPresent_thenReturnFalse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jkube.micronaut.enricher;

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

import org.eclipse.jkube.kit.common.Configs;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
Expand All @@ -27,13 +28,16 @@
import lombok.Getter;

import static org.eclipse.jkube.kit.common.Configs.asInteger;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION;
import static org.eclipse.jkube.micronaut.MicronautUtils.extractPort;
import static org.eclipse.jkube.micronaut.MicronautUtils.getMicronautConfiguration;
import static org.eclipse.jkube.micronaut.MicronautUtils.hasMicronautPlugin;
import static org.eclipse.jkube.micronaut.MicronautUtils.isHealthEnabled;

public class MicronautHealthCheckEnricher extends AbstractHealthCheckEnricher {

private final Properties micronautApplicationConfiguration;

@AllArgsConstructor
private enum Config implements Configs.Config {
READINESS_PROBE_INITIAL_DELAY_SECONDS("readinessProbeInitialDelaySeconds", null),
Expand All @@ -55,6 +59,9 @@ private enum Config implements Configs.Config {

public MicronautHealthCheckEnricher(JKubeEnricherContext buildContext) {
super(buildContext, "jkube-healthcheck-micronaut");
micronautApplicationConfiguration = getMicronautConfiguration(getContext().getProject());
log.debug("Micronaut Application Config loaded from: %s",
micronautApplicationConfiguration.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION));
}

@Override
Expand All @@ -77,7 +84,7 @@ private boolean isApplicable() {
if (!hasMicronautPlugin(getContext().getProject())){
return false;
}
return isHealthEnabled(getMicronautConfiguration(getContext().getProject()));
return isHealthEnabled(micronautApplicationConfiguration);
}

private Probe buildProbe(Integer initialDelaySeconds, Integer periodSeconds){
Expand Down
Loading

0 comments on commit e2c44f4

Please sign in to comment.