Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leveragte MP Config to load application and user properties #546

Merged
merged 4 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.camel.k.Runtime;
import org.apache.camel.k.support.Constants;
import org.apache.camel.k.support.KubernetesPropertiesFunction;
import org.apache.camel.k.support.PropertiesSupport;

public class PropertiesConfigurer extends AbstractPhaseListener {
public PropertiesConfigurer() {
Expand All @@ -34,13 +33,6 @@ public int getOrder() {

@Override
protected void accept(Runtime runtime) {
runtime.setInitialProperties(
PropertiesSupport.loadApplicationProperties()
);
runtime.setPropertiesLocations(
PropertiesSupport.resolveUserPropertiesLocations()
);

//
// Register properties functions to resolve k8s secrets or config maps like:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,9 @@
*/
package org.apache.camel.k.support;

import java.io.IOException;
import java.io.Reader;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.function.Predicate;

import org.apache.camel.CamelContext;
Expand All @@ -41,7 +28,6 @@
import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.ObjectHelper;

public final class PropertiesSupport {
private PropertiesSupport() {
Expand Down Expand Up @@ -103,93 +89,4 @@ public static <T> T bindProperties(CamelContext context, T target, Predicate<Str
return target;
}

public static String resolveApplicationPropertiesLocation() {
return System.getProperty(Constants.PROPERTY_CAMEL_K_CONF, System.getenv(Constants.ENV_CAMEL_K_CONF));
}

public static Properties loadApplicationProperties() {
final String conf = resolveApplicationPropertiesLocation();
final Properties properties = new Properties();

if (ObjectHelper.isEmpty(conf)) {
return properties;
}

try {
Path confPath = Paths.get(conf);

if (Files.exists(confPath)) {
try (Reader reader = Files.newBufferedReader(confPath)) {
properties.load(reader);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}

return properties;
}

public static String resolveUserPropertiesLocation() {
return System.getProperty(Constants.PROPERTY_CAMEL_K_CONF_D, System.getenv(Constants.ENV_CAMEL_K_CONF_D));
}

public static Properties loadUserProperties() {
final Properties properties = new Properties();

try {
for (String location: resolveUserPropertiesLocations()) {
try (Reader reader = Files.newBufferedReader(Paths.get(location))) {
properties.load(reader);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}

return properties;
}

public static Properties loadProperties() {
final Properties app = loadApplicationProperties();
final Properties usr = loadUserProperties();

app.putAll(usr);

return app;
}

public static Collection<String> resolveUserPropertiesLocations() {
final String conf = resolveUserPropertiesLocation();
final Set<String> locations = new LinkedHashSet<>();

// Additional locations
if (ObjectHelper.isNotEmpty(conf)) {
Path root = Paths.get(conf);
FileVisitor<Path> visitor = new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Objects.requireNonNull(file);
Objects.requireNonNull(attrs);

final String path = file.toFile().getAbsolutePath();
if (path.endsWith(".properties")) {
locations.add(path);
}

return FileVisitResult.CONTINUE;
}
};

if (Files.exists(root)) {
try {
Files.walkFileTree(root, visitor);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

return locations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@
*/
package org.apache.camel.k.support;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -294,4 +305,77 @@ public static String getRuntimeVersion() {
return Objects.requireNonNull(version, "Could not determine Camel K Runtime version");
}

// *********************************
//
// Properties
//
// *********************************

public static Map<String, String> loadApplicationProperties() {
final String conf = System.getProperty(Constants.PROPERTY_CAMEL_K_CONF, System.getenv(Constants.ENV_CAMEL_K_CONF));
final Map<String, String> properties = new HashMap<>();

if (ObjectHelper.isEmpty(conf)) {
return properties;
}

try {
Path confPath = Paths.get(conf);

if (Files.exists(confPath)) {
try (Reader reader = Files.newBufferedReader(confPath)) {
Properties p = new Properties();
p.load(reader);
p.forEach((key, value) -> properties.put(String.valueOf(key), String.valueOf(value)));
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}

return properties;
}

public static Map<String, String> loadUserProperties() {
final String conf = System.getProperty(Constants.PROPERTY_CAMEL_K_CONF_D, System.getenv(Constants.ENV_CAMEL_K_CONF_D));
final Map<String, String> properties = new HashMap<>();

if (ObjectHelper.isEmpty(conf)) {
return properties;
}

FileVisitor<Path> visitor = new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Objects.requireNonNull(file);
Objects.requireNonNull(attrs);

if (file.toFile().getAbsolutePath().endsWith(".properties")) {
try (Reader reader = Files.newBufferedReader(file)) {
Properties p = new Properties();
p.load(reader);
p.forEach((key, value) -> properties.put(String.valueOf(key), String.valueOf(value)));
}
} else {
properties.put(
file.getFileName().toString(),
Files.readString(file, StandardCharsets.UTF_8));
}

return FileVisitResult.CONTINUE;
}
};

Path root = Paths.get(conf);

if (Files.exists(root)) {
try {
Files.walkFileTree(root, visitor);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,23 @@
*/
package org.apache.camel.k.quarkus;

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

import io.smallrye.config.PropertiesConfigSource;
import org.apache.camel.k.support.PropertiesSupport;
import org.apache.camel.k.support.RuntimeSupport;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;

public class ApplicationConfigSourceProvider implements ConfigSourceProvider {
@Override
public Iterable<ConfigSource> getConfigSources(ClassLoader forClassLoader) {
final Properties applicationProperties = PropertiesSupport.loadProperties();
final Properties quarkusProperties = new Properties();
final Map<String, String> appProperties = RuntimeSupport.loadApplicationProperties();
final Map<String, String> usrProperties = RuntimeSupport.loadUserProperties();

for (String name : applicationProperties.stringPropertyNames()) {
if (name.startsWith("quarkus.")) {
quarkusProperties.put(name, applicationProperties.get(name));
}
}

return Collections.singletonList(
new PropertiesConfigSource(quarkusProperties, "camel-k")
return List.of(
new PropertiesConfigSource(appProperties, "camel-k-app", ConfigSource.DEFAULT_ORDINAL),
new PropertiesConfigSource(usrProperties, "camel-k-usr", ConfigSource.DEFAULT_ORDINAL + 1)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ public JsonObject inspect() {
.add(
"routes-collector",
instance(CamelMain.class).map(BaseMainSupport::getRoutesCollector).map(Object::getClass).map(Class::getName).orElse(""))
.add(
"properties-locations",
Json.createArrayBuilder(instance(CamelContext.class)
.map(CamelContext::getPropertiesComponent)
.map(PropertiesComponent.class::cast)
.map(PropertiesComponent::getLocations)
.orElseGet(Collections::emptyList)))
.build();
}

Expand All @@ -88,27 +81,16 @@ public String property(@PathParam("name") String name) {
.flatMap(pc -> pc.resolveProperty(name)).orElse("");
}

@GET
@Path("/initial-property/{name}")
@Produces(MediaType.TEXT_PLAIN)
public String initialProperty(@PathParam("name") String name) {
return (String)instance(CamelContext.class)
.map(CamelContext::getPropertiesComponent)
.map(PropertiesComponent.class::cast)
.map(pc -> pc.getInitialProperties().get(name))
.orElse("");
}

@SuppressWarnings("unchecked")
@GET
@Path("/initial-properties")
@Path("/properties")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject initialProperties() {
public JsonObject properties() {
return Json.createObjectBuilder(
instance(CamelContext.class)
.map(CamelContext::getPropertiesComponent)
.map(PropertiesComponent.class::cast)
.map(PropertiesComponent::getInitialProperties)
.map(PropertiesComponent::loadProperties)
.map(Map.class::cast)
.orElseGet(Collections::emptyMap)
).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@
*/
package org.apache.camel.k.quarkus.it;

import java.util.Map;

import javax.ws.rs.core.MediaType;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.json.JsonPath;
import org.apache.camel.k.quarkus.Application;
import org.apache.camel.k.support.PropertiesSupport;
import org.apache.camel.quarkus.core.FastCamelContext;
import org.junit.jupiter.api.Test;

Expand All @@ -50,25 +47,6 @@ public void inspect() {
.isEqualTo(Application.Runtime.class.getName());
assertThat(p.getString("routes-collector"))
.isEqualTo(Application.NoRoutesCollector.class.getName());
assertThat(p.getList("properties-locations", String.class))
.contains("file:" + System.getProperty("camel.k.conf.d", System.getenv("CAMEL_K_CONF_D")) + "/001/conf.properties")
.contains("file:" + System.getProperty("camel.k.conf.d", System.getenv("CAMEL_K_CONF_D")) + "/002/conf.properties");
}

@SuppressWarnings("unchecked")
@Test
public void initialProperties() {
Map<String, String> initialProperties = given()
.accept(MediaType.APPLICATION_JSON)
.get("/test/initial-properties")
.then()
.statusCode(200)
.extract()
.body().jsonPath().getMap(".", String.class, String.class);

assertThat(initialProperties).containsExactlyEntriesOf((Map)PropertiesSupport.loadApplicationProperties());
assertThat(initialProperties).containsEntry("root.key", "root.value");
assertThat(initialProperties).containsEntry("a.key", "a.root");
}

@Test
Expand All @@ -78,5 +56,6 @@ public void properties() {
given().get("/test/property/001.key").then().statusCode(200).body(is("001.value"));
given().get("/test/property/002.key").then().statusCode(200).body(is("002.value"));
given().get("/test/property/a.key").then().statusCode(200).body(is("a.002"));
given().get("/test/property/flat-property").then().statusCode(200).body(is("flat-value"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flat-value