Search in sources :

Example 1 with Kamelet

use of io.fabric8.camelk.v1alpha1.Kamelet in project kubernetes-client by fabric8io.

the class KameletModelTest method shouldCreateKamelet.

@Test
void shouldCreateKamelet() {
    Kamelet kamelet = new KameletBuilder().withNewMetadata().withName("my-kamelet").endMetadata().withNewSpec().withDefinition(new JSONSchemaPropsBuilder().build()).endSpec().build();
    assertNotNull(kamelet);
    assertEquals("my-kamelet", kamelet.getMetadata().getName());
}
Also used : JSONSchemaPropsBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsBuilder) KameletBuilder(io.fabric8.camelk.v1alpha1.KameletBuilder) Kamelet(io.fabric8.camelk.v1alpha1.Kamelet) Test(org.junit.jupiter.api.Test)

Example 2 with Kamelet

use of io.fabric8.camelk.v1alpha1.Kamelet in project camel-kamelets by apache.

the class KameletsCatalog method initCatalog.

private static Map<String, Kamelet> initCatalog() {
    Map<String, Kamelet> kameletModels = new HashMap<>();
    try (ScanResult scanResult = new ClassGraph().acceptPaths("/" + KAMELETS_DIR + "/").scan()) {
        for (Resource resource : scanResult.getAllResources()) {
            try (InputStream is = resource.open()) {
                String name = sanitizeFileName(resource.getPath());
                Kamelet kamelet = MAPPER.readValue(is, Kamelet.class);
                LOG.debug("Loading kamelet from: {}, path: {}, name: {}", resource.getClasspathElementFile(), resource.getPath(), name);
                kameletModels.put(name, kamelet);
            } catch (IOException e) {
                LOG.warn("Cannot init Kamelet Catalog with content of " + resource.getPath(), e);
            }
        }
    }
    return Collections.unmodifiableMap(kameletModels);
}
Also used : ScanResult(io.github.classgraph.ScanResult) Kamelet(io.fabric8.camelk.v1alpha1.Kamelet) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ClassGraph(io.github.classgraph.ClassGraph) Resource(io.github.classgraph.Resource) IOException(java.io.IOException)

Example 3 with Kamelet

use of io.fabric8.camelk.v1alpha1.Kamelet in project yaks by citrusframework.

the class CreateKameletAction method createKamelet.

