PVCProvisioner { 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); } } } @Inject  PVCProvisioner(
      @Named("che.infra.kubernetes.pvc.name") String pvcNamePrefix,
      @Named("che.infra.kubernetes.pvc.quantity") String pvcQuantity,
      @Named("che.infra.kubernetes.pvc.access_mode") String pvcAccessMode,
      @Named("che.infra.kubernetes.pvc.storage_class_name") String pvcStorageClassName,
      PodsVolumes podsVolumes); void convertCheVolumes(KubernetesEnvironment k8sEnv, String workspaceId); void provision(
      KubernetesEnvironment k8sEnv, Map<String, PersistentVolumeClaim> toProvision);  }
@Test public void testProvisionPVCsForEachVolumeWithUniqueName() throws Exception { k8sEnv.getPersistentVolumeClaims().clear(); provisioner.convertCheVolumes(k8sEnv, WORKSPACE_ID); assertEquals(pod.getSpec().getVolumes().size(), 2); assertEquals(pod.getSpec().getContainers().get(0).getVolumeMounts().size(), 2); assertEquals(pod.getSpec().getContainers().get(1).getVolumeMounts().size(), 1); assertEquals(pod2.getSpec().getVolumes().size(), 1); assertEquals(pod2.getSpec().getContainers().get(0).getVolumeMounts().size(), 1); assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 2); PersistentVolumeClaim pvcForVolume1 = findPvc(VOLUME_1_NAME, k8sEnv.getPersistentVolumeClaims()); assertNotNull(pvcForVolume1); assertTrue(pvcForVolume1.getMetadata().getName().startsWith(PVC_NAME_PREFIX)); PersistentVolumeClaim pvcForVolume2 = findPvc(VOLUME_2_NAME, k8sEnv.getPersistentVolumeClaims()); assertNotNull(pvcForVolume2); assertTrue(pvcForVolume2.getMetadata().getName().startsWith(PVC_NAME_PREFIX)); }
@Test public void testMatchingUserDefinedPVCWithCheVolume() throws Exception { k8sEnv.getPersistentVolumeClaims().put("userDataPVC", newPVC("userDataPVC")); pod.getSpec() .getVolumes() .add( new VolumeBuilder() .withName("userData") .withPersistentVolumeClaim( new PersistentVolumeClaimVolumeSourceBuilder() .withClaimName("userDataPVC") .build()) .build()); pod.getSpec() .getContainers() .get(0) .getVolumeMounts() .add(new VolumeMountBuilder().withName("userData").withSubPath("/home/user/data").build()); k8sEnv.getMachines().values().forEach(m -> m.getVolumes().clear()); k8sEnv .getMachines() .get(MACHINE_2_NAME) .getVolumes() .put("userDataPVC", new VolumeImpl().withPath("/")); provisioner.convertCheVolumes(k8sEnv, WORKSPACE_ID); assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 1); PersistentVolumeClaim pvcForUserData = findPvc("userDataPVC", k8sEnv.getPersistentVolumeClaims()); assertNotNull(pvcForUserData); assertEquals("userDataPVC", pvcForUserData.getMetadata().getName()); PodSpec podSpec = k8sEnv.getPodsData().get(POD_1_NAME).getSpec(); io.fabric8.kubernetes.api.model.Volume userPodVolume = podSpec.getVolumes().get(0); assertEquals( userPodVolume.getPersistentVolumeClaim().getClaimName(), pvcForUserData.getMetadata().getName()); assertEquals( podSpec.getVolumes().get(0).getPersistentVolumeClaim().getClaimName(), pvcForUserData.getMetadata().getName()); Container container1 = podSpec.getContainers().get(0); assertEquals(container1.getVolumeMounts().size(), 1); VolumeMount volumeMount = container1.getVolumeMounts().get(0); assertEquals(volumeMount.getName(), userPodVolume.getName()); Container container2 = podSpec.getContainers().get(1); VolumeMount cheVolumeMount2 = container2.getVolumeMounts().get(0); assertEquals(cheVolumeMount2.getName(), userPodVolume.getName()); }
@Test public void testDoNotProvisionPVCsWhenItIsAlreadyProvisionedForGivenVolumeAndWorkspace() throws Exception { final String pvcUniqueName1 = PVC_NAME_PREFIX + "-3121"; PersistentVolumeClaim pvc1 = newPVC(pvcUniqueName1, ImmutableMap.of(CHE_VOLUME_NAME_LABEL, VOLUME_1_NAME)); pvc1.getAdditionalProperties().put("CHE_PROVISIONED", true); final String pvcUniqueName2 = PVC_NAME_PREFIX + "-71333"; PersistentVolumeClaim pvc2 = newPVC(pvcUniqueName2, ImmutableMap.of(CHE_VOLUME_NAME_LABEL, VOLUME_2_NAME)); pvc2.getAdditionalProperties().put("CHE_PROVISIONED", true); k8sEnv.getPersistentVolumeClaims().put(pvc1.getMetadata().getName(), pvc1); k8sEnv.getPersistentVolumeClaims().put(pvc2.getMetadata().getName(), pvc2); provisioner.convertCheVolumes(k8sEnv, WORKSPACE_ID); assertEquals(pod.getSpec().getVolumes().size(), 2); assertEquals(pod.getSpec().getContainers().get(0).getVolumeMounts().size(), 2); assertEquals(pod.getSpec().getContainers().get(1).getVolumeMounts().size(), 1); assertEquals(pod2.getSpec().getVolumes().size(), 1); assertEquals(pod2.getSpec().getContainers().get(0).getVolumeMounts().size(), 1); assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 2); assertTrue(k8sEnv.getPersistentVolumeClaims().containsKey(pvcUniqueName1)); assertTrue(k8sEnv.getPersistentVolumeClaims().containsKey(pvcUniqueName2)); }