KubernetesPluginsToolingApplier implements ChePluginsApplier { @Override public void apply( RuntimeIdentity runtimeIdentity, InternalEnvironment internalEnvironment, Collection<ChePlugin> chePlugins) throws InfrastructureException { if (chePlugins.isEmpty()) { return; } KubernetesEnvironment k8sEnv = (KubernetesEnvironment) internalEnvironment; Map<String, PodData> pods = k8sEnv.getPodsData(); switch (pods.size()) { case 0: addToolingPod(k8sEnv); pods = k8sEnv.getPodsData(); break; case 1: break; default: throw new InfrastructureException( "Che plugins tooling configuration can be applied to a workspace with one pod only"); } PodData pod = pods.values().iterator().next(); CommandsResolver commandsResolver = new CommandsResolver(k8sEnv); for (ChePlugin chePlugin : chePlugins) { Map<String, ComponentImpl> devfilePlugins = k8sEnv .getDevfile() .getComponents() .stream() .filter(c -> c.getType().equals("cheEditor") || c.getType().equals("chePlugin")) .collect(Collectors.toMap(ComponentImpl::getId, Function.identity())); if (!devfilePlugins.containsKey(chePlugin.getId())) { throw new InfrastructureException( String.format( "The downloaded plugin '%s' configuration does not have the " + "corresponding component in devfile. Devfile contains the following cheEditor/chePlugins: %s", chePlugin.getId(), devfilePlugins.keySet())); } ComponentImpl pluginRelatedComponent = devfilePlugins.get(chePlugin.getId()); for (CheContainer container : chePlugin.getInitContainers()) { Container k8sInitContainer = toK8sContainer(container); envVars.apply(k8sInitContainer, pluginRelatedComponent.getEnv()); chePluginsVolumeApplier.applyVolumes(pod, k8sInitContainer, container.getVolumes(), k8sEnv); pod.getSpec().getInitContainers().add(k8sInitContainer); } Collection<CommandImpl> pluginRelatedCommands = commandsResolver.resolve(chePlugin); for (CheContainer container : chePlugin.getContainers()) { addSidecar( pod, container, chePlugin, k8sEnv, pluginRelatedCommands, pluginRelatedComponent, runtimeIdentity); } } chePlugins.forEach(chePlugin -> populateWorkspaceEnvVars(chePlugin, k8sEnv)); } @Inject  KubernetesPluginsToolingApplier(
      @Named("che.workspace.sidecar.image_pull_policy") String sidecarImagePullPolicy,
      @Named("che.workspace.sidecar.default_memory_limit_mb") long defaultSidecarMemoryLimitMB,
      @Named("che.workspace.sidecar.default_memory_request_mb") long defaultSidecarMemoryRequestMB,
      @Named("che.workspace.sidecar.default_cpu_limit_cores") String defaultSidecarCpuLimitCores,
      @Named("che.workspace.sidecar.default_cpu_request_cores")
          String defaultSidecarCpuRequestCores,
      @Named("che.agents.auth_enabled") boolean isAuthEnabled,
      ProjectsRootEnvVariableProvider projectsRootEnvVariableProvider,
      ChePluginsVolumeApplier chePluginsVolumeApplier,
      EnvVars envVars); @Override void apply(
      RuntimeIdentity runtimeIdentity,
      InternalEnvironment internalEnvironment,
      Collection<ChePlugin> chePlugins);  }
