Search in sources :

Example 1 with PortName

use of io.fabric8.annotations.PortName in project camel by apache.

the class KubernetesClientServiceDiscovery method getServices.

@Override
public List<ServiceDefinition> getServices(String name) {
    LOG.debug("Discovering endpoints from namespace: {} with name: {}", getNamespace(), name);
    Endpoints endpoints = client.endpoints().inNamespace(getNamespace()).withName(name).get();
    List<ServiceDefinition> result = new ArrayList<>();
    if (endpoints != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Found {} endpoints in namespace: {} for name: {} and portName: {}", endpoints.getSubsets().size(), getNamespace(), name, getPortName());
        }
        for (EndpointSubset subset : endpoints.getSubsets()) {
            if (subset.getPorts().size() == 1) {
                addServers(name, result, subset.getPorts().get(FIRST), subset);
            } else {
                final List<EndpointPort> ports = subset.getPorts();
                final int portSize = ports.size();
                EndpointPort port;
                for (int p = 0; p < portSize; p++) {
                    port = ports.get(p);
                    if (ObjectHelper.isEmpty(getPortName()) || getPortName().endsWith(port.getName())) {
                        addServers(name, result, port, subset);
                    }
                }
            }
        }
    }
    return result;
}
Also used : Endpoints(io.fabric8.kubernetes.api.model.Endpoints) EndpointSubset(io.fabric8.kubernetes.api.model.EndpointSubset) EndpointPort(io.fabric8.kubernetes.api.model.EndpointPort) ArrayList(java.util.ArrayList) DefaultServiceDefinition(org.apache.camel.impl.cloud.DefaultServiceDefinition) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition)

Example 2 with PortName

use of io.fabric8.annotations.PortName in project fabric8 by fabric8io.

the class AbstractServiceRegistar method registerBeanDefinitions.

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    for (Method method : REFLECTIONS.getMethodsAnnotatedWith(Factory.class)) {
        String methodName = method.getName();
        Class sourceType = getSourceType(method);
        Class targetType = method.getReturnType();
        Class beanType = method.getDeclaringClass();
        BeanDefinitionHolder holder = createConverterBean(beanType, methodName, sourceType, targetType);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
    }
    for (Field field : REFLECTIONS.getFieldsAnnotatedWith(ServiceName.class)) {
        Class targetClass = field.getType();
        Alias alias = field.getAnnotation(Alias.class);
        ServiceName name = field.getAnnotation(ServiceName.class);
        PortName port = field.getAnnotation(PortName.class);
        Protocol protocol = field.getAnnotation(Protocol.class);
        External external = field.getAnnotation(External.class);
        String serviceName = name != null ? name.value() : null;
        // We copy the service since we are going to add properties to it.
        Service serviceInstance = new ServiceBuilder(getService(serviceName)).build();
        String servicePort = port != null ? port.value() : null;
        String serviceProtocol = protocol != null ? protocol.value() : DEFAULT_PROTOCOL;
        Boolean serviceExternal = external != null && external.value();
        String serviceAlias = alias != null ? alias.value() : createAlias(serviceName, targetClass, serviceProtocol, servicePort, serviceExternal);
        // Add annotation info as additional properties
        serviceInstance.getAdditionalProperties().put(ALIAS, serviceAlias);
        serviceInstance.getAdditionalProperties().put(PROTOCOL, serviceProtocol);
        serviceInstance.getAdditionalProperties().put(EXTERNAL, serviceExternal);
        // We don't want to add a fallback value to the attributes.
        if (port != null) {
            serviceInstance.getAdditionalProperties().put(PORT, servicePort);
        }
        BeanDefinitionHolder holder = createServiceDefinition(serviceInstance, serviceAlias, serviceProtocol, servicePort, targetClass);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
    }
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) Method(java.lang.reflect.Method) PortName(io.fabric8.annotations.PortName) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Field(java.lang.reflect.Field) ServiceName(io.fabric8.annotations.ServiceName) Alias(io.fabric8.annotations.Alias) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) External(io.fabric8.annotations.External) Protocol(io.fabric8.annotations.Protocol)

