Search in sources :

Example 1 with Volume

use of org.eclipse.che.api.core.model.workspace.config.Volume in project che-server by eclipse-che.

the class PVCProvisioner method convertCheVolumes.

/**
 * Converts {@link Volume} specified in {@link MachineConfig#getVolumes()} to {@link
 * PersistentVolumeClaim}s and provision them to {@link KubernetesEnvironment}. The machines
 * corresponding pods and containers are updated in accordance.
 *
 * @param k8sEnv environment to provision
 * @param workspaceId identifier of workspace to which the specified environment belongs to
 */
public void convertCheVolumes(KubernetesEnvironment k8sEnv, String workspaceId) {
    Map<String, PersistentVolumeClaim> volumeName2PVC = groupByVolumeName(k8sEnv.getPersistentVolumeClaims().values());
    for (PodData pod : k8sEnv.getPodsData().values()) {
        final PodSpec podSpec = pod.getSpec();
        List<Container> containers = new ArrayList<>();
        containers.addAll(podSpec.getContainers());
        containers.addAll(podSpec.getInitContainers());
        for (Container container : containers) {
            final String machineName = Names.machineName(pod, container);
            InternalMachineConfig machineConfig = k8sEnv.getMachines().get(machineName);
            if (machineConfig == null) {
                continue;
            }
            Map<String, Volume> volumes = machineConfig.getVolumes();
            addMachineVolumes(workspaceId, k8sEnv, volumeName2PVC, pod, container, volumes);
        }
    }
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) Container(io.fabric8.kubernetes.api.model.Container) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) KubernetesObjectUtil.newVolume(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolume) Volume(org.eclipse.che.api.core.model.workspace.config.Volume) ArrayList(java.util.ArrayList) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim)

Example 2 with Volume

use of org.eclipse.che.api.core.model.workspace.config.Volume in project che-server by eclipse-che.

the class WorkspaceValidator method validateMachine.

private void validateMachine(String machineName, MachineConfig machine) throws ValidationException {
    validateLongAttribute(MEMORY_LIMIT_ATTRIBUTE, machine.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE), machineName);
    validateLongAttribute(MEMORY_REQUEST_ATTRIBUTE, machine.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE), machineName);
    for (Entry<String, ? extends Volume> volumeEntry : machine.getVolumes().entrySet()) {
        String volumeName = volumeEntry.getKey();
        check(VOLUME_NAME.matcher(volumeName).matches(), "Volume name '%s' in machine '%s' is invalid", volumeName, machineName);
        Volume volume = volumeEntry.getValue();
        check(volume != null && !isNullOrEmpty(volume.getPath()), "Path of volume '%s' in machine '%s' is invalid. It should not be empty", volumeName, machineName);
        check(VOLUME_PATH.matcher(volume.getPath()).matches(), "Path '%s' of volume '%s' in machine '%s' is invalid. It should be absolute", volume.getPath(), volumeName, machineName);
    }
}
Also used : Volume(org.eclipse.che.api.core.model.workspace.config.Volume)

Example 3 with Volume

use of org.eclipse.che.api.core.model.workspace.config.Volume in project che-server by eclipse-che.

the class PVCProvisioner method addMachineVolumes.