@Test public void shouldProvisionPluginsCommandsToEnvironment() throws Exception { Command pluginCommand = new Command() .name("test-command") .workingDir("~") .command(Arrays.asList("./build.sh", "--no-pull")); applier.apply( runtimeIdentity, internalEnvironment, singletonList(createChePlugin(createContainer("container", pluginCommand)))); List<CommandImpl> envCommands = internalEnvironment.getCommands(); assertEquals(envCommands.size(), 1); CommandImpl envCommand = envCommands.get(0); assertEquals(envCommand.getName(), pluginCommand.getName()); assertEquals(envCommand.getCommandLine(), String.join(" ", pluginCommand.getCommand())); assertEquals(envCommand.getType(), "custom"); assertEquals( envCommand.getAttributes().get(WORKING_DIRECTORY_ATTRIBUTE), pluginCommand.getWorkingDir()); validateContainerNameName(envCommand.getAttributes().get(MACHINE_NAME_ATTRIBUTE), "container"); }
@Test public void shouldProvisionApplyEnvironmentVariableToContainersAndInitContainersOfPlugin() throws Exception { CheContainer container = new CheContainer(); container.setName("container"); CheContainer initContainer = new CheContainer(); initContainer.setName("initContainer"); ChePlugin chePlugin = createChePlugin( "publisher/id/1.0.0", singletonList(container), singletonList(initContainer)); ComponentImpl component = internalEnvironment.getDevfile().getComponents().get(0); component.getEnv().add(new EnvImpl("TEST", "VALUE")); ArgumentCaptor<Container> containerArgumentCaptor = ArgumentCaptor.forClass(Container.class); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); verify(envVars, times(2)).apply(containerArgumentCaptor.capture(), eq(component.getEnv())); List<Container> containers = containerArgumentCaptor.getAllValues(); assertTrue(containers.get(0).getName().startsWith("initcontainer")); assertTrue(containers.get(1).getName().startsWith("container")); }
@Test( expectedExceptions = InfrastructureException.class, expectedExceptionsMessageRegExp = "The downloaded plugin 'publisher/id/version' configuration does " + "not have the corresponding component in devfile. Devfile contains the following " + "cheEditor/chePlugins: \\[publisher/editor/1, publisher/plugin/1\\]") public void shouldThrowExceptionWhenDownloadedPluginIsNotMatchedWitComponent() throws Exception { ChePlugin chePlugin = createChePlugin("publisher/id/version"); internalEnvironment.getDevfile().getComponents().clear(); internalEnvironment .getDevfile() .getComponents() .add(new ComponentImpl("cheEditor", "publisher/editor/1")); internalEnvironment .getDevfile() .getComponents() .add(new ComponentImpl("chePlugin", "publisher/plugin/1")); internalEnvironment.getDevfile().getComponents().add(new ComponentImpl("k8s", null)); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); }
@Test public void shouldResolveMachineNameForCommandsInEnvironment() throws Exception { ChePlugin chePlugin = createChePlugin(createContainer("container")); String pluginRef = chePlugin.getId(); CommandImpl pluginCommand = new CommandImpl("test-command", "echo Hello World!", "custom"); pluginCommand.getAttributes().put("plugin", pluginRef); internalEnvironment.getCommands().add(pluginCommand); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); List<CommandImpl> envCommands = internalEnvironment.getCommands(); assertEquals(envCommands.size(), 1); CommandImpl envCommand = envCommands.get(0); assertEquals(envCommand.getName(), pluginCommand.getName()); assertEquals(envCommand.getType(), pluginCommand.getType()); assertEquals(envCommand.getCommandLine(), pluginCommand.getCommandLine()); assertEquals(envCommand.getAttributes().get("plugin"), pluginRef); validateContainerNameName(envCommand.getAttributes().get(MACHINE_NAME_ATTRIBUTE), "container"); }
@Test public void shouldFillInWarningIfChePluginDoesNotHaveAnyContainersButThereAreRelatedCommands() throws Exception { ChePlugin chePlugin = createChePlugin("somePublisher/custom-name/0.0.3"); String pluginRef = chePlugin.getId(); CommandImpl pluginCommand = new CommandImpl("test-command", "echo Hello World!", "custom"); pluginCommand.getAttributes().put("plugin", pluginRef); internalEnvironment.getCommands().add(pluginCommand); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); List<Warning> envWarnings = internalEnvironment.getWarnings(); assertEquals(envWarnings.size(), 1); Warning warning = envWarnings.get(0); assertEquals( warning.getCode(), Warnings.COMMAND_IS_CONFIGURED_IN_PLUGIN_WITHOUT_CONTAINERS_WARNING_CODE); assertEquals( warning.getMessage(), "There are configured commands for plugin 'somePublisher/custom-name/0.0.3' that doesn't have any containers"); }
@Test public void shouldNotFillInWarningIfChePluginDoesNotHaveAnyContainersAndThereAreNotRelatedCommands() throws Exception { ChePlugin chePlugin = createChePlugin(); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); assertTrue(internalEnvironment.getWarnings().isEmpty()); }
@Test public void shouldFillInWarningIfChePluginHasMultiplyContainersButThereAreRelatedCommands() throws Exception { ChePlugin chePlugin = createChePlugin(createContainer(), createContainer()); String pluginRef = chePlugin.getId(); CommandImpl pluginCommand = new CommandImpl("test-command", "echo Hello World!", "custom"); pluginCommand.getAttributes().put("plugin", pluginRef); internalEnvironment.getCommands().add(pluginCommand); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); List<Warning> envWarnings = internalEnvironment.getWarnings(); assertEquals(envWarnings.size(), 1); Warning warning = envWarnings.get(0); assertEquals( warning.getCode(), Warnings.COMMAND_IS_CONFIGURED_IN_PLUGIN_WITH_MULTIPLY_CONTAINERS_WARNING_CODE); assertEquals( warning.getMessage(), "There are configured commands for plugin '" + pluginRef + "' that has multiply containers. Commands will be configured to be run in first container"); }
@Test public void shouldNotFillInWarningIfChePluginHasMultiplyContainersAndThereAreNotRelatedCommands() throws Exception { ChePlugin chePlugin = createChePlugin(createContainer(), createContainer()); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); assertTrue(internalEnvironment.getWarnings().isEmpty()); }
@Test public void shouldUseContainerNameForMachinesName() throws Exception { internalEnvironment.getMachines().clear(); ChePlugin chePlugin = createChePlugin("publisher/plugin1/0.2.1", createContainer("container1")); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); Map<String, InternalMachineConfig> machines = internalEnvironment.getMachines(); assertEquals(machines.size(), 1); validateContainerNameName(machines.keySet().iterator().next(), "container1"); }
@Test( expectedExceptions = InfrastructureException.class, expectedExceptionsMessageRegExp = "Che plugins tooling configuration can be applied to a workspace with one pod only") public void throwsExceptionWhenTheNumberOfPodsIsNot1() throws Exception { PodData podData = new PodData(podSpec, meta); when(internalEnvironment.getPodsData()).thenReturn(of("pod1", podData, "pod2", podData)); applier.apply(runtimeIdentity, internalEnvironment, singletonList(createChePlugin())); }
@Test public void addToolingContainerToAPod() throws Exception { applier.apply(runtimeIdentity, internalEnvironment, singletonList(createChePlugin())); verifyPodAndContainersNumber(2); Container toolingContainer = getOneAndOnlyNonUserContainer(internalEnvironment); verifyContainer(toolingContainer); }
@Test public void addToolingInitContainerToAPod() throws Exception { lenient().when(podSpec.getInitContainers()).thenReturn(new ArrayList<>()); ChePlugin chePlugin = createChePlugin(); chePlugin.setInitContainers(singletonList(createContainer())); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); verifyPodAndInitContainersNumber(1); Container toolingInitContainer = getOnlyOneInitContainerFromPod(internalEnvironment); verifyContainer(toolingInitContainer); }
@Test public void addToolingInitContainerWithCommand() throws InfrastructureException { List<String> command = Arrays.asList("cp", "-rf", "test-file", "/some-volume/test"); lenient().when(podSpec.getInitContainers()).thenReturn(new ArrayList<>()); ChePlugin chePlugin = createChePlugin(); List<CheContainer> initContainers = singletonList(createContainer(command, null)); chePlugin.setInitContainers(initContainers); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); verifyPodAndInitContainersNumber(1); Container toolingInitContainer = getOnlyOneInitContainerFromPod(internalEnvironment); verifyContainer(toolingInitContainer); assertEquals(toolingInitContainer.getCommand(), command); }
@Test public void addToolingInitContainerWithArgs() throws InfrastructureException { List<String> args = Arrays.asList("cp", "-rf", "test-file", "/some-volume/test"); lenient().when(podSpec.getInitContainers()).thenReturn(new ArrayList<>()); ChePlugin chePlugin = createChePlugin(); List<CheContainer> initContainers = singletonList(createContainer(null, args)); chePlugin.setInitContainers(initContainers); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); verifyPodAndInitContainersNumber(1); Container toolingInitContainer = getOnlyOneInitContainerFromPod(internalEnvironment); verifyContainer(toolingInitContainer); assertEquals(toolingInitContainer.getArgs(), args); }
@Test public void addToolingInitContainerWithCommandAndArgs() throws InfrastructureException { List<String> command = singletonList("cp"); List<String> args = Arrays.asList("-rf", "test-file", "/some-volume/test"); lenient().when(podSpec.getInitContainers()).thenReturn(new ArrayList<>()); ChePlugin chePlugin = createChePlugin(); List<CheContainer> initContainers = singletonList(createContainer(command, args)); chePlugin.setInitContainers(initContainers); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); verifyPodAndInitContainersNumber(1); Container toolingInitContainer = getOnlyOneInitContainerFromPod(internalEnvironment); verifyContainer(toolingInitContainer); assertEquals(toolingInitContainer.getCommand(), command); assertEquals(toolingInitContainer.getArgs(), args); }
@Test public void addToolingContainerWithCommand() throws InfrastructureException { List<String> command = Arrays.asList("tail", "-f", "/dev/null"); lenient().when(podSpec.getContainers()).thenReturn(new ArrayList<>()); ChePlugin chePlugin = createChePlugin(); List<CheContainer> containers = singletonList(createContainer(command, null)); chePlugin.setContainers(containers); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); verifyPodAndContainersNumber(1); Container toolingContainer = getOneAndOnlyNonUserContainer(internalEnvironment); verifyContainer(toolingContainer); assertEquals(toolingContainer.getCommand(), command); }
@Test public void addToolingContainerWithArgs() throws InfrastructureException { List<String> args = Arrays.asList("tail", "-f", "/dev/null"); lenient().when(podSpec.getContainers()).thenReturn(new ArrayList<>()); ChePlugin chePlugin = createChePlugin(); List<CheContainer> containers = singletonList(createContainer(null, args)); chePlugin.setContainers(containers); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); verifyPodAndContainersNumber(1); Container toolingContainer = getOneAndOnlyNonUserContainer(internalEnvironment); verifyContainer(toolingContainer); assertEquals(toolingContainer.getArgs(), args); }
@Test public void addToolingContainerWithCommandAndArgs() throws InfrastructureException { List<String> command = singletonList("tail"); List<String> args = Arrays.asList("-f", "/dev/null"); lenient().when(podSpec.getContainers()).thenReturn(new ArrayList<>()); ChePlugin chePlugin = createChePlugin(); List<CheContainer> containers = singletonList(createContainer(command, args)); chePlugin.setContainers(containers); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); verifyPodAndContainersNumber(1); Container toolingContainer = getOneAndOnlyNonUserContainer(internalEnvironment); verifyContainer(toolingContainer); assertEquals(toolingContainer.getCommand(), command); assertEquals(toolingContainer.getArgs(), args); }
@Test public void createsPodAndAddToolingIfNoPodIsPresent() throws Exception { internalEnvironment = spy(KubernetesEnvironment.builder().build()); internalEnvironment.setDevfile(new DevfileImpl()); Map<String, InternalMachineConfig> machines = new HashMap<>(); machines.put(USER_MACHINE_NAME, userMachineConfig); internalEnvironment.getMachines().putAll(machines); applier.apply(runtimeIdentity, internalEnvironment, singletonList(createChePlugin())); verifyPodAndContainersNumber(1); Container toolingContainer = getOneAndOnlyNonUserContainer(internalEnvironment); verifyContainer(toolingContainer); }
@Test public void canAddMultipleToolingContainersToAPodFromOnePlugin() throws Exception { applier.apply( runtimeIdentity, internalEnvironment, singletonList(createChePlugin(createContainer(), createContainer()))); verifyPodAndContainersNumber(3); List<Container> nonUserContainers = getNonUserContainers(internalEnvironment); verifyContainers(nonUserContainers); }
@Test public void canAddMultipleToolingContainersToAPodFromSeveralPlugins() throws Exception { applier.apply( runtimeIdentity, internalEnvironment, ImmutableList.of(createChePlugin(), createChePlugin())); verifyPodAndContainersNumber(3); List<Container> nonUserContainers = getNonUserContainers(internalEnvironment); verifyContainers(nonUserContainers); }
@Test public void applyPluginContainerWithOneVolume() throws InfrastructureException { lenient().when(podSpec.getContainers()).thenReturn(new ArrayList<>()); ChePlugin chePlugin = createChePlugin(); CheContainer cheContainer = chePlugin.getContainers().get(0); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); verifyPodAndContainersNumber(1); Container container = getOneAndOnlyNonUserContainer(internalEnvironment); verifyContainer(container); verify(chePluginsVolumeApplier) .applyVolumes( any(PodData.class), eq(container), eq(cheContainer.getVolumes()), eq(internalEnvironment)); }
@Test public void applyPluginInitContainerWithOneVolume() throws InfrastructureException { lenient().when(podSpec.getInitContainers()).thenReturn(new ArrayList<>()); ChePlugin chePlugin = createChePlugin(); CheContainer initContainer = createContainer(); chePlugin.setInitContainers(singletonList(initContainer)); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); verifyPodAndInitContainersNumber(1); Container toolingInitContainer = getOnlyOneInitContainerFromPod(internalEnvironment); verifyContainer(toolingInitContainer); verify(chePluginsVolumeApplier) .applyVolumes( any(PodData.class), eq(toolingInitContainer), eq(initContainer.getVolumes()), eq(internalEnvironment)); }
@Test public void addsMachinesWithVolumesToAllToolingContainer() throws Exception { ChePlugin chePluginWithNonDefaultVolume = createChePlugin(); String anotherVolumeName = VOLUME_NAME + "1"; String anotherVolumeMountPath = VOLUME_MOUNT_PATH + "/something"; List<Volume> volumes = singletonList(new Volume().name(anotherVolumeName).mountPath(anotherVolumeMountPath)); CheContainer toolingContainer = chePluginWithNonDefaultVolume.getContainers().get(0); toolingContainer.setVolumes(volumes); ChePlugin chePlugin = createChePlugin(); applier.apply( runtimeIdentity, internalEnvironment, asList(chePlugin, chePluginWithNonDefaultVolume)); Collection<InternalMachineConfig> machineConfigs = getNonUserMachines(internalEnvironment); assertEquals(machineConfigs.size(), 2); verify(chePluginsVolumeApplier) .applyVolumes( any(PodData.class), any(Container.class), eq(chePlugin.getContainers().get(0).getVolumes()), eq(internalEnvironment)); }
@Test public void addsMachineWithVolumeFromChePlugin() throws Exception { ChePlugin chePluginWithNoVolume = createChePlugin(); chePluginWithNoVolume.getContainers().get(0).setVolumes(emptyList()); applier.apply( runtimeIdentity, internalEnvironment, asList(createChePlugin(), chePluginWithNoVolume)); Collection<InternalMachineConfig> machineConfigs = getNonUserMachines(internalEnvironment); assertEquals(machineConfigs.size(), 2); verifyNumberOfMachinesWithSpecificNumberOfVolumes(machineConfigs, 2, 0); }
@Test public void addsMachineWithServersForContainer() throws Exception { ChePlugin chePlugin = createChePlugin(); addPortToSingleContainerPlugin(chePlugin, 80, "test-port", emptyMap(), true); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); InternalMachineConfig machineConfig = getOneAndOnlyNonUserMachine(internalEnvironment); assertEquals( machineConfig.getServers(), expectedSingleServer(80, "test-port", emptyMap(), true)); }
@Test public void addsTwoServersForContainers() throws Exception { ChePlugin chePlugin = createChePlugin(); addPortToSingleContainerPlugin(chePlugin, 80, "test-port", emptyMap(), true); addPortToSingleContainerPlugin(chePlugin, 8090, "another-test-port", emptyMap(), false); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); InternalMachineConfig machineConfig = getOneAndOnlyNonUserMachine(internalEnvironment); assertEquals( machineConfig.getServers(), expectedTwoServers( 80, "test-port", emptyMap(), true, 8090, "another-test-port", emptyMap(), false)); }
@Test public void addsMachineWithServersThatUseSamePortButDifferentNames() throws Exception { ChePlugin chePlugin = createChePlugin(); addPortToSingleContainerPlugin(chePlugin, 80, "test-port/http", emptyMap(), true); addPortToSingleContainerPlugin(chePlugin, 80, "test-port/ws", emptyMap(), true); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); InternalMachineConfig machineConfig = getOneAndOnlyNonUserMachine(internalEnvironment); assertEquals( machineConfig.getServers(), expectedTwoServers( 80, "test-port/http", emptyMap(), true, 80, "test-port/ws", emptyMap(), true)); }
@Test public void addsMachineWithServersThatSetProtocolAndPath() throws Exception { ChePlugin chePlugin = createChePlugin(); addPortToSingleContainerPlugin( chePlugin, 443, "test-port", ImmutableMap.of("path", "/path/1", "protocol", "https", "attr1", "value1"), true); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); InternalMachineConfig machineConfig = getOneAndOnlyNonUserMachine(internalEnvironment); assertEquals( machineConfig.getServers(), expectedSingleServer( 443, "test-port", singletonMap("attr1", "value1"), true, "https", "/path/1")); }
@Test public void setsSourceAndPluginAttributeForMachineAssociatedWithSidecar() throws Exception { ChePlugin chePlugin = createChePlugin(); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); InternalMachineConfig machineConfig = getOneAndOnlyNonUserMachine(internalEnvironment); Map<String, String> attributes = machineConfig.getAttributes(); assertEquals(attributes.get(CONTAINER_SOURCE_ATTRIBUTE), TOOL_CONTAINER_SOURCE); assertEquals(attributes.get(PLUGIN_MACHINE_ATTRIBUTE), chePlugin.getId()); }
@Test public void setsDefaultMemoryLimitForMachineAssociatedWithContainer() throws Exception { applier.apply(runtimeIdentity, internalEnvironment, singletonList(createChePlugin())); InternalMachineConfig machineConfig = getOneAndOnlyNonUserMachine(internalEnvironment); String memoryLimitAttribute = machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE); assertEquals(memoryLimitAttribute, Integer.toString(MEMORY_LIMIT_MB * 1024 * 1024)); }
@Test public void shouldExposeChePluginEndpointsPortsInToolingContainer() throws Exception { ChePluginEndpoint endpoint1 = new ChePluginEndpoint().name(CHE_PLUGIN_ENDPOINT_NAME).targetPort(101010).setPublic(true); ChePluginEndpoint endpoint2 = new ChePluginEndpoint().name("test-endpoint-2").targetPort(2020).setPublic(false); CheContainerPort cheContainerPort1 = new CheContainerPort().exposedPort(101010); CheContainerPort cheContainerPort2 = new CheContainerPort().exposedPort(2020); ChePlugin chePlugin = createChePlugin(); chePlugin.setEndpoints(asList(endpoint1, endpoint2)); chePlugin.getContainers().get(0).setPorts(asList(cheContainerPort1, cheContainerPort2)); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); Container container = getOneAndOnlyNonUserContainer(internalEnvironment); verifyPortsExposed(container, 101010, 2020); }
@Test public void shouldNotExposeChePluginPortIfThereIsNoEndpoint() throws Exception { ChePluginEndpoint endpoint1 = new ChePluginEndpoint().name(CHE_PLUGIN_ENDPOINT_NAME).targetPort(101010).setPublic(true); CheContainerPort cheContainerPort1 = new CheContainerPort().exposedPort(101010); CheContainerPort cheContainerPort2 = new CheContainerPort().exposedPort(2020); ChePlugin chePlugin = createChePlugin(); chePlugin.setEndpoints(singletonList(endpoint1)); chePlugin.getContainers().get(0).setPorts(asList(cheContainerPort1, cheContainerPort2)); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); Container container = getOneAndOnlyNonUserContainer(internalEnvironment); verifyPortsExposed(container, 101010); }
@Test public void shouldAddK8sServicesForChePluginEndpoints() throws Exception { ChePluginEndpoint endpoint1 = new ChePluginEndpoint().name(CHE_PLUGIN_ENDPOINT_NAME).targetPort(101010).setPublic(true); ChePluginEndpoint endpoint2 = new ChePluginEndpoint().name("test-endpoint-2").targetPort(2020).setPublic(false); CheContainerPort cheContainerPort1 = new CheContainerPort().exposedPort(101010); CheContainerPort cheContainerPort2 = new CheContainerPort().exposedPort(2020); ChePlugin chePlugin = createChePlugin(); chePlugin.setEndpoints(asList(endpoint1, endpoint2)); chePlugin.getContainers().get(0).setPorts(asList(cheContainerPort1, cheContainerPort2)); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); verifyK8sServices(internalEnvironment, endpoint1, endpoint2); }
@Test( expectedExceptions = InfrastructureException.class, expectedExceptionsMessageRegExp = "Applying of sidecar tooling failed. Kubernetes service with name '" + CHE_PLUGIN_ENDPOINT_NAME + "' already exists in the workspace environment.") public void throwsExceptionOnAddingChePluginEndpointServiceIfServiceExists() throws Exception { ChePluginEndpoint endpoint1 = new ChePluginEndpoint().name(CHE_PLUGIN_ENDPOINT_NAME).targetPort(101010).setPublic(true); CheContainerPort cheContainerPort1 = new CheContainerPort().exposedPort(101010); ChePlugin chePlugin = createChePlugin(); chePlugin.setEndpoints(singletonList(endpoint1)); chePlugin.getContainers().get(0).setPorts(singletonList(cheContainerPort1)); internalEnvironment.getServices().put(CHE_PLUGIN_ENDPOINT_NAME, new Service()); applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin)); }
@Test public void shouldNotSetJWTServerExposerAttributeIfAuthEnabledButAttributeIsPresent() throws Exception { applier = new KubernetesPluginsToolingApplier( TEST_IMAGE_POLICY, MEMORY_LIMIT_MB, MEMORY_REQUEST_MB, CPU_LIMIT, CPU_REQUEST, true, projectsRootEnvVariableProvider, chePluginsVolumeApplier, envVars); internalEnvironment.getAttributes().put(SECURE_EXPOSER_IMPL_PROPERTY, "somethingElse"); applier.apply(runtimeIdentity, internalEnvironment, singletonList(createChePlugin())); assertEquals( internalEnvironment.getAttributes().get(SECURE_EXPOSER_IMPL_PROPERTY), "somethingElse"); }
@Test public void shouldSetSpecifiedImagePullPolicy() throws Exception { applier = new KubernetesPluginsToolingApplier( TEST_IMAGE_POLICY, MEMORY_LIMIT_MB, MEMORY_REQUEST_MB, CPU_LIMIT, CPU_REQUEST, true, projectsRootEnvVariableProvider, chePluginsVolumeApplier, envVars); applier.apply(runtimeIdentity, internalEnvironment, singletonList(createChePlugin())); assertEquals( internalEnvironment .getPodsCopy() .values() .iterator() .next() .getSpec() .getContainers() .get(1) .getImagePullPolicy(), TEST_IMAGE_POLICY); }
@Test public void shouldSetNullImagePullPolicyIfValueIsNotStandard() throws Exception { applier = new KubernetesPluginsToolingApplier( "None", MEMORY_LIMIT_MB, MEMORY_REQUEST_MB, CPU_LIMIT, CPU_REQUEST, true, projectsRootEnvVariableProvider, chePluginsVolumeApplier, envVars); applier.apply(runtimeIdentity, internalEnvironment, singletonList(createChePlugin())); assertNull( internalEnvironment .getPodsCopy() .values() .iterator() .next() .getSpec() .getContainers() .get(1) .getImagePullPolicy()); }
@Test public void shouldNotSetJWTServerExposerAttributeIfAuthDisabled() throws Exception { applier.apply(runtimeIdentity, internalEnvironment, singletonList(createChePlugin())); assertNull(internalEnvironment.getAttributes().get(SECURE_EXPOSER_IMPL_PROPERTY)); }