CommonPVCStrategy implements WorkspaceVolumesStrategy { @Override public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException { final String workspaceId = identity.getWorkspaceId(); if (EphemeralWorkspaceUtility.isEphemeral(k8sEnv.getAttributes())) { ephemeralWorkspaceAdapter.provision(k8sEnv, identity); return; } log.debug("Provisioning PVC strategy for workspace '{}'", workspaceId); pvcProvisioner.convertCheVolumes(k8sEnv, workspaceId); subpathPrefixes.prefixVolumeMountsSubpaths(k8sEnv, identity.getWorkspaceId()); PersistentVolumeClaim commonPVC = replacePVCsWithCommon(k8sEnv, identity); podsVolumes.replacePVCVolumesWithCommon( k8sEnv.getPodsData(), commonPVC.getMetadata().getName()); if (preCreateDirs) { Set<String> subPaths = combineVolumeMountsSubpaths(k8sEnv); if (!subPaths.isEmpty()) { commonPVC.setAdditionalProperty( format(SUBPATHS_PROPERTY_FMT, workspaceId), subPaths.toArray(new String[subPaths.size()])); } } log.debug("PVC strategy provisioning done for workspace '{}'", workspaceId); } @Inject  CommonPVCStrategy(
      @Named("che.infra.kubernetes.pvc.name") String configuredPVCName,
      @Named("che.infra.kubernetes.pvc.quantity") String pvcQuantity,
      @Named("che.infra.kubernetes.pvc.access_mode") String pvcAccessMode,
      @Named("che.infra.kubernetes.pvc.precreate_subpaths") boolean preCreateDirs,
      @Named("che.infra.kubernetes.pvc.storage_class_name") String pvcStorageClassName,
      @Named("che.infra.kubernetes.pvc.wait_bound") boolean waitBound,
      PVCSubPathHelper pvcSubPathHelper,
      KubernetesNamespaceFactory factory,
      EphemeralWorkspaceAdapter ephemeralWorkspaceAdapter,
      PVCProvisioner pvcProvisioner,
      PodsVolumes podsVolumes,
      SubPathPrefixes subpathPrefixes); @Override void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity); @Override @Traced void prepare(
      KubernetesEnvironment k8sEnv,
      RuntimeIdentity identity,
      long timeoutMillis,
      Map<String, String> startOptions); @Override void cleanup(Workspace workspace); static final String COMMON_STRATEGY; }
@Test public void testProvisionVolumesIntoKubernetesEnvironment() throws Exception { k8sEnv.getPersistentVolumeClaims().put("pvc1", newPVC("pvc1")); k8sEnv.getPersistentVolumeClaims().put("pvc2", newPVC("pvc2")); commonPVCStrategy.provision(k8sEnv, IDENTITY); provisionOrder.verify(volumeConverter).convertCheVolumes(k8sEnv, WORKSPACE_ID); provisionOrder.verify(subpathPrefixes).prefixVolumeMountsSubpaths(k8sEnv, WORKSPACE_ID); provisionOrder.verify(podsVolumes).replacePVCVolumesWithCommon(k8sEnv.getPodsData(), PVC_NAME); assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 1); PersistentVolumeClaim commonPVC = k8sEnv.getPersistentVolumeClaims().get(PVC_NAME); assertNotNull(commonPVC); assertEquals(commonPVC.getMetadata().getName(), PVC_NAME); assertEquals(commonPVC.getSpec().getAccessModes(), Collections.singletonList(PVC_ACCESS_MODE)); assertEquals( commonPVC.getSpec().getResources().getRequests().get("storage"), new Quantity(PVC_QUANTITY)); }
@Test public void testReplacePVCWhenItsAlreadyInKubernetesEnvironment() throws Exception { PersistentVolumeClaim provisioned = newPVC(PVC_NAME); k8sEnv.getPersistentVolumeClaims().put(PVC_NAME, provisioned); commonPVCStrategy.provision(k8sEnv, IDENTITY); assertNotEquals(k8sEnv.getPersistentVolumeClaims().get(PVC_NAME), provisioned); }
@Test public void testDoNotAddsSubpathsPropertyWhenPreCreationIsNotNeeded() throws Exception { commonPVCStrategy = new CommonPVCStrategy( PVC_NAME, PVC_QUANTITY, PVC_ACCESS_MODE, false, PVC_STORAGE_CLASS_NAME, true, pvcSubPathHelper, factory, ephemeralWorkspaceAdapter, volumeConverter, podsVolumes, subpathPrefixes); commonPVCStrategy.provision(k8sEnv, IDENTITY); final Map<String, PersistentVolumeClaim> actual = k8sEnv.getPersistentVolumeClaims(); assertFalse(actual.isEmpty()); assertTrue(actual.containsKey(PVC_NAME)); assertFalse( actual .get(PVC_NAME) .getAdditionalProperties() .containsKey(format(SUBPATHS_PROPERTY_FMT, WORKSPACE_ID))); }