Search in sources :

Example 21 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class DevfileConverter method devFileToWorkspaceConfig.

/**
 * Converts given {@link Devfile} into {@link WorkspaceConfigImpl workspace config}.
 *
 * @param devfile initial devfile
 * @param contentProvider content provider for recipe-type component or plugin references
 * @return constructed workspace config
 * @throws DevfileException when general devfile error occurs
 * @throws DevfileException when devfile requires additional files content but the specified
 *     content provider does not support it
 * @throws DevfileFormatException when devfile format is invalid
 * @throws DevfileRecipeFormatException when content of the file specified in recipe type
 *     component is empty or its format is invalid
 */
public WorkspaceConfigImpl devFileToWorkspaceConfig(DevfileImpl devfile, FileContentProvider contentProvider) throws DevfileException {
    checkArgument(devfile != null, "Devfile must not be null");
    checkArgument(contentProvider != null, "Content provider must not be null");
    // make copy to avoid modification of original devfile
    devfile = new DevfileImpl(devfile);
    validateCurrentVersion(devfile);
    defaultEditorProvisioner.apply(devfile, contentProvider);
    WorkspaceConfigImpl config = new WorkspaceConfigImpl();
    config.setName(devfile.getName());
    for (Command command : devfile.getCommands()) {
        CommandImpl com = commandConverter.toWorkspaceCommand(command, contentProvider);
        if (com != null) {
            config.getCommands().add(com);
        }
    }
    // so, commands should be already converted
    for (ComponentImpl component : devfile.getComponents()) {
        ComponentToWorkspaceApplier applier = componentTypeToApplier.get(component.getType());
        if (applier == null) {
            throw new DevfileException(String.format("Devfile contains component `%s` with type `%s` that can not be converted to workspace", getIdentifiableComponentName(component), component.getType()));
        }
        applier.apply(config, component, contentProvider);
    }
    for (ProjectImpl project : devfile.getProjects()) {
        ProjectConfigImpl projectConfig = projectConverter.toWorkspaceProject(project);
        config.getProjects().add(projectConfig);
    }
    config.getAttributes().putAll(devfile.getAttributes());
    config.setDevfile(devfile);
    return config;
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) Command(org.eclipse.che.api.core.model.workspace.devfile.Command) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) ProjectImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ProjectImpl) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ComponentToWorkspaceApplier(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier) ProjectConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl)

Example 22 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class DockerimageComponentToWorkspaceApplierTest method shouldProvisionMachineConfigWithoutSourcesByDefault.

@Test
public void shouldProvisionMachineConfigWithoutSourcesByDefault() throws Exception {
    // given
    ComponentImpl dockerimageComponent = new ComponentImpl();
    dockerimageComponent.setAlias("jdk");
    dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE);
    dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest");
    dockerimageComponent.setMemoryLimit("1G");
    // when
    dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null);
    // then
    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));
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 23 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class DockerimageComponentToWorkspaceApplierTest method shouldProvisionK8sEnvironmentWithMachineConfigFromSpecifiedDockerimageWithoutAlias.

@Test
public void shouldProvisionK8sEnvironmentWithMachineConfigFromSpecifiedDockerimageWithoutAlias() throws Exception {
    ComponentImpl dockerimageComponent = new ComponentImpl();
    dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE);
    dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest");
    dockerimageComponent.setMemoryLimit("1G");
    // when
    dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null);
    // then
    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");
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Container(io.fabric8.kubernetes.api.model.Container) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 24 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class DockerimageComponentToWorkspaceApplierTest method shouldProvisionMachineConfigWithConfiguredServers.

@Test
public void shouldProvisionMachineConfigWithConfiguredServers() throws Exception {
    // given
    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));
    // when
    dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null);
    // then
    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");
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) EndpointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 25 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class DockerimageComponentToWorkspaceApplierTest method shouldProvisionK8sEnvironmentWithMachineConfigAndGeneratedDeploymentForSpecifiedDockerimage.

@Test
public void shouldProvisionK8sEnvironmentWithMachineConfigAndGeneratedDeploymentForSpecifiedDockerimage() throws Exception {
    // given
    ComponentImpl dockerimageComponent = new ComponentImpl();
    dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE);
    dockerimageComponent.setAlias("jdk");
    dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest");
    dockerimageComponent.setMemoryLimit("1G");
    // when
    dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null);
    // then
    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");
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Container(io.fabric8.kubernetes.api.model.Container) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Aggregations

ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)218 Test (org.testng.annotations.Test)184 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)76 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)56 Container (io.fabric8.kubernetes.api.model.Container)48 MachineConfigImpl (org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)44 EndpointImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl)42 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)38 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)36 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)26 EntrypointImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EntrypointImpl)26 Map (java.util.Map)24 EnvImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl)24 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)22 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)20 Collections.emptyMap (java.util.Collections.emptyMap)18 DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)18 ImmutableMap (com.google.common.collect.ImmutableMap)16 ArrayList (java.util.ArrayList)14 List (java.util.List)14