KubernetesServerExposer { public void expose(Map<String, ? extends ServerConfig> servers) throws InfrastructureException { Map<String, ServerConfig> internalServers = new HashMap<>(); Map<String, ServerConfig> externalServers = new HashMap<>(); Map<String, ServerConfig> secureServers = new HashMap<>(); Map<String, ServicePort> unsecuredPorts = new HashMap<>(); Map<String, ServicePort> securedPorts = new HashMap<>(); splitServersAndPortsByExposureType( servers, internalServers, externalServers, secureServers, unsecuredPorts, securedPorts); provisionServicesForDiscoverableServers(servers); Optional<Service> serviceOpt = createService(internalServers, unsecuredPorts); if (serviceOpt.isPresent()) { Service service = serviceOpt.get(); String serviceName = service.getMetadata().getName(); k8sEnv.getServices().put(serviceName, service); exposeNonSecureServers(serviceName, externalServers, unsecuredPorts); } exposeSecureServers(secureServers, securedPorts); } KubernetesServerExposer(
      ExternalServerExposer<T> externalServerExposer,
      SecureServerExposer<T> secureServerExposer,
      String machineName,
      PodData pod,
      Container container,
      T k8sEnv); static String makeServerNameValidForDns(String name); void expose(Map<String, ? extends ServerConfig> servers); static final int SERVER_UNIQUE_PART_SIZE; static final String SERVER_PREFIX; }
