DockerimageComponentToWorkspaceApplier implements ComponentToWorkspaceApplier { @Override public void apply( WorkspaceConfigImpl workspaceConfig, ComponentImpl dockerimageComponent, FileContentProvider contentProvider) throws DevfileException { checkArgument(workspaceConfig != null, "Workspace config must not be null"); checkArgument(dockerimageComponent != null, "Component must not be null"); checkArgument( DOCKERIMAGE_COMPONENT_TYPE.equals(dockerimageComponent.getType()), format("Plugin must have `%s` type", DOCKERIMAGE_COMPONENT_TYPE)); String componentAlias = dockerimageComponent.getAlias(); String machineName = componentAlias == null ? toMachineName(dockerimageComponent.getImage()) : componentAlias; MachineConfigImpl machineConfig = createMachineConfig(dockerimageComponent, componentAlias); List<HasMetadata> componentObjects = createComponentObjects(dockerimageComponent, machineName); k8sEnvProvisioner.provision( workspaceConfig, KubernetesEnvironment.TYPE, componentObjects, ImmutableMap.of(machineName, machineConfig)); workspaceConfig .getCommands() .stream() .filter( c -> componentAlias != null && componentAlias.equals( c.getAttributes().get(Constants.COMPONENT_ALIAS_COMMAND_ATTRIBUTE))) .forEach(c -> c.getAttributes().put(MACHINE_NAME_ATTRIBUTE, machineName)); } @Inject  DockerimageComponentToWorkspaceApplier(
      @Named("che.workspace.projects.storage") String projectFolderPath,
      @Named("che.workspace.sidecar.image_pull_policy") String imagePullPolicy,
      KubernetesEnvironmentProvisioner k8sEnvProvisioner); @Override void apply(
      WorkspaceConfigImpl workspaceConfig,
      ComponentImpl dockerimageComponent,
      FileContentProvider contentProvider);  }
