Search in sources :

Example 11 with LoadBalancer

use of io.fabric8.gateway.loadbalancer.LoadBalancer 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)

Example 12 with LoadBalancer

use of io.fabric8.gateway.loadbalancer.LoadBalancer 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 13 with LoadBalancer

use of io.fabric8.gateway.loadbalancer.LoadBalancer in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method init.

/**
 *  ServiceName  Namespace   Criteria  Type          Ports  LoadBalancer  ExternalIP
 *
 *  service0     dev         app=web   ClusterIP     http       -         included
 *  service1     dev         -         ExternalName  http       -             -
 *  service2     dev         -         LoadBalancer  https      IP            -
 *  service3     prod        app=web   ClusterIP     http       -             -
 *  service4     prod        -         LoadBalancer  http     hostname        -
 */
@BeforeTest(description = "Create a list of services with all combinations included")
void init() {
    Map<String, String> oneLabel = createOneLabelHashMap();
    ServicePortBuilder httpPortBuilder = new ServicePortBuilder().withName("http").withPort(80);
    ServicePort httpPort = httpPortBuilder.build();
    ServicePort nodePort2 = new ServicePortBuilder().withName("https").withPort(443).withNodePort(30002).build();
    ServicePort nodePort4 = httpPortBuilder.withNodePort(30004).build();
    List<LoadBalancerIngress> ipIngresses = new ArrayList<>();
    LoadBalancerIngress ingressWithIP = new LoadBalancerIngressBuilder().withIp("100.1.1.2").build();
    ipIngresses.add(ingressWithIP);
    List<LoadBalancerIngress> hostnameIngresses = new ArrayList<>();
    LoadBalancerIngress ingressWithHostname = new LoadBalancerIngressBuilder().withHostname("abc.com").build();
    hostnameIngresses.add(ingressWithHostname);
    Service service0 = new ServiceBuilder().withNewMetadata().withName("service0").withNamespace("dev").withLabels(oneLabel).and().withNewSpec().withType("ClusterIP").withClusterIP("1.1.1.0").withPorts(httpPort).withExternalIPs("100.2.1.0").and().build();
    Service service1 = new ServiceBuilder().withNewMetadata().withName("service1").withNamespace("dev").and().withNewSpec().withType("ExternalName").withExternalName("aaa.com").withPorts(httpPort).and().build();
    Service service2 = new ServiceBuilder().withNewMetadata().withName("service2").withNamespace("dev").and().withNewSpec().withType("LoadBalancer").withClusterIP("1.1.1.2").withPorts(nodePort2).and().withNewStatus().withNewLoadBalancer().withIngress(ipIngresses).endLoadBalancer().and().build();
    Service service3 = new ServiceBuilder().withNewMetadata().withName("service3").withNamespace("prod").withLabels(oneLabel).and().withNewSpec().withType("ClusterIP").withClusterIP("1.1.1.3").withPorts(httpPort).and().build();
    Service service4 = new ServiceBuilder().withNewMetadata().withName("service4").withNamespace("prod").and().withNewSpec().withType("LoadBalancer").withClusterIP("1.1.1.4").withPorts(nodePort4).and().withNewStatus().withNewLoadBalancer().withIngress(hostnameIngresses).endLoadBalancer().and().build();
    List<Service> servicesList = new ArrayList<>();
    servicesList.add(service0);
    servicesList.add(service1);
    servicesList.add(service2);
    servicesList.add(service3);
    servicesList.add(service4);
    this.listOfServices = servicesList;
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) LoadBalancerIngress(io.fabric8.kubernetes.api.model.LoadBalancerIngress) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) ArrayList(java.util.ArrayList) LoadBalancerIngressBuilder(io.fabric8.kubernetes.api.model.LoadBalancerIngressBuilder) Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) BeforeTest(org.testng.annotations.BeforeTest)

Example 14 with LoadBalancer

use of io.fabric8.gateway.loadbalancer.LoadBalancer 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 15 with LoadBalancer

use of io.fabric8.gateway.loadbalancer.LoadBalancer in project fabric8 by jboss-fuse.

the class DetectingGatewayTest method createGateway.