private void addMachineVolumes(String workspaceId, KubernetesEnvironment k8sEnv, Map<String, PersistentVolumeClaim> volumeName2PVC, PodData pod, Container container, Map<String, Volume> volumes) {
    if (volumes.isEmpty()) {
        return;
    }
    for (Entry<String, Volume> volumeEntry : volumes.entrySet()) {
        final String volumePath = volumeEntry.getValue().getPath();
        final String volumeName = LOGS_VOLUME_NAME.equals(volumeEntry.getKey()) ? volumeEntry.getKey() + '-' + pod.getMetadata().getName() : volumeEntry.getKey();
        final PersistentVolumeClaim pvc;
        // checks whether PVC for given workspace and volume present in environment
        if (volumeName2PVC.containsKey(volumeName)) {
            pvc = volumeName2PVC.get(volumeName);
        } else // when PVC is not found in environment then create new one
        {
            final String uniqueName = Names.generateName(pvcNamePrefix);
            pvc = newPVC(uniqueName, pvcAccessMode, pvcQuantity, pvcStorageClassName);
            putLabel(pvc, CHE_WORKSPACE_ID_LABEL, workspaceId);
            putLabel(pvc, CHE_VOLUME_NAME_LABEL, volumeName);
            k8sEnv.getPersistentVolumeClaims().put(uniqueName, pvc);
            volumeName2PVC.put(volumeName, pvc);
        }
        // binds pvc to pod and container
        String pvcName = pvc.getMetadata().getName();
        PodSpec podSpec = pod.getSpec();
        Optional<io.fabric8.kubernetes.api.model.Volume> volumeOpt = podSpec.getVolumes().stream().filter(volume -> volume.getPersistentVolumeClaim() != null && pvcName.equals(volume.getPersistentVolumeClaim().getClaimName())).findAny();
        io.fabric8.kubernetes.api.model.Volume podVolume;
        if (volumeOpt.isPresent()) {
            podVolume = volumeOpt.get();
        } else {
            podVolume = newVolume(pvcName, pvcName);
            podSpec.getVolumes().add(podVolume);
        }
        container.getVolumeMounts().add(newVolumeMount(podVolume.getName(), volumePath, ""));
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) KubernetesObjectUtil.putLabel(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putLabel) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Map(java.util.Map) Named(javax.inject.Named) KubernetesObjectUtil.newPVC(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newPVC) LOGS_VOLUME_NAME(org.eclipse.che.workspace.infrastructure.kubernetes.provision.LogsVolumeMachineProvisioner.LOGS_VOLUME_NAME) CHE_VOLUME_NAME_LABEL(org.eclipse.che.workspace.infrastructure.kubernetes.Constants.CHE_VOLUME_NAME_LABEL) Collection(java.util.Collection) KubernetesObjectUtil.newVolume(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolume) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) List(java.util.List) Volume(org.eclipse.che.api.core.model.workspace.config.Volume) CHE_WORKSPACE_ID_LABEL(org.eclipse.che.workspace.infrastructure.kubernetes.Constants.CHE_WORKSPACE_ID_LABEL) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Entry(java.util.Map.Entry) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Optional(java.util.Optional) KubernetesObjectUtil.newVolumeMount(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolumeMount) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) KubernetesObjectUtil.newVolume(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolume) Volume(org.eclipse.che.api.core.model.workspace.config.Volume) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim)

Example 4 with Volume

use of org.eclipse.che.api.core.model.workspace.config.Volume in project devspaces-images by redhat-developer.

the class WorkspaceValidator method validateMachine.

private void validateMachine(String machineName, MachineConfig machine) throws ValidationException {
    validateLongAttribute(MEMORY_LIMIT_ATTRIBUTE, machine.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE), machineName);
    validateLongAttribute(MEMORY_REQUEST_ATTRIBUTE, machine.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE), machineName);
    for (Entry<String, ? extends Volume> volumeEntry : machine.getVolumes().entrySet()) {
        String volumeName = volumeEntry.getKey();
        check(VOLUME_NAME.matcher(volumeName).matches(), "Volume name '%s' in machine '%s' is invalid", volumeName, machineName);
        Volume volume = volumeEntry.getValue();
        check(volume != null && !isNullOrEmpty(volume.getPath()), "Path of volume '%s' in machine '%s' is invalid. It should not be empty", volumeName, machineName);
        check(VOLUME_PATH.matcher(volume.getPath()).matches(), "Path '%s' of volume '%s' in machine '%s' is invalid. It should be absolute", volume.getPath(), volumeName, machineName);
    }
}
Also used : Volume(org.eclipse.che.api.core.model.workspace.config.Volume)

Example 5 with Volume

use of org.eclipse.che.api.core.model.workspace.config.Volume in project devspaces-images by redhat-developer.

the class PVCProvisioner method addMachineVolumes.

