Search in sources :

Example 1 with DOCKERIMAGE_COMPONENT_TYPE

use of org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE in project che-server by eclipse-che.

the class DevfileIntegrityValidator method validateComponents.

private Set<String> validateComponents(Devfile devfile) throws DevfileFormatException {
    Set<String> definedAliases = new HashSet<>();
    Component editorComponent = null;
    Map<String, Set<String>> idsPerComponentType = new HashMap<>();
    for (Component component : devfile.getComponents()) {
        if (component.getAlias() != null && !definedAliases.add(component.getAlias())) {
            throw new DevfileFormatException(format("Duplicate component alias found:'%s'", component.getAlias()));
        }
        Optional<Map.Entry<String, Long>> duplicatedEndpoint = component.getEndpoints().stream().map(Endpoint::getName).collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream().filter(e -> e.getValue() > 1L).findFirst();
        if (duplicatedEndpoint.isPresent()) {
            throw new DevfileFormatException(format("Duplicated endpoint name '%s' found in '%s' component", duplicatedEndpoint.get().getKey(), getIdentifiableComponentName(component)));
        }
        Set<String> tempSet = new HashSet<>();
        for (Env env : component.getEnv()) {
            if (!tempSet.add(env.getName())) {
                throw new DevfileFormatException(format("Duplicate environment variable '%s' found in component '%s'", env.getName(), getIdentifiableComponentName(component)));
            }
        }
        if (!idsPerComponentType.computeIfAbsent(component.getType(), __ -> new HashSet<>()).add(getIdentifiableComponentName(component))) {
            throw new DevfileFormatException(format("There are multiple components '%s' of type '%s' that cannot be uniquely" + " identified. Please add aliases that would distinguish the components.", getIdentifiableComponentName(component), component.getType()));
        }
        if (component.getAutomountWorkspaceSecrets() != null && component.getAlias() == null) {
            throw new DevfileFormatException(format("The 'automountWorkspaceSecrets' property cannot be used in component which doesn't have alias. " + "Please add alias to component '%s' that would allow to distinguish its containers.", getIdentifiableComponentName(component)));
        }
        switch(component.getType()) {
            case EDITOR_COMPONENT_TYPE:
                if (editorComponent != null) {
                    throw new DevfileFormatException(format("Multiple editor components found: '%s', '%s'", getIdentifiableComponentName(editorComponent), getIdentifiableComponentName(component)));
                }
                editorComponent = component;
                break;
            case PLUGIN_COMPONENT_TYPE:
            case KUBERNETES_COMPONENT_TYPE:
            case OPENSHIFT_COMPONENT_TYPE:
            case DOCKERIMAGE_COMPONENT_TYPE:
                // do nothing
                break;
            default:
                throw new DevfileFormatException(format("One of the components has unsupported component type: '%s'", component.getType()));
        }
    }
    return definedAliases;
}
Also used : KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Env(org.eclipse.che.api.core.model.workspace.devfile.Env) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) PLUGIN_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.PLUGIN_COMPONENT_TYPE) Function(java.util.function.Function) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Map(java.util.Map) Components.getIdentifiableComponentName(org.eclipse.che.api.workspace.server.devfile.Components.getIdentifiableComponentName) EDITOR_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.EDITOR_COMPONENT_TYPE) DOCKERIMAGE_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) Devfile(org.eclipse.che.api.core.model.workspace.devfile.Devfile) OPENSHIFT_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.OPENSHIFT_COMPONENT_TYPE) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) Optional(java.util.Optional) Command(org.eclipse.che.api.core.model.workspace.devfile.Command) Project(org.eclipse.che.api.core.model.workspace.devfile.Project) Pattern(java.util.regex.Pattern) Action(org.eclipse.che.api.core.model.workspace.devfile.Action) Endpoint(org.eclipse.che.api.core.model.workspace.devfile.Endpoint) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Env(org.eclipse.che.api.core.model.workspace.devfile.Env) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) HashSet(java.util.HashSet)

Example 2 with DOCKERIMAGE_COMPONENT_TYPE

use of org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE in project devspaces-images by redhat-developer.

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 3 with DOCKERIMAGE_COMPONENT_TYPE

use of org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE 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 4 with DOCKERIMAGE_COMPONENT_TYPE

use of org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE in project devspaces-images by redhat-developer.

the class DevfileIntegrityValidator method validateComponents.

private Set<String> validateComponents(Devfile devfile) throws DevfileFormatException {
    Set<String> definedAliases = new HashSet<>();
    Component editorComponent = null;
    Map<String, Set<String>> idsPerComponentType = new HashMap<>();
    for (Component component : devfile.getComponents()) {
        if (component.getAlias() != null && !definedAliases.add(component.getAlias())) {
            throw new DevfileFormatException(format("Duplicate component alias found:'%s'", component.getAlias()));
        }
        Optional<Map.Entry<String, Long>> duplicatedEndpoint = component.getEndpoints().stream().map(Endpoint::getName).collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream().filter(e -> e.getValue() > 1L).findFirst();
        if (duplicatedEndpoint.isPresent()) {
            throw new DevfileFormatException(format("Duplicated endpoint name '%s' found in '%s' component", duplicatedEndpoint.get().getKey(), getIdentifiableComponentName(component)));
        }
        Set<String> tempSet = new HashSet<>();
        for (Env env : component.getEnv()) {
            if (!tempSet.add(env.getName())) {
                throw new DevfileFormatException(format("Duplicate environment variable '%s' found in component '%s'", env.getName(), getIdentifiableComponentName(component)));
            }
        }
        if (!idsPerComponentType.computeIfAbsent(component.getType(), __ -> new HashSet<>()).add(getIdentifiableComponentName(component))) {
            throw new DevfileFormatException(format("There are multiple components '%s' of type '%s' that cannot be uniquely" + " identified. Please add aliases that would distinguish the components.", getIdentifiableComponentName(component), component.getType()));
        }
        if (component.getAutomountWorkspaceSecrets() != null && component.getAlias() == null) {
            throw new DevfileFormatException(format("The 'automountWorkspaceSecrets' property cannot be used in component which doesn't have alias. " + "Please add alias to component '%s' that would allow to distinguish its containers.", getIdentifiableComponentName(component)));
        }
        switch(component.getType()) {
            case EDITOR_COMPONENT_TYPE:
                if (editorComponent != null) {
                    throw new DevfileFormatException(format("Multiple editor components found: '%s', '%s'", getIdentifiableComponentName(editorComponent), getIdentifiableComponentName(component)));
                }
                editorComponent = component;
                break;
            case PLUGIN_COMPONENT_TYPE:
            case KUBERNETES_COMPONENT_TYPE:
            case OPENSHIFT_COMPONENT_TYPE:
            case DOCKERIMAGE_COMPONENT_TYPE:
                // do nothing
                break;
            default:
                throw new DevfileFormatException(format("One of the components has unsupported component type: '%s'", component.getType()));
        }
    }
    return definedAliases;
}
Also used : KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Env(org.eclipse.che.api.core.model.workspace.devfile.Env) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) PLUGIN_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.PLUGIN_COMPONENT_TYPE) Function(java.util.function.Function) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Map(java.util.Map) Components.getIdentifiableComponentName(org.eclipse.che.api.workspace.server.devfile.Components.getIdentifiableComponentName) EDITOR_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.EDITOR_COMPONENT_TYPE) DOCKERIMAGE_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) Devfile(org.eclipse.che.api.core.model.workspace.devfile.Devfile) OPENSHIFT_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.OPENSHIFT_COMPONENT_TYPE) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) Optional(java.util.Optional) Command(org.eclipse.che.api.core.model.workspace.devfile.Command) Project(org.eclipse.che.api.core.model.workspace.devfile.Project) Pattern(java.util.regex.Pattern) Action(org.eclipse.che.api.core.model.workspace.devfile.Action) Endpoint(org.eclipse.che.api.core.model.workspace.devfile.Endpoint) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Env(org.eclipse.che.api.core.model.workspace.devfile.Env) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) HashSet(java.util.HashSet)

Aggregations

String.format (java.lang.String.format)4 Collectors (java.util.stream.Collectors)4 Inject (javax.inject.Inject)4 DOCKERIMAGE_COMPONENT_TYPE (org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE)4 FileContentProvider (org.eclipse.che.api.workspace.server.devfile.FileContentProvider)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Container (io.fabric8.kubernetes.api.model.Container)2 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)2 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)2 DeploymentBuilder (io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2