Search in sources :

Example 1 with ServicePort

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

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

use of io.fabric8.kubernetes.api.model.ServicePort in project che by eclipse.

the class KubernetesServiceTest method shouldReturnServicePortListFromImageExposedPortList.

@Test
public void shouldReturnServicePortListFromImageExposedPortList() {
    // Given
    Map<String, ExposedPort> imageExposedPorts = new HashMap<>();
    imageExposedPorts.put("8080/TCP", new ExposedPort());
    // When
    List<ServicePort> servicePorts = KubernetesService.getServicePortsFrom(imageExposedPorts.keySet());
    // Then
    List<String> portsAndProtocols = servicePorts.stream().map(p -> Integer.toString(p.getPort()) + "/" + p.getProtocol()).collect(Collectors.toList());
    assertTrue(imageExposedPorts.keySet().stream().anyMatch(portsAndProtocols::contains));
}
Also used : ExposedPort(org.eclipse.che.plugin.docker.client.json.ExposedPort) HashSet(java.util.HashSet) List(java.util.List) Map(java.util.Map) BeforeClass(org.testng.annotations.BeforeClass) Assert.assertTrue(org.testng.Assert.assertTrue) Set(java.util.Set) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) Collectors(java.util.stream.Collectors) ExposedPort(org.eclipse.che.plugin.docker.client.json.ExposedPort) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 4 with ServicePort

use of io.fabric8.kubernetes.api.model.ServicePort in project che by eclipse.

the class OpenShiftConnector method createOpenShiftService.

private void createOpenShiftService(String workspaceID, Set<String> exposedPorts, Map<String, String> additionalLabels) {
    Map<String, String> selector = Collections.singletonMap(OPENSHIFT_DEPLOYMENT_LABEL, CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID);
    List<ServicePort> ports = KubernetesService.getServicePortsFrom(exposedPorts);
    Service service = openShiftClient.services().inNamespace(this.openShiftCheProjectName).createNew().withNewMetadata().withName(CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID).withAnnotations(KubernetesLabelConverter.labelsToNames(additionalLabels)).endMetadata().withNewSpec().withType(OPENSHIFT_SERVICE_TYPE_NODE_PORT).withSelector(selector).withPorts(ports).endSpec().done();
    LOG.info("OpenShift service {} created", service.getMetadata().getName());
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) KubernetesService(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService) Service(io.fabric8.kubernetes.api.model.Service)

Example 5 with ServicePort

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

ServicePort (io.fabric8.kubernetes.api.model.ServicePort)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)3 Service (io.fabric8.kubernetes.api.model.Service)3 Map (java.util.Map)3 ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)2 Exchange (org.apache.camel.Exchange)2 Processor (org.apache.camel.Processor)2 Test (org.junit.Test)2 PodList (io.fabric8.kubernetes.api.model.PodList)1 ServiceList (io.fabric8.kubernetes.api.model.ServiceList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ExposedPort (org.eclipse.che.plugin.docker.client.json.ExposedPort)1 PortBinding (org.eclipse.che.plugin.docker.client.json.PortBinding)1 KubernetesService (org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService)1 Assert.assertTrue (org.testng.Assert.assertTrue)1