Search in sources :

Example 16 with Endpoint

use of io.fabric8.annotations.Endpoint in project kie-wb-common by kiegroup.

the class OpenShiftClient method getRuntimeEndpoint.

public OpenShiftRuntimeEndpoint getRuntimeEndpoint(String id) throws OpenShiftClientException {
    try {
        OpenShiftRuntimeId runtimeId = OpenShiftRuntimeId.fromString(id);
        String prjName = runtimeId.project();
        String svcName = runtimeId.service();
        OpenShiftRuntimeEndpoint endpoint = new OpenShiftRuntimeEndpoint();
        Route route = delegate.routes().inNamespace(prjName).withName(svcName).get();
        if (route != null) {
            RouteSpec routeSpec = route.getSpec();
            endpoint.setProtocol(routeSpec.getTls() != null ? "https" : "http");
            endpoint.setHost(routeSpec.getHost());
            RoutePort routePort = routeSpec.getPort();
            if (routePort != null) {
                IntOrString targetPort = routePort.getTargetPort();
                if (targetPort != null) {
                    endpoint.setPort(targetPort.getIntVal());
                }
            }
        }
        return endpoint;
    } catch (Throwable t) {
        throw new OpenShiftClientException(t.getMessage(), t);
    }
}
Also used : RoutePort(io.fabric8.openshift.api.model.RoutePort) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) OpenShiftClientException(org.guvnor.ala.openshift.access.exceptions.OpenShiftClientException) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) RouteSpec(io.fabric8.openshift.api.model.RouteSpec) OpenShiftRuntimeEndpoint(org.guvnor.ala.openshift.model.OpenShiftRuntimeEndpoint) Route(io.fabric8.openshift.api.model.Route)

Example 17 with Endpoint

use of io.fabric8.annotations.Endpoint in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method addLoadBalancerEndpoint.

/**
 * Adds the LoadBalancer Endpoint to the endpointList
 *
 * @param serviceName  name of the service the endpoint belongs to
 * @param service      service object instance
 * @param port         port number
 * @param protocol     whether http or https
 * @param namespace    namespace of the service
 * @param labels       labels of the service
 * @param endpointList endpointList which the endpoint has to be added to
 * @throws MalformedURLException if protocol unknown, therefore will not get thrown
 */
private void addLoadBalancerEndpoint(String serviceName, Service service, int port, String protocol, String namespace, String labels, List<Endpoint> endpointList) throws MalformedURLException {
    List<LoadBalancerIngress> loadBalancerIngresses = service.getStatus().getLoadBalancer().getIngress();
    if (!loadBalancerIngresses.isEmpty()) {
        for (LoadBalancerIngress loadBalancerIngress : loadBalancerIngresses) {
            String hostname = loadBalancerIngress.getHostname();
            String host = (hostname == null || "".equals(hostname)) ? loadBalancerIngress.getIp() : hostname;
            URL url = new URL(protocol, host, port, "");
            endpointList.add(constructEndpoint(serviceName, namespace, protocol, LOAD_BALANCER, url, labels));
        }
    } else {
        log.debug("Service:{}  Namespace:{}  Port:{}/{} has no loadBalancer ingresses available.", serviceName, namespace, port, protocol);
    }
}
Also used : LoadBalancerIngress(io.fabric8.kubernetes.api.model.LoadBalancerIngress) URL(java.net.URL)

Example 18 with Endpoint

use of io.fabric8.annotations.Endpoint in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method testListServicesWithWrongPortType.

@Test(description = "Test .listServices() while the list has only one service and its port is not http nor https")
public void testListServicesWithWrongPortType() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
    sdKubernetes.setClient(openShiftClient);
    // Include ExternalNames (includeClusterIP not checked)
    sdKubernetes.setIncludeExternalNameTypeServices(true);
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    Mockito.when(openShiftClient.services().inNamespace(null)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.list()).thenReturn(createMalformedServiceList("somePort"));
    List<Endpoint> endpoints = sdKubernetes.listServices();
    Assert.assertTrue(endpoints.isEmpty());
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 19 with Endpoint

use of io.fabric8.annotations.Endpoint in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method testListServicesOfLoadBalancerTypeWithoutIngress.

@Test(description = "Test .listServices() while the list only has a LoadBalancer type service without any ingress")
public void testListServicesOfLoadBalancerTypeWithoutIngress() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
    sdKubernetes.setClient(openShiftClient);
    // Not include ClusterIPs
    sdKubernetes.setIncludeClusterIP(false);
    // Not include ExternalNames
    sdKubernetes.setIncludeExternalNameTypeServices(false);
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    Mockito.when(openShiftClient.services().inNamespace(null)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.list()).thenReturn(createMalformedServiceList("http"));
    Mockito.when(openShiftClient.getMasterUrl()).thenReturn(new URL(MASTER_URL));
    List<Endpoint> endpoints = sdKubernetes.listServices();
    Assert.assertEquals(endpoints.size(), 1);
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) URL(java.net.URL) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 20 with Endpoint

use of io.fabric8.annotations.Endpoint in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method testListServicesWithCriteria.

@Test(description = "Test .listServices(Criteria) method")
public void testListServicesWithCriteria() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
    sdKubernetes.setClient(openShiftClient);
    // Include ClusterIPs
    sdKubernetes.setIncludeClusterIP(true);
    // Not include ExternalNames
    sdKubernetes.setIncludeExternalNameTypeServices(false);
    Map<String, String> oneLabel = createOneLabelHashMap();
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    BaseOperation baseOperation = Mockito.mock(BaseOperation.class);
    Mockito.when(openShiftClient.services().inNamespace(null)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withLabels(oneLabel)).thenReturn(baseOperation);
    Mockito.when(baseOperation.list()).thenReturn(createServiceListWithCriteria());
    Mockito.when(openShiftClient.getMasterUrl()).thenReturn(new URL(MASTER_URL));
    List<Endpoint> endpoints = sdKubernetes.listServices(oneLabel);
    Assert.assertEquals(endpoints.size(), 3);
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) BaseOperation(io.fabric8.kubernetes.client.dsl.base.BaseOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) URL(java.net.URL) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)12 Service (io.fabric8.kubernetes.api.model.Service)9 ArrayList (java.util.ArrayList)9 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)7 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)7 URL (java.net.URL)7 BeforeTest (org.testng.annotations.BeforeTest)7 Test (org.testng.annotations.Test)7 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 IOException (java.io.IOException)5 MalformedURLException (java.net.MalformedURLException)5 Endpoints (io.fabric8.kubernetes.api.model.Endpoints)4 ServiceDiscoveryException (org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException)4 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)3 EndpointsList (io.fabric8.kubernetes.api.model.EndpointsList)3 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)3 File (java.io.File)3 Collection (java.util.Collection)3 Configuration (io.fabric8.annotations.Configuration)2 Endpoint (io.fabric8.annotations.Endpoint)2