Search in sources :

Example 16 with Event

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

the class HttpMappingZooKeeperTreeCache method treeCacheEvent.

protected void treeCacheEvent(PathChildrenCacheEvent event) {
    String zkPath = zooKeeperPath;
    ChildData childData = event.getData();
    if (childData == null) {
        return;
    }
    String path = childData.getPath();
    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());
    }
    // TODO should we remove the version too and pick that one?
    // and include the version in the service chooser?
    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);
        List<String> services = dto.getServices();
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", paramValue(dto.getId()));
        params.put("container", paramValue(dto.getContainer()));
        params.put("version", paramValue(dto.getVersion()));
        params.put("bundleName", paramValue(dto.getBundleName()));
        params.put("bundleVersion", paramValue(dto.getBundleVersion()));
        mappingRuleConfiguration.updateMappingRules(remove, path, services, params, 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 : Type(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceDTO(io.fabric8.gateway.ServiceDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 17 with Event

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

the class FabricDiscoveryAgent method initGroup.

private synchronized void initGroup() throws Exception {
    group = (MultiGroup) factory.createMultiGroup("/fabric/registry/clusters/amq/" + groupName, ActiveMQNode.class, new NamedThreadFactory("zkgroup-fabric-mq-discovery"));
    group.add(new GroupListener<ActiveMQNode>() {

        @Override
        public void groupEvent(Group<ActiveMQNode> group, GroupEvent event) {
            LOG.debug("Event: " + event);
            Map<String, ActiveMQNode> masters = new HashMap<String, ActiveMQNode>();
            for (ActiveMQNode node : group.members().values()) {
                if (!masters.containsKey(node.id)) {
                    masters.put(node.id, node);
                }
            }
            update(masters.values());
        }
    });
}
Also used : NamedThreadFactory(io.fabric8.utils.NamedThreadFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with Event

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

the class KarafContainerRegistration method configurationEvent.

/**
 * Receives notification of a Configuration that has changed.
 *
 * @param event The <code>ConfigurationEvent</code>.
 */
@Override
public void configurationEvent(ConfigurationEvent event) {
    if (isValid()) {
        try {
            Container current = new ImmutableContainerBuilder().id(runtimeIdentity).ip(ip).build();
            RuntimeProperties sysprops = runtimeProperties.get();
            String runtimeIdentity = sysprops.getRuntimeIdentity();
            if (event.getPid().equals(SSH_PID) && event.getType() == ConfigurationEvent.CM_UPDATED) {
                Configuration config = configAdmin.get().getConfiguration(SSH_PID, null);
                int sshPort = Integer.parseInt((String) config.getProperties().get(SSH_BINDING_PORT_KEY));
                int sshConnectionPort = getSshConnectionPort(current, sshPort);
                String sshUrl = getSshUrl(runtimeIdentity, sshConnectionPort);
                setData(curator.get(), CONTAINER_SSH.getPath(runtimeIdentity), sshUrl);
                if (portService.get().lookupPort(current, SSH_PID, SSH_BINDING_PORT_KEY) != sshPort) {
                    portService.get().unregisterPort(current, SSH_PID);
                    portService.get().registerPort(current, SSH_PID, SSH_BINDING_PORT_KEY, sshPort);
                }
            }
            if (event.getPid().equals(HTTP_PID) && event.getType() == ConfigurationEvent.CM_UPDATED) {
                Configuration config = configAdmin.get().getConfiguration(HTTP_PID, null);
                boolean httpEnabled = isHttpEnabled();
                boolean httpsEnabled = isHttpsEnabled();
                String protocol = httpsEnabled && !httpEnabled ? "https" : "http";
                int httpConnectionPort = -1;
                if (httpEnabled) {
                    int httpPort = Integer.parseInt((String) config.getProperties().get(HTTP_BINDING_PORT_KEY));
                    httpConnectionPort = getHttpConnectionPort(current, httpPort);
                    if (portService.get().lookupPort(current, HTTP_PID, HTTP_BINDING_PORT_KEY) != httpPort) {
                        portService.get().unregisterPort(current, HTTP_PID, HTTP_BINDING_PORT_KEY);
                        portService.get().registerPort(current, HTTP_PID, HTTP_BINDING_PORT_KEY, httpPort);
                    }
                }
                if (httpsEnabled) {
                    int httpsPort = Integer.parseInt((String) config.getProperties().get(HTTPS_BINDING_PORT_KEY));
                    if (httpConnectionPort == -1) {
                        httpConnectionPort = getHttpsConnectionPort(current, httpsPort);
                    }
                    if (portService.get().lookupPort(current, HTTP_PID, HTTPS_BINDING_PORT_KEY) != httpsPort) {
                        portService.get().unregisterPort(current, HTTP_PID, HTTPS_BINDING_PORT_KEY);
                        portService.get().registerPort(current, HTTP_PID, HTTPS_BINDING_PORT_KEY, httpsPort);
                    }
                }
                String httpUrl = getHttpUrl(protocol, runtimeIdentity, httpConnectionPort);
                setData(curator.get(), CONTAINER_HTTP.getPath(runtimeIdentity), httpUrl);
            }
            if (event.getPid().equals(MANAGEMENT_PID) && event.getType() == ConfigurationEvent.CM_UPDATED) {
                Configuration config = configAdmin.get().getConfiguration(MANAGEMENT_PID, null);
                int rmiServerPort = Integer.parseInt((String) config.getProperties().get(RMI_SERVER_BINDING_PORT_KEY));
                int rmiServerConnectionPort = getRmiServerConnectionPort(current, rmiServerPort);
                int rmiRegistryPort = Integer.parseInt((String) config.getProperties().get(RMI_REGISTRY_BINDING_PORT_KEY));
                int rmiRegistryConnectionPort = getRmiRegistryConnectionPort(current, rmiRegistryPort);
                String jmxUrl = getJmxUrl(runtimeIdentity, rmiServerConnectionPort, rmiRegistryConnectionPort);
                setData(curator.get(), CONTAINER_JMX.getPath(runtimeIdentity), jmxUrl);
                // Whenever the JMX URL changes we need to make sure that the java.rmi.server.hostname points to a valid address.
                System.setProperty(SystemProperties.JAVA_RMI_SERVER_HOSTNAME, current.getIp());
                if (portService.get().lookupPort(current, MANAGEMENT_PID, RMI_REGISTRY_BINDING_PORT_KEY) != rmiRegistryPort || portService.get().lookupPort(current, MANAGEMENT_PID, RMI_SERVER_BINDING_PORT_KEY) != rmiServerPort) {
                    portService.get().unregisterPort(current, MANAGEMENT_PID);
                    portService.get().registerPort(current, MANAGEMENT_PID, RMI_SERVER_BINDING_PORT_KEY, rmiServerPort);
                    portService.get().registerPort(current, MANAGEMENT_PID, RMI_REGISTRY_BINDING_PORT_KEY, rmiRegistryPort);
                }
            }
        } catch (Exception ex) {
            LOGGER.error("Cannot reconfigure container", ex);
        }
    }
}
Also used : Container(io.fabric8.api.Container) BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) Configuration(org.osgi.service.cm.Configuration) ImmutableContainerBuilder(io.fabric8.internal.ImmutableContainerBuilder) RuntimeProperties(io.fabric8.api.RuntimeProperties) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 19 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by fabric8io.

the class Fabric8Extension method afterDiscovery.

public void afterDiscovery(@Observes final AfterBeanDiscovery event, BeanManager beanManager) {
    KubernetesHolder.useBeanManager(beanManager);
    // Only add the bean if no other bean is found.
    if (beanManager.getBeans(KubernetesClient.class).isEmpty()) {
        event.addBean(new KubernetesClientBean());
    }
    // We need to process factories in reverse order so that we make feasible forwarding for service id etc.
    List<FactoryMethodContext> reverseFactories = new ArrayList<>(FactoryMethodContext.sort(factories));
    Collections.reverse(reverseFactories);
    for (final FactoryMethodContext factoryMethodContext : reverseFactories) {
        ServiceBean.doWith(factoryMethodContext.getReturnType(), new ServiceBean.Callback() {

            @Override
            public ServiceBean apply(ServiceBean bean) {
                String serviceId = bean.getServiceName();
                String serviceProtocol = or(bean.getServiceProtocol(), getFactoryMethodProtocol(factoryMethodContext.getFactoryMethod().getJavaMember()));
                String servicePort = or(bean.getServicePort(), getFactoryMethodPort(factoryMethodContext.getFactoryMethod().getJavaMember()));
                String servicePath = or(bean.getServicePath(), getFactoryMethodPath(factoryMethodContext.getFactoryMethod().getJavaMember()));
                Boolean serviceExternal = bean.getServiceExternal();
                Boolean serviceEndpoint = bean.getServiceEndpoint();
                // Ensure that there is a factory String -> sourceType before adding producer.
                if (!String.class.equals(factoryMethodContext.getSourceType())) {
                    ServiceBean.getBean(serviceId, serviceProtocol, servicePort, servicePath, null, serviceEndpoint, serviceExternal, factoryMethodContext.getSourceType());
                }
                return bean.withProducer(new FactoryMethodProducer(factoryMethodContext.getBean(), factoryMethodContext.getFactoryMethod(), serviceId, serviceProtocol, servicePort, servicePath));
            }
        });
    }
    for (ServiceUrlBean bean : ServiceUrlBean.getBeans()) {
        event.addBean(bean);
    }
    for (ServiceUrlCollectionBean bean : ServiceUrlCollectionBean.getBeans()) {
        event.addBean(bean);
    }
    for (ServiceBean bean : ServiceBean.getBeans()) {
        if (bean.getProducer() != null) {
            event.addBean(bean);
        }
    }
    for (ConfigurationBean b : ConfigurationBean.getBeans()) {
        event.addBean(b);
    }
}
Also used : FactoryMethodProducer(io.fabric8.cdi.producers.FactoryMethodProducer) ConfigurationBean(io.fabric8.cdi.bean.ConfigurationBean) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ArrayList(java.util.ArrayList) ServiceUrlCollectionBean(io.fabric8.cdi.bean.ServiceUrlCollectionBean) KubernetesClientBean(io.fabric8.cdi.bean.KubernetesClientBean) ServiceUrlBean(io.fabric8.cdi.bean.ServiceUrlBean) ServiceBean(io.fabric8.cdi.bean.ServiceBean)

Example 20 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by fabric8io.

the class Fabric8Extension method onManagedBean.

public <X> void onManagedBean(@Observes final ProcessManagedBean<X> event) {
    for (final AnnotatedMethod<?> method : event.getAnnotatedBeanClass().getMethods()) {
        final Factory factory = method.getAnnotation(Factory.class);
        if (factory != null) {
            final Type sourceType = getSourceType(method);
            final Type returnType = method.getJavaMember().getReturnType();
            factories.add(new FactoryMethodContext(event.getBean(), sourceType, returnType, method));
        }
    }
}
Also used : AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Factory(io.fabric8.annotations.Factory)

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