Search in sources :

Example 11 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class HttpGatewayTest method testENTESB7600.

@Test
public void testENTESB7600() throws Exception {
    // response can not contain CONTENT_LENGTH and TRANSFER_ENCODING see https://tools.ietf.org/html/rfc7230#section-3.3.3
    startRestEndpoint();
    startHttpGateway();
    System.out.println("Requesting...");
    final FutureHandler<HttpClientResponse> future = new FutureHandler<>();
    vertx.createHttpClient().setHost("localhost").setPort(8080).get("/hello/world?wsdl", new Handler<HttpClientResponse>() {

        @Override
        public void handle(HttpClientResponse event) {
            future.handle(event);
        }
    }).end();
    MultiMap responseHeaders = future.await().headers();
    assertTrue((responseHeaders.contains(CONTENT_LENGTH) && !responseHeaders.contains(TRANSFER_ENCODING)) || (!responseHeaders.contains(CONTENT_LENGTH) && responseHeaders.contains(TRANSFER_ENCODING)));
    stopHttpGateway();
    stopVertx();
}
Also used : MultiMap(org.vertx.java.core.MultiMap) HttpClientResponse(org.vertx.java.core.http.HttpClientResponse) HttpGatewayHandler(io.fabric8.gateway.handlers.http.HttpGatewayHandler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) Handler(org.vertx.java.core.Handler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) Test(org.junit.Test)

Example 12 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class ServiceFactoryTest method testDiscoveryOnRestartCurator.

