Search in sources :

Example 51 with MachineConfigImpl

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

the class InternalEnvironmentFactoryTest method testApplyContainerSourceAttributeToTheMachineSpecifiedInEnv.

@Test
public void testApplyContainerSourceAttributeToTheMachineSpecifiedInEnv() throws Exception {
    // given
    final Environment sourceEnv = mock(Environment.class);
    MachineConfigImpl machineConfig = mock(MachineConfigImpl.class);
    final Map<String, MachineConfigImpl> machineConfigMap = ImmutableMap.of("envMachine", machineConfig);
    doReturn(machineConfigMap).when(sourceEnv).getMachines();
    when(environmentFactory.doCreate(any(), any(), any())).thenAnswer(invocation -> {
        Map<String, InternalMachineConfig> envMachines = invocation.getArgument(1);
        final InternalEnvironment internalEnv = mock(InternalEnvironment.class);
        when(internalEnv.getMachines()).thenReturn(envMachines);
        return internalEnv;
    });
    // when
    InternalEnvironment resultEnv = environmentFactory.create(sourceEnv);
    // then
    assertEquals(resultEnv.getMachines().get("envMachine").getAttributes().get(CONTAINER_SOURCE_ATTRIBUTE), RECIPE_CONTAINER_SOURCE);
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) Test(org.testng.annotations.Test)

Example 52 with MachineConfigImpl

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

the class DockerimageComponentToWorkspaceApplier method apply.

/**
 * Applies changes on workspace config according to the specified dockerimage component.
 *
 * <p>Dockerimage component is provisioned as Deployment in Kubernetes recipe.<br>
 * Generated deployment contains container with environment variables, memory limit, docker image,
 * arguments and commands specified in component.<br>
 * Also, environment is provisioned with machine config with volumes and servers specified, then
 * Kubernetes infra will created needed PVC, Services, Ingresses, Routes according to specified
 * configuration.
 *
 * @param workspaceConfig workspace config on which changes should be applied
 * @param dockerimageComponent dockerimage component that should be applied
 * @param contentProvider optional content provider that may be used for external component
 *     resource fetching
 * @throws DevfileException if specified workspace config already has default environment where
 *     dockerimage component should be stored
 * @throws IllegalArgumentException if specified workspace config or plugin component is null
 * @throws IllegalArgumentException if specified component has type different from dockerimage
 */
@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));
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) DEVFILE_COMPONENT_ALIAS_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.MachineConfig.DEVFILE_COMPONENT_ALIAS_ATTRIBUTE) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) ComponentToWorkspaceApplier(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ComponentToWorkspaceApplier.convertEndpointsIntoServers(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier.convertEndpointsIntoServers) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) Containers(org.eclipse.che.workspace.infrastructure.kubernetes.util.Containers) MACHINE_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.MACHINE_NAME_ATTRIBUTE) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Named(javax.inject.Named) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) DOCKERIMAGE_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE) ImmutableMap(com.google.common.collect.ImmutableMap) VolumeImpl(org.eclipse.che.api.workspace.server.model.impl.VolumeImpl) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) List(java.util.List) SINGLE_HOST_STRATEGY(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.SingleHostExternalServiceExposureStrategy.SINGLE_HOST_STRATEGY) PROJECTS_VOLUME_NAME(org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME) Constants(org.eclipse.che.api.workspace.server.devfile.Constants) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) KubernetesSize(org.eclipse.che.workspace.infrastructure.kubernetes.util.KubernetesSize) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)

Example 53 with MachineConfigImpl

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

the class KubernetesEnvironmentProvisioner method provision.

/**
 * Provisions default K8s/OS environment with specified objects (K8s/OS objects, machines) into
 * {@link WorkspaceConfigImpl}.
 *
 * <p>If there is already a default environment with kubernetes/openshift recipe then content will
 * be updated with result or merging existing objects and specified ones.
 *
 * @param workspaceConfig workspace where recipe should be provisioned
 * @param environmentType type of environment that should be provisioned. Should be one of the
 *     Kubernetes-based environments.
 * @param componentObjects objects that should be provisioned into the workspace config
 * @param machines machines that should be provisioned into the workspace config
 * @throws DevfileRecipeFormatException if exception occurred during existing environment parsing
 * @throws DevfileRecipeFormatException if exception occurred during kubernetes object
 *     serialization
 * @throws DevfileException if any other exception occurred
 */