public DetectingGateway createGateway() {
    String loadBalancerType = LoadBalancers.STICKY_LOAD_BALANCER;
    int stickyLoadBalancerCacheSize = LoadBalancers.STICKY_LOAD_BALANCER_DEFAULT_CACHE_SIZE;
    LoadBalancer serviceLoadBalancer = LoadBalancers.createLoadBalancer(loadBalancerType, stickyLoadBalancerCacheSize);
    ArrayList<Protocol> protocols = new ArrayList<Protocol>();
    protocols.add(new StompProtocol());
    protocols.add(new MqttProtocol());
    protocols.add(new AmqpProtocol());
    protocols.add(new OpenwireProtocol());
    protocols.add(new HttpProtocol());
    protocols.add(new SslProtocol());
    DetectingGateway gateway = new DetectingGateway();
    gateway.setPort(0);
    gateway.setVertx(vertx);
    SslConfig sslConfig = new SslConfig(new File(basedir(), "src/test/resources/server.ks"), "password");
    sslConfig.setKeyPassword("password");
    gateway.setSslConfig(sslConfig);
    gateway.setServiceMap(serviceMap);
    gateway.setProtocols(protocols);
    gateway.setServiceLoadBalancer(serviceLoadBalancer);
    gateway.setDefaultVirtualHost("broker1");
    gateway.setConnectionTimeout(5000);
    gateway.init();
    gateways.add(gateway);
    return gateway;
}
Also used : SslProtocol(io.fabric8.gateway.handlers.detecting.protocol.ssl.SslProtocol) ArrayList(java.util.ArrayList) LoadBalancer(io.fabric8.gateway.loadbalancer.LoadBalancer) MqttProtocol(io.fabric8.gateway.handlers.detecting.protocol.mqtt.MqttProtocol) OpenwireProtocol(io.fabric8.gateway.handlers.detecting.protocol.openwire.OpenwireProtocol) SslConfig(io.fabric8.gateway.handlers.detecting.protocol.ssl.SslConfig) HttpProtocol(io.fabric8.gateway.handlers.detecting.protocol.http.HttpProtocol) StompProtocol(io.fabric8.gateway.handlers.detecting.protocol.stomp.StompProtocol) AmqpProtocol(io.fabric8.gateway.handlers.detecting.protocol.amqp.AmqpProtocol) MqttProtocol(io.fabric8.gateway.handlers.detecting.protocol.mqtt.MqttProtocol) SslProtocol(io.fabric8.gateway.handlers.detecting.protocol.ssl.SslProtocol) StompProtocol(io.fabric8.gateway.handlers.detecting.protocol.stomp.StompProtocol) AmqpProtocol(io.fabric8.gateway.handlers.detecting.protocol.amqp.AmqpProtocol) HttpProtocol(io.fabric8.gateway.handlers.detecting.protocol.http.HttpProtocol) OpenwireProtocol(io.fabric8.gateway.handlers.detecting.protocol.openwire.OpenwireProtocol) File(java.io.File)

Aggregations

LoadBalancer (io.fabric8.gateway.loadbalancer.LoadBalancer)14 RoundRobinLoadBalancer (io.fabric8.gateway.loadbalancer.RoundRobinLoadBalancer)8 ArrayList (java.util.ArrayList)7 InetSocketAddress (java.net.InetSocketAddress)5 AmqpProtocol (io.fabric8.gateway.handlers.detecting.protocol.amqp.AmqpProtocol)4 HttpProtocol (io.fabric8.gateway.handlers.detecting.protocol.http.HttpProtocol)4 MqttProtocol (io.fabric8.gateway.handlers.detecting.protocol.mqtt.MqttProtocol)4 OpenwireProtocol (io.fabric8.gateway.handlers.detecting.protocol.openwire.OpenwireProtocol)4 SslConfig (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslConfig)4 SslProtocol (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslProtocol)4 StompProtocol (io.fabric8.gateway.handlers.detecting.protocol.stomp.StompProtocol)4 MappedServices (io.fabric8.gateway.handlers.http.MappedServices)4 Service (io.fabric8.kubernetes.api.model.Service)4 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)4 Test (org.junit.Test)4 HttpGateway (io.fabric8.gateway.handlers.http.HttpGateway)3 HttpGatewayHandler (io.fabric8.gateway.handlers.http.HttpGatewayHandler)3 HttpGatewayServer (io.fabric8.gateway.handlers.http.HttpGatewayServer)3 HttpMappingRule (io.fabric8.gateway.handlers.http.HttpMappingRule)3 RandomLoadBalancer (io.fabric8.gateway.loadbalancer.RandomLoadBalancer)3