You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2019/02/28 12:15:59 UTC
[camel-k-runtime] branch master updated: Default service definition
not taken into account #19
This is an automated email from the ASF dual-hosted git repository.
lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
The following commit(s) were added to refs/heads/master by this push:
new 20a63aa Default service definition not taken into account #19
20a63aa is described below
commit 20a63aa63f5f2e6eafa4af653a33932a4881eeee
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Thu Feb 28 12:24:51 2019 +0100
Default service definition not taken into account #19
---
.../component/knative/KnativeEnvironment.java | 116 ++++++++++++---------
.../component/knative/KnativeComponentTest.java | 12 ++-
camel-knative/src/test/resources/environment.json | 11 ++
3 files changed, 86 insertions(+), 53 deletions(-)
diff --git a/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeEnvironment.java b/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeEnvironment.java
index 52ca039..2e8b710 100644
--- a/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeEnvironment.java
+++ b/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeEnvironment.java
@@ -55,36 +55,12 @@ public class KnativeEnvironment {
return services.stream();
}
- public Optional<KnativeServiceDefinition> lookupService(Knative.Type type, String name) {
- final String contextPath = StringHelper.after(name, "/");
- final String serviceName = (contextPath == null) ? name : StringHelper.before(name, "/");
-
- return services.stream()
- .filter(definition -> {
- return Objects.equals(type.name(), definition.getMetadata().get(Knative.KNATIVE_TYPE))
- && Objects.equals(serviceName, definition.getName());
- })
- .map(definition -> {
- //
- // The context path set on the endpoint overrides the one
- // eventually provided by the service definition.
- //
- if (contextPath != null) {
- return new KnativeServiceDefinition(
- definition.getType(),
- definition.getProtocol(),
- definition.getName(),
- definition.getHost(),
- definition.getPort(),
- KnativeSupport.mergeMaps(
- definition.getMetadata(),
- Collections.singletonMap(ServiceDefinition.SERVICE_META_PATH, "/" + contextPath)
- )
- );
- }
-
- return definition;
- })
+ public Optional<KnativeServiceDefinition> lookupService(Knative.Type type, String name, String... aliases) {
+ return Stream.concat(Stream.of(name), Stream.of(aliases))
+ .sequential()
+ .map(n -> lookup(type, n))
+ .filter(Optional::isPresent)
+ .map(Optional::get)
.findFirst();
}
@@ -96,28 +72,7 @@ public class KnativeEnvironment {
public KnativeServiceDefinition lookupServiceOrDefault(Knative.Type type, String name) {
- return lookupService(type, name).orElseGet(() -> {
- String contextPath = StringHelper.after(name, "/");
- String serviceName = (contextPath == null) ? name : StringHelper.before(name, "/");
- Map<String, String> meta = new HashMap<>();
-
- if (type == Knative.Type.channel && !serviceName.endsWith(".channel")) {
- serviceName += "-channel";
-
- }
- if (contextPath != null) {
- meta.put(ServiceDefinition.SERVICE_META_PATH, "/" + contextPath);
- }
-
- return new KnativeEnvironment.KnativeServiceDefinition(
- type,
- Knative.Protocol.http,
- serviceName,
- "",
- -1,
- meta
- );
- });
+ return lookupService(type, name, "default").orElseGet(() -> computeServiceDefinition(type, name));
}
// ************************
@@ -126,6 +81,63 @@ public class KnativeEnvironment {
//
// ************************
+ private Optional<KnativeServiceDefinition> lookup(Knative.Type type, String name) {
+ final String contextPath = StringHelper.after(name, "/");
+ final String serviceName = (contextPath == null) ? name : StringHelper.before(name, "/");
+
+ return services.stream()
+ .filter(definition -> {
+ return Objects.equals(type.name(), definition.getMetadata().get(Knative.KNATIVE_TYPE))
+ && Objects.equals(serviceName, definition.getName());
+ })
+ .map(definition -> {
+ //
+ // The context path set on the endpoint overrides the one
+ // eventually provided by the service definition.
+ //
+ if (contextPath != null) {
+ return new KnativeServiceDefinition(
+ definition.getType(),
+ definition.getProtocol(),
+ definition.getName(),
+ definition.getHost(),
+ definition.getPort(),
+ KnativeSupport.mergeMaps(
+ definition.getMetadata(),
+ Collections.singletonMap(ServiceDefinition.SERVICE_META_PATH, "/" + contextPath)
+ )
+ );
+ }
+
+ return definition;
+ })
+ .findFirst();
+
+ }
+
+ public static KnativeServiceDefinition computeServiceDefinition(Knative.Type type, String name) {
+ String contextPath = StringHelper.after(name, "/");
+ String serviceName = (contextPath == null) ? name : StringHelper.before(name, "/");
+ Map<String, String> meta = new HashMap<>();
+
+ if (type == Knative.Type.channel && !serviceName.endsWith("-channel")) {
+ serviceName += "-channel";
+
+ }
+ if (contextPath != null) {
+ meta.put(ServiceDefinition.SERVICE_META_PATH, "/" + contextPath);
+ }
+
+ return new KnativeEnvironment.KnativeServiceDefinition(
+ type,
+ Knative.Protocol.http,
+ serviceName,
+ "",
+ -1,
+ meta
+ );
+ }
+
public static KnativeEnvironment mandatoryLoadFromSerializedString(CamelContext context, String configuration) throws Exception {
try (Reader reader = new StringReader(configuration)) {
return Knative.MAPPER.readValue(reader, KnativeEnvironment.class);
diff --git a/camel-knative/src/test/java/org/apache/camel/component/knative/KnativeComponentTest.java b/camel-knative/src/test/java/org/apache/camel/component/knative/KnativeComponentTest.java
index 56f4df3..358149e 100644
--- a/camel-knative/src/test/java/org/apache/camel/component/knative/KnativeComponentTest.java
+++ b/camel-knative/src/test/java/org/apache/camel/component/knative/KnativeComponentTest.java
@@ -73,7 +73,7 @@ public class KnativeComponentTest {
void testLoadEnvironment() throws Exception {
KnativeEnvironment env = mandatoryLoadFromResource(context, "classpath:/environment.json");
- assertThat(env.stream()).hasSize(2);
+ assertThat(env.stream()).hasSize(3);
assertThat(env.stream()).anyMatch(s -> s.getType() == Knative.Type.channel);
assertThat(env.stream()).anyMatch(s -> s.getType() == Knative.Type.endpoint);
@@ -82,6 +82,16 @@ public class KnativeComponentTest {
assertThat(env.lookupService(Knative.Type.endpoint, "e1")).isPresent();
assertThat(env.lookupService(Knative.Type.endpoint, "c1")).isNotPresent();
+ assertThat(env.lookupServiceOrDefault(Knative.Type.endpoint, "undefined"))
+ .hasFieldOrPropertyWithValue("name", "default")
+ .hasFieldOrPropertyWithValue("host", "0.0.0.0")
+ .hasFieldOrPropertyWithValue("port", 8080);
+
+ assertThat(env.lookupServiceOrDefault(Knative.Type.channel, "myChannel"))
+ .hasFieldOrPropertyWithValue("name", "myChannel-channel")
+ .hasFieldOrPropertyWithValue("host", "")
+ .hasFieldOrPropertyWithValue("port", -1);
+
assertThatThrownBy(() -> env.mandatoryLookupService(Knative.Type.endpoint, "unknown"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unable to find the service \"unknown\" with type \"endpoint\"");
diff --git a/camel-knative/src/test/resources/environment.json b/camel-knative/src/test/resources/environment.json
index 4fbe864..03c0dfe 100644
--- a/camel-knative/src/test/resources/environment.json
+++ b/camel-knative/src/test/resources/environment.json
@@ -21,6 +21,17 @@
"service.path": "",
"knative.event.type": ""
}
+ },
+ {
+ "type": "endpoint",
+ "protocol": "http",
+ "name": "default",
+ "host": "0.0.0.0",
+ "port": "8080",
+ "metadata": {
+ "service.path": "",
+ "knative.event.type": ""
+ }
}
]
}
\ No newline at end of file