UniqueWorkspacePVCStrategy 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); Map<String, PersistentVolumeClaim> userDefinedPVCs = new HashMap<>(k8sEnv.getPersistentVolumeClaims()); k8sEnv.getPersistentVolumeClaims().clear(); fillInExistingPVCs(k8sEnv, identity); pvcProvisioner.provision(k8sEnv, userDefinedPVCs); pvcProvisioner.convertCheVolumes(k8sEnv, workspaceId); subpathPrefixes.prefixVolumeMountsSubpaths(k8sEnv, identity.getWorkspaceId()); provisionWorkspaceIdLabel(k8sEnv.getPersistentVolumeClaims(), identity.getWorkspaceId()); LOG.debug("PVC strategy provisioning done for workspace '{}'", workspaceId); } @Inject  UniqueWorkspacePVCStrategy(
      @Named("che.infra.kubernetes.pvc.wait_bound") boolean waitBound,
      KubernetesNamespaceFactory factory,
      EphemeralWorkspaceAdapter ephemeralWorkspaceAdapter,
      PVCProvisioner pvcProvisioner,
      SubPathPrefixes subpathPrefixes); @Override void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity); @Traced @Override void prepare(
      KubernetesEnvironment k8sEnv,
      RuntimeIdentity identity,
      long timeoutMillis,
      Map<String, String> startOptions); @Override void cleanup(Workspace workspace); static final String UNIQUE_STRATEGY; }
@Test public void testProvisionVolumesIntoKubernetesEnvironment() throws Exception { PersistentVolumeClaim pvc1 = newPVC("pvc1"); PersistentVolumeClaim pvc2 = newPVC("pvc2"); k8sEnv.getPersistentVolumeClaims().put("pvc1", pvc1); k8sEnv.getPersistentVolumeClaims().put("pvc2", pvc2); PersistentVolumeClaim existingPVC = newPVC("existingPVC"); when(pvcs.getByLabel(CHE_WORKSPACE_ID_LABEL, WORKSPACE_ID)) .thenReturn(singletonList(existingPVC)); strategy.provision(k8sEnv, IDENTITY); provisionOrder .verify(pvcProvisioner) .provision(k8sEnvCaptor.capture(), eq(ImmutableMap.of("pvc1", pvc1, "pvc2", pvc2))); provisionOrder.verify(pvcProvisioner).convertCheVolumes(k8sEnv, WORKSPACE_ID); provisionOrder.verify(subpathPrefixes).prefixVolumeMountsSubpaths(k8sEnv, WORKSPACE_ID); assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 1); assertNotNull(k8sEnv.getPersistentVolumeClaims().get("existingPVC")); ; }
@Test public void shouldProvisionWorkspaceIdLabelToPVCs() throws Exception { PersistentVolumeClaim existingPVC = newPVC("existingPVC"); when(pvcs.getByLabel(CHE_WORKSPACE_ID_LABEL, WORKSPACE_ID)) .thenReturn(singletonList(existingPVC)); strategy.provision(k8sEnv, IDENTITY); assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 1); PersistentVolumeClaim pvc = k8sEnv.getPersistentVolumeClaims().get("existingPVC"); assertNotNull(pvc); assertEquals(pvc.getMetadata().getLabels().get(CHE_WORKSPACE_ID_LABEL), WORKSPACE_ID); }