@Test
public void testDiscoveryOnRestartCurator() throws Exception {
    underTest = new ActiveMQServiceFactory();
    underTest.curator = curator;
    Properties props = new Properties();
    props.put("config", "amq.xml");
    props.put("broker-name", "amq");
    props.put("group", "amq");
    props.put("connectors", "openwire");
    props.put("openwire-port", "0");
    props.put("container.ip", "localhost");
    underTest.updated("b", props);
    final AtomicReference<CuratorFramework> curatorFrameworkAtomicReference = new AtomicReference<>(curator);
    OsgiFabricDiscoveryAgent osgiFabricDiscoveryAgent = new OsgiFabricDiscoveryAgent(new BundleContext() {

        @Override
        public String getProperty(String s) {
            return null;
        }

        @Override
        public Bundle getBundle() {
            return null;
        }

        @Override
        public Bundle installBundle(String s, InputStream inputStream) throws BundleException {
            return null;
        }

        @Override
        public Bundle installBundle(String s) throws BundleException {
            return null;
        }

        @Override
        public Bundle getBundle(long l) {
            return null;
        }

        @Override
        public Bundle[] getBundles() {
            return new Bundle[0];
        }

        @Override
        public void addServiceListener(ServiceListener serviceListener, String s) throws InvalidSyntaxException {
        }

        @Override
        public void addServiceListener(ServiceListener serviceListener) {
        }

        @Override
        public void removeServiceListener(ServiceListener serviceListener) {
        }

        @Override
        public void addBundleListener(BundleListener bundleListener) {
        }

        @Override
        public void removeBundleListener(BundleListener bundleListener) {
        }

        @Override
        public void addFrameworkListener(FrameworkListener frameworkListener) {
        }

        @Override
        public void removeFrameworkListener(FrameworkListener frameworkListener) {
        }

        @Override
        public ServiceRegistration<?> registerService(String[] strings, Object o, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public ServiceRegistration<?> registerService(String s, Object o, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public <S> ServiceRegistration<S> registerService(Class<S> aClass, S s, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public ServiceReference<?>[] getServiceReferences(String s, String s1) throws InvalidSyntaxException {
            return new ServiceReference<?>[0];
        }

        @Override
        public ServiceReference<?>[] getAllServiceReferences(String s, String s1) throws InvalidSyntaxException {
            return new ServiceReference<?>[0];
        }

        @Override
        public ServiceReference<?> getServiceReference(String s) {
            return null;
        }

        @Override
        public <S> ServiceReference<S> getServiceReference(Class<S> aClass) {
            return null;
        }

        @Override
        public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> aClass, String s) throws InvalidSyntaxException {
            return null;
        }

        @Override
        public <S> S getService(ServiceReference<S> serviceReference) {
            return (S) curatorFrameworkAtomicReference.get();
        }

        @Override
        public boolean ungetService(ServiceReference<?> serviceReference) {
            return false;
        }

        @Override
        public File getDataFile(String s) {
            return null;
        }

        @Override
        public Filter createFilter(String s) throws InvalidSyntaxException {
            return null;
        }

        @Override
        public Bundle getBundle(String s) {
            return null;
        }
    });
    final LinkedBlockingQueue<DiscoveryEvent> discoveryEvents = new LinkedBlockingQueue<DiscoveryEvent>(10);
    osgiFabricDiscoveryAgent.setDiscoveryListener(new DiscoveryListener() {

        @Override
        public void onServiceAdd(DiscoveryEvent discoveryEvent) {
            discoveryEvents.offer(discoveryEvent);
        }

        @Override
        public void onServiceRemove(DiscoveryEvent discoveryEvent) {
        }
    });
    // will call into dummy bundle and get curator
    osgiFabricDiscoveryAgent.addingService(null);
    osgiFabricDiscoveryAgent.setGroupName("amq");
    osgiFabricDiscoveryAgent.start();
    DiscoveryEvent event = discoveryEvents.poll(5, TimeUnit.SECONDS);
    LOG.info("event: " + event);
    assertNotNull("got added service", event);
    underTest.deleted("b");
    // swap curator ref
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + zkPort).sessionTimeoutMs(15000).retryPolicy(new RetryNTimes(5000, 1000));
    curator = builder.build();
    LOG.debug("Starting new curator " + curator);
    curator.start();
    curatorFrameworkAtomicReference.get().close();
    curatorFrameworkAtomicReference.set(curator);
    // will call into dummy bundle and get new curator ref
    osgiFabricDiscoveryAgent.addingService(null);
    // start broker again
    underTest.curator = curator;
    underTest.updated("b", props);
    event = discoveryEvents.poll(5, TimeUnit.SECONDS);
    LOG.info("new event: " + event);
    assertNotNull("got newly added service", event);
    underTest.deleted("b");
}
Also used : CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) DiscoveryEvent(org.apache.activemq.command.DiscoveryEvent) Properties(java.util.Properties) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryNTimes(org.apache.curator.retry.RetryNTimes) InputStream(java.io.InputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) OsgiFabricDiscoveryAgent(io.fabric8.mq.fabric.discovery.OsgiFabricDiscoveryAgent) Collection(java.util.Collection) File(java.io.File) DiscoveryListener(org.apache.activemq.transport.discovery.DiscoveryListener) Test(org.junit.Test)

Example 13 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class GatewayServiceTreeCache method treeCacheEvent.