Example 3 with PortName

use of io.fabric8.annotations.PortName in project fabric8 by fabric8io.

the class Fabric8Extension method onInjectionPoint.

public <T, X> void onInjectionPoint(@Observes ProcessInjectionPoint<T, X> event, BeanManager beanManager) {
    final InjectionPoint injectionPoint = event.getInjectionPoint();
    if (isServiceInjectionPoint(injectionPoint)) {
        Annotated annotated = injectionPoint.getAnnotated();
        ServiceName name = annotated.getAnnotation(ServiceName.class);
        Protocol protocol = annotated.getAnnotation(Protocol.class);
        PortName port = annotated.getAnnotation(PortName.class);
        Path path = annotated.getAnnotation(Path.class);
        Alias alias = annotated.getAnnotation(Alias.class);
        Endpoint endpoint = annotated.getAnnotation(Endpoint.class);
        External external = annotated.getAnnotation(External.class);
        String serviceName = name.value();
        String serviceProtocol = protocol != null ? protocol.value() : null;
        String servicePort = port != null ? port.value() : null;
        String servicePath = path != null ? path.value() : null;
        String serviceAlias = alias != null ? alias.value() : null;
        Boolean serviceExternal = external != null ? external.value() : false;
        Boolean serviceEndpoint = endpoint != null ? endpoint.value() : false;
        Type type = annotated.getBaseType();
        if (type instanceof ParameterizedType && Instance.class.equals(((ParameterizedType) type).getRawType())) {
            type = ((ParameterizedType) type).getActualTypeArguments()[0];
        }
        if (type.equals(String.class)) {
            ServiceUrlBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal);
        } else if (isGenericOf(type, List.class, String.class)) {
            ServiceUrlCollectionBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal, Types.LIST_OF_STRINGS);
        } else if (isGenericOf(type, List.class, null)) {
        // TODO: Integrate with Factories(?)
        } else if (isGenericOf(type, Set.class, String.class)) {
            ServiceUrlCollectionBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal, Types.SET_OF_STRINGS);
        } else if (isGenericOf(type, Set.class, null)) {
        // TODO: Integrate with Factories(?)
        } else if (type instanceof Class) {
            ServiceBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal, type);
        } else {
            throw new RuntimeException(String.format(INJECTION_POINT_UNKNOWN_TYPE, injectionPoint.getBean().getBeanClass(), type));
        }
        if (protocol == null) {
            setDefaultProtocol(event);
        }
        if (port == null) {
            setDefaultPort(event);
        }
        if (path == null) {
            setDefaultPath(event);
        }
        if (endpoint == null) {
            setDefaultEndpoint(event);
        }
        if (external == null) {
            setDefaultExternal(event);
        }
    } else if (isConfigurationInjectionPoint(injectionPoint)) {
        Annotated annotated = injectionPoint.getAnnotated();
        Configuration configuration = annotated.getAnnotation(Configuration.class);
        Type type = injectionPoint.getType();
        String configurationId = configuration.value();
        ConfigurationBean.getBean(configurationId, type);
    }
}
Also used : Path(io.fabric8.annotations.Path) Utils.getFactoryMethodPath(io.fabric8.cdi.Utils.getFactoryMethodPath) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Configuration(io.fabric8.annotations.Configuration) ProcessInjectionPoint(javax.enterprise.inject.spi.ProcessInjectionPoint) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) Instance(javax.enterprise.inject.Instance) PortName(io.fabric8.annotations.PortName) ParameterizedType(java.lang.reflect.ParameterizedType) Annotated(javax.enterprise.inject.spi.Annotated) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Endpoint(io.fabric8.annotations.Endpoint) ServiceName(io.fabric8.annotations.ServiceName) Alias(io.fabric8.annotations.Alias) External(io.fabric8.annotations.External) ArrayList(java.util.ArrayList) List(java.util.List) Utils.getFactoryMethodProtocol(io.fabric8.cdi.Utils.getFactoryMethodProtocol) Protocol(io.fabric8.annotations.Protocol)

