Search in sources :

Example 51 with KubernetesEnvironment

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project che-server by eclipse-che.

the class PreviewUrlExposerTest method shouldProvisionServiceAndIngressWhenNotFound.

@Test
public void shouldProvisionServiceAndIngressWhenNotFound() throws InternalInfrastructureException {
    Mockito.when(externalServiceExposureStrategy.getExternalPath(Mockito.anyString(), Mockito.any())).thenReturn("some-server-path");
    final int PORT = 8080;
    final String SERVER_PORT_NAME = "server-" + PORT;
    CommandImpl command = new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap());
    KubernetesEnvironment env = KubernetesEnvironment.builder().setCommands(singletonList(new CommandImpl(command))).setIngresses(new HashMap<>()).setServices(new HashMap<>()).build();
    previewUrlExposer.expose(env);
    assertEquals(env.getIngresses().size(), 1);
    assertEquals(env.getServices().size(), 1);
    Service provisionedService = env.getServices().values().iterator().next();
    ServicePort provisionedServicePort = provisionedService.getSpec().getPorts().get(0);
    assertEquals(provisionedServicePort.getName(), SERVER_PORT_NAME);
    assertEquals(provisionedServicePort.getPort().intValue(), PORT);
    Ingress provisionedIngress = env.getIngresses().values().iterator().next();
    IngressBackend provisionedIngressBackend = provisionedIngress.getSpec().getRules().get(0).getHttp().getPaths().get(0).getBackend();
    assertEquals(provisionedIngressBackend.getService().getPort().getName(), SERVER_PORT_NAME);
    assertEquals(provisionedIngressBackend.getService().getName(), provisionedService.getMetadata().getName());
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) PreviewUrlImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.PreviewUrlImpl) HashMap(java.util.HashMap) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) Service(io.fabric8.kubernetes.api.model.Service) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) IngressBackend(io.fabric8.kubernetes.api.model.networking.v1.IngressBackend) Test(org.testng.annotations.Test)

Example 52 with KubernetesEnvironment

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project che-server by eclipse-che.

the class CombinedSingleHostServerExposerTest method shouldExposeDevfileServersOnSubdomans.

@Test
public void shouldExposeDevfileServersOnSubdomans() {
    // given
    ServerConfig s1 = new ServerConfigImpl("1", "http", "/", emptyMap());
    ServerConfig s2 = new ServerConfigImpl("2", "http", "/", singletonMap(REQUIRE_SUBDOMAIN, "false"));
    ServerConfig s3 = new ServerConfigImpl("3", "http", "/", singletonMap(REQUIRE_SUBDOMAIN, "true"));
    CombinedSingleHostServerExposer<KubernetesEnvironment> serverExposer = new CombinedSingleHostServerExposer<>(subdomainExposer, subpathExposer);
    // when
    serverExposer.expose(env, MACHINE, SERVICE, SERVER, PORT, Map.of("s1", s1, "s2", s2, "s3", s3));
    // then
    verify(subdomainExposer).expose(env, MACHINE, SERVICE, SERVER, PORT, Map.of("s3", s3));
    verify(subpathExposer).expose(env, MACHINE, SERVICE, SERVER, PORT, Map.of("s1", s1, "s2", s2));
}
Also used : ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) Test(org.testng.annotations.Test)

Example 53 with KubernetesEnvironment

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project che-server by eclipse-che.

the class GatewayServerExposerTest method testExposeServiceWithGatewayConfigmap.

@Test
public void testExposeServiceWithGatewayConfigmap() {
    // given
    KubernetesEnvironment k8sEnv = KubernetesEnvironment.builder().build();
    // when
    serverExposer.expose(k8sEnv, machineName, serviceName, serverId, servicePort, servers);
    // then
    Map<String, ConfigMap> configMaps = k8sEnv.getConfigMaps();
    assertTrue(configMaps.containsKey(serviceName + "-" + serverId));
    ConfigMap serverConfigMap = configMaps.get("service-server");
    // data should be empty at this point
    assertTrue(serverConfigMap.getData() == null || serverConfigMap.getData().isEmpty());
    assertEquals(serverConfigMap.getMetadata().getLabels(), GATEWAY_CONFIGMAP_LABELS);
    Map<String, String> annotations = serverConfigMap.getMetadata().getAnnotations();
    Annotations.Deserializer deserializer = Annotations.newDeserializer(annotations);
    assertEquals(deserializer.machineName(), machineName);
    Map<String, ServerConfigImpl> exposedServers = deserializer.servers();
    assertTrue(exposedServers.containsKey("serverOne"));
    ServerConfig s1 = exposedServers.get("serverOne");
    assertEquals(s1.getAttributes().get(s1attrs.keySet().iterator().next()), s1attrs.values().iterator().next());
    assertEquals(s1.getAttributes().get(ServerConfigImpl.SERVICE_NAME_ATTRIBUTE), "service");
    assertEquals(s1.getAttributes().get(ServerConfigImpl.SERVICE_PORT_ATTRIBUTE), "1234");
    assertEquals(s1.getPort(), "1111");
    assertEquals(s1.getProtocol(), "ws");
    assertNull(s1.getPath());
    assertEquals(s1.getEndpointOrigin(), "/service/server/");
}
Also used : ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) Annotations(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) Test(org.testng.annotations.Test)

Example 54 with KubernetesEnvironment

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project che-server by eclipse-che.

the class KubernetesServerExposerTest method assertThatExternalServersAreExposed.

