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);
}
}
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);
}
}
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;
}
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());
}
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);
}
Aggregations