Search in sources :

Example 11 with Service

use of io.fabric8.kubernetes.api.model.Service in project camel by apache.

the class KubernetesServicesProducerTest method listByLabelsTest.

@Test
public void listByLabelsTest() throws Exception {
    if (ObjectHelper.isEmpty(authToken)) {
        return;
    }
    Exchange ex = template.request("direct:listByLabels", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("component", "elasticsearch");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_LABELS, labels);
        }
    });
    List<Service> result = ex.getOut().getBody(List.class);
    boolean serviceExists = false;
    Iterator<Service> it = result.iterator();
    while (it.hasNext()) {
        Service service = it.next();
        if ("elasticsearch".equalsIgnoreCase(service.getMetadata().getName())) {
            serviceExists = true;
        }
    }
    assertFalse(serviceExists);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Service(io.fabric8.kubernetes.api.model.Service) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 12 with Service

use of io.fabric8.kubernetes.api.model.Service in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method addServicesToEndpointList.

/**
 * For each service in {@code serviceList} list, methods are called to add endpoints of different types,
 * for each of service's ports
 *
 * @param serviceList filtered list of services
 */
private void addServicesToEndpointList(List<Service> serviceList, List<Endpoint> endpointList) throws MalformedURLException {
    for (Service service : serviceList) {
        // Set the parameters that does not change with the service port
        String serviceName = service.getMetadata().getName();
        String namespace = service.getMetadata().getNamespace();
        Map<String, String> labelsMap = service.getMetadata().getLabels();
        String labels = (labelsMap != null) ? labelsMap.toString() : "";
        ServiceSpec serviceSpec = service.getSpec();
        String serviceType = serviceSpec.getType();
        if (includeExternalNameTypeServices && EXTERNAL_NAME.equals(serviceType)) {
            // Since only a "ExternalName" type service can have an "externalName" (the alias in kube-dns)
            addExternalNameEndpoint(serviceName, serviceSpec.getExternalName(), namespace, labels, endpointList);
        }
        for (ServicePort servicePort : serviceSpec.getPorts()) {
            String protocol = servicePort.getName();
            if (APIMgtConstants.HTTP.equals(protocol) || APIMgtConstants.HTTPS.equals(protocol)) {
                int port = servicePort.getPort();
                if (includeClusterIP && !EXTERNAL_NAME.equals(serviceType)) {
                    // Since almost every service has a cluster IP, except for ExternalName type
                    addClusterIPEndpoint(serviceName, serviceSpec.getClusterIP(), port, protocol, namespace, labels, endpointList);
                }
                if (NODE_PORT.equals(serviceType) || LOAD_BALANCER.equals(serviceType)) {
                    // Because both "NodePort" and "LoadBalancer" types of services have "NodePort" type URLs
                    addNodePortEndpoint(serviceName, servicePort.getNodePort(), protocol, namespace, labels, endpointList);
                }
                if (LOAD_BALANCER.equals(serviceType)) {
                    // Since only "LoadBalancer" type services have "LoadBalancer" type URLs
                    addLoadBalancerEndpoint(serviceName, service, port, protocol, namespace, labels, endpointList);
                }
                // A Special case (can be any of the service types above)
                addExternalIPEndpoint(serviceName, serviceSpec.getExternalIPs(), port, protocol, namespace, labels, endpointList);
            } else if (log.isDebugEnabled()) {
                log.debug("Service:{} Namespace:{} Port:{}/{}  Application level protocol not defined.", serviceName, namespace, servicePort.getPort(), protocol);
            }
        }
    }
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) Service(io.fabric8.kubernetes.api.model.Service) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint)

Example 13 with Service

use of io.fabric8.kubernetes.api.model.Service in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method listServices.

/**
 * {@inheritDoc}
 */
@Override
public List<Endpoint> listServices(String namespace, Map<String, String> criteria) throws ServiceDiscoveryException {
    List<Endpoint> endpointList = new ArrayList<>();
    if (client != null) {
        log.debug("Looking for services, with the specified labels, in namespace {}", namespace);
        try {
            List<Service> serviceList = client.services().inNamespace(namespace).withLabels(criteria).list().getItems();
            addServicesToEndpointList(serviceList, endpointList);
        } catch (KubernetesClientException | MalformedURLException e) {
            String msg = "Error occurred while trying to list services using Kubernetes client";
            throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_WHILE_TRYING_TO_DISCOVER_SERVICES);
        } catch (NoSuchMethodError e) {
            String msg = "Filtering criteria in the deployment yaml includes unwanted characters";
            throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_WHILE_TRYING_TO_DISCOVER_SERVICES);
        }
    }
    return endpointList;
}
Also used : MalformedURLException(java.net.MalformedURLException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) ArrayList(java.util.ArrayList) Service(io.fabric8.kubernetes.api.model.Service) ServiceDiscoveryException(org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 14 with Service

use of io.fabric8.kubernetes.api.model.Service in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method listServices.

/**
 * {@inheritDoc}
 */
@Override
public List<Endpoint> listServices() throws ServiceDiscoveryException {
    List<Endpoint> endpointList = new ArrayList<>();
    if (client != null) {
        log.debug("Looking for services in all namespaces");
        try {
            List<Service> serviceList = client.services().inNamespace(null).list().getItems();
            addServicesToEndpointList(serviceList, endpointList);
        } catch (KubernetesClientException | MalformedURLException e) {
            String msg = "Error occurred while trying to list services using Kubernetes client";
            throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_WHILE_TRYING_TO_DISCOVER_SERVICES);
        }
    }
    return endpointList;
}
Also used : MalformedURLException(java.net.MalformedURLException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) ArrayList(java.util.ArrayList) Service(io.fabric8.kubernetes.api.model.Service) ServiceDiscoveryException(org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 15 with Service

use of io.fabric8.kubernetes.api.model.Service in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method listServices.

/**
 * {@inheritDoc}
 */
@Override
public List<Endpoint> listServices(String namespace) throws ServiceDiscoveryException {
    List<Endpoint> endpointList = new ArrayList<>();
    if (client != null) {
        log.debug("Looking for services in namespace {}", namespace);
        try {
            List<Service> serviceList = client.services().inNamespace(namespace).list().getItems();
            addServicesToEndpointList(serviceList, endpointList);
        } catch (KubernetesClientException | MalformedURLException e) {
            String msg = "Error occurred while trying to list services using Kubernetes client";
            throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_WHILE_TRYING_TO_DISCOVER_SERVICES);
        }
    }
    return endpointList;
}
Also used : MalformedURLException(java.net.MalformedURLException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) ArrayList(java.util.ArrayList) Service(io.fabric8.kubernetes.api.model.Service) ServiceDiscoveryException(org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)29 ArrayList (java.util.ArrayList)15 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)12 Test (org.junit.Test)12 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)9 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)8 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)8 ContainerBasedGatewayException (org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException)8 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)7 HashMap (java.util.HashMap)7 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)7 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)6 ScalableResource (io.fabric8.kubernetes.client.dsl.ScalableResource)6 Map (java.util.Map)6 KubernetesService (org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService)6 API (org.wso2.carbon.apimgt.core.models.API)6 Pod (io.fabric8.kubernetes.api.model.Pod)5 List (java.util.List)5 OpenShiftException (org.eclipse.che.plugin.openshift.client.exception.OpenShiftException)5 ServiceDiscoveryException (org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException)5