UniqueWorkspacePVCStrategy implements WorkspaceVolumesStrategy { @Traced @Override public void prepare( KubernetesEnvironment k8sEnv, RuntimeIdentity identity, long timeoutMillis, Map<String, String> startOptions) throws InfrastructureException { String workspaceId = identity.getWorkspaceId(); TracingTags.WORKSPACE_ID.set(workspaceId); if (EphemeralWorkspaceUtility.isEphemeral(k8sEnv.getAttributes())) { return; } if (k8sEnv.getPersistentVolumeClaims().isEmpty()) { return; } final KubernetesPersistentVolumeClaims k8sClaims = factory.getOrCreate(identity).persistentVolumeClaims(); LOG.debug("Creating PVCs for workspace '{}'", workspaceId); k8sClaims.createIfNotExist(k8sEnv.getPersistentVolumeClaims().values()); if (waitBound) { LOG.debug("Waiting for PVC(s) of workspace '{}' to be bound", workspaceId); for (PersistentVolumeClaim pvc : k8sEnv.getPersistentVolumeClaims().values()) { k8sClaims.waitBound(pvc.getMetadata().getName(), timeoutMillis); } } LOG.debug("Preparing PVCs 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 testCreatesProvisionedPVCsOnPrepare() throws Exception { final String uniqueName = PVC_NAME_PREFIX + "-3121"; final PersistentVolumeClaim pvc = newPVC(uniqueName); k8sEnv.getPersistentVolumeClaims().clear(); k8sEnv.getPersistentVolumeClaims().putAll(singletonMap(uniqueName, pvc)); doReturn(pvc).when(pvcs).create(any()); strategy.prepare(k8sEnv, IDENTITY, 100, emptyMap()); verify(pvcs).createIfNotExist(any()); verify(pvcs).waitBound(uniqueName, 100); }
@Test public void testCreatesProvisionedPVCsOnPrepareIfWaitIsDisabled() throws Exception { strategy = new UniqueWorkspacePVCStrategy( false, factory, ephemeralWorkspaceAdapter, pvcProvisioner, subpathPrefixes); final String uniqueName = PVC_NAME_PREFIX + "-3121"; final PersistentVolumeClaim pvc = newPVC(uniqueName); k8sEnv.getPersistentVolumeClaims().clear(); k8sEnv.getPersistentVolumeClaims().putAll(singletonMap(uniqueName, pvc)); doReturn(pvc).when(pvcs).create(any()); strategy.prepare(k8sEnv, IDENTITY, 100, emptyMap()); verify(pvcs).createIfNotExist(any()); verify(pvcs, never()).waitBound(anyString(), anyLong()); }
@Test(expectedExceptions = InfrastructureException.class) public void throwsInfrastructureExceptionWhenFailedToCreatePVCs() throws Exception { final PersistentVolumeClaim pvc = mock(PersistentVolumeClaim.class); when(pvc.getMetadata()).thenReturn(new ObjectMetaBuilder().withName(PVC_NAME_PREFIX).build()); k8sEnv.getPersistentVolumeClaims().clear(); k8sEnv.getPersistentVolumeClaims().put(PVC_NAME_PREFIX, pvc); doThrow(InfrastructureException.class).when(pvcs).createIfNotExist(any()); strategy.prepare(k8sEnv, IDENTITY, 100, emptyMap()); }