Example 4 with PortName

use of io.fabric8.annotations.PortName in project che by eclipse.

the class KubernetesContainer method getContainerPortsFrom.

/**
     * Retrieves list of ({@link ContainerPort} based on ports defined in
     * {@link ContainerConfig} and {@link ImageConfig}
     *
     * @param exposedPorts
     * @return list of {@link ContainerPort}
     */
public static List<ContainerPort> getContainerPortsFrom(Set<String> exposedPorts) {
    List<ContainerPort> containerPorts = new ArrayList<>(exposedPorts.size());
    for (String exposedPort : exposedPorts) {
        String[] portAndProtocol = exposedPort.split("/", 2);
        String port = portAndProtocol[0];
        String protocol = portAndProtocol[1].toUpperCase();
        int portNumber = Integer.parseInt(port);
        String portName = CheServicePorts.get().get(portNumber);
        portName = isNullOrEmpty(portName) ? exposedPort.replace("/", "-") : portName;
        ContainerPort containerPort = new ContainerPortBuilder().withName(portName).withProtocol(protocol).withContainerPort(portNumber).build();
        containerPorts.add(containerPort);
    }
    return containerPorts;
}
Also used : ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) ArrayList(java.util.ArrayList) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort)

Example 5 with PortName

use of io.fabric8.annotations.PortName in project che by eclipse.

the class KubernetesService method getServicePortsFrom.

/**
     * Retrieves list of {@link ServicePort} based on ports defined in
     * {@link ContainerConfig} and {@link ImageConfig}
     *
     * @param exposedPorts
     * @return list of {@link ServicePort}
     */
public static List<ServicePort> getServicePortsFrom(Set<String> exposedPorts) {
    List<ServicePort> servicePorts = new ArrayList<>(exposedPorts.size());
    for (String exposedPort : exposedPorts) {
        String[] portAndProtocol = exposedPort.split("/", 2);
        String port = portAndProtocol[0];
        String protocol = portAndProtocol[1];
        int portNumber = Integer.parseInt(port);
        String portName = CheServicePorts.get().get(portNumber);
        portName = isNullOrEmpty(portName) ? exposedPort.replace("/", "-") : portName;
        int targetPortNumber = portNumber;
        ServicePort servicePort = new ServicePort();
        servicePort.setName(portName);
        servicePort.setProtocol(protocol.toUpperCase());
        servicePort.setPort(portNumber);
        servicePort.setTargetPort(new IntOrString(targetPortNumber));
        servicePorts.add(servicePort);
    }
    return servicePorts;
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ArrayList(java.util.ArrayList) IntOrString(io.fabric8.kubernetes.api.model.IntOrString)

Aggregations

ArrayList (java.util.ArrayList)6 External (io.fabric8.annotations.External)3 PortName (io.fabric8.annotations.PortName)3 Protocol (io.fabric8.annotations.Protocol)3 ServiceName (io.fabric8.annotations.ServiceName)3 List (java.util.List)3 Alias (io.fabric8.annotations.Alias)2 Configuration (io.fabric8.annotations.Configuration)2 Endpoint (io.fabric8.annotations.Endpoint)2 Path (io.fabric8.annotations.Path)2 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)2 ContainerPortBuilder (io.fabric8.kubernetes.api.model.ContainerPortBuilder)2 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)2 Type (java.lang.reflect.Type)2 Set (java.util.Set)2 Utils.getFactoryMethodPath (io.fabric8.cdi.Utils.getFactoryMethodPath)1 Utils.getFactoryMethodProtocol (io.fabric8.cdi.Utils.getFactoryMethodProtocol)1 EndpointPort (io.fabric8.kubernetes.api.model.EndpointPort)1 EndpointSubset (io.fabric8.kubernetes.api.model.EndpointSubset)1 Endpoints (io.fabric8.kubernetes.api.model.Endpoints)1