SidecarServicesProvisioner { public void provision(KubernetesEnvironment kubernetesEnvironment) throws InfrastructureException { for (ChePluginEndpoint endpoint : endpoints) { if (!isDiscoverable(endpoint)) { continue; } String serviceName = endpoint.getName(); Service service = createService(serviceName, podName, endpoint.getTargetPort()); Map<String, Service> services = kubernetesEnvironment.getServices(); if (!services.containsKey(serviceName)) { services.put(serviceName, service); } else { throw new InfrastructureException( format( "Applying of sidecar tooling failed. Kubernetes service with name '%s' already exists in the workspace environment.", serviceName)); } } } SidecarServicesProvisioner(List<ChePluginEndpoint> containerEndpoints, String podName); void provision(KubernetesEnvironment kubernetesEnvironment);  }
@Test public void shouldAddServiceForEachEndpoint() throws Exception { List<ChePluginEndpoint> actualEndpoints = asList( new ChePluginEndpoint().name("testE1").targetPort(8080), new ChePluginEndpoint().name("testE2").targetPort(10000)); endpoints.addAll(actualEndpoints); KubernetesEnvironment kubernetesEnvironment = KubernetesEnvironment.builder().build(); provisioner.provision(kubernetesEnvironment); assertEquals(kubernetesEnvironment.getServices(), toK8sServices(actualEndpoints)); }
@Test public void shouldNotAddServiceForNotdDiscoverableEndpoint() throws Exception { List<ChePluginEndpoint> actualEndpoints = asList( new ChePluginEndpoint().name("testE1").targetPort(8080), new ChePluginEndpoint() .name("testE2") .targetPort(10000) .attributes(ImmutableMap.of("discoverable", "false"))); endpoints.addAll(actualEndpoints); KubernetesEnvironment kubernetesEnvironment = KubernetesEnvironment.builder().build(); provisioner.provision(kubernetesEnvironment); assertEquals( kubernetesEnvironment.getServices(), toK8sServices(ImmutableList.of(new ChePluginEndpoint().name("testE1").targetPort(8080)))); }
@Test( expectedExceptions = InfrastructureException.class, expectedExceptionsMessageRegExp = "Applying of sidecar tooling failed. Kubernetes service with name '" + CONFLICTING_SERVICE_NAME + "' already exists in the workspace environment.") public void shouldNotDuplicateServicesWhenThereAreConflictingEndpoints() throws Exception { List<ChePluginEndpoint> actualEndpoints = asList( new ChePluginEndpoint().name(CONFLICTING_SERVICE_NAME).targetPort(8080), new ChePluginEndpoint().name(CONFLICTING_SERVICE_NAME).targetPort(10000)); endpoints.addAll(actualEndpoints); KubernetesEnvironment kubernetesEnvironment = KubernetesEnvironment.builder().build(); provisioner.provision(kubernetesEnvironment); }
@Test( expectedExceptions = InfrastructureException.class, expectedExceptionsMessageRegExp = "Applying of sidecar tooling failed. Kubernetes service with name '" + CONFLICTING_SERVICE_NAME + "' already exists in the workspace environment.") public void shouldNotDuplicateServicesWhenThereIsConflictingServiceInK8sEnv() throws Exception { List<ChePluginEndpoint> actualEndpoints = singletonList(new ChePluginEndpoint().name(CONFLICTING_SERVICE_NAME).targetPort(8080)); endpoints.addAll(actualEndpoints); KubernetesEnvironment kubernetesEnvironment = KubernetesEnvironment.builder() .setServices(singletonMap(CONFLICTING_SERVICE_NAME, new Service())) .build(); provisioner.provision(kubernetesEnvironment); }