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

Default service definition not taken into account #20

Merged
merged 1 commit into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -55,36 +55,12 @@ public Stream<KnativeServiceDefinition> stream() {
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();
}

Expand All @@ -96,28 +72,7 @@ public KnativeServiceDefinition mandatoryLookupService(Knative.Type type, String


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));
}

// ************************
Expand All @@ -126,6 +81,63 @@ public KnativeServiceDefinition lookupServiceOrDefault(Knative.Type type, String
//
// ************************

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void after() throws Exception {
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);

Expand All @@ -82,6 +82,16 @@ void testLoadEnvironment() throws Exception {
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\"");
Expand Down
11 changes: 11 additions & 0 deletions camel-knative/src/test/resources/environment.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ""
}
}
]
}