Search in sources :

Example 1 with MachineConfig

use of org.eclipse.che.api.core.model.workspace.config.MachineConfig in project devspaces-images by redhat-developer.

the class KubernetesComponentToWorkspaceApplierTest method shouldProvisionDevfileVolumesIfSpecifiedIntoMachineConfig.

@Test
public void shouldProvisionDevfileVolumesIfSpecifiedIntoMachineConfig() throws Exception {
    // given
    String yamlRecipeContent = getResource("devfile/petclinic.yaml");
    List<HasMetadata> k8sList = toK8SList(yamlRecipeContent).getItems();
    doReturn(k8sList).when(k8sRecipeParser).parse(anyString());
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    component.setVolumes(asList(new VolumeImpl("foo", "/foo1"), new VolumeImpl("bar", "/bar1")));
    ArgumentCaptor<Map<String, MachineConfigImpl>> mapCaptor = ArgumentCaptor.forClass(Map.class);
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
    // then
    verify(k8sEnvProvisioner).provision(any(), any(), any(), mapCaptor.capture());
    Map<String, MachineConfigImpl> configMaps = mapCaptor.getValue();
    for (MachineConfig config : configMaps.values()) {
        assertEquals(config.getVolumes().size(), 2);
        assertTrue(config.getVolumes().entrySet().stream().anyMatch(entry -> entry.getKey().equals("foo") && entry.getValue().getPath().equals("/foo1")));
        assertTrue(config.getVolumes().entrySet().stream().anyMatch(entry -> entry.getKey().equals("bar") && entry.getValue().getPath().equals("/bar1")));
    }
}
Also used : Arrays(java.util.Arrays) Listeners(org.testng.annotations.Listeners) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) EnvVars(org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars) Test(org.testng.annotations.Test) Files(org.testng.reporters.Files) Collections.singletonList(java.util.Collections.singletonList) Mockito.doThrow(org.mockito.Mockito.doThrow) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) REQUIRE_SUBDOMAIN(org.eclipse.che.api.core.model.workspace.config.ServerConfig.REQUIRE_SUBDOMAIN) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) Serialization.unmarshal(io.fabric8.kubernetes.client.utils.Serialization.unmarshal) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) MULTI_HOST_STRATEGY(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.MultiHostExternalServiceExposureStrategy.MULTI_HOST_STRATEGY) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) EndpointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl) ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) Stream(java.util.stream.Stream) PROJECTS_VOLUME_NAME(org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Assert.assertNull(org.testng.Assert.assertNull) Container(io.fabric8.kubernetes.api.model.Container) DEVFILE_COMPONENT_ALIAS_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.MachineConfig.DEVFILE_COMPONENT_ALIAS_ATTRIBUTE) Mock(org.mockito.Mock) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) Assert.assertEquals(org.testng.Assert.assertEquals) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Captor(org.mockito.Captor) ValidationException(org.eclipse.che.api.core.ValidationException) ArrayList(java.util.ArrayList) VolumeImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) MACHINE_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.MACHINE_NAME_ATTRIBUTE) Collections.singletonMap(java.util.Collections.singletonMap) COMPONENT_ALIAS_COMMAND_ATTRIBUTE(org.eclipse.che.api.workspace.server.devfile.Constants.COMPONENT_ALIAS_COMMAND_ATTRIBUTE) EnvImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) Collections.emptyMap(java.util.Collections.emptyMap) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) URLFileContentProvider(org.eclipse.che.api.workspace.server.devfile.URLFileContentProvider) OPENSHIFT_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.OPENSHIFT_COMPONENT_TYPE) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) EntrypointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EntrypointImpl) SINGLE_HOST_STRATEGY(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.SingleHostExternalServiceExposureStrategy.SINGLE_HOST_STRATEGY) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Assert.assertTrue(org.testng.Assert.assertTrue) KubernetesRecipeParser(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesRecipeParser) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) VolumeImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) Test(org.testng.annotations.Test)

Example 2 with MachineConfig

use of org.eclipse.che.api.core.model.workspace.config.MachineConfig in project devspaces-images by redhat-developer.

the class TestObjects method createEnvironment.

public static EnvironmentImpl createEnvironment(String... machineRams) throws Exception {
    final Map<String, MachineConfig> machines = new HashMap<>();
    for (String machineRam : machineRams) {
        final MachineConfigImpl machineConfig = new MachineConfigImpl();
        machineConfig.setAttributes(ImmutableMap.of(MachineConfig.MEMORY_LIMIT_ATTRIBUTE, machineRam));
        machines.put("dev-machine", machineConfig);
    }
    final RecipeImpl recipe = new RecipeImpl("compose", "application/x-yaml", "", null);
    return new EnvironmentImpl(recipe, machines);
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) HashMap(java.util.HashMap) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)