@Test public void shouldProvisionK8sEnvironmentWithMachineConfigAndGeneratedDeploymentForSpecifiedDockerimage() throws Exception { ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setAlias("jdk"); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); MachineConfigImpl machineConfig = machinesCaptor.getValue().get("jdk"); assertNotNull(machineConfig); List<HasMetadata> objects = objectsCaptor.getValue(); assertEquals(objects.size(), 1); assertTrue(objects.get(0) instanceof Deployment); Deployment deployment = (Deployment) objects.get(0); PodTemplateSpec podTemplate = deployment.getSpec().getTemplate(); ObjectMeta podMeta = podTemplate.getMetadata(); assertEquals(podMeta.getName(), "jdk"); Map<String, String> deploymentSelector = deployment.getSpec().getSelector().getMatchLabels(); assertFalse(deploymentSelector.isEmpty()); assertTrue(podMeta.getLabels().entrySet().containsAll(deploymentSelector.entrySet())); Container container = podTemplate.getSpec().getContainers().get(0); assertEquals(container.getName(), "jdk"); assertEquals(container.getImage(), "eclipse/ubuntu_jdk8:latest"); assertEquals(container.getImagePullPolicy(), "Always"); assertEquals(Names.machineName(podMeta, container), "jdk"); }
@Test public void shouldBeAbleToSetupSpecificImagePullPolicy() throws DevfileException { ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponentApplier = new DockerimageComponentToWorkspaceApplier(PROJECTS_MOUNT_PATH, "Never", k8sEnvProvisioner); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( any(), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); MachineConfigImpl machineConfig = machinesCaptor.getValue().get("eclipse-ubuntu_jdk8-latest"); assertNotNull(machineConfig); List<HasMetadata> objects = objectsCaptor.getValue(); assertEquals(objects.size(), 1); assertTrue(objects.get(0) instanceof Deployment); Deployment deployment = (Deployment) objects.get(0); PodTemplateSpec podTemplate = deployment.getSpec().getTemplate(); ObjectMeta podMeta = podTemplate.getMetadata(); assertEquals(podMeta.getName(), "eclipse-ubuntu_jdk8-latest"); Map<String, String> deploymentSelector = deployment.getSpec().getSelector().getMatchLabels(); assertFalse(deploymentSelector.isEmpty()); assertTrue(podMeta.getLabels().entrySet().containsAll(deploymentSelector.entrySet())); Container container = podTemplate.getSpec().getContainers().get(0); assertEquals(container.getName(), "eclipse-ubuntu_jdk8-latest"); assertEquals(container.getImagePullPolicy(), "Never"); }
@Test public void shouldProvisionK8sEnvironmentWithMachineConfigFromSpecifiedDockerimageWithoutAlias() throws Exception { ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( any(), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); MachineConfigImpl machineConfig = machinesCaptor.getValue().get("eclipse-ubuntu_jdk8-latest"); assertNotNull(machineConfig); List<HasMetadata> objects = objectsCaptor.getValue(); assertEquals(objects.size(), 1); assertTrue(objects.get(0) instanceof Deployment); Deployment deployment = (Deployment) objects.get(0); PodTemplateSpec podTemplate = deployment.getSpec().getTemplate(); ObjectMeta podMeta = podTemplate.getMetadata(); assertEquals(podMeta.getName(), "eclipse-ubuntu_jdk8-latest"); Map<String, String> deploymentSelector = deployment.getSpec().getSelector().getMatchLabels(); assertFalse(deploymentSelector.isEmpty()); assertTrue(podMeta.getLabels().entrySet().containsAll(deploymentSelector.entrySet())); Container container = podTemplate.getSpec().getContainers().get(0); assertEquals(container.getName(), "eclipse-ubuntu_jdk8-latest"); assertEquals(container.getImage(), "eclipse/ubuntu_jdk8:latest"); assertEquals( Names.machineName(podTemplate.getMetadata(), container), "eclipse-ubuntu_jdk8-latest"); }
@Test public void shouldProvisionSpecifiedEnvVars() throws Exception { ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setAlias("jdk"); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponent.setEnv(singletonList(new EnvImpl("envName", "envValue"))); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); List<HasMetadata> objects = objectsCaptor.getValue(); assertEquals(objects.size(), 1); assertTrue(objects.get(0) instanceof Deployment); Deployment deployment = (Deployment) objects.get(0); PodTemplateSpec podTemplate = deployment.getSpec().getTemplate(); assertEquals(podTemplate.getSpec().getContainers().size(), 1); Container container = podTemplate.getSpec().getContainers().get(0); int env = container.getEnv().size(); assertEquals(env, 1); EnvVar containerEnvVar = container.getEnv().get(0); assertEquals(containerEnvVar.getName(), "envName"); assertEquals(containerEnvVar.getValue(), "envValue"); }
@Test public void shouldProvisionContainerWithMemoryLimitSpecified() throws Exception { ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setAlias("jdk"); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); List<HasMetadata> objects = objectsCaptor.getValue(); assertEquals(objects.size(), 1); assertTrue(objects.get(0) instanceof Deployment); Deployment deployment = (Deployment) objects.get(0); PodTemplateSpec podTemplate = deployment.getSpec().getTemplate(); assertEquals(podTemplate.getSpec().getContainers().size(), 1); Container container = podTemplate.getSpec().getContainers().get(0); Quantity memoryLimit = container.getResources().getLimits().get("memory"); assertEquals(memoryLimit.getAmount(), "1"); assertEquals(memoryLimit.getFormat(), "G"); }
@Test public void shouldProvisionContainerWithCpuLimitsSpecified() throws Exception { ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setAlias("jdk"); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponent.setCpuRequest("1576m"); dockerimageComponent.setCpuLimit("2.22"); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); List<HasMetadata> objects = objectsCaptor.getValue(); assertEquals(objects.size(), 1); assertTrue(objects.get(0) instanceof Deployment); Deployment deployment = (Deployment) objects.get(0); PodTemplateSpec podTemplate = deployment.getSpec().getTemplate(); assertEquals(podTemplate.getSpec().getContainers().size(), 1); Container container = podTemplate.getSpec().getContainers().get(0); Quantity cpuLimit = container.getResources().getLimits().get("cpu"); Quantity cpuRequest = container.getResources().getRequests().get("cpu"); assertEquals(cpuRequest.getAmount(), "1.576"); assertEquals(cpuLimit.getAmount(), "2.22"); }
@Test public void shouldProvisionMachineConfigWithConfiguredServers() throws Exception { EndpointImpl endpoint = new EndpointImpl( "jdk-ls", 4923, ImmutableMap.of( "protocol", "http", "path", "/ls", PUBLIC_ENDPOINT_ATTRIBUTE, "false", "secure", "false")); ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setAlias("jdk"); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponent.setEndpoints(singletonList(endpoint)); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); MachineConfigImpl machineConfig = machinesCaptor.getValue().get("jdk"); assertNotNull(machineConfig); assertEquals(machineConfig.getServers().size(), 1); ServerConfigImpl serverConfig = machineConfig.getServers().get("jdk-ls"); assertEquals(serverConfig.getProtocol(), "http"); assertEquals(serverConfig.getPath(), "/ls"); assertEquals(serverConfig.getPort(), "4923"); Map<String, String> attributes = serverConfig.getAttributes(); assertEquals(attributes.get(ServerConfig.INTERNAL_SERVER_ATTRIBUTE), "true"); assertEquals(attributes.get("secure"), "false"); assertEquals(attributes.get(ServerConfig.REQUIRE_SUBDOMAIN), "true"); }
@Test public void shouldProvisionServiceForDiscoverableServer() throws Exception { EndpointImpl endpoint = new EndpointImpl( "jdk-ls", 4923, ImmutableMap.of( "protocol", "http", "path", "/ls", PUBLIC_ENDPOINT_ATTRIBUTE, "false", "secure", "false", "discoverable", "true")); ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setAlias("jdk"); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponent.setEndpoints(singletonList(endpoint)); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); List<HasMetadata> objects = objectsCaptor.getValue(); assertEquals(objects.size(), 1); assertTrue(objects.get(0) instanceof Deployment); Deployment deployment = (Deployment) objects.get(0); assertEquals( deployment.getSpec().getTemplate().getMetadata().getLabels().get(CHE_COMPONENT_NAME_LABEL), "jdk"); }
@Test public void shouldProvisionServersWithHttpPortIsTheCorrespondingAttrIsMissing() throws Exception { EndpointImpl endpoint = new EndpointImpl("jdk-ls", 4923, emptyMap()); ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setAlias("jdk"); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponent.setEndpoints(singletonList(endpoint)); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); MachineConfigImpl machineConfig = machinesCaptor.getValue().get("jdk"); assertNotNull(machineConfig); assertEquals(machineConfig.getServers().size(), 1); ServerConfigImpl serverConfig = machineConfig.getServers().get("jdk-ls"); assertEquals(serverConfig.getProtocol(), "http"); }
@Test public void shouldProvisionMachineConfigWithConfiguredVolumes() throws Exception { ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setAlias("jdk"); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponent.setVolumes(singletonList(new VolumeImpl("data", "/tmp/data/"))); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); MachineConfigImpl machineConfig = machinesCaptor.getValue().get("jdk"); assertNotNull(machineConfig); org.eclipse.che.api.workspace.server.model.impl.VolumeImpl volume = machineConfig.getVolumes().get("data"); assertNotNull(volume); assertEquals(volume.getPath(), "/tmp/data/"); }
@Test public void shouldProvisionMachineConfigWithMountSources() throws Exception { ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setAlias("jdk"); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponent.setMountSources(true); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); MachineConfigImpl machineConfig = machinesCaptor.getValue().get("jdk"); assertNotNull(machineConfig); org.eclipse.che.api.workspace.server.model.impl.VolumeImpl projectsVolume = machineConfig.getVolumes().get(PROJECTS_VOLUME_NAME); assertNotNull(projectsVolume); assertEquals(projectsVolume.getPath(), PROJECTS_MOUNT_PATH); }
@Test public void shouldProvisionMachineConfigWithAliasAttribute() throws Exception { ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setAlias("jdk-alias"); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); MachineConfigImpl machineConfig = machinesCaptor.getValue().get("jdk-alias"); assertNotNull(machineConfig); assertEquals(machineConfig.getAttributes().get(DEVFILE_COMPONENT_ALIAS_ATTRIBUTE), "jdk-alias"); }
@Test public void shouldProvisionMachineConfigWithoutSourcesByDefault() throws Exception { ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setAlias("jdk"); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); MachineConfigImpl machineConfig = machinesCaptor.getValue().get("jdk"); assertNotNull(machineConfig); assertFalse(machineConfig.getVolumes().containsKey(PROJECTS_VOLUME_NAME)); }
@Test public void shouldProvisionCommandAndArgs() throws Exception { List<String> command = singletonList("/usr/bin/rf"); List<String> args = Arrays.asList("-r", "f"); ComponentImpl dockerimageComponent = new ComponentImpl(); dockerimageComponent.setAlias("jdk"); dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE); dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest"); dockerimageComponent.setMemoryLimit("1G"); dockerimageComponent.setCommand(command); dockerimageComponent.setArgs(args); dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null); verify(k8sEnvProvisioner) .provision( eq(workspaceConfig), eq(KubernetesEnvironment.TYPE), objectsCaptor.capture(), machinesCaptor.capture()); List<HasMetadata> objects = objectsCaptor.getValue(); assertEquals(objects.size(), 1); assertTrue(objects.get(0) instanceof Deployment); Deployment deployment = (Deployment) objects.get(0); PodTemplateSpec podTemplate = deployment.getSpec().getTemplate(); assertEquals(podTemplate.getSpec().getContainers().size(), 1); Container container = podTemplate.getSpec().getContainers().get(0); assertEquals(container.getCommand(), command); assertEquals(container.getArgs(), args); }