Search in sources :

Example 31 with RecipeImpl

use of org.eclipse.che.api.workspace.server.model.impl.RecipeImpl 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 32 with RecipeImpl

use of org.eclipse.che.api.workspace.server.model.impl.RecipeImpl 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 33 with RecipeImpl

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

the class KubernetesEnvironmentProvisionerTest method shouldUpgradeKubernetesEnvironmentToOpenShiftTypeOnOpenShiftComponentProvisioning.

@Test
public void shouldUpgradeKubernetesEnvironmentToOpenShiftTypeOnOpenShiftComponentProvisioning() throws Exception {
    // given
    workspaceConfig.setDefaultEnv("default");
    RecipeImpl existingRecipe = new RecipeImpl(KubernetesEnvironment.TYPE, "yaml", "existing-content", null);
    workspaceConfig.getEnvironments().put("default", new EnvironmentImpl(existingRecipe, emptyMap()));
    List<HasMetadata> componentsObject = new ArrayList<>();
    Deployment componentDeployment = new DeploymentBuilder().withNewMetadata().withName("web-app").endMetadata().withNewSpec().endSpec().build();
    componentsObject.add(new DeploymentBuilder(componentDeployment).build());
    doReturn(new ArrayList<>()).when(k8sRecipeParser).parse(anyString());
    // when
    k8sEnvProvisioner.provision(workspaceConfig, "openshift", componentsObject, emptyMap());
    // then
    EnvironmentImpl resultEnv = workspaceConfig.getEnvironments().get(workspaceConfig.getDefaultEnv());
    RecipeImpl resultRecipe = resultEnv.getRecipe();
    assertEquals(resultRecipe.getType(), "openshift");
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) ArrayList(java.util.ArrayList) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.testng.annotations.Test)

Example 34 with RecipeImpl

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

the class KubernetesEnvironmentProvisionerTest method shouldMergeProjectPVCIntoOne.

@Test
public void shouldMergeProjectPVCIntoOne() throws Exception {
    // given
    PersistentVolumeClaim volumeClaim = newPVC(PROJECTS_VOLUME_NAME, "ReadWriteMany", "1Gb");
    workspaceConfig.setDefaultEnv("default");
    RecipeImpl existingRecipe = new RecipeImpl("kubernetes", YAML_CONTENT_TYPE, Serialization.asYaml(volumeClaim), null);
    doReturn(singletonList(volumeClaim)).when(k8sRecipeParser).parse(anyString());
    workspaceConfig.getEnvironments().put("default", new EnvironmentImpl(existingRecipe, emptyMap()));
    // try add same claim one more time (like another component adds it)
    List<HasMetadata> componentsObject = new ArrayList<>();
    componentsObject.add(volumeClaim);
    // when
    k8sEnvProvisioner.provision(workspaceConfig, KubernetesEnvironment.TYPE, componentsObject, emptyMap());
    // we still have only one PVC
    EnvironmentImpl resultEnv = workspaceConfig.getEnvironments().get(workspaceConfig.getDefaultEnv());
    assertEquals(toK8SList(resultEnv.getRecipe().getContent()).getItems().size(), 1);
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) ArrayList(java.util.ArrayList) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) Test(org.testng.annotations.Test)

Example 35 with RecipeImpl

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

the class KubernetesEnvironmentProvisionerTest method shouldThrowAnExceptionIfWorkspaceAlreadyContainNonK8sNorOSRecipe.

@Test(expectedExceptions = DevfileException.class, expectedExceptionsMessageRegExp = "Kubernetes component can only be applied to a workspace with any of kubernetes or openshift " + "recipe type but workspace has a recipe of type 'any'")
public void shouldThrowAnExceptionIfWorkspaceAlreadyContainNonK8sNorOSRecipe() throws Exception {
    // given
    workspaceConfig.setDefaultEnv("default");
    RecipeImpl existingRecipe = new RecipeImpl("any", "yaml", "existing-content", null);
    workspaceConfig.getEnvironments().put("default", new EnvironmentImpl(existingRecipe, emptyMap()));
    List<HasMetadata> componentsObject = new ArrayList<>();
    Deployment componentDeployment = new DeploymentBuilder().withNewMetadata().withName("db").endMetadata().withNewSpec().endSpec().build();
    componentsObject.add(new DeploymentBuilder(componentDeployment).build());
    // when
    k8sEnvProvisioner.provision(workspaceConfig, KubernetesEnvironment.TYPE, componentsObject, emptyMap());
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) ArrayList(java.util.ArrayList) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.testng.annotations.Test)

Aggregations

RecipeImpl (org.eclipse.che.api.workspace.server.model.impl.RecipeImpl)42 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)38 Test (org.testng.annotations.Test)20 MachineConfigImpl (org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)18 ArrayList (java.util.ArrayList)16 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)12 ServerConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl)12 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)12 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)8 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)7 ProjectConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl)7 SourceStorageImpl (org.eclipse.che.api.workspace.server.model.impl.SourceStorageImpl)7 HashMap (java.util.HashMap)6 AccountImpl (org.eclipse.che.account.spi.AccountImpl)6 WorkspaceDao (org.eclipse.che.api.workspace.server.spi.WorkspaceDao)6 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)5 DeploymentBuilder (io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)5 VolumeImpl (org.eclipse.che.api.workspace.server.model.impl.VolumeImpl)5 ServerException (org.eclipse.che.api.core.ServerException)4 TypeLiteral (com.google.inject.TypeLiteral)3