Example 3 with MachineConfig

use of org.eclipse.che.api.core.model.workspace.config.MachineConfig in project devspaces-images by redhat-developer.

the class InternalEnvironmentFactory method create.

/**
 * Creates a valid instance of InternalEnvironment.
 *
 * <p>To construct a valid instance it performs the following actions:
 *
 * <ul>
 *   <li>download recipe content if it is needed;
 *   <li>retrieve the configured installers from installers registry;
 *   <li>normalize servers port by adding default protocol in port if it is absent;
 *   <li>validate the environment machines;
 *   <li>invoke implementation specific method that should validate and parse recipe;
 *   <li>ensure there are environment variables pointing to machine names;
 * </ul>
 *
 * @param sourceEnv the environment
 * @return InternalEnvironment a valid InternalEnvironment instance
 * @throws InfrastructureException if exception occurs on recipe downloading
 * @throws InfrastructureException if infrastructure specific error occurs
 * @throws ValidationException if validation fails
 */
public T create(@Nullable final Environment sourceEnv) throws InfrastructureException, ValidationException {
    Map<String, InternalMachineConfig> machines = new HashMap<>();
    List<Warning> warnings = new ArrayList<>();
    InternalRecipe recipe = null;
    if (sourceEnv != null) {
        recipe = recipeRetriever.getRecipe(sourceEnv.getRecipe());
        for (Map.Entry<String, ? extends MachineConfig> machineEntry : sourceEnv.getMachines().entrySet()) {
            MachineConfig machineConfig = machineEntry.getValue();
            machines.put(machineEntry.getKey(), new InternalMachineConfig(normalizeServers(machineConfig.getServers()), machineConfig.getEnv(), machineConfig.getAttributes(), machineConfig.getVolumes()));
        }
        machinesValidator.validate(machines);
    }
    T internalEnv = doCreate(recipe, machines, warnings);
    internalEnv.getMachines().values().forEach(m -> m.getAttributes().put(CONTAINER_SOURCE_ATTRIBUTE, RECIPE_CONTAINER_SOURCE));
    return internalEnv;
}
Also used : Warning(org.eclipse.che.api.core.model.workspace.Warning) HashMap(java.util.HashMap) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with MachineConfig

use of org.eclipse.che.api.core.model.workspace.config.MachineConfig in project che-server by eclipse-che.

the class InternalEnvironmentFactory method create.

/**
 * Creates a valid instance of InternalEnvironment.
 *
 * <p>To construct a valid instance it performs the following actions:
 *
 * <ul>
 *   <li>download recipe content if it is needed;
 *   <li>retrieve the configured installers from installers registry;
 *   <li>normalize servers port by adding default protocol in port if it is absent;
 *   <li>validate the environment machines;
 *   <li>invoke implementation specific method that should validate and parse recipe;
 *   <li>ensure there are environment variables pointing to machine names;
 * </ul>
 *
 * @param sourceEnv the environment
 * @return InternalEnvironment a valid InternalEnvironment instance
 * @throws InfrastructureException if exception occurs on recipe downloading
 * @throws InfrastructureException if infrastructure specific error occurs
 * @throws ValidationException if validation fails
 */
public T create(@Nullable final Environment sourceEnv) throws InfrastructureException, ValidationException {
    Map<String, InternalMachineConfig> machines = new HashMap<>();
    List<Warning> warnings = new ArrayList<>();
    InternalRecipe recipe = null;
    if (sourceEnv != null) {
        recipe = recipeRetriever.getRecipe(sourceEnv.getRecipe());
        for (Map.Entry<String, ? extends MachineConfig> machineEntry : sourceEnv.getMachines().entrySet()) {
            MachineConfig machineConfig = machineEntry.getValue();
            machines.put(machineEntry.getKey(), new InternalMachineConfig(normalizeServers(machineConfig.getServers()), machineConfig.getEnv(), machineConfig.getAttributes(), machineConfig.getVolumes()));
        }
        machinesValidator.validate(machines);
    }
    T internalEnv = doCreate(recipe, machines, warnings);
    internalEnv.getMachines().values().forEach(m -> m.getAttributes().put(CONTAINER_SOURCE_ATTRIBUTE, RECIPE_CONTAINER_SOURCE));
    return internalEnv;
}
Also used : Warning(org.eclipse.che.api.core.model.workspace.Warning) HashMap(java.util.HashMap) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with MachineConfig

