Search in sources :

Example 51 with CommandImpl

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

the class KubernetesPluginsToolingApplier method apply.

@Override
public void apply(RuntimeIdentity runtimeIdentity, InternalEnvironment internalEnvironment, Collection<ChePlugin> chePlugins) throws InfrastructureException {
    if (chePlugins.isEmpty()) {
        return;
    }
    KubernetesEnvironment k8sEnv = (KubernetesEnvironment) internalEnvironment;
    Map<String, PodData> pods = k8sEnv.getPodsData();
    switch(pods.size()) {
        case 0:
            addToolingPod(k8sEnv);
            pods = k8sEnv.getPodsData();
            break;
        case 1:
            break;
        default:
            throw new InfrastructureException("Che plugins tooling configuration can be applied to a workspace with one pod only");
    }
    PodData pod = pods.values().iterator().next();
    CommandsResolver commandsResolver = new CommandsResolver(k8sEnv);
    for (ChePlugin chePlugin : chePlugins) {
        Map<String, ComponentImpl> devfilePlugins = k8sEnv.getDevfile().getComponents().stream().filter(c -> c.getType().equals("cheEditor") || c.getType().equals("chePlugin")).collect(Collectors.toMap(ComponentImpl::getId, Function.identity()));
        if (!devfilePlugins.containsKey(chePlugin.getId())) {
            throw new InfrastructureException(String.format("The downloaded plugin '%s' configuration does not have the " + "corresponding component in devfile. Devfile contains the following cheEditor/chePlugins: %s", chePlugin.getId(), devfilePlugins.keySet()));
        }
        ComponentImpl pluginRelatedComponent = devfilePlugins.get(chePlugin.getId());
        for (CheContainer container : chePlugin.getInitContainers()) {
            Container k8sInitContainer = toK8sContainer(container);
            envVars.apply(k8sInitContainer, pluginRelatedComponent.getEnv());
            chePluginsVolumeApplier.applyVolumes(pod, k8sInitContainer, container.getVolumes(), k8sEnv);
            pod.getSpec().getInitContainers().add(k8sInitContainer);
        }
        Collection<CommandImpl> pluginRelatedCommands = commandsResolver.resolve(chePlugin);
        for (CheContainer container : chePlugin.getContainers()) {
            addSidecar(pod, container, chePlugin, k8sEnv, pluginRelatedCommands, pluginRelatedComponent, runtimeIdentity);
        }
    }
    chePlugins.forEach(chePlugin -> populateWorkspaceEnvVars(chePlugin, k8sEnv));
}
Also used : PLUGIN_MACHINE_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.PLUGIN_MACHINE_ATTRIBUTE) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) ChePluginEndpoint(org.eclipse.che.api.workspace.server.wsplugins.model.ChePluginEndpoint) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) EnvVars(org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars) Singleton(javax.inject.Singleton) CONTAINER_SOURCE_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.CONTAINER_SOURCE_ATTRIBUTE) Function(java.util.function.Function) WORKING_DIRECTORY_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.WORKING_DIRECTORY_ATTRIBUTE) Inject(javax.inject.Inject) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) Map(java.util.Map) MACHINE_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.MACHINE_NAME_ATTRIBUTE) Service(io.fabric8.kubernetes.api.model.Service) Named(javax.inject.Named) CheContainer(org.eclipse.che.api.workspace.server.wsplugins.model.CheContainer) Command(org.eclipse.che.api.workspace.server.wsplugins.model.Command) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) ChePluginsApplier(org.eclipse.che.api.workspace.server.wsplugins.ChePluginsApplier) TOOL_CONTAINER_SOURCE(org.eclipse.che.api.workspace.shared.Constants.TOOL_CONTAINER_SOURCE) WarningImpl(org.eclipse.che.api.workspace.server.model.impl.WarningImpl) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Pod(io.fabric8.kubernetes.api.model.Pod) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Sets(com.google.common.collect.Sets) Beta(com.google.common.annotations.Beta) ChePlugin(org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) Warnings(org.eclipse.che.workspace.infrastructure.kubernetes.Warnings) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) ProjectsRootEnvVariableProvider(org.eclipse.che.api.workspace.server.spi.provision.env.ProjectsRootEnvVariableProvider) KubernetesSize(org.eclipse.che.workspace.infrastructure.kubernetes.util.KubernetesSize) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) CheContainer(org.eclipse.che.api.workspace.server.wsplugins.model.CheContainer) Container(io.fabric8.kubernetes.api.model.Container) CheContainer(org.eclipse.che.api.workspace.server.wsplugins.model.CheContainer) ChePlugin(org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 52 with CommandImpl

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

the class FactoryDaoTest method createWorkspaceConfig.

private static WorkspaceConfigImpl createWorkspaceConfig(int index) {
    // Project Sources configuration
    final SourceStorageImpl source1 = new SourceStorageImpl();
    source1.setType("type1");
    source1.setLocation("location1");
    source1.setParameters(new HashMap<>(ImmutableMap.of("param1", "value1")));
    final SourceStorageImpl source2 = new SourceStorageImpl();
    source2.setType("type2");
    source2.setLocation("location2");
    source2.setParameters(new HashMap<>(ImmutableMap.of("param4", "value1")));
    // Project Configuration
    final ProjectConfigImpl pCfg1 = new ProjectConfigImpl();
    pCfg1.setPath("/path1");
    pCfg1.setType("type1");
    pCfg1.setName("project1");
    pCfg1.setDescription("description1");
    pCfg1.getMixins().addAll(asList("mixin1", "mixin2"));
    pCfg1.setSource(source1);
    final ProjectConfigImpl pCfg2 = new ProjectConfigImpl();
    pCfg2.setPath("/path2");
    pCfg2.setType("type2");
    pCfg2.setName("project2");
    pCfg2.setDescription("description2");
    pCfg2.getMixins().addAll(asList("mixin3", "mixin4"));
    pCfg2.setSource(source2);
    final List<ProjectConfigImpl> projects = new ArrayList<>(asList(pCfg1, pCfg2));
    // Commands
    final CommandImpl cmd1 = new CommandImpl("name1", "cmd1", "type1");
    cmd1.getAttributes().putAll(ImmutableMap.of("key1", "value1"));
    final CommandImpl cmd2 = new CommandImpl("name2", "cmd2", "type2");
    cmd2.getAttributes().putAll(ImmutableMap.of("key4", "value4"));
    final List<CommandImpl> commands = new ArrayList<>(asList(cmd1, cmd2));
    // Machine configs
    final MachineConfigImpl exMachine1 = new MachineConfigImpl();
    final ServerConfigImpl serverConf1 = new ServerConfigImpl("2265", "http", "/path1", singletonMap("key", "value"));
    final ServerConfigImpl serverConf2 = new ServerConfigImpl("2266", "ftp", "/path2", singletonMap("key", "value"));
    exMachine1.setServers(ImmutableMap.of("ref1", serverConf1, "ref2", serverConf2));
    exMachine1.setAttributes(singletonMap("att1", "val"));
    exMachine1.setEnv(singletonMap("CHE_ENV", "value"));
    final MachineConfigImpl exMachine2 = new MachineConfigImpl();
    final ServerConfigImpl serverConf3 = new ServerConfigImpl("2333", "https", "/path1", singletonMap("key", "value"));
    final ServerConfigImpl serverConf4 = new ServerConfigImpl("2334", "wss", "/path2", singletonMap("key", "value"));
    exMachine2.setServers(ImmutableMap.of("ref1", serverConf3, "ref2", serverConf4));
    exMachine2.setAttributes(singletonMap("att1", "val"));
    exMachine2.setEnv(singletonMap("CHE_ENV2", "value"));
    final MachineConfigImpl exMachine3 = new MachineConfigImpl();
    final ServerConfigImpl serverConf5 = new ServerConfigImpl("2333", "https", "/path3", singletonMap("key", "value"));
    exMachine3.setServers(singletonMap("ref1", serverConf5));
    exMachine3.setAttributes(singletonMap("att1", "val"));
    exMachine3.setEnv(singletonMap("CHE_ENV3", "value"));
    // Environments
    final RecipeImpl recipe1 = new RecipeImpl();
    recipe1.setLocation("https://eclipse.che/Dockerfile");
    recipe1.setType("dockerfile");
    recipe1.setContentType("text/x-dockerfile");
    recipe1.setContent("content");
    final EnvironmentImpl env1 = new EnvironmentImpl();
    env1.setMachines(new HashMap<>(ImmutableMap.of("machine1", exMachine1, "machine2", exMachine2, "machine3", exMachine3)));
    env1.setRecipe(recipe1);
    final RecipeImpl recipe2 = new RecipeImpl();
    recipe2.setLocation("https://eclipse.che/Dockerfile");
    recipe2.setType("dockerfile");
    recipe2.setContentType("text/x-dockerfile");
    recipe2.setContent("content");
    final EnvironmentImpl env2 = new EnvironmentImpl();
    env2.setMachines(new HashMap<>(ImmutableMap.of("machine1", exMachine1, "machine3", exMachine3)));
    env2.setRecipe(recipe2);
    final Map<String, EnvironmentImpl> environments = ImmutableMap.of("env1", env1, "env2", env2);
    // Workspace configuration
    final WorkspaceConfigImpl wCfg = new WorkspaceConfigImpl();
    wCfg.setDefaultEnv("env1");
    wCfg.setName("cfgName_" + index);
    wCfg.setDescription("description");
    wCfg.setCommands(commands);
    wCfg.setProjects(projects);
    wCfg.setEnvironments(environments);
    return wCfg;
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) ArrayList(java.util.ArrayList) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) SourceStorageImpl(org.eclipse.che.api.workspace.server.model.impl.SourceStorageImpl) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) ProjectConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl)