@Test public void shouldExposeContainerPortAndCreateServiceForServer() throws Exception { ServerConfigImpl httpServerConfig = new ServerConfigImpl("8080/tcp", "http", "/api", ATTRIBUTES_MAP); Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of("http-server", httpServerConfig); serverExposer.expose(serversToExpose); assertThatExternalServerIsExposed( MACHINE_NAME, "tcp", 8080, "http-server", new ServerConfigImpl(httpServerConfig).withAttributes(ATTRIBUTES_MAP)); }
@Test public void shouldExposeContainerPortAndCreateServiceAndForServerWhenTwoServersHasTheSamePort() throws Exception { ServerConfigImpl httpServerConfig = new ServerConfigImpl("8080/tcp", "http", "/api", ATTRIBUTES_MAP); ServerConfigImpl wsServerConfig = new ServerConfigImpl("8080/tcp", "ws", "/connect", ATTRIBUTES_MAP); Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of( "http-server", httpServerConfig, "ws-server", wsServerConfig); serverExposer.expose(serversToExpose); assertEquals(kubernetesEnvironment.getServices().size(), 1); assertThatExternalServersAreExposed( MACHINE_NAME, "tcp", 8080, ImmutableMap.of( "http-server", new ServerConfigImpl(httpServerConfig).withAttributes(ATTRIBUTES_MAP), "ws-server", new ServerConfigImpl(wsServerConfig).withAttributes(ATTRIBUTES_MAP))); }
@Test public void shouldExposeContainerPortsAndCreateServiceForServerWhenTwoServersHasDifferentPorts() throws Exception { ServerConfigImpl httpServerConfig = new ServerConfigImpl("8080/tcp", "http", "/api", ATTRIBUTES_MAP); ServerConfigImpl wsServerConfig = new ServerConfigImpl("8081/tcp", "ws", "/connect", ATTRIBUTES_MAP); Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of( "http-server", httpServerConfig, "ws-server", wsServerConfig); serverExposer.expose(serversToExpose); assertEquals(kubernetesEnvironment.getServices().size(), 1); assertThatExternalServerIsExposed( MACHINE_NAME, "tcp", 8080, "http-server", new ServerConfigImpl(httpServerConfig).withAttributes(ATTRIBUTES_MAP)); assertThatExternalServerIsExposed( MACHINE_NAME, "tcp", 8081, "ws-server", new ServerConfigImpl(wsServerConfig).withAttributes(ATTRIBUTES_MAP)); }
@Test public void shouldExposeTcpContainerPortsAndCreateServiceAndForServerWhenProtocolIsMissedInPort() throws Exception { ServerConfigImpl httpServerConfig = new ServerConfigImpl("8080", "http", "/api", ATTRIBUTES_MAP); Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of("http-server", httpServerConfig); serverExposer.expose(serversToExpose); assertEquals(kubernetesEnvironment.getServices().size(), 1); assertThatExternalServerIsExposed( MACHINE_NAME, "TCP", 8080, "http-server", new ServerConfigImpl(httpServerConfig).withAttributes(ATTRIBUTES_MAP)); }
@Test public void shouldNotAddAdditionalContainerPortWhenItIsAlreadyExposed() throws Exception { ServerConfigImpl httpServerConfig = new ServerConfigImpl("8080/tcp", "http", "/api", ATTRIBUTES_MAP); Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of("http-server", httpServerConfig); container.setPorts( singletonList( new ContainerPortBuilder() .withName("port-8080") .withContainerPort(8080) .withProtocol("TCP") .build())); serverExposer.expose(serversToExpose); assertThatExternalServerIsExposed( MACHINE_NAME, "tcp", 8080, "http-server", new ServerConfigImpl(httpServerConfig).withAttributes(ATTRIBUTES_MAP)); }
@Test public void shouldAddAdditionalContainerPortWhenThereIsTheSameButWithDifferentProtocol() throws Exception { ServerConfigImpl udpServerConfig = new ServerConfigImpl("8080/udp", "udp", "/api", ATTRIBUTES_MAP); Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of("server", udpServerConfig); container.setPorts( new ArrayList<>( singletonList( new ContainerPortBuilder() .withName("port-8080") .withContainerPort(8080) .withProtocol("TCP") .build()))); serverExposer.expose(serversToExpose); assertEquals(container.getPorts().size(), 2); assertEquals(container.getPorts().get(1).getContainerPort(), new Integer(8080)); assertEquals(container.getPorts().get(1).getProtocol(), "UDP"); assertThatExternalServerIsExposed( MACHINE_NAME, "udp", 8080, "server", new ServerConfigImpl(udpServerConfig).withAttributes(ATTRIBUTES_MAP)); }
@Test public void shouldExposeContainerPortAndCreateServiceForInternalServer() throws Exception { ServerConfigImpl httpServerConfig = new ServerConfigImpl("8080/tcp", "http", "/api", INTERNAL_SERVER_ATTRIBUTE_MAP); Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of("http-server", httpServerConfig); serverExposer.expose(serversToExpose); assertThatInternalServerIsExposed( MACHINE_NAME, "http-server", "tcp", 8080, new ServerConfigImpl(httpServerConfig).withAttributes(INTERNAL_SERVER_ATTRIBUTE_MAP)); }
@Test public void shouldExposeInternalAndExternalAndSecureServers() throws Exception { ServerConfigImpl secureServerConfig = new ServerConfigImpl("8282/tcp", "http", "/api", SECURE_SERVER_ATTRIBUTE_MAP); ServerConfigImpl internalServerConfig = new ServerConfigImpl("8080/tcp", "http", "/api", INTERNAL_SERVER_ATTRIBUTE_MAP); ServerConfigImpl externalServerConfig = new ServerConfigImpl("9090/tcp", "http", "/api", ATTRIBUTES_MAP); Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of( "int-server", internalServerConfig, "ext-server", externalServerConfig, "secure-server", secureServerConfig); serverExposer.expose(serversToExpose); assertThatInternalServerIsExposed( MACHINE_NAME, "int-server", "tcp", 8080, new ServerConfigImpl(internalServerConfig)); assertThatExternalServerIsExposed( MACHINE_NAME, "tcp", 9090, "ext-server", new ServerConfigImpl(externalServerConfig)); assertThatSecureServerIsExposed( MACHINE_NAME, "tcp", 8282, "secure-server", new ServerConfigImpl(secureServerConfig)); }
@Test public void shouldCreateIngressPerUniqueServerWithTheSamePort() throws Exception { ServerConfigImpl httpServerConfig = new ServerConfigImpl("8080/tcp", "http", "/api", UNIQUE_SERVER_ATTRIBUTES); ServerConfigImpl wsServerConfig = new ServerConfigImpl("8080/tcp", "ws", "/connect", UNIQUE_SERVER_ATTRIBUTES); ServicePort servicePort = new ServicePortBuilder() .withName("server-8080") .withPort(8080) .withProtocol("TCP") .withTargetPort(new IntOrString(8080)) .build(); Map<String, ServerConfig> serversToExpose = ImmutableMap.of( "http-server", httpServerConfig, "ws-server", wsServerConfig); serverExposer.expose(serversToExpose); assertThatExternalServerIsExposed( MACHINE_NAME, "tcp", 8080, "http-server", new ServerConfigImpl(httpServerConfig).withAttributes(UNIQUE_SERVER_ATTRIBUTES)); assertThatExternalServerIsExposed( MACHINE_NAME, "tcp", 8080, "ws-server", new ServerConfigImpl(wsServerConfig).withAttributes(UNIQUE_SERVER_ATTRIBUTES)); }
@Test public void shouldCreateIngressForServerWhenTwoServersHasTheSamePort() throws InfrastructureException { 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); serverExposer.expose(serversToExpose); assertThatExternalServersAreExposed( MACHINE_NAME, "tcp", 8080, ImmutableMap.of( "http-server", new ServerConfigImpl(httpServerConfig).withAttributes(ATTRIBUTES_MAP), "ws-server", new ServerConfigImpl(wsServerConfig).withAttributes(ATTRIBUTES_MAP))); }
@Test public void testCreateExtraServiceForDiscoverableServerConfig() throws InfrastructureException { ServerConfigImpl httpServerConfig = new ServerConfigImpl( "8080/tcp", "http", "/api", ImmutableMap.of(SERVER_NAME_ATTRIBUTE, "hello", DISCOVERABLE_SERVER_ATTRIBUTE, "true")); Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of("http-server", httpServerConfig); serverExposer.expose(serversToExpose); assertEquals(kubernetesEnvironment.getServices().size(), 2); assertTrue(kubernetesEnvironment.getServices().containsKey("hello")); assertEquals(kubernetesEnvironment.getServices().get("hello").getMetadata().getName(), "hello"); }
@Test public void testDiscoverableServerConfigWithoutNameAttributeIsNotProvisioned() throws InfrastructureException { ServerConfigImpl httpServerConfig = new ServerConfigImpl( "8080/tcp", "http", "/api", ImmutableMap.of(DISCOVERABLE_SERVER_ATTRIBUTE, "true")); Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of("http-server", httpServerConfig); serverExposer.expose(serversToExpose); assertEquals(kubernetesEnvironment.getServices().size(), 1); assertFalse(kubernetesEnvironment.getServices().containsKey("hello")); }
@Test public void testExposeUniqueSecureServersWithOnlyMatchingServers() throws InfrastructureException { 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); serverExposer.expose(serversToExpose); 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); } }