Search in sources :

Example 36 with Service

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

the class KubernetesServiceAccountsProducerTest method listTest.

@Test
public void listTest() throws Exception {
    if (ObjectHelper.isEmpty(authToken)) {
        return;
    }
    List<ServiceAccount> result = template.requestBody("direct:list", "", List.class);
    boolean fabric8Exists = false;
    Iterator<ServiceAccount> it = result.iterator();
    while (it.hasNext()) {
        ServiceAccount service = it.next();
        if ("fabric8".equalsIgnoreCase(service.getMetadata().getName())) {
            fabric8Exists = true;
        }
    }
    assertTrue(fabric8Exists);
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) Test(org.junit.Test)

Example 37 with Service

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

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

the class KubernetesGatewayImpl method removeContainerBasedGateway.

/**
 * @see ContainerBasedGatewayGenerator#removeContainerBasedGateway(String, API) (String)
 */
@Override
public void removeContainerBasedGateway(String label, API api) throws ContainerBasedGatewayException {
    try {
        client.services().inNamespace(namespace).withLabel(ContainerBasedGatewayConstants.GATEWAY, label).delete();
        client.extensions().deployments().inNamespace(namespace).withLabel(ContainerBasedGatewayConstants.GATEWAY, label).delete();
        client.extensions().ingresses().inNamespace(namespace).withLabel(ContainerBasedGatewayConstants.GATEWAY, label).delete();
        log.info(String.format("Completed deleting the container gateway related %s deployment, service and " + "ingress resources.", cmsType));
    } catch (KubernetesClientException e) {
        throw new ContainerBasedGatewayException("Error while removing container based gateway", e, ExceptionCodes.CONTAINER_GATEWAY_REMOVAL_FAILED);
    }
}
Also used : ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 39 with Service

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

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

the class KubernetesGatewayImplTestCase method testCreateContainerGateway.

@Test
public void testCreateContainerGateway() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    Mockito.when(openShiftClient.load(Mockito.any()).get()).thenReturn(getServiceResources(), getDeploymentResources(), getIngressResources());
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    ScalableResource scalableResource = Mockito.mock(ScalableResource.class);
    Mockito.when(scalableResource.get()).thenReturn(null);
    Service service = createService(openShiftClient, nonNamespaceOperation);
    Deployment deployment = createDeployment(openShiftClient, nonNamespaceOperation, scalableResource);
    Ingress ingress = createIngress(openShiftClient, nonNamespaceOperation, scalableResource);
    Mockito.when(nonNamespaceOperation.create(Mockito.any())).thenReturn(service, deployment, ingress);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    kubernetesGateway.createContainerGateway(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX, api);
    Mockito.verify(openShiftClient, Mockito.times(4)).load(Mockito.any());
    Mockito.verify(openShiftClient, Mockito.times(3)).services();
    Mockito.verify(openShiftClient, Mockito.times(6)).extensions();
}
Also used : ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) API(org.wso2.carbon.apimgt.core.models.API) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)29 ArrayList (java.util.ArrayList)15 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)12 Test (org.junit.Test)12 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)9 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)8 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)8 ContainerBasedGatewayException (org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException)8 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)7 HashMap (java.util.HashMap)7 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)7 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)6 ScalableResource (io.fabric8.kubernetes.client.dsl.ScalableResource)6 Map (java.util.Map)6 KubernetesService (org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService)6 API (org.wso2.carbon.apimgt.core.models.API)6 Pod (io.fabric8.kubernetes.api.model.Pod)5 List (java.util.List)5 OpenShiftException (org.eclipse.che.plugin.openshift.client.exception.OpenShiftException)5 ServiceDiscoveryException (org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException)5