Search in sources :

Example 16 with DevfileException

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

the class KubernetesComponentToWorkspaceApplier method provisionVolumes.

private void provisionVolumes(ComponentImpl component, Container container, MachineConfigImpl config) throws DevfileException {
    for (org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl componentVolume : component.getVolumes()) {
        Optional<VolumeMount> sameNameMount = container.getVolumeMounts().stream().filter(vm -> vm.getName().equals(componentVolume.getName())).findFirst();
        if (sameNameMount.isPresent() && sameNameMount.get().getMountPath().equals(componentVolume.getContainerPath())) {
            continue;
        } else if (sameNameMount.isPresent()) {
            throw new DevfileException(format("Conflicting volume with same name ('%s') but different path ('%s') found for component '%s' and its container '%s'.", componentVolume.getName(), componentVolume.getContainerPath(), getIdentifiableComponentName(component), container.getName()));
        }
        if (container.getVolumeMounts().stream().anyMatch(vm -> vm.getMountPath().equals(componentVolume.getContainerPath()))) {
            throw new DevfileException(format("Conflicting volume with same path ('%s') but different name ('%s') found for component '%s' and its container '%s'.", componentVolume.getContainerPath(), componentVolume.getName(), getIdentifiableComponentName(component), container.getName()));
        }
        config.getVolumes().put(componentVolume.getName(), new VolumeImpl().withPath(componentVolume.getContainerPath()));
    }
}
Also used : VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) WorkspaceConfig(org.eclipse.che.api.core.model.workspace.WorkspaceConfig) 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) ComponentToWorkspaceApplier(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ComponentToWorkspaceApplier.convertEndpointsIntoServers(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier.convertEndpointsIntoServers) Map(java.util.Map) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Command(org.eclipse.che.api.core.model.workspace.config.Command) KubernetesObjectUtil.newPVC(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newPVC) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) Set(java.util.Set) VolumeImpl(org.eclipse.che.api.workspace.server.model.impl.VolumeImpl) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) String.format(java.lang.String.format) 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) Optional(java.util.Optional) Names.machineName(org.eclipse.che.workspace.infrastructure.kubernetes.Names.machineName) KubernetesObjectUtil.newVolumeMount(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolumeMount) Entrypoint(org.eclipse.che.api.core.model.workspace.devfile.Entrypoint) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) 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) HashMap(java.util.HashMap) DevfileRecipeFormatException(org.eclipse.che.api.workspace.server.devfile.DevfileRecipeFormatException) ArrayList(java.util.ArrayList) KUBERNETES_BASED_COMPONENTS_KEY_NAME(org.eclipse.che.workspace.infrastructure.kubernetes.devfile.KubernetesDevfileBindings.KUBERNETES_BASED_COMPONENTS_KEY_NAME) Inject(javax.inject.Inject) MACHINE_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.MACHINE_NAME_ATTRIBUTE) Named(javax.inject.Named) Components.getIdentifiableComponentName(org.eclipse.che.api.workspace.server.devfile.Components.getIdentifiableComponentName) Volume(io.fabric8.kubernetes.api.model.Volume) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) KubernetesObjectUtil.newVolume(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolume) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) Collectors.toList(java.util.stream.Collectors.toList) SINGLE_HOST_STRATEGY(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.SingleHostExternalServiceExposureStrategy.SINGLE_HOST_STRATEGY) Constants(org.eclipse.che.api.workspace.server.devfile.Constants) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) KubernetesRecipeParser(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesRecipeParser) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) VolumeImpl(org.eclipse.che.api.workspace.server.model.impl.VolumeImpl) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) KubernetesObjectUtil.newVolumeMount(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolumeMount) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)

Example 17 with DevfileException

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

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 18 with DevfileException

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

the class DevfileToApiExceptionMapperTest method shouldReturnUnauthorizedExceptionIfCauseIsScmUnauthorized.

@Test
public void shouldReturnUnauthorizedExceptionIfCauseIsScmUnauthorized() {
    ScmUnauthorizedException scmUnauthorizedException = new ScmUnauthorizedException("msg", "gitlab", "2.0", "http://gitlab.com/oauth/authenticate");
    ApiException exception = DevfileToApiExceptionMapper.toApiException(new DevfileException("text", scmUnauthorizedException));
    assertTrue(exception instanceof UnauthorizedException);
    assertEquals(((ExtendedError) exception.getServiceError()).getErrorCode(), 401);
    assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().size(), 3);
    assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().get("oauth_version"), "2.0");
    assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().get("oauth_authentication_url"), "http://gitlab.com/oauth/authenticate");
}
Also used : ExtendedError(org.eclipse.che.api.core.rest.shared.dto.ExtendedError) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ApiException(org.eclipse.che.api.core.ApiException) Test(org.testng.annotations.Test)

Example 19 with DevfileException

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

the class DevfileToApiExceptionMapperTest method shouldReturnServerExceptionWhenCauseIsUnknownProvider.