private void createKamelet(TestContext context) {
    final Kamelet kamelet;
    if (resource != null) {
        try {
            String resolvedSource;
            if (supportVariables) {
                resolvedSource = context.replaceDynamicContentInString(FileUtils.readToString(resource));
            } else {
                resolvedSource = FileUtils.readToString(resource);
            }
            kamelet = KubernetesSupport.yaml().loadAs(resolvedSource, Kamelet.class);
        } catch (IOException e) {
            throw new CitrusRuntimeException(String.format("Failed to load Kamelet from resource %s", name + ".kamelet.yaml"));
        }
    } else {
        if (definition.getTitle() != null) {
            definition.setTitle(context.replaceDynamicContentInString(definition.getTitle()));
        }
        if (definition.getDescription() != null) {
            definition.setDescription(context.replaceDynamicContentInString(definition.getDescription()));
        }
        definition.setProperties(context.resolveDynamicValuesInMap(definition.getProperties()));
        definition.setRequired(context.resolveDynamicValuesInList(definition.getRequired()));
        final Kamelet.Builder builder = new Kamelet.Builder().name(context.replaceDynamicContentInString(name)).definition(definition);
        if (template != null) {
            builder.template(context.replaceDynamicContentInString(template));
        }
        if (source != null) {
            builder.source(source.getName(), context.replaceDynamicContentInString(source.getContent()));
        }
        if (dependencies != null && !dependencies.isEmpty()) {
            builder.dependencies(context.resolveDynamicValuesInList(dependencies));
        }
        if (types != null && !types.isEmpty()) {
            builder.types(context.resolveDynamicValuesInMap(types));
        }
        kamelet = builder.build();
    }
    if (LOG.isDebugEnabled()) {
        try {
            LOG.debug(KubernetesSupport.json().writeValueAsString(kamelet));
        } catch (JsonProcessingException e) {
            LOG.warn("Unable to dump Kamelet data", e);
        }
    }
    CustomResourceDefinitionContext ctx = CamelKSupport.kameletCRDContext(CamelKSettings.getKameletApiVersion());
    getKubernetesClient().customResources(ctx, Kamelet.class, KameletList.class).inNamespace(namespace(context)).createOrReplace(kamelet);
    LOG.info(String.format("Successfully created Kamelet '%s'", kamelet.getMetadata().getName()));
}
Also used : CustomResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext) Kamelet(org.citrusframework.yaks.camelk.model.Kamelet) CitrusRuntimeException(com.consol.citrus.exceptions.CitrusRuntimeException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 4 with Kamelet

use of io.fabric8.camelk.v1alpha1.Kamelet in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class CamelOperandSupport method createSteps.

public static List<KameletEndpoint> createSteps(ManagedConnector connector, ConnectorConfiguration<ObjectNode, ObjectNode> connectorConfiguration, CamelShardMetadata shardMetadata, KameletEndpoint kafkaEndpoint) {
    String consumes = Optional.ofNullable(connectorConfiguration.getDataShapeSpec()).map(spec -> spec.at("/consumes/format")).filter(node -> !node.isMissingNode()).map(JsonNode::asText).orElse(shardMetadata.getConsumes());
    String produces = Optional.ofNullable(connectorConfiguration.getDataShapeSpec()).map(spec -> spec.at("/produces/format")).filter(node -> !node.isMissingNode()).map(JsonNode::asText).orElse(shardMetadata.getProduces());
    final ArrayNode steps = connectorConfiguration.getProcessorsSpec();
    final List<KameletEndpoint> stepDefinitions = new ArrayList<>(steps.size() + 2);
    if (consumes != null) {
        switch(consumes) {
            case "application/json":
                stepDefinitions.add(kamelet("cos-decoder-json-action", properties -> {
                    if (shardMetadata.getConsumesClass() != null) {
                        properties.put("contentClass", shardMetadata.getConsumesClass());
                    }
                }));
                break;
            case "avro/binary":
                stepDefinitions.add(kamelet("cos-decoder-avro-action", properties -> {
                    if (shardMetadata.getConsumesClass() != null) {
                        properties.put("contentClass", shardMetadata.getConsumesClass());
                    }
                }));
                break;
            case "application/x-java-object":
                stepDefinitions.add(kamelet("cos-decoder-pojo-action", properties -> {
                    if (shardMetadata.getConsumesClass() != null) {
                        properties.put("mimeType", produces);
                    }
                }));
                break;
            case "text/plain":
            case "application/octet-stream":
                break;
            default:
                throw new IllegalArgumentException("Unsupported value format " + consumes);
        }
    }
    for (JsonNode step : steps) {
        var element = step.fields().next();
        String templateId = shardMetadata.getKamelets().getProcessors().get(element.getKey());
        if (templateId == null) {
            throw new IllegalArgumentException("Unknown processor: " + element.getKey());
        }
        stepDefinitions.add(configureStep(templateId, (ObjectNode) element.getValue()));
    }
    if (produces != null) {
        switch(produces) {
            case "application/json":
                stepDefinitions.add(kamelet("cos-encoder-json-action", properties -> {
                    if (shardMetadata.getProducesClass() != null) {
                        properties.put("contentClass", shardMetadata.getProducesClass());
                    }
                }));
                break;
            case "avro/binary":
                stepDefinitions.add(kamelet("cos-encoder-avro-action", properties -> {
                    if (shardMetadata.getProducesClass() != null) {
                        properties.put("contentClass", shardMetadata.getProducesClass());
                    }
                }));
                break;
            case "text/plain":
                stepDefinitions.add(kamelet("cos-encoder-string-action"));
                break;
            case "application/octet-stream":
                stepDefinitions.add(kamelet("cos-encoder-bytearray-action"));
                break;
            default:
                throw new IllegalArgumentException("Unsupported value format " + produces);
        }
    }
    // If it is a sink, then it consumes from kafka
    if (isSink(shardMetadata)) {
        String valueDeserializer = "org.bf2.cos.connector.camel.serdes.bytes.ByteArrayDeserializer";
        if ("application/json".equals(consumes) && hasSchemaRegistry(connector)) {
            valueDeserializer = "org.bf2.cos.connector.camel.serdes.json.JsonDeserializer";
        } else if ("avro/binary".equals(produces) && hasSchemaRegistry(connector)) {
            valueDeserializer = "org.bf2.cos.connector.camel.serdes.avro.AvroDeserializer";
        }
        kafkaEndpoint.getProperties().put("valueDeserializer", valueDeserializer);
    }
    // If it is a source, then it produces to kafka
    if (isSource(shardMetadata)) {
        String valueSerializer = "org.bf2.cos.connector.camel.serdes.bytes.ByteArraySerializer";
        if ("application/json".equals(produces) && hasSchemaRegistry(connector)) {
            valueSerializer = "org.bf2.cos.connector.camel.serdes.json.JsonSerializer";
        } else if ("avro/binary".equals(produces) && hasSchemaRegistry(connector)) {
            valueSerializer = "org.bf2.cos.connector.camel.serdes.avro.AvroSerializer";
        }
        kafkaEndpoint.getProperties().put("valueSerializer", valueSerializer);
    }
    return stepDefinitions;
}
Also used : SA_CLIENT_ID_PROPERTY(org.bf2.cos.fleetshard.operator.camel.CamelConstants.SA_CLIENT_ID_PROPERTY) ConnectorStatusSpec(org.bf2.cos.fleetshard.api.ConnectorStatusSpec) LoggerFactory(org.slf4j.LoggerFactory) ERROR_HANDLER_LOG_TYPE(org.bf2.cos.fleetshard.operator.camel.CamelConstants.ERROR_HANDLER_LOG_TYPE) KameletEndpoint(org.bf2.cos.fleetshard.operator.camel.model.KameletEndpoint) StringUtils(org.apache.commons.lang3.StringUtils) KameletBindingStatus(org.bf2.cos.fleetshard.operator.camel.model.KameletBindingStatus) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) EndpointKamelet(org.bf2.cos.fleetshard.operator.camel.model.EndpointKamelet) SA_CLIENT_SECRET_PLACEHOLDER(org.bf2.cos.fleetshard.operator.camel.CamelConstants.SA_CLIENT_SECRET_PLACEHOLDER) ERROR_HANDLER_STOP_URI(org.bf2.cos.fleetshard.operator.camel.CamelConstants.ERROR_HANDLER_STOP_URI) ERROR_HANDLER_TYPE_DLQ(org.bf2.cos.fleetshard.operator.camel.CamelConstants.ERROR_HANDLER_TYPE_DLQ) Serialization(io.fabric8.kubernetes.client.utils.Serialization) Locale(java.util.Locale) Map(java.util.Map) SA_CLIENT_ID_PLACEHOLDER(org.bf2.cos.fleetshard.operator.camel.CamelConstants.SA_CLIENT_ID_PLACEHOLDER) JsonNode(com.fasterxml.jackson.databind.JsonNode) ERROR_HANDLER_SINK_CHANNEL_TYPE(org.bf2.cos.fleetshard.operator.camel.CamelConstants.ERROR_HANDLER_SINK_CHANNEL_TYPE) ManagedConnector(org.bf2.cos.fleetshard.api.ManagedConnector) ConnectorConfiguration(org.bf2.cos.fleetshard.operator.connector.ConnectorConfiguration) ERROR_HANDLER_DEAD_LETTER_CHANNEL_KAMELET(org.bf2.cos.fleetshard.operator.camel.CamelConstants.ERROR_HANDLER_DEAD_LETTER_CHANNEL_KAMELET) CamelShardMetadata(org.bf2.cos.fleetshard.operator.camel.model.CamelShardMetadata) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ERROR_HANDLER_TYPE_LOG(org.bf2.cos.fleetshard.operator.camel.CamelConstants.ERROR_HANDLER_TYPE_LOG) KameletEndpoint.kamelet(org.bf2.cos.fleetshard.operator.camel.model.KameletEndpoint.kamelet) CaseUtils(org.apache.commons.text.CaseUtils) ERROR_HANDLER_TYPE_STOP(org.bf2.cos.fleetshard.operator.camel.CamelConstants.ERROR_HANDLER_TYPE_STOP) ServiceAccountSpec(org.bf2.cos.fleetshard.api.ServiceAccountSpec) KameletBinding(org.bf2.cos.fleetshard.operator.camel.model.KameletBinding) StandardCharsets(java.nio.charset.StandardCharsets) Kamelet(org.bf2.cos.fleetshard.operator.camel.model.Kamelet) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Base64(java.util.Base64) List(java.util.List) Condition(io.fabric8.kubernetes.api.model.Condition) CONNECTOR_TYPE_SOURCE(org.bf2.cos.fleetshard.operator.camel.CamelConstants.CONNECTOR_TYPE_SOURCE) TreeMap(java.util.TreeMap) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Optional(java.util.Optional) SA_CLIENT_SECRET_PROPERTY(org.bf2.cos.fleetshard.operator.camel.CamelConstants.SA_CLIENT_SECRET_PROPERTY) CONNECTOR_TYPE_SINK(org.bf2.cos.fleetshard.operator.camel.CamelConstants.CONNECTOR_TYPE_SINK) JacksonUtil.iterator(org.bf2.cos.fleetshard.support.json.JacksonUtil.iterator) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) KameletEndpoint(org.bf2.cos.fleetshard.operator.camel.model.KameletEndpoint) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 5 with Kamelet

use of io.fabric8.camelk.v1alpha1.Kamelet in project yaks by citrusframework.

the class VerifyKameletAction method doExecute.

@Override
public void doExecute(TestContext context) {
    String kameletName = context.replaceDynamicContentInString(name);
    CustomResourceDefinitionContext ctx = CamelKSupport.kameletCRDContext(CamelKSettings.getKameletApiVersion());
    Kamelet kamelet = getKubernetesClient().customResources(ctx, Kamelet.class, KameletList.class).inNamespace(namespace(context)).withName(kameletName).get();
    if (kamelet == null) {
        throw new ValidationException(String.format("Failed to retrieve Kamelet '%s' in namespace '%s'", name, namespace(context)));
    }
    LOG.info("Kamlet validation successful - All values OK!");
    if (LOG.isDebugEnabled()) {
        try {
            LOG.debug(KubernetesSupport.json().writeValueAsString(kamelet));
        } catch (JsonProcessingException e) {
            LOG.warn("Unable to dump Kamelet data", e);
        }
    }
}
Also used : CustomResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext) ValidationException(com.consol.citrus.exceptions.ValidationException) KameletList(org.citrusframework.yaks.camelk.model.KameletList) Kamelet(org.citrusframework.yaks.camelk.model.Kamelet) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

Kamelet (io.fabric8.camelk.v1alpha1.Kamelet)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 CustomResourceDefinitionContext (io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext)2 IOException (java.io.IOException)2 CitrusRuntimeException (com.consol.citrus.exceptions.CitrusRuntimeException)1 ValidationException (com.consol.citrus.exceptions.ValidationException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 KameletBuilder (io.fabric8.camelk.v1alpha1.KameletBuilder)1 Condition (io.fabric8.kubernetes.api.model.Condition)1 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 JSONSchemaPropsBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsBuilder)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 Serialization (io.fabric8.kubernetes.client.utils.Serialization)1 ClassGraph (io.github.classgraph.ClassGraph)1 Resource (io.github.classgraph.Resource)1 ScanResult (io.github.classgraph.ScanResult)1 KameletSpec (io.kaoto.backend.model.deployment.kamelet.KameletSpec)1