use of org.eclipse.che.api.core.model.workspace.config.MachineConfig in project che-server by eclipse-che.

the class KubernetesComponentToWorkspaceApplierTest method shouldProvisionDevfileVolumesIfSpecifiedIntoMachineConfig.

@Test
public void shouldProvisionDevfileVolumesIfSpecifiedIntoMachineConfig() throws Exception {
    // given
    String yamlRecipeContent = getResource("devfile/petclinic.yaml");
    List<HasMetadata> k8sList = toK8SList(yamlRecipeContent).getItems();
    doReturn(k8sList).when(k8sRecipeParser).parse(anyString());
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    component.setVolumes(asList(new VolumeImpl("foo", "/foo1"), new VolumeImpl("bar", "/bar1")));
    ArgumentCaptor<Map<String, MachineConfigImpl>> mapCaptor = ArgumentCaptor.forClass(Map.class);
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
    // then
    verify(k8sEnvProvisioner).provision(any(), any(), any(), mapCaptor.capture());
    Map<String, MachineConfigImpl> configMaps = mapCaptor.getValue();
    for (MachineConfig config : configMaps.values()) {
        assertEquals(config.getVolumes().size(), 2);
        assertTrue(config.getVolumes().entrySet().stream().anyMatch(entry -> entry.getKey().equals("foo") && entry.getValue().getPath().equals("/foo1")));
        assertTrue(config.getVolumes().entrySet().stream().anyMatch(entry -> entry.getKey().equals("bar") && entry.getValue().getPath().equals("/bar1")));
    }
}
Also used : Arrays(java.util.Arrays) Listeners(org.testng.annotations.Listeners) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) EnvVars(org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars) Test(org.testng.annotations.Test) Files(org.testng.reporters.Files) Collections.singletonList(java.util.Collections.singletonList) Mockito.doThrow(org.mockito.Mockito.doThrow) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) REQUIRE_SUBDOMAIN(org.eclipse.che.api.core.model.workspace.config.ServerConfig.REQUIRE_SUBDOMAIN) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) Serialization.unmarshal(io.fabric8.kubernetes.client.utils.Serialization.unmarshal) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) MULTI_HOST_STRATEGY(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.MultiHostExternalServiceExposureStrategy.MULTI_HOST_STRATEGY) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) EndpointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl) ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) Stream(java.util.stream.Stream) PROJECTS_VOLUME_NAME(org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Assert.assertNull(org.testng.Assert.assertNull) Container(io.fabric8.kubernetes.api.model.Container) DEVFILE_COMPONENT_ALIAS_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.MachineConfig.DEVFILE_COMPONENT_ALIAS_ATTRIBUTE) Mock(org.mockito.Mock) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) Assert.assertEquals(org.testng.Assert.assertEquals) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Captor(org.mockito.Captor) ValidationException(org.eclipse.che.api.core.ValidationException) ArrayList(java.util.ArrayList) VolumeImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) MACHINE_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.MACHINE_NAME_ATTRIBUTE) Collections.singletonMap(java.util.Collections.singletonMap) COMPONENT_ALIAS_COMMAND_ATTRIBUTE(org.eclipse.che.api.workspace.server.devfile.Constants.COMPONENT_ALIAS_COMMAND_ATTRIBUTE) EnvImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) Collections.emptyMap(java.util.Collections.emptyMap) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) URLFileContentProvider(org.eclipse.che.api.workspace.server.devfile.URLFileContentProvider) OPENSHIFT_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.OPENSHIFT_COMPONENT_TYPE) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) EntrypointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EntrypointImpl) SINGLE_HOST_STRATEGY(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.SingleHostExternalServiceExposureStrategy.SINGLE_HOST_STRATEGY) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Assert.assertTrue(org.testng.Assert.assertTrue) KubernetesRecipeParser(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesRecipeParser) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) VolumeImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) Test(org.testng.annotations.Test)

Aggregations

MachineConfig (org.eclipse.che.api.core.model.workspace.config.MachineConfig)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 MachineConfigImpl (org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 Container (io.fabric8.kubernetes.api.model.Container)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)2 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)2 Serialization.unmarshal (io.fabric8.kubernetes.client.utils.Serialization.unmarshal)2 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 Arrays.asList (java.util.Arrays.asList)2 Collections (java.util.Collections)2 Collections.emptyMap (java.util.Collections.emptyMap)2 Collections.singletonList (java.util.Collections.singletonList)2 Collections.singletonMap (java.util.Collections.singletonMap)2