Search in sources :

Example 46 with ServerConfig

use of org.eclipse.che.api.core.model.workspace.config.ServerConfig 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 47 with ServerConfig

use of org.eclipse.che.api.core.model.workspace.config.ServerConfig in project che-server by eclipse-che.

the class KubernetesServerExposerTest method shouldCreateIngressForServerWhenTwoServersHasTheSamePort.

@Test
public void shouldCreateIngressForServerWhenTwoServersHasTheSamePort() throws InfrastructureException {
    // given
    ServerConfigImpl httpServerConfig = new ServerConfigImpl("8080/tcp", "http", "/api", ATTRIBUTES_MAP);
    ServerConfigImpl wsServerConfig = new ServerConfigImpl("8080/tcp", "ws", "/connect", ATTRIBUTES_MAP);
    IntOrString targetPort = new IntOrString(8080);
    ServicePort servicePort = new ServicePortBuilder().withName("server-8080").withPort(8080).withProtocol("TCP").withTargetPort(targetPort).build();
    Map<String, ServerConfig> serversToExpose = ImmutableMap.of("http-server", httpServerConfig, "ws-server", wsServerConfig);
    // when
    serverExposer.expose(serversToExpose);
    // then
    assertThatExternalServersAreExposed(MACHINE_NAME, "tcp", 8080, ImmutableMap.of("http-server", new ServerConfigImpl(httpServerConfig).withAttributes(ATTRIBUTES_MAP), "ws-server", new ServerConfigImpl(wsServerConfig).withAttributes(ATTRIBUTES_MAP)));
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) Test(org.testng.annotations.Test)

Example 48 with ServerConfig

use of org.eclipse.che.api.core.model.workspace.config.ServerConfig 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)

Example 49 with ServerConfig

use of org.eclipse.che.api.core.model.workspace.config.ServerConfig in project che-server by eclipse-che.

the class KubernetesServerExposerTest method testExposeUniqueSecureServersWithOnlyMatchingServers.

@Test
public void testExposeUniqueSecureServersWithOnlyMatchingServers() throws InfrastructureException {
    // https://github.com/eclipse/che/issues/16330
    // given 2 servers which one of them is unique
    ServerConfigImpl theiaSC = new ServerConfigImpl("3100/tcp", "https", null, ImmutableMap.of("internal", "false", "discoverable", "false", "secure", "true", "type", "ide"));
    ServerConfigImpl webviewsSC = new ServerConfigImpl("3100/tcp", "https", null, ImmutableMap.of("internal", "false", "discoverable", "false", "unique", "true", "secure", "true", "type", "webview"));
    Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of("theia", theiaSC, "webviews", webviewsSC);
    // when
    serverExposer.expose(serversToExpose);
    // then expose is called twice with only one server each
    ArgumentCaptor<Map<String, ServerConfig>> serversCaptor = ArgumentCaptor.forClass(Map.class);
    verify(secureServerExposer, times(2)).expose(any(), any(), any(), any(), any(), any(), serversCaptor.capture());
    for (Map<String, ServerConfig> captures : serversCaptor.getAllValues()) {
        assertEquals(captures.size(), 1);
    }
}
Also used : ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test)

Example 50 with ServerConfig

use of org.eclipse.che.api.core.model.workspace.config.ServerConfig in project che-server by eclipse-che.

the class MachineConfigsValidator method validateMachine.

private void validateMachine(String machineName, InternalMachineConfig machineConfig) throws ValidationException {
    checkArgument(MACHINE_NAME_PATTERN.matcher(machineName).matches(), "Name of machine '%s' in environment is invalid", machineName);
    if (machineConfig.getServers() != null) {
        for (Map.Entry<String, ? extends ServerConfig> serverEntry : machineConfig.getServers().entrySet()) {
            String serverName = serverEntry.getKey();
            ServerConfig server = serverEntry.getValue();
            checkArgument(server.getPort() != null && SERVER_PORT_PATTERN.matcher(server.getPort()).matches(), "Machine '%s' in environment contains server conf '%s' with invalid port '%s'", machineName, serverName, server.getPort());
            checkArgument(server.getProtocol() == null || SERVER_PROTOCOL_PATTERN.matcher(server.getProtocol()).matches(), "Machine '%s' in environment contains server conf '%s' with invalid protocol '%s'", machineName, serverName, server.getProtocol());
        }
    }
    if (machineConfig.getAttributes() != null) {
        String memoryLimit = machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE);
        String memoryRequest = machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE);
        if (memoryLimit != null && memoryRequest != null) {
            checkArgument(Long.parseLong(memoryLimit) >= Long.parseLong(memoryRequest), "Machine '%s' in environment contains inconsistent memory attributes: Memory limit: '%s', Memory request: '%s'", machineName, Long.parseLong(memoryLimit), Long.parseLong(memoryRequest));
        }
    }
}
Also used : ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) Map(java.util.Map)

Aggregations

ServerConfig (org.eclipse.che.api.core.model.workspace.config.ServerConfig)74 Test (org.testng.annotations.Test)54 ServerConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl)42 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)30 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)28 EndpointImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl)20 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)18 HashMap (java.util.HashMap)16 Annotations (org.eclipse.che.workspace.infrastructure.kubernetes.Annotations)16 Service (io.fabric8.kubernetes.api.model.Service)14 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)12 Map (java.util.Map)10 ImmutableMap (com.google.common.collect.ImmutableMap)8 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)8 Container (io.fabric8.kubernetes.api.model.Container)6 ContainerPortBuilder (io.fabric8.kubernetes.api.model.ContainerPortBuilder)6 Ingress (io.fabric8.kubernetes.api.model.networking.v1.Ingress)6 ArrayList (java.util.ArrayList)6 Collections.singletonMap (java.util.Collections.singletonMap)6 Optional (java.util.Optional)6