public void provision(WorkspaceConfigImpl workspaceConfig, String environmentType, List<HasMetadata> componentObjects, Map<String, MachineConfigImpl> machines) throws DevfileException, DevfileRecipeFormatException {
    String defaultEnv = workspaceConfig.getDefaultEnv();
    EnvironmentImpl environment = workspaceConfig.getEnvironments().get(defaultEnv);
    if (environment == null) {
        checkItemsHasUniqueKindToName(componentObjects);
        RecipeImpl recipe = new RecipeImpl(environmentType, YAML_CONTENT_TYPE, asYaml(componentObjects), null);
        String envName = "default";
        EnvironmentImpl env = new EnvironmentImpl(recipe, emptyMap());
        env.getMachines().putAll(machines);
        workspaceConfig.getEnvironments().put(envName, env);
        workspaceConfig.setDefaultEnv(envName);
    } else {
        RecipeImpl envRecipe = environment.getRecipe();
        for (Entry<String, MachineConfigImpl> machineEntry : machines.entrySet()) {
            if (environment.getMachines().put(machineEntry.getKey(), machineEntry.getValue()) != null) {
                throw new DevfileException(format("Environment already contains machine '%s'", machineEntry.getKey()));
            }
        }
        environment.getMachines().putAll(machines);
        // check if it is needed to update recipe type since
        // kubernetes component is compatible with openshift but not vice versa
        Set<String> allowedEnvTypeBases = allowedEnvironmentTypeUpgrades.get(environmentType);
        if (allowedEnvTypeBases != null) {
            envRecipe.setType(environmentType);
        }
        // workspace already has k8s/OS recipe
        // it is needed to merge existing recipe objects with component's ones
        List<HasMetadata> envObjects = unmarshalObjects(envRecipe);
        mergeProjectsPVC(envObjects, componentObjects);
        envObjects.addAll(componentObjects);
        checkItemsHasUniqueKindToName(envObjects);
        envRecipe.setContent(asYaml(envObjects));
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)

Example 54 with MachineConfigImpl

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

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 55 with MachineConfigImpl

use of org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl in project devspaces-images by redhat-developer.

the class RamResourceUsageTrackerTest method createWorkspace.

/**
 * Creates users workspace object based on the status and machines RAM.
 */
private static WorkspaceImpl createWorkspace(WorkspaceStatus status, Integer... machineRams) {
    final Map<String, MachineImpl> machines = new HashMap<>(machineRams.length - 1);
    final Map<String, MachineConfigImpl> machineConfigs = new HashMap<>(machineRams.length - 1);
    byte i = 1;
    for (Integer machineRam : machineRams) {
        final String machineName = "machine_" + i++;
        machines.put(machineName, createMachine(machineRam));
        machineConfigs.put(machineName, createMachineConfig(machineRam));
    }
    return WorkspaceImpl.builder().setConfig(WorkspaceConfigImpl.builder().setEnvironments(ImmutableBiMap.of(ACTIVE_ENV_NAME, new EnvironmentImpl(null, machineConfigs))).build()).setRuntime(new RuntimeImpl(ACTIVE_ENV_NAME, machines, null)).setStatus(status).build();
}
Also used : MachineImpl(org.eclipse.che.api.workspace.server.model.impl.MachineImpl) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) HashMap(java.util.HashMap) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) RuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Aggregations

MachineConfigImpl (org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)70 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)44 Test (org.testng.annotations.Test)44 ServerConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl)26 EndpointImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl)26 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)22 Map (java.util.Map)20 ArrayList (java.util.ArrayList)18 RecipeImpl (org.eclipse.che.api.workspace.server.model.impl.RecipeImpl)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 ImmutableMap (com.google.common.collect.ImmutableMap)16 Container (io.fabric8.kubernetes.api.model.Container)16 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)16 Collections.emptyMap (java.util.Collections.emptyMap)16 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)16 Collections.singletonMap (java.util.Collections.singletonMap)14 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)12 ArgumentMatchers.anyMap (org.mockito.ArgumentMatchers.anyMap)12 HashMap (java.util.HashMap)10 HashSet (java.util.HashSet)10