PreviewUrlExposer { public void expose(T env) throws InternalInfrastructureException { List<CommandImpl> previewUrlCommands = env.getCommands() .stream() .filter(c -> c.getPreviewUrl() != null) .collect(Collectors.toList()); List<ServicePort> portsToProvision = new ArrayList<>(); for (CommandImpl command : previewUrlCommands) { int port = command.getPreviewUrl().getPort(); Optional<Service> foundService = Services.findServiceWithPort(env.getServices().values(), port); if (foundService.isPresent()) { if (!hasMatchingEndpoint(env, foundService.get(), port)) { ServicePort servicePort = Services.findPort(foundService.get(), port) .orElseThrow( () -> new InternalInfrastructureException( String.format( "Port '%d' in service '%s' not found. This is not expected, please report a bug!", port, foundService.get().getMetadata().getName()))); String serviceName = foundService.get().getMetadata().getName(); externalServerExposer.expose( env, null, serviceName, serviceName, servicePort, Collections.emptyMap()); } } else { portsToProvision.add(createServicePort(port)); } } if (!portsToProvision.isEmpty()) { String serverName = generate(SERVER_PREFIX, SERVER_UNIQUE_PART_SIZE) + "-previewUrl"; Service service = new ServerServiceBuilder().withName(serverName).withPorts(portsToProvision).build(); env.getServices().put(serverName, service); portsToProvision.forEach( port -> externalServerExposer.expose( env, null, service.getMetadata().getName(), service.getMetadata().getName(), port, Collections.emptyMap())); } } @Inject  PreviewUrlExposer(ExternalServerExposerProvider<T> externalServerExposer); @VisibleForTesting protected  PreviewUrlExposer(ExternalServerExposer<T> externalServerExposer); void expose(T env);  }
@Test public void shouldDoNothingWhenNoCommandsDefined() throws InternalInfrastructureException { KubernetesEnvironment env = KubernetesEnvironment.builder().build(); previewUrlExposer.expose(env); assertTrue(env.getCommands().isEmpty()); assertTrue(env.getServices().isEmpty()); assertTrue(env.getIngresses().isEmpty()); }
@Test public void shouldDoNothingWhenNoCommandWithPreviewUrlDefined() throws InternalInfrastructureException { CommandImpl command = new CommandImpl("a", "a", "a"); KubernetesEnvironment env = KubernetesEnvironment.builder() .setCommands(singletonList(new CommandImpl(command))) .build(); previewUrlExposer.expose(env); assertEquals(env.getCommands().get(0), command); assertTrue(env.getServices().isEmpty()); assertTrue(env.getIngresses().isEmpty()); }
@Test public void shouldNotProvisionWhenServiceAndIngressFound() throws InternalInfrastructureException { final int PORT = 8080; final String SERVER_PORT_NAME = "server-" + PORT; CommandImpl command = new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap()); Service service = new Service(); ObjectMeta serviceMeta = new ObjectMeta(); serviceMeta.setName("servicename"); service.setMetadata(serviceMeta); ServiceSpec serviceSpec = new ServiceSpec(); serviceSpec.setPorts( singletonList(new ServicePort(SERVER_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT)))); service.setSpec(serviceSpec); Ingress ingress = new Ingress(); ObjectMeta ingressMeta = new ObjectMeta(); ingressMeta.setName("ingressname"); ingress.setMetadata(ingressMeta); IngressSpec ingressSpec = new IngressSpec(); IngressRule ingressRule = new IngressRule(); ingressRule.setHost("ingresshost"); IngressBackend ingressBackend = new IngressBackend("servicename", new IntOrString(SERVER_PORT_NAME)); ingressRule.setHttp( new HTTPIngressRuleValue(singletonList(new HTTPIngressPath(ingressBackend, null)))); ingressSpec.setRules(singletonList(ingressRule)); ingress.setSpec(ingressSpec); Map<String, Service> services = new HashMap<>(); services.put("servicename", service); Map<String, Ingress> ingresses = new HashMap<>(); ingresses.put("ingressname", ingress); KubernetesEnvironment env = KubernetesEnvironment.builder() .setCommands(singletonList(new CommandImpl(command))) .setServices(services) .setIngresses(ingresses) .build(); assertEquals(env.getIngresses().size(), 1); previewUrlExposer.expose(env); assertEquals(env.getIngresses().size(), 1); }
@Test public void shouldProvisionIngressWhenNotFound() 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; final String SERVICE_NAME = "servicename"; CommandImpl command = new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap()); Service service = new Service(); ObjectMeta serviceMeta = new ObjectMeta(); serviceMeta.setName(SERVICE_NAME); service.setMetadata(serviceMeta); ServiceSpec serviceSpec = new ServiceSpec(); serviceSpec.setPorts( singletonList(new ServicePort(SERVER_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT)))); service.setSpec(serviceSpec); Map<String, Service> services = new HashMap<>(); services.put(SERVICE_NAME, service); KubernetesEnvironment env = KubernetesEnvironment.builder() .setCommands(singletonList(new CommandImpl(command))) .setServices(services) .setIngresses(new HashMap<>()) .build(); previewUrlExposer.expose(env); assertEquals(env.getIngresses().size(), 1); Ingress provisionedIngress = env.getIngresses().values().iterator().next(); IngressBackend provisionedIngressBackend = provisionedIngress.getSpec().getRules().get(0).getHttp().getPaths().get(0).getBackend(); assertEquals(provisionedIngressBackend.getServicePort().getStrVal(), SERVER_PORT_NAME); assertEquals(provisionedIngressBackend.getServiceName(), SERVICE_NAME); }
@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.getServicePort().getStrVal(), SERVER_PORT_NAME); assertEquals( provisionedIngressBackend.getServiceName(), provisionedService.getMetadata().getName()); }