Search in sources :

Example 6 with PortName

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

the class OpenShiftConnector method getCheServicePorts.

private Map<String, List<PortBinding>> getCheServicePorts(Service service) {
    Map<String, List<PortBinding>> networkSettingsPorts = new HashMap<>();
    List<ServicePort> servicePorts = service.getSpec().getPorts();
    LOG.info("Retrieving {} ports exposed by service {}", servicePorts.size(), service.getMetadata().getName());
    for (ServicePort servicePort : servicePorts) {
        String protocol = servicePort.getProtocol();
        String targetPort = String.valueOf(servicePort.getTargetPort().getIntVal());
        String nodePort = String.valueOf(servicePort.getNodePort());
        String portName = servicePort.getName();
        LOG.info("Port: {}{}{} ({})", targetPort, DOCKER_PROTOCOL_PORT_DELIMITER, protocol, portName);
        networkSettingsPorts.put(targetPort + DOCKER_PROTOCOL_PORT_DELIMITER + protocol.toLowerCase(), Collections.singletonList(new PortBinding().withHostIp(CHE_DEFAULT_EXTERNAL_ADDRESS).withHostPort(nodePort)));
    }
    return networkSettingsPorts;
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) PortBinding(org.eclipse.che.plugin.docker.client.json.PortBinding) HashMap(java.util.HashMap) List(java.util.List) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) ArrayList(java.util.ArrayList) PodList(io.fabric8.kubernetes.api.model.PodList)

Example 7 with PortName

use of io.fabric8.annotations.PortName in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method addPort.

public static boolean addPort(List<ContainerPort> ports, String portNumberText, String portName, Logger log) {
    if (Strings.isNullOrBlank(portNumberText)) {
        return false;
    }
    int portValue;
    try {
        portValue = Integer.parseInt(portNumberText);
    } catch (NumberFormatException e) {
        log.warn("Could not parse remote debugging port %s as an integer: %s", portNumberText, e);
        return false;
    }
    for (ContainerPort port : ports) {
        String name = port.getName();
        Integer containerPort = port.getContainerPort();
        if (containerPort != null && containerPort.intValue() == portValue) {
            return false;
        }
    }
    ports.add(new ContainerPortBuilder().withName(portName).withContainerPort(portValue).build());
    return true;
}
Also used : ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort)

Example 8 with PortName

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

the class FactoryMethodProducer method produce.

@Override
public T produce(CreationalContext<T> ctx) {
    List<Object> arguments = new ArrayList<>();
    for (AnnotatedParameter<X> parameter : factoryMethod.getParameters()) {
        Type type = parameter.getBaseType();
        ServiceName parameterServiceName = parameter.getAnnotation(ServiceName.class);
        Protocol parameterProtocol = parameter.getAnnotation(Protocol.class);
        PortName parameterPortName = parameter.getAnnotation(PortName.class);
        Path parameterPath = parameter.getAnnotation(Path.class);
        Endpoint paramEndpoint = parameter.getAnnotation(Endpoint.class);
        External paramExternal = parameter.getAnnotation(External.class);
        Configuration configuration = parameter.getAnnotation(Configuration.class);
        // A point without @ServiceName is invalid.
        // Even if method defines @ServiceName, the annotation on the injection point takes precedence
        String serviceName = pointName;
        String serviceProtocol = or(pointProtocol, parameterProtocol != null ? parameterProtocol.value() : null);
        String servicePort = or(pointPort, parameterPortName != null ? parameterPortName.value() : null);
        String servicePath = or(pointPath, parameterPath != null ? parameterPath.value() : null);
        Boolean serviceEndpoint = paramEndpoint != null ? paramEndpoint.value() : false;
        Boolean serviceExternal = paramExternal != null ? paramExternal.value() : false;
        // If the @ServiceName exists on the current String property
        if (parameterServiceName != null && String.class.equals(type)) {
            try {
                String serviceUrl = getServiceUrl(serviceName, serviceProtocol, servicePort, servicePath, serviceEndpoint, serviceExternal, ctx);
                arguments.add(serviceUrl);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current List property
        if (parameterServiceName != null && List.class.equals(Types.asClass(type))) {
            try {
                List<String> endpointList = getEndpointList(serviceName, serviceProtocol, servicePort, servicePath, serviceExternal, ctx);
                arguments.add(endpointList);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current List property
        if (parameterServiceName != null && Set.class.equals(Types.asClass(type))) {
            try {
                List<String> endpointList = getEndpointList(serviceName, serviceProtocol, servicePort, servicePath, serviceExternal, ctx);
                arguments.add(new HashSet<>(endpointList));
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current property which is a non-String
        if (parameterServiceName != null && !String.class.equals(type)) {
            try {
                Object serviceBean = getServiceBean(serviceName, serviceProtocol, servicePort, servicePath, serviceEndpoint, serviceExternal, type, ctx);
                arguments.add(serviceBean);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(BEAN_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), type, serviceName), t);
            }
        } else // If the current parameter is annotated with @Configuration
        if (configuration != null) {
            try {
                Object config = getConfiguration(serviceName, (Class<Object>) type, ctx);
                arguments.add(config);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(CONF_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else {
            try {
                Object other = BeanProvider.getContextualReference(Types.asClass(type), true);
                arguments.add(other);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(PARAMETER_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), parameter.getPosition()), t);
            }
        }
    }
    try {
        return (T) factoryMethod.getJavaMember().invoke(bean.create(ctx), arguments.toArray());
    } catch (Throwable t) {
        throw new RuntimeException(String.format(INVOCATION_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), arguments), t);
    }
}
Also used : Path(io.fabric8.annotations.Path) HashSet(java.util.HashSet) Set(java.util.Set) Configuration(io.fabric8.annotations.Configuration) ArrayList(java.util.ArrayList) PortName(io.fabric8.annotations.PortName) Type(java.lang.reflect.Type) Endpoint(io.fabric8.annotations.Endpoint) ServiceName(io.fabric8.annotations.ServiceName) External(io.fabric8.annotations.External) ArrayList(java.util.ArrayList) List(java.util.List) Protocol(io.fabric8.annotations.Protocol)

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