Search in sources :

Example 1 with ComponentImpl

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

the class EnvironmentVariableSecretApplier method applySecret.

/**
 * Applies secret as environment variable into workspace containers, respecting automount
 * attribute and optional devfile automount property override.
 *
 * @param env kubernetes environment with workspace containers configuration
 * @param runtimeIdentity identity of current runtime
 * @param secret source secret to apply
 * @throws InfrastructureException on misconfigured secrets or other apply error
 */
@Override
public void applySecret(KubernetesEnvironment env, RuntimeIdentity runtimeIdentity, Secret secret) throws InfrastructureException {
    boolean secretAutomount = Boolean.parseBoolean(secret.getMetadata().getAnnotations().get(ANNOTATION_AUTOMOUNT));
    for (PodData podData : env.getPodsData().values()) {
        if (!podData.getRole().equals(PodRole.DEPLOYMENT)) {
            continue;
        }
        for (Container container : podData.getSpec().getContainers()) {
            Optional<ComponentImpl> component = getComponent(env, container.getName());
            // skip components that explicitly disable automount
            if (component.isPresent() && isComponentAutomountFalse(component.get())) {
                continue;
            }
            // if automount disabled globally and not overridden in component
            if (!secretAutomount && (!component.isPresent() || !isComponentAutomountTrue(component.get()))) {
                continue;
            }
            for (Entry<String, String> secretDataEntry : secret.getData().entrySet()) {
                final String mountEnvName = envName(secret, secretDataEntry.getKey(), runtimeIdentity);
                container.getEnv().add(new EnvVarBuilder().withName(mountEnvName).withValueFrom(new EnvVarSourceBuilder().withSecretKeyRef(new SecretKeySelectorBuilder().withName(secret.getMetadata().getName()).withKey(secretDataEntry.getKey()).build()).build()).build());
            }
        }
    }
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Container(io.fabric8.kubernetes.api.model.Container) EnvVarSourceBuilder(io.fabric8.kubernetes.api.model.EnvVarSourceBuilder) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) SecretKeySelectorBuilder(io.fabric8.kubernetes.api.model.SecretKeySelectorBuilder)

Example 2 with ComponentImpl

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

the class OpenshiftComponentToWorkspaceApplierTest method serverMustHaveRequireSubdomainWhenNonSinglehostDevfileExpose.

@Test
public void serverMustHaveRequireSubdomainWhenNonSinglehostDevfileExpose() throws DevfileException, IOException, ValidationException, InfrastructureException {
    Set<String> openshiftBasedComponents = new HashSet<>();
    openshiftBasedComponents.add(OPENSHIFT_COMPONENT_TYPE);
    applier = new OpenshiftComponentToWorkspaceApplier(k8sRecipeParser, k8sEnvProvisioner, envVars, "/projects", "1Gi", "ReadWriteOnce", "", "Always", MULTI_HOST_STRATEGY, openshiftBasedComponents);
    String yamlRecipeContent = getResource("devfile/petclinic.yaml");
    doReturn(toK8SList(yamlRecipeContent).getItems()).when(k8sRecipeParser).parse(anyString());
    // given
    ComponentImpl component = new ComponentImpl();
    component.setType(OPENSHIFT_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    component.setEndpoints(Arrays.asList(new EndpointImpl("e1", 1111, emptyMap()), new EndpointImpl("e2", 2222, emptyMap())));
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
    // then
    @SuppressWarnings("unchecked") ArgumentCaptor<Map<String, MachineConfigImpl>> objectsCaptor = ArgumentCaptor.forClass(Map.class);
    verify(k8sEnvProvisioner).provision(any(), any(), any(), objectsCaptor.capture());
    Map<String, MachineConfigImpl> machineConfigs = objectsCaptor.getValue();
    assertEquals(machineConfigs.size(), 4);
    machineConfigs.values().forEach(machineConfig -> {
        assertEquals(machineConfig.getServers().size(), 2);
        assertTrue(ServerConfig.isRequireSubdomain(machineConfig.getServers().get("e1").getAttributes()));
        assertTrue(ServerConfig.isRequireSubdomain(machineConfig.getServers().get("e2").getAttributes()));
    });
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) EndpointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 3 with ComponentImpl

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

the class OpenshiftComponentToWorkspaceApplierTest method serverCantHaveRequireSubdomainWhenSinglehostDevfileExpose.

@Test
public void serverCantHaveRequireSubdomainWhenSinglehostDevfileExpose() throws DevfileException, IOException, ValidationException, InfrastructureException {
    Set<String> openshiftBasedComponents = new HashSet<>();
    openshiftBasedComponents.add(OPENSHIFT_COMPONENT_TYPE);
    applier = new OpenshiftComponentToWorkspaceApplier(k8sRecipeParser, k8sEnvProvisioner, envVars, "/projects", "1Gi", "ReadWriteOnce", "", "Always", SINGLE_HOST_STRATEGY, openshiftBasedComponents);
    String yamlRecipeContent = getResource("devfile/petclinic.yaml");
    doReturn(toK8SList(yamlRecipeContent).getItems()).when(k8sRecipeParser).parse(anyString());
    // given
    ComponentImpl component = new ComponentImpl();
    component.setType(OPENSHIFT_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    component.setEndpoints(Arrays.asList(new EndpointImpl("e1", 1111, emptyMap()), new EndpointImpl("e2", 2222, emptyMap())));
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
    // then
    @SuppressWarnings("unchecked") ArgumentCaptor<Map<String, MachineConfigImpl>> objectsCaptor = ArgumentCaptor.forClass(Map.class);
    verify(k8sEnvProvisioner).provision(any(), any(), any(), objectsCaptor.capture());
    Map<String, MachineConfigImpl> machineConfigs = objectsCaptor.getValue();
    assertEquals(machineConfigs.size(), 4);
    machineConfigs.values().forEach(machineConfig -> {
        assertEquals(machineConfig.getServers().size(), 2);
        assertFalse(ServerConfig.isRequireSubdomain(machineConfig.getServers().get("e1").getAttributes()));
        assertFalse(ServerConfig.isRequireSubdomain(machineConfig.getServers().get("e2").getAttributes()));
    });
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) EndpointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 4 with ComponentImpl

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

the class OpenshiftComponentToWorkspaceApplierTest method shouldProvisionEnvironmentWithCorrectRecipeTypeAndContentFromOSList.

@Test
public void shouldProvisionEnvironmentWithCorrectRecipeTypeAndContentFromOSList() throws Exception {
    // given
    doReturn(emptyList()).when(k8sRecipeParser).parse(anyString());
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    // when
    applier.apply(workspaceConfig, component, s -> "content");
    // then
    verify(k8sEnvProvisioner).provision(workspaceConfig, OpenShiftEnvironment.TYPE, emptyList(), emptyMap());
}
Also used : ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 5 with ComponentImpl

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

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)

Aggregations

ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)218 Test (org.testng.annotations.Test)184 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)76 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)56 Container (io.fabric8.kubernetes.api.model.Container)48 MachineConfigImpl (org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)44 EndpointImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl)42 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)38 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)36 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)26 EntrypointImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EntrypointImpl)26 Map (java.util.Map)24 EnvImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl)24 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)22 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)20 Collections.emptyMap (java.util.Collections.emptyMap)18 DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)18 ImmutableMap (com.google.common.collect.ImmutableMap)16 ArrayList (java.util.ArrayList)14 List (java.util.List)14