Search in sources :

Example 26 with Service

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

the class KubernetesGatewayImplTestCase method testCreateIngressResourceForInvalidResource.

@Test
public void testCreateIngressResourceForInvalidResource() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    ScalableResource scalableResource = Mockito.mock(ScalableResource.class);
    Mockito.when(scalableResource.get()).thenReturn(null);
    HasMetadata invalidMetadata = Mockito.mock(Deployment.class);
    List<HasMetadata> ingressResources = new ArrayList<>();
    ingressResources.add(invalidMetadata);
    Mockito.when(openShiftClient.load(Mockito.any()).get()).thenReturn(getServiceResources(), getDeploymentResources(), ingressResources);
    Service service = createService(openShiftClient, nonNamespaceOperation);
    Deployment deployment = createDeployment(openShiftClient, nonNamespaceOperation, scalableResource);
    Mockito.when(nonNamespaceOperation.create(Mockito.any())).thenReturn(service, deployment);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    try {
        kubernetesGateway.createContainerGateway(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX, api);
    } catch (ContainerBasedGatewayException e) {
        Assert.assertEquals(e.getErrorHandler(), ExceptionCodes.LOADED_RESOURCE_DEFINITION_IS_NOT_VALID);
    }
}
Also used : ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ArrayList(java.util.ArrayList) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 27 with Service

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

the class KubernetesGatewayImplTestCase method testCreateIngressResourceForKubernetesException.

@Test
public void testCreateIngressResourceForKubernetesException() 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);
    Mockito.when(nonNamespaceOperation.create(Mockito.any())).thenReturn(service, deployment);
    ScalableResource scalableResourceIngress = Mockito.mock(ScalableResource.class);
    String ingressName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX + ContainerBasedGatewayConstants.CMS_INGRESS_SUFFIX;
    Mockito.when(openShiftClient.extensions().ingresses().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withName(ingressName)).thenReturn(scalableResourceIngress);
    Mockito.when(scalableResourceIngress.get()).thenThrow(KubernetesClientException.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    try {
        kubernetesGateway.createContainerGateway(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX, api);
    } catch (ContainerBasedGatewayException e) {
        Assert.assertEquals(e.getErrorHandler(), ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
    }
}
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) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 28 with Service

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

the class KubernetesGatewayImplTestCase method createService.

/**
 * Create Service resource
 *
 * @param openShiftClient       Openshift client
 * @param nonNamespaceOperation NonNamespaceOperation instance
 */
private Service createService(OpenShiftClient openShiftClient, NonNamespaceOperation nonNamespaceOperation) {
    BaseOperation baseOperation = Mockito.mock(BaseOperation.class);
    String serviceName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX + ContainerBasedGatewayConstants.CMS_SERVICE_SUFFIX;
    Mockito.when(openShiftClient.services().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withName(serviceName)).thenReturn(baseOperation);
    Mockito.when(baseOperation.get()).thenReturn(null);
    Service service = Mockito.mock(Service.class, Mockito.RETURNS_DEEP_STUBS);
    return service;
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) BaseOperation(io.fabric8.kubernetes.client.dsl.base.BaseOperation)

Example 29 with Service

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

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

the class OpenShiftConnector method inspectNetwork.

@Override
public Network inspectNetwork(InspectNetworkParams params) throws IOException {
    String netId = params.getNetworkId();
    ServiceList services = openShiftClient.services().inNamespace(this.openShiftCheProjectName).list();
    Map<String, ContainerInNetwork> containers = new HashMap<>();
    for (Service svc : services.getItems()) {
        String selector = svc.getSpec().getSelector().get(OPENSHIFT_DEPLOYMENT_LABEL);
        if (selector == null || !selector.startsWith(CHE_OPENSHIFT_RESOURCES_PREFIX)) {
            continue;
        }
        PodList pods = openShiftClient.pods().inNamespace(openShiftCheProjectName).withLabel(OPENSHIFT_DEPLOYMENT_LABEL, selector).list();
        for (Pod pod : pods.getItems()) {
            String podName = pod.getMetadata().getName();
            ContainerInNetwork container = new ContainerInNetwork().withName(podName).withIPv4Address(svc.getSpec().getClusterIP());
            String podId = KubernetesStringUtils.getLabelFromContainerID(pod.getMetadata().getLabels().get(CHE_CONTAINER_IDENTIFIER_LABEL_KEY));
            if (podId == null) {
                continue;
            }
            containers.put(podId, container);
        }
    }
    List<IpamConfig> ipamConfig = new ArrayList<>();
    Ipam ipam = new Ipam().withDriver("bridge").withOptions(Collections.emptyMap()).withConfig(ipamConfig);
    return new Network().withName("OpenShift").withId(netId).withContainers(containers).withLabels(Collections.emptyMap()).withOptions(Collections.emptyMap()).withDriver("default").withIPAM(ipam).withScope("local").withInternal(false).withEnableIPv6(false);
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) HashMap(java.util.HashMap) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) ArrayList(java.util.ArrayList) KubernetesService(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService) Service(io.fabric8.kubernetes.api.model.Service) IpamConfig(org.eclipse.che.plugin.docker.client.json.network.IpamConfig) ContainerInNetwork(org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork) Ipam(org.eclipse.che.plugin.docker.client.json.network.Ipam) Network(org.eclipse.che.plugin.docker.client.json.network.Network) ContainerInNetwork(org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork)

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