Search in sources :

Example 16 with Namespace

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

the class KubernetesNamespacesProducerTest method getNamespace.

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

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
        }
    });
    Namespace ns = ex.getOut().getBody(Namespace.class);
    assertEquals(ns.getMetadata().getName(), "default");
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Namespace(io.fabric8.kubernetes.api.model.Namespace) Test(org.junit.Test)

Example 17 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace 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 18 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace 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 19 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace 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)

Example 20 with Namespace

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

the class KubernetesGatewayImpl method createDeploymentResource.

/**
 * Create a deployment in cms
 *
 * @param deploymentTemplate Deployment template as a String
 * @param deploymentName     Name of the deployment
 * @throws ContainerBasedGatewayException if failed to create a deployment
 */
private void createDeploymentResource(String deploymentTemplate, String deploymentName) throws ContainerBasedGatewayException {
    HasMetadata resource = getResourcesFromTemplate(deploymentTemplate);
    try {
        if (resource instanceof Deployment) {
            // check whether there are existing service already
            if (client.extensions().deployments().inNamespace(namespace).withName(deploymentName).get() == null) {
                log.debug("Deploying in CMS type: {} and the Deployment resource definition: {} ", cmsType, deploymentTemplate);
                Deployment deployment = (Deployment) resource;
                Deployment result = client.extensions().deployments().inNamespace(namespace).create(deployment);
                log.info("Created Deployment : " + result.getMetadata().getName() + " in Namespace : " + result.getMetadata().getNamespace() + " in " + cmsType);
            } else {
                log.info("There exist a deployment with the same name in " + cmsType + ". Deployment name : " + deploymentName);
            }
        } else {
            throw new ContainerBasedGatewayException("Loaded Resource is not a Deployment in " + cmsType + "! " + resource, ExceptionCodes.LOADED_RESOURCE_DEFINITION_IS_NOT_VALID);
        }
    } catch (KubernetesClientException e) {
        throw new ContainerBasedGatewayException("Error while creating container based gateway deployment in " + cmsType + "!", e, ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)12 Test (org.junit.Test)11 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)10 Namespace (io.fabric8.kubernetes.api.model.Namespace)8 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)7 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)7 ContainerBasedGatewayException (org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException)6 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)6 BaseOperation (io.fabric8.kubernetes.client.dsl.base.BaseOperation)5 ArrayList (java.util.ArrayList)5 API (org.wso2.carbon.apimgt.core.models.API)5 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)4 Map (java.util.Map)4 Exchange (org.apache.camel.Exchange)4 Processor (org.apache.camel.Processor)4 DoneableNamespace (io.fabric8.kubernetes.api.model.DoneableNamespace)3 DoneableReplicationController (io.fabric8.kubernetes.api.model.DoneableReplicationController)3 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)3 Ingress (io.fabric8.kubernetes.api.model.extensions.Ingress)3 ScalableResource (io.fabric8.kubernetes.client.dsl.ScalableResource)3