private void addMachineVolumes(String workspaceId, KubernetesEnvironment k8sEnv, Map<String, PersistentVolumeClaim> volumeName2PVC, PodData pod, Container container, Map<String, Volume> volumes) {
    if (volumes.isEmpty()) {
        return;
    }
    for (Entry<String, Volume> volumeEntry : volumes.entrySet()) {
        final String volumePath = volumeEntry.getValue().getPath();
        final String volumeName = LOGS_VOLUME_NAME.equals(volumeEntry.getKey()) ? volumeEntry.getKey() + '-' + pod.getMetadata().getName() : volumeEntry.getKey();
        final PersistentVolumeClaim pvc;
        // checks whether PVC for given workspace and volume present in environment
        if (volumeName2PVC.containsKey(volumeName)) {
            pvc = volumeName2PVC.get(volumeName);
        } else // when PVC is not found in environment then create new one
        {
            final String uniqueName = Names.generateName(pvcNamePrefix);
            pvc = newPVC(uniqueName, pvcAccessMode, pvcQuantity, pvcStorageClassName);
            putLabel(pvc, CHE_WORKSPACE_ID_LABEL, workspaceId);
            putLabel(pvc, CHE_VOLUME_NAME_LABEL, volumeName);
            k8sEnv.getPersistentVolumeClaims().put(uniqueName, pvc);
            volumeName2PVC.put(volumeName, pvc);
        }
        // binds pvc to pod and container
        String pvcName = pvc.getMetadata().getName();
        PodSpec podSpec = pod.getSpec();
        Optional<io.fabric8.kubernetes.api.model.Volume> volumeOpt = podSpec.getVolumes().stream().filter(volume -> volume.getPersistentVolumeClaim() != null && pvcName.equals(volume.getPersistentVolumeClaim().getClaimName())).findAny();
        io.fabric8.kubernetes.api.model.Volume podVolume;
        if (volumeOpt.isPresent()) {
            podVolume = volumeOpt.get();
        } else {
            podVolume = newVolume(pvcName, pvcName);
            podSpec.getVolumes().add(podVolume);
        }
        container.getVolumeMounts().add(newVolumeMount(podVolume.getName(), volumePath, ""));
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) KubernetesObjectUtil.putLabel(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putLabel) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Map(java.util.Map) Named(javax.inject.Named) KubernetesObjectUtil.newPVC(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newPVC) LOGS_VOLUME_NAME(org.eclipse.che.workspace.infrastructure.kubernetes.provision.LogsVolumeMachineProvisioner.LOGS_VOLUME_NAME) CHE_VOLUME_NAME_LABEL(org.eclipse.che.workspace.infrastructure.kubernetes.Constants.CHE_VOLUME_NAME_LABEL) Collection(java.util.Collection) KubernetesObjectUtil.newVolume(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolume) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) List(java.util.List) Volume(org.eclipse.che.api.core.model.workspace.config.Volume) CHE_WORKSPACE_ID_LABEL(org.eclipse.che.workspace.infrastructure.kubernetes.Constants.CHE_WORKSPACE_ID_LABEL) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Entry(java.util.Map.Entry) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Optional(java.util.Optional) KubernetesObjectUtil.newVolumeMount(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolumeMount) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) KubernetesObjectUtil.newVolume(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolume) Volume(org.eclipse.che.api.core.model.workspace.config.Volume) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim)

Aggregations

Volume (org.eclipse.che.api.core.model.workspace.config.Volume)6 Container (io.fabric8.kubernetes.api.model.Container)4 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)4 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)4 ArrayList (java.util.ArrayList)4 InternalMachineConfig (org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig)4 PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)4 KubernetesObjectUtil.newVolume (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolume)4 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Optional (java.util.Optional)2 Inject (javax.inject.Inject)2 Named (javax.inject.Named)2 MachineConfig (org.eclipse.che.api.core.model.workspace.config.MachineConfig)2 CHE_VOLUME_NAME_LABEL (org.eclipse.che.workspace.infrastructure.kubernetes.Constants.CHE_VOLUME_NAME_LABEL)2 CHE_WORKSPACE_ID_LABEL (org.eclipse.che.workspace.infrastructure.kubernetes.Constants.CHE_WORKSPACE_ID_LABEL)2