Search in sources :

Example 1 with ServiceSpec

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

the class KubernetesServicesProducerTest method createAndDeleteService.

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

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("this", "rocks");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_LABELS, labels);
            ServiceSpec serviceSpec = new ServiceSpec();
            List<ServicePort> lsp = new ArrayList<ServicePort>();
            ServicePort sp = new ServicePort();
            sp.setPort(8080);
            sp.setTargetPort(new IntOrString(8080));
            sp.setProtocol("TCP");
            lsp.add(sp);
            serviceSpec.setPorts(lsp);
            Map<String, String> selectorMap = new HashMap<String, String>();
            selectorMap.put("containter", "test");
            serviceSpec.setSelector(selectorMap);
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_SPEC, serviceSpec);
        }
    });
    Service serv = ex.getOut().getBody(Service.class);
    assertEquals(serv.getMetadata().getName(), "test");
    ex = template.request("direct:deleteService", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
        }
    });
    boolean servDeleted = ex.getOut().getBody(Boolean.class);
    assertTrue(servDeleted);
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) Processor(org.apache.camel.Processor) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) Service(io.fabric8.kubernetes.api.model.Service) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) Exchange(org.apache.camel.Exchange) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with ServiceSpec

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

the class KubernetesServicesProducer method doCreateService.

protected void doCreateService(Exchange exchange, String operation) throws Exception {
    Service service = null;
    String serviceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, String.class);
    String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
    ServiceSpec serviceSpec = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_SPEC, ServiceSpec.class);
    if (ObjectHelper.isEmpty(serviceName)) {
        LOG.error("Create a specific service require specify a service name");
        throw new IllegalArgumentException("Create a specific service require specify a service name");
    }
    if (ObjectHelper.isEmpty(namespaceName)) {
        LOG.error("Create a specific service require specify a namespace name");
        throw new IllegalArgumentException("Create a specific service require specify a namespace name");
    }
    if (ObjectHelper.isEmpty(serviceSpec)) {
        LOG.error("Create a specific service require specify a service spec bean");
        throw new IllegalArgumentException("Create a specific service require specify a service spec bean");
    }
    Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_LABELS, Map.class);
    Service serviceCreating = new ServiceBuilder().withNewMetadata().withName(serviceName).withLabels(labels).endMetadata().withSpec(serviceSpec).build();
    service = getEndpoint().getKubernetesClient().services().inNamespace(namespaceName).create(serviceCreating);
    exchange.getOut().setBody(service);
}
Also used : ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Example 3 with ServiceSpec

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

the class KubernetesServicesConsumerTest method createAndDeleteService.

@Test
public void createAndDeleteService() throws Exception {
    if (ObjectHelper.isEmpty(authToken)) {
        return;
    }
    mockResultEndpoint.expectedMessageCount(2);
    mockResultEndpoint.expectedHeaderValuesReceivedInAnyOrder(KubernetesConstants.KUBERNETES_EVENT_ACTION, "ADDED", "DELETED");
    Exchange ex = template.request("direct:createService", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("this", "rocks");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_LABELS, labels);
            ServiceSpec serviceSpec = new ServiceSpec();
            List<ServicePort> lsp = new ArrayList<ServicePort>();
            ServicePort sp = new ServicePort();
            sp.setPort(8080);
            sp.setTargetPort(new IntOrString(8080));
            sp.setProtocol("TCP");
            lsp.add(sp);
            serviceSpec.setPorts(lsp);
            Map<String, String> selectorMap = new HashMap<String, String>();
            selectorMap.put("containter", "test");
            serviceSpec.setSelector(selectorMap);
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_SPEC, serviceSpec);
        }
    });
    Service serv = ex.getOut().getBody(Service.class);
    assertEquals(serv.getMetadata().getName(), "test");
    ex = template.request("direct:deleteService", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
        }
    });
    boolean servDeleted = ex.getOut().getBody(Boolean.class);
    assertTrue(servDeleted);
    Thread.sleep(3000);
    mockResultEndpoint.assertIsSatisfied();
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) Processor(org.apache.camel.Processor) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) Service(io.fabric8.kubernetes.api.model.Service) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) Exchange(org.apache.camel.Exchange) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with ServiceSpec

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

use of io.fabric8.kubernetes.api.model.ServiceSpec in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method shouldCreateExternalURLForService.

/**
 * Should we try to create an external URL for the given service?
 * <p/>
 * By default lets ignore the kubernetes services and any service which does not expose ports 80 and 443
 *
 * @return true if we should create an OpenShift Route for this service.
 */
private boolean shouldCreateExternalURLForService(Service service, String id) {
    if ("kubernetes".equals(id) || "kubernetes-ro".equals(id)) {
        return false;
    }
    Set<Integer> ports = KubernetesHelper.getPorts(service);
    log.debug("Service " + id + " has ports: " + ports);
    if (ports.size() == 1) {
        String type = null;
        ServiceSpec spec = service.getSpec();
        if (spec != null) {
            type = spec.getType();
            if (Objects.equals(type, "LoadBalancer")) {
                return true;
            }
        }
        log.info("Not generating route for service " + id + " type is not LoadBalancer: " + type);
        return false;
    } else {
        log.info("Not generating route for service " + id + " as only single port services are supported. Has ports: " + ports);
        return false;
    }
}
Also used : ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString)

Aggregations

ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)12 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)7 Service (io.fabric8.kubernetes.api.model.Service)6 ArrayList (java.util.ArrayList)5 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)3 HashMap (java.util.HashMap)3 KubernetesHelper.createIntOrString (io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString)2 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)2 List (java.util.List)2 Map (java.util.Map)2 Exchange (org.apache.camel.Exchange)2 Processor (org.apache.camel.Processor)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 KubernetesContainerClient (de.zalando.ep.zalenium.container.kubernetes.KubernetesContainerClient)1 Container (io.fabric8.kubernetes.api.model.Container)1 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)1 DoneableService (io.fabric8.kubernetes.api.model.DoneableService)1 EndpointAddress (io.fabric8.kubernetes.api.model.EndpointAddress)1 EndpointSubset (io.fabric8.kubernetes.api.model.EndpointSubset)1