Example 53 with CommandImpl

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

the class WorkspaceRuntimes method createInternalEnvironment.

@VisibleForTesting
InternalEnvironment createInternalEnvironment(@Nullable Environment environment, Map<String, String> workspaceConfigAttributes, List<? extends Command> commands, DevfileImpl devfile) throws InfrastructureException, ValidationException, NotFoundException {
    String recipeType;
    if (environment == null) {
        recipeType = Constants.NO_ENVIRONMENT_RECIPE_TYPE;
    } else {
        recipeType = environment.getRecipe().getType();
    }
    InternalEnvironmentFactory factory = environmentFactories.get(recipeType);
    if (factory == null) {
        throw new NotFoundException(format("InternalEnvironmentFactory is not configured for recipe type: '%s'", recipeType));
    }
    InternalEnvironment internalEnvironment = factory.create(environment);
    internalEnvironment.setAttributes(new HashMap<>(workspaceConfigAttributes));
    internalEnvironment.setCommands(commands.stream().map(CommandImpl::new).collect(toList()));
    internalEnvironment.setDevfile(devfile);
    return internalEnvironment;
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) NotFoundException(org.eclipse.che.api.core.NotFoundException) InternalEnvironmentFactory(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironmentFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 54 with CommandImpl

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

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

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

the class PreviewUrlExposer method expose.

public void expose(T env) throws InternalInfrastructureException {
    List<CommandImpl> previewUrlCommands = env.getCommands().stream().filter(c -> c.getPreviewUrl() != null).collect(Collectors.toList());
    List<ServicePort> portsToProvision = new ArrayList<>();
    for (CommandImpl command : previewUrlCommands) {
        int port = command.getPreviewUrl().getPort();
        Optional<Service> foundService = Services.findServiceWithPort(env.getServices().values(), port);
        if (foundService.isPresent()) {
            if (!hasMatchingEndpoint(env, foundService.get(), port)) {
                ServicePort servicePort = Services.findPort(foundService.get(), port).orElseThrow(() -> new InternalInfrastructureException(String.format("Port '%d' in service '%s' not found. This is not expected, please report a bug!", port, foundService.get().getMetadata().getName())));
                String serviceName = foundService.get().getMetadata().getName();
                externalServerExposer.expose(env, null, serviceName, serviceName, servicePort, Collections.emptyMap());
            }
        } else {
            portsToProvision.add(createServicePort(port));
        }
    }
    if (!portsToProvision.isEmpty()) {
        String serverName = generate(SERVER_PREFIX, SERVER_UNIQUE_PART_SIZE) + "-previewUrl";
        Service service = new ServerServiceBuilder().withName(serverName).withPorts(portsToProvision).build();
        env.getServices().put(serverName, service);
        portsToProvision.forEach(port -> externalServerExposer.expose(env, null, service.getMetadata().getName(), service.getMetadata().getName(), port, Collections.emptyMap()));
    }
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) ExternalServerExposerProvider(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.ExternalServerExposerProvider) NameGenerator.generate(org.eclipse.che.commons.lang.NameGenerator.generate) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ExternalServerExposer(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.ExternalServerExposer) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) Singleton(javax.inject.Singleton) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) SERVER_UNIQUE_PART_SIZE(org.eclipse.che.workspace.infrastructure.kubernetes.server.KubernetesServerExposer.SERVER_UNIQUE_PART_SIZE) Inject(javax.inject.Inject) SERVER_PREFIX(org.eclipse.che.workspace.infrastructure.kubernetes.server.KubernetesServerExposer.SERVER_PREFIX) List(java.util.List) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) Services(org.eclipse.che.workspace.infrastructure.kubernetes.util.Services) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Service(io.fabric8.kubernetes.api.model.Service) Collections(java.util.Collections) Ingresses(org.eclipse.che.workspace.infrastructure.kubernetes.util.Ingresses) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ArrayList(java.util.ArrayList) Service(io.fabric8.kubernetes.api.model.Service) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)

Aggregations

CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)106 Test (org.testng.annotations.Test)80 PreviewUrlImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.PreviewUrlImpl)40 ArrayList (java.util.ArrayList)32 Service (io.fabric8.kubernetes.api.model.Service)30 HashMap (java.util.HashMap)30 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)26 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)24 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)24 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)20 ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)18 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)18 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)18 OpenShiftEnvironment (org.eclipse.che.workspace.infrastructure.openshift.environment.OpenShiftEnvironment)16 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)14 Ingress (io.fabric8.kubernetes.api.model.networking.v1.Ingress)14 List (java.util.List)12 IngressBackend (io.fabric8.kubernetes.api.model.networking.v1.IngressBackend)10 ChePlugin (org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin)10 ProjectConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl)9