protected void treeCacheEvent(PathChildrenCacheEvent event) {
    ChildData childData = event.getData();
    if (childData == null) {
        return;
    }
    String path = childData.getPath();
    LOG.trace("Event {} on path {}", event.getType().toString(), path);
    PathChildrenCacheEvent.Type type = event.getType();
    byte[] data = childData.getData();
    if (data == null || data.length == 0 || path == null) {
        return;
    }
    if (path.startsWith(zkPath)) {
        path = path.substring(zkPath.length());
    }
    List<String> split = Strings.splitAndTrimAsList(path, "/");
    // Lets just use the group name as the service path.
    path = split.get(0);
    String zNode = split.get(1);
    boolean remove = false;
    switch(type) {
        case CHILD_ADDED:
        case CHILD_UPDATED:
            break;
        case CHILD_REMOVED:
            remove = true;
            break;
        default:
            return;
    }
    ServiceDTO dto = null;
    try {
        dto = mapper.readValue(data, ServiceDTO.class);
        expandPropertyResolvers(dto);
        dto.setContainer(dto.getContainer() + "_zNode_" + zNode);
        if (remove) {
            LOG.info("Removed gateway service: " + path + ": " + dto);
            serviceMap.serviceRemoved(path, dto);
        } else {
            LOG.info("Updated gateway service: " + path + ": " + dto);
            serviceMap.serviceUpdated(path, dto);
        }
    } catch (IOException e) {
        LOG.warn("Failed to parse the JSON: " + new String(data) + ". Reason: " + e, e);
    } catch (URISyntaxException e) {
        LOG.warn("Failed to update URI for dto: " + dto + ", .Reason: " + e, e);
    }
}
Also used : PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceDTO(io.fabric8.gateway.ServiceDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 14 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class GatewayServiceTreeCache method treeCacheEvent.

protected void treeCacheEvent(PathChildrenCacheEvent event) {
    ChildData childData = event.getData();
    if (childData == null) {
        return;
    }
    String path = childData.getPath();
    PathChildrenCacheEvent.Type type = event.getType();
    byte[] data = childData.getData();
    if (data == null || data.length == 0 || path == null) {
        return;
    }
    if (path.startsWith(zkPath)) {
        path = path.substring(zkPath.length());
    }
    boolean remove = false;
    switch(type) {
        case CHILD_ADDED:
        case CHILD_UPDATED:
            break;
        case CHILD_REMOVED:
            remove = true;
            break;
        default:
            return;
    }
    ServiceDTO dto = null;
    try {
        dto = mapper.readValue(data, ServiceDTO.class);
        expandPropertyResolvers(dto);
        if (remove) {
            serviceMap.serviceRemoved(path, dto);
        } else {
            serviceMap.serviceUpdated(path, dto);
        }
    } catch (IOException e) {
        LOG.warn("Failed to parse the JSON: " + new String(data) + ". Reason: " + e, e);
    } catch (URISyntaxException e) {
        LOG.warn("Failed to update URI for dto: " + dto + ", .Reason: " + e, e);
    }
}
Also used : PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceDTO(io.fabric8.gateway.ServiceDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 15 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class GatewayServiceTreeCacheTest method treeCacheEvent.

@Test
public void treeCacheEvent() throws Exception {
    String zkPath = "/fabric/registry/clusters/test";
    ServiceMap serviceMap = new ServiceMap();
    CuratorFramework curator = mock(CuratorFramework.class);
    GatewayServiceTreeCache cache = new GatewayServiceTreeCache(curator, zkPath, serviceMap);
    String path = "/fabric/registry/clusters/test/default/0000001";
    // Add container1 - master
    // Add container2 - slave
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container1", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
    assertEquals(1, serviceMap.getServices("default").size());
    // Remove container1
    // Update container2 - master
    // Add container1 - slave
    cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container1", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_UPDATED, data("test", "container2", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container1")));
    assertEquals(1, serviceMap.getServices("default").size());
    // Remove container2
    // Update container1 - master
    // Add container2 - slave
    cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container2", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_UPDATED, data("test", "container1", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
    assertEquals(1, serviceMap.getServices("default").size());
    // Remove container2
    // Add container2 - slave
    cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
    assertEquals(1, serviceMap.getServices("default").size());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ServiceMap(io.fabric8.gateway.ServiceMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 IOException (java.io.IOException)11 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)8 ArrayList (java.util.ArrayList)6 ConnectionParameters (io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters)5 File (java.io.File)5 Map (java.util.Map)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 Logger (org.slf4j.Logger)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 LoggerFactory (org.slf4j.LoggerFactory)4 ServiceDTO (io.fabric8.gateway.ServiceDTO)3 FutureHandler (io.fabric8.gateway.handlers.detecting.FutureHandler)3 HttpGatewayHandler (io.fabric8.gateway.handlers.http.HttpGatewayHandler)3 LogEvent (io.fabric8.insight.log.LogEvent)3 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 ChildData (org.apache.curator.framework.recipes.cache.ChildData)3