PVCProvisioner { public void provision( KubernetesEnvironment k8sEnv, Map<String, PersistentVolumeClaim> toProvision) { final Map<String, PersistentVolumeClaim> volumeName2PVC = groupByVolumeName(k8sEnv.getPersistentVolumeClaims().values()); final Map<String, PersistentVolumeClaim> envClaims = k8sEnv.getPersistentVolumeClaims(); for (PersistentVolumeClaim pvc : toProvision.values()) { String originalPVCName = pvc.getMetadata().getName(); PersistentVolumeClaim existingPVC = volumeName2PVC.get(originalPVCName); if (existingPVC != null) { podsVolumes.changePVCReferences( k8sEnv.getPodsData().values(), originalPVCName, existingPVC.getMetadata().getName()); } else { putLabel(pvc, CHE_VOLUME_NAME_LABEL, originalPVCName); final String uniqueName = Names.generateName(pvcNamePrefix + '-'); pvc.getMetadata().setName(uniqueName); envClaims.put(uniqueName, pvc); volumeName2PVC.put(originalPVCName, pvc); podsVolumes.changePVCReferences(k8sEnv.getPodsData().values(), originalPVCName, uniqueName); } } } @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 testProvisioningPVCsToK8sEnvironment() throws Exception { k8sEnv = KubernetesEnvironment.builder().build(); Map<String, PersistentVolumeClaim> toProvision = new HashMap<>(); toProvision.put("appStorage", newPVC("appStorage")); Pod pod = newPod(POD_1_NAME) .withContainers( newContainer(CONTAINER_1_NAME) .withVolumeMount("appStorage", "/data", "data") .withVolumeMount("appStorage", "/config", "config") .build()) .withPVCVolume("appStorage", "appStorage") .build(); k8sEnv.addPod(pod); provisioner.provision(k8sEnv, toProvision); assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 1); PersistentVolumeClaim pvcForUserData = findPvc("appStorage", k8sEnv.getPersistentVolumeClaims()); assertNotNull(pvcForUserData); assertTrue(pvcForUserData.getMetadata().getName().startsWith(PVC_NAME_PREFIX)); verify(podsVolumes) .changePVCReferences( k8sEnv.getPodsData().values(), "appStorage", pvcForUserData.getMetadata().getName()); }
@Test public void testMatchEnvPVCsByVolumeNameWhenProvisioningPVCsToK8sEnvironment() throws Exception { k8sEnv = KubernetesEnvironment.builder().build(); k8sEnv .getPersistentVolumeClaims() .put( "appStorage", newPVC("pvc123123", ImmutableMap.of(CHE_VOLUME_NAME_LABEL, "appStorage"))); Map<String, PersistentVolumeClaim> toProvision = new HashMap<>(); toProvision.put("appStorage", newPVC("appStorage")); Pod pod = newPod(POD_1_NAME) .withContainers( newContainer(CONTAINER_1_NAME) .withVolumeMount("appStorage", "/data", "data") .withVolumeMount("appStorage", "/config", "config") .build()) .withPVCVolume("appStorage", "appStorage") .build(); k8sEnv.addPod(pod); provisioner.provision(k8sEnv, toProvision); assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 1); PersistentVolumeClaim pvcForUserData = findPvc("appStorage", k8sEnv.getPersistentVolumeClaims()); assertNotNull(pvcForUserData); assertEquals("pvc123123", pvcForUserData.getMetadata().getName()); verify(podsVolumes) .changePVCReferences(k8sEnv.getPodsData().values(), "appStorage", "pvc123123"); }