Skip to content

Commit

Permalink
knative: support configuring the knative environment using properties a…
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Nov 17, 2020
1 parent 057ea58 commit 7500b72
Show file tree
Hide file tree
Showing 57 changed files with 1,932 additions and 557 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ jobs:
- :camel-k-itests-cron
- :camel-k-itests-kamelet
- :camel-k-itests-knative
- :camel-k-itests-knative-env-from-registry
- :camel-k-itests-knative-env-from-properties
- :camel-k-itests-knative-producer
- :camel-k-itests-knative-consumer
- :camel-k-itests-knative-sinkbinding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.camel.component.knative.KnativeComponent;
import org.apache.camel.component.knative.KnativeConstants;
import org.apache.camel.component.knative.spi.KnativeEnvironment;
import org.apache.camel.component.knative.spi.KnativeResource;
import org.apache.camel.k.quarkus.knative.KnativeRecorder;
import org.apache.camel.quarkus.core.deployment.spi.CamelRuntimeBeanBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.CamelServiceFilter;
Expand All @@ -43,7 +44,7 @@ List<UnremovableBeanBuildItem> unremovableBeans() {
List<ReflectiveClassBuildItem> reflectiveClasses() {
return List.of(
new ReflectiveClassBuildItem(true, false, KnativeEnvironment.class),
new ReflectiveClassBuildItem(true, false, KnativeEnvironment.KnativeResource.class)
new ReflectiveClassBuildItem(true, false, KnativeResource.class)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Optional;

import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.camel.CamelContext;
import org.apache.camel.component.knative.spi.Knative;
import org.apache.camel.component.knative.spi.KnativeEnvironment;
import org.apache.camel.component.knative.spi.KnativeResource;
import org.apache.camel.k.ContextCustomizer;
import org.apache.camel.k.annotation.Customizer;
import org.apache.camel.spi.Configurer;
Expand All @@ -34,72 +33,11 @@
@Configurer
@Customizer("sinkbinding")
public class KnativeSinkBindingContextCustomizer implements ContextCustomizer {

private String name;

private Knative.Type type;

private String kind;

private String apiVersion;

@Override
public void apply(CamelContext camelContext) {
createSyntheticDefinition(camelContext, name).ifPresent(serviceDefinition -> {
// publish the synthetic service definition
camelContext.getRegistry().bind(name, serviceDefinition);
});
}

private Optional<KnativeEnvironment.KnativeResource> createSyntheticDefinition(
CamelContext camelContext,
String sinkName) {

final String kSinkUrl = camelContext.resolvePropertyPlaceholders("{{k.sink:}}");
final String kCeOverride = camelContext.resolvePropertyPlaceholders("{{k.ce.overrides:}}");

if (ObjectHelper.isNotEmpty(kSinkUrl)) {
// create a synthetic service definition to target the K_SINK url
var serviceBuilder = KnativeEnvironment.serviceBuilder(type, sinkName)
.withMeta(Knative.CAMEL_ENDPOINT_KIND, Knative.EndpointKind.sink)
.withMeta(Knative.SERVICE_META_URL, kSinkUrl);

if (ObjectHelper.isNotEmpty(kind)) {
serviceBuilder = serviceBuilder.withMeta(Knative.KNATIVE_KIND, kind);
}

if (ObjectHelper.isNotEmpty(apiVersion)) {
serviceBuilder = serviceBuilder.withMeta(Knative.KNATIVE_API_VERSION, apiVersion);
}

if (ObjectHelper.isNotEmpty(kCeOverride)) {
try (Reader reader = new StringReader(kCeOverride)) {
// assume K_CE_OVERRIDES is defined as simple key/val json
var overrides = Knative.MAPPER.readValue(
reader,
new TypeReference<HashMap<String, String>>() {
}
);

for (var entry : overrides.entrySet()) {
// generate proper ce-override meta-data for the service
// definition
serviceBuilder.withMeta(
Knative.KNATIVE_CE_OVERRIDE_PREFIX + entry.getKey(),
entry.getValue()
);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

return Optional.of(serviceBuilder.build());
}

return Optional.empty();
}

public String getName() {
return name;
}
Expand Down Expand Up @@ -132,4 +70,35 @@ public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
}

@Override
public void apply(CamelContext camelContext) {
final String kSinkUrl = camelContext.resolvePropertyPlaceholders("{{k.sink:}}");
final String kCeOverride = camelContext.resolvePropertyPlaceholders("{{k.ce.overrides:}}");

if (ObjectHelper.isNotEmpty(kSinkUrl)) {
// create a synthetic service definition to target the K_SINK url
KnativeResource resource = new KnativeResource();
resource.setEndpointKind(Knative.EndpointKind.sink);
resource.setType(type);
resource.setName(name);
resource.setUrl(kSinkUrl);
resource.setObjectApiVersion(apiVersion);
resource.setObjectKind(kind);

if (ObjectHelper.isNotEmpty(kCeOverride)) {
try (Reader reader = new StringReader(kCeOverride)) {
// assume K_CE_OVERRIDES is defined as simple key/val json
Knative.MAPPER.readValue(
reader,
new TypeReference<HashMap<String, String>>() {
}
).forEach(resource::addCeOverride);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

camelContext.getRegistry().bind(name, resource);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Generated by camel build tools - do NOT edit this file! */
package org.apache.camel.component.knative.spi;

import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.spi.GeneratedPropertyConfigurer;
import org.apache.camel.spi.PropertyConfigurerGetter;
import org.apache.camel.util.CaseInsensitiveMap;
import org.apache.camel.component.knative.spi.KnativeEnvironment;

/**
* Generated by camel build tools - do NOT edit this file!
*/
@SuppressWarnings("unchecked")
public class KnativeEnvironmentConfigurer extends org.apache.camel.support.component.PropertyConfigurerSupport implements GeneratedPropertyConfigurer, PropertyConfigurerGetter {

private static final Map<String, Object> ALL_OPTIONS;
static {
Map<String, Object> map = new CaseInsensitiveMap();
map.put("Resources", java.util.List.class);
ALL_OPTIONS = map;
}

@Override
public boolean configure(CamelContext camelContext, Object obj, String name, Object value, boolean ignoreCase) {
org.apache.camel.component.knative.spi.KnativeEnvironment target = (org.apache.camel.component.knative.spi.KnativeEnvironment) obj;
switch (ignoreCase ? name.toLowerCase() : name) {
case "resources":
case "Resources": target.setResources(property(camelContext, java.util.List.class, value)); return true;
default: return false;
}
}

@Override
public Map<String, Object> getAllOptions(Object target) {
return ALL_OPTIONS;
}

@Override
public Object getOptionValue(Object obj, String name, boolean ignoreCase) {
org.apache.camel.component.knative.spi.KnativeEnvironment target = (org.apache.camel.component.knative.spi.KnativeEnvironment) obj;
switch (ignoreCase ? name.toLowerCase() : name) {
case "resources":
case "Resources": return target.getResources();
default: return null;
}
}

@Override
public Object getCollectionValueType(Object target, String name, boolean ignoreCase) {
switch (ignoreCase ? name.toLowerCase() : name) {
case "resources":
case "Resources": return org.apache.camel.component.knative.spi.KnativeResource.class;
default: return null;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* Generated by camel build tools - do NOT edit this file! */
package org.apache.camel.component.knative.spi;

import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.spi.GeneratedPropertyConfigurer;
import org.apache.camel.spi.PropertyConfigurerGetter;
import org.apache.camel.util.CaseInsensitiveMap;
import org.apache.camel.component.knative.spi.KnativeResource;

/**
* Generated by camel build tools - do NOT edit this file!
*/
@SuppressWarnings("unchecked")
public class KnativeResourceConfigurer extends org.apache.camel.support.component.PropertyConfigurerSupport implements GeneratedPropertyConfigurer, PropertyConfigurerGetter {

private static final Map<String, Object> ALL_OPTIONS;
static {
Map<String, Object> map = new CaseInsensitiveMap();
map.put("CeOverrides", java.util.Map.class);
map.put("CloudEventType", java.lang.String.class);
map.put("ContentType", java.lang.String.class);
map.put("EndpointKind", org.apache.camel.component.knative.spi.Knative.EndpointKind.class);
map.put("Filters", java.util.Map.class);
map.put("Metadata", java.util.Map.class);
map.put("Name", java.lang.String.class);
map.put("ObjectApiVersion", java.lang.String.class);
map.put("ObjectKind", java.lang.String.class);
map.put("ObjectName", java.lang.String.class);
map.put("Path", java.lang.String.class);
map.put("Reply", java.lang.Boolean.class);
map.put("Type", org.apache.camel.component.knative.spi.Knative.Type.class);
map.put("Url", java.lang.String.class);
ALL_OPTIONS = map;
}

@Override
public boolean configure(CamelContext camelContext, Object obj, String name, Object value, boolean ignoreCase) {
org.apache.camel.component.knative.spi.KnativeResource target = (org.apache.camel.component.knative.spi.KnativeResource) obj;
switch (ignoreCase ? name.toLowerCase() : name) {
case "ceoverrides":
case "CeOverrides": target.setCeOverrides(property(camelContext, java.util.Map.class, value)); return true;
case "cloudeventtype":
case "CloudEventType": target.setCloudEventType(property(camelContext, java.lang.String.class, value)); return true;
case "contenttype":
case "ContentType": target.setContentType(property(camelContext, java.lang.String.class, value)); return true;
case "endpointkind":
case "EndpointKind": target.setEndpointKind(property(camelContext, org.apache.camel.component.knative.spi.Knative.EndpointKind.class, value)); return true;
case "filters":
case "Filters": target.setFilters(property(camelContext, java.util.Map.class, value)); return true;
case "metadata":
case "Metadata": target.setMetadata(property(camelContext, java.util.Map.class, value)); return true;
case "name":
case "Name": target.setName(property(camelContext, java.lang.String.class, value)); return true;
case "objectapiversion":
case "ObjectApiVersion": target.setObjectApiVersion(property(camelContext, java.lang.String.class, value)); return true;
case "objectkind":
case "ObjectKind": target.setObjectKind(property(camelContext, java.lang.String.class, value)); return true;
case "objectname":
case "ObjectName": target.setObjectName(property(camelContext, java.lang.String.class, value)); return true;
case "path":
case "Path": target.setPath(property(camelContext, java.lang.String.class, value)); return true;
case "reply":
case "Reply": target.setReply(property(camelContext, java.lang.Boolean.class, value)); return true;
case "type":
case "Type": target.setType(property(camelContext, org.apache.camel.component.knative.spi.Knative.Type.class, value)); return true;
case "url":
case "Url": target.setUrl(property(camelContext, java.lang.String.class, value)); return true;
default: return false;
}
}

@Override
public Map<String, Object> getAllOptions(Object target) {
return ALL_OPTIONS;
}

@Override
public Object getOptionValue(Object obj, String name, boolean ignoreCase) {
org.apache.camel.component.knative.spi.KnativeResource target = (org.apache.camel.component.knative.spi.KnativeResource) obj;
switch (ignoreCase ? name.toLowerCase() : name) {
case "ceoverrides":
case "CeOverrides": return target.getCeOverrides();
case "cloudeventtype":
case "CloudEventType": return target.getCloudEventType();
case "contenttype":
case "ContentType": return target.getContentType();
case "endpointkind":
case "EndpointKind": return target.getEndpointKind();
case "filters":
case "Filters": return target.getFilters();
case "metadata":
case "Metadata": return target.getMetadata();
case "name":
case "Name": return target.getName();
case "objectapiversion":
case "ObjectApiVersion": return target.getObjectApiVersion();
case "objectkind":
case "ObjectKind": return target.getObjectKind();
case "objectname":
case "ObjectName": return target.getObjectName();
case "path":
case "Path": return target.getPath();
case "reply":
case "Reply": return target.getReply();
case "type":
case "Type": return target.getType();
case "url":
case "Url": return target.getUrl();
default: return null;
}
}

@Override
public Object getCollectionValueType(Object target, String name, boolean ignoreCase) {
switch (ignoreCase ? name.toLowerCase() : name) {
case "ceoverrides":
case "CeOverrides": return java.lang.String.class;
case "filters":
case "Filters": return java.lang.String.class;
case "metadata":
case "Metadata": return java.lang.String.class;
default: return null;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by camel build tools - do NOT edit this file!
class=org.apache.camel.component.knative.spi.KnativeEnvironmentConfigurer
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by camel build tools - do NOT edit this file!
class=org.apache.camel.component.knative.spi.KnativeResourceConfigurer
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@
public final class Knative {
public static final ObjectMapper MAPPER = new ObjectMapper().registerModule(new Jdk8Module());

public static final String MIME_STRUCTURED_CONTENT_MODE = "application/cloudevents+json";
public static final String MIME_BATCH_CONTENT_MODE = "application/cloudevents-batch+json";

public static final String KNATIVE_TRANSPORT_RESOURCE_PATH = "META-INF/services/org/apache/camel/knative/transport/";

public static final String KNATIVE_FILTER_PREFIX = "filter.";
public static final String KNATIVE_CE_OVERRIDE_PREFIX = "ce.override.";
public static final String KNATIVE_TYPE = "knative.type";
public static final String KNATIVE_EVENT_TYPE = "knative.event.type";
public static final String KNATIVE_KIND = "knative.kind";
public static final String KNATIVE_NAME = "knative.name";
public static final String KNATIVE_API_VERSION = "knative.apiVersion";
public static final String KNATIVE_CLOUD_EVENT_TYPE = "knative.event.type";
public static final String KNATIVE_REPLY = "knative.reply";
public static final String CONTENT_TYPE = "content.type";
public static final String MIME_STRUCTURED_CONTENT_MODE = "application/cloudevents+json";
public static final String MIME_BATCH_CONTENT_MODE = "application/cloudevents-batch+json";
public static final String CAMEL_ENDPOINT_KIND = "camel.endpoint.kind";

public static final String SERVICE_META_HOST = "service.host";
public static final String SERVICE_META_ZONE = "service.zone";
public static final String SERVICE_META_PATH = "service.path";
public static final String SERVICE_META_URL = "service.url";

public static final String KNATIVE_OBJECT_API_VERSION = "knative.apiVersion";
public static final String KNATIVE_OBJECT_KIND = "knative.kind";
public static final String KNATIVE_OBJECT_NAME = "knative.name";


private Knative() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ public interface KnativeConsumerFactory extends Service {
Consumer createConsumer(
Endpoint endpoint,
KnativeTransportConfiguration configuration,
KnativeEnvironment.KnativeResource service, Processor processor);
KnativeResource service, Processor processor);
}
Loading

0 comments on commit 7500b72

Please sign in to comment.