@SuppressWarnings("SameParameterValue")
private void assertThatExternalServersAreExposed(String machineName, String portProtocol, Integer port, Map<String, ServerConfig> expectedServers) {
    // then
    assertThatContainerPortIsExposed(portProtocol, port);
    // ensure that service is created
    Service service = findContainerRelatedService();
    assertNotNull(service);
    // ensure that required service port is exposed
    ServicePort servicePort = assertThatServicePortIsExposed(port, service);
    Annotations.Deserializer serviceAnnotations = Annotations.newDeserializer(service.getMetadata().getAnnotations());
    assertEquals(serviceAnnotations.machineName(), machineName);
    // check that we did not create servers for public endpoints
    assertFalse(serviceAnnotations.servers().keySet().stream().anyMatch(key -> expectedServers.containsKey(key)));
    verify(externalServerExposer).expose(eq(kubernetesEnvironment), eq(machineName), eq(service.getMetadata().getName()), any(), eq(servicePort), eq(expectedServers));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Listeners(org.testng.annotations.Listeners) Container(io.fabric8.kubernetes.api.model.Container) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) Assert.assertEquals(org.testng.Assert.assertEquals) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) Test(org.testng.annotations.Test) SERVER_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.ServerConfig.SERVER_NAME_ATTRIBUTE) IngressServerExposer(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.IngressServerExposer) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Map(java.util.Map) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Collections.singletonMap(java.util.Collections.singletonMap) Service(io.fabric8.kubernetes.api.model.Service) Assert.assertFalse(org.testng.Assert.assertFalse) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) DISCOVERABLE_SERVER_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.ServerConfig.DISCOVERABLE_SERVER_ATTRIBUTE) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) BeforeMethod(org.testng.annotations.BeforeMethod) Pod(io.fabric8.kubernetes.api.model.Pod) Mockito.times(org.mockito.Mockito.times) SecureServerExposer(org.eclipse.che.workspace.infrastructure.kubernetes.server.secure.SecureServerExposer) Assert.assertNotNull(org.testng.Assert.assertNotNull) Mockito.verify(org.mockito.Mockito.verify) ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) SERVER_UNIQUE_PART_SIZE(org.eclipse.che.workspace.infrastructure.kubernetes.server.KubernetesServerExposer.SERVER_UNIQUE_PART_SIZE) Annotations(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations) SERVER_PREFIX(org.eclipse.che.workspace.infrastructure.kubernetes.server.KubernetesServerExposer.SERVER_PREFIX) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Entry(java.util.Map.Entry) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Assert.assertTrue(org.testng.Assert.assertTrue) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) Annotations(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations) Service(io.fabric8.kubernetes.api.model.Service)

Example 55 with KubernetesEnvironment

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project che-server by eclipse-che.

the class KubernetesServerExposerTest method assertThatSecureServerIsExposed.

@SuppressWarnings("SameParameterValue")
private void assertThatSecureServerIsExposed(String machineName, String portProtocol, Integer port, String serverName, ServerConfig serverConfig) throws Exception {
    // then
    assertThatContainerPortIsExposed(portProtocol, port);
    // ensure that service is created
    Service service = findContainerRelatedService();
    assertNotNull(service);
    // ensure that no service port is exposed
    assertTrue(service.getSpec().getPorts().stream().noneMatch(p -> p.getTargetPort().getIntVal().equals(port)));
    ServicePort servicePort = new ServicePortBuilder().withName("server-" + port).withPort(port).withProtocol(portProtocol.toUpperCase()).withNewTargetPort(port).build();
    verify(secureServerExposer).expose(eq(kubernetesEnvironment), any(), eq(machineName), // no service exists for the backed server
    isNull(), // a non-unique server
    isNull(), eq(servicePort), eq(ImmutableMap.of(serverName, serverConfig)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Listeners(org.testng.annotations.Listeners) Container(io.fabric8.kubernetes.api.model.Container) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) Assert.assertEquals(org.testng.Assert.assertEquals) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) Test(org.testng.annotations.Test) SERVER_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.ServerConfig.SERVER_NAME_ATTRIBUTE) IngressServerExposer(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.IngressServerExposer) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Map(java.util.Map) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Collections.singletonMap(java.util.Collections.singletonMap) Service(io.fabric8.kubernetes.api.model.Service) Assert.assertFalse(org.testng.Assert.assertFalse) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) DISCOVERABLE_SERVER_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.ServerConfig.DISCOVERABLE_SERVER_ATTRIBUTE) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) BeforeMethod(org.testng.annotations.BeforeMethod) Pod(io.fabric8.kubernetes.api.model.Pod) Mockito.times(org.mockito.Mockito.times) SecureServerExposer(org.eclipse.che.workspace.infrastructure.kubernetes.server.secure.SecureServerExposer) Assert.assertNotNull(org.testng.Assert.assertNotNull) Mockito.verify(org.mockito.Mockito.verify) ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) SERVER_UNIQUE_PART_SIZE(org.eclipse.che.workspace.infrastructure.kubernetes.server.KubernetesServerExposer.SERVER_UNIQUE_PART_SIZE) Annotations(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations) SERVER_PREFIX(org.eclipse.che.workspace.infrastructure.kubernetes.server.KubernetesServerExposer.SERVER_PREFIX) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Entry(java.util.Map.Entry) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Assert.assertTrue(org.testng.Assert.assertTrue) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) Service(io.fabric8.kubernetes.api.model.Service)

Aggregations

KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)94 Test (org.testng.annotations.Test)68 Map (java.util.Map)30 HashMap (java.util.HashMap)28 Service (io.fabric8.kubernetes.api.model.Service)26 Container (io.fabric8.kubernetes.api.model.Container)24 Pod (io.fabric8.kubernetes.api.model.Pod)24 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)22 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)20 PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)20 ArrayList (java.util.ArrayList)19 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)18 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)18 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)18 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)18 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)18 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)14 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)14 ServerConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl)14 InternalRecipe (org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe)14