@Test
public void shouldReturnServerExceptionWhenCauseIsUnknownProvider() {
    UnknownScmProviderException scmProviderException = new UnknownScmProviderException("unknown", "http://gitlab.com/oauth/authenticate");
    ApiException exception = DevfileToApiExceptionMapper.toApiException(new DevfileException("text", scmProviderException));
    assertTrue(exception instanceof ServerException);
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) UnknownScmProviderException(org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ApiException(org.eclipse.che.api.core.ApiException) Test(org.testng.annotations.Test)

Example 20 with DevfileException

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

the class URLFactoryBuilderTest method testDevfileSpecifyingFilename.

@Test
public void testDevfileSpecifyingFilename() throws ApiException, DevfileException {
    String myLocation = "http://foo-location/";
    Map<String, Object> devfileAsMap = Map.of("hello", "there", "how", "are", "you", "?");
    JsonNode devfile = new ObjectNode(JsonNodeFactory.instance);
    when(devfileParser.parseYamlRaw(anyString())).thenReturn(devfile);
    when(devfileParser.convertYamlToMap(devfile)).thenReturn(devfileAsMap);
    when(devfileVersionDetector.devfileMajorVersion(devfile)).thenReturn(2);
    RemoteFactoryUrl githubLikeRemoteUrl = new RemoteFactoryUrl() {

        private String devfileName = "default-devfile.yaml";

        @Override
        public String getProviderName() {
            return null;
        }

        @Override
        public List<DevfileLocation> devfileFileLocations() {
            return Collections.singletonList(new DevfileLocation() {

                @Override
                public Optional<String> filename() {
                    return Optional.of(devfileName);
                }

                @Override
                public String location() {
                    return myLocation;
                }
            });
        }

        @Override
        public String rawFileLocation(String filename) {
            return null;
        }

        @Override
        public String getHostName() {
            return null;
        }

        @Override
        public String getBranch() {
            return null;
        }

        @Override
        public void setDevfileFilename(String devfileName) {
            this.devfileName = devfileName;
        }
    };
    String pathToDevfile = "my-custom-devfile-path.yaml";
    Map<String, String> propertiesMap = singletonMap(URLFactoryBuilder.DEVFILE_FILENAME, pathToDevfile);
    FactoryMetaDto factory = urlFactoryBuilder.createFactoryFromDevfile(githubLikeRemoteUrl, s -> myLocation + ".list", propertiesMap).get();
    assertNotNull(factory);
    // Check that we didn't fetch from default files but from the parameter
    assertEquals(factory.getSource(), pathToDevfile);
    assertTrue(factory instanceof FactoryDevfileV2Dto);
    assertEquals(((FactoryDevfileV2Dto) factory).getDevfile(), devfileAsMap);
}
Also used : UnknownScmProviderException(org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException) Listeners(org.testng.annotations.Listeners) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) Test(org.testng.annotations.Test) Collections.singletonList(java.util.Collections.singletonList) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) DevfileLocation(org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl.DevfileLocation) DevfileVersionDetector(org.eclipse.che.api.workspace.server.devfile.DevfileVersionDetector) FactoryMetaDto(org.eclipse.che.api.factory.shared.dto.FactoryMetaDto) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) WORKSPACE_TOOLING_PLUGINS_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_PLUGINS_ATTRIBUTE) DtoFactory.newDto(org.eclipse.che.dto.server.DtoFactory.newDto) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertNotNull(org.testng.Assert.assertNotNull) MetadataImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.MetadataImpl) List(java.util.List) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Optional(java.util.Optional) FactoryDevfileV2Dto(org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Assert.assertNull(org.testng.Assert.assertNull) DataProvider(org.testng.annotations.DataProvider) URLFetcher(org.eclipse.che.api.workspace.server.devfile.URLFetcher) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) Mock(org.mockito.Mock) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Assert.assertEquals(org.testng.Assert.assertEquals) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DevfileParser(org.eclipse.che.api.workspace.server.devfile.DevfileParser) ApiException(org.eclipse.che.api.core.ApiException) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) Collections.singletonMap(java.util.Collections.singletonMap) ExtendedError(org.eclipse.che.api.core.rest.shared.dto.ExtendedError) Collections.emptyMap(java.util.Collections.emptyMap) WorkspaceConfigDto(org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) WORKSPACE_TOOLING_EDITOR_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_EDITOR_ATTRIBUTE) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) ServerException(org.eclipse.che.api.core.ServerException) OverrideParameterException(org.eclipse.che.api.workspace.server.devfile.exception.OverrideParameterException) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DevfileLocation(org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl.DevfileLocation) FactoryDevfileV2Dto(org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto) FactoryMetaDto(org.eclipse.che.api.factory.shared.dto.FactoryMetaDto) Test(org.testng.annotations.Test)

Aggregations

DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)52 Test (org.testng.annotations.Test)22 IOException (java.io.IOException)20 ApiException (org.eclipse.che.api.core.ApiException)18 FileContentProvider (org.eclipse.che.api.workspace.server.devfile.FileContentProvider)18 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)16 HashMap (java.util.HashMap)14 List (java.util.List)14 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)14 Map (java.util.Map)12 Optional (java.util.Optional)12 ServerException (org.eclipse.che.api.core.ServerException)12 ExtendedError (org.eclipse.che.api.core.rest.shared.dto.ExtendedError)12 ScmCommunicationException (org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException)12 ScmUnauthorizedException (org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)12 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 Collections (java.util.Collections)10 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)10 UnknownScmProviderException (org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException)10