Search in sources :

Example 1 with KubernetesEndpoint

use of org.apache.camel.component.kubernetes.KubernetesEndpoint in project wildfly-camel by wildfly-extras.

the class KubernetesIntegrationTest method testKubernetesProducer.

@Test
public void testKubernetesProducer() throws Exception {
    String kubernetesProducerURI = String.format("kubernetes:%s?category=pods&operation=createPod", getKubernetesMaster());
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to(kubernetesProducerURI);
        }
    });
    ProducerTemplate template = camelctx.createProducerTemplate();
    String podName = POD_NAME + "-producer";
    camelctx.start();
    KubernetesEndpoint kubernetesEndpoint = camelctx.getEndpoint(kubernetesProducerURI, KubernetesEndpoint.class);
    String kubernetesNamespace = getNamespace(kubernetesEndpoint.getKubernetesClient());
    try {
        Map<String, Object> headers = new HashMap<>();
        headers.put(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, kubernetesNamespace);
        headers.put(KubernetesConstants.KUBERNETES_POD_NAME, podName);
        headers.put(KubernetesConstants.KUBERNETES_POD_SPEC, createPodSpec());
        Pod pod = template.requestBodyAndHeaders("direct:start", null, headers, Pod.class);
        Assert.assertNotNull("Expected pod to be not null", pod);
        Assert.assertEquals("Pending", pod.getStatus().getPhase());
    } finally {
        // Clean up
        Map<String, Object> headers = new HashMap<>();
        headers.put(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, kubernetesNamespace);
        headers.put(KubernetesConstants.KUBERNETES_POD_NAME, podName);
        template.requestBodyAndHeaders("kubernetes:" + getKubernetesMaster() + "?category=pods&operation=deletePod", null, headers);
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) KubernetesEndpoint(org.apache.camel.component.kubernetes.KubernetesEndpoint) RouteBuilder(org.apache.camel.builder.RouteBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) HashMap(java.util.HashMap) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with KubernetesEndpoint

use of org.apache.camel.component.kubernetes.KubernetesEndpoint in project wildfly-camel by wildfly-extras.

the class KubernetesIntegrationTest method testKubernetesConsumer.

@Test
public void testKubernetesConsumer() throws Exception {
    String kubernetesConsumerURI = String.format("kubernetes:%s?category=pods", getKubernetesMaster());
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from(kubernetesConsumerURI).to("mock:result");
        }
    });
    MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpoint.expectedMinimumMessageCount(1);
    ProducerTemplate template = camelctx.createProducerTemplate();
    String podName = POD_NAME + "-consumer";
    camelctx.start();
    KubernetesEndpoint kubernetesEndpoint = camelctx.getEndpoint(kubernetesConsumerURI, KubernetesEndpoint.class);
    String kubernetesNamespace = getNamespace(kubernetesEndpoint.getKubernetesClient());
    try {
        // Create a pod so we can generate some watch events
        Map<String, Object> headers = new HashMap<>();
        headers.put(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, kubernetesNamespace);
        headers.put(KubernetesConstants.KUBERNETES_POD_NAME, podName);
        headers.put(KubernetesConstants.KUBERNETES_POD_SPEC, createPodSpec());
        template.requestBodyAndHeaders("kubernetes:" + getKubernetesMaster() + "?category=pods&operation=createPod", null, headers);
        mockEndpoint.assertIsSatisfied();
    } finally {
        // Clean up
        Map<String, Object> headers = new HashMap<>();
        headers.put(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, kubernetesNamespace);
        headers.put(KubernetesConstants.KUBERNETES_POD_NAME, podName);
        template.requestBodyAndHeaders("kubernetes:" + getKubernetesMaster() + "?category=pods&operation=deletePod", null, headers);
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) KubernetesEndpoint(org.apache.camel.component.kubernetes.KubernetesEndpoint) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) HashMap(java.util.HashMap) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 CamelContext (org.apache.camel.CamelContext)2 ProducerTemplate (org.apache.camel.ProducerTemplate)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 KubernetesEndpoint (org.apache.camel.component.kubernetes.KubernetesEndpoint)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 Test (org.junit.Test)2 Pod (io.fabric8.kubernetes.api.model.Pod)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1