AsyncStoragePodInterceptor { public void intercept(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException { if (!COMMON_STRATEGY.equals(pvcStrategy) || isEphemeral(k8sEnv.getAttributes())) { return; } removeAsyncStoragePodWithoutDeployment(identity); String namespace = identity.getInfrastructureNamespace(); String workspaceId = identity.getWorkspaceId(); RollableScalableResource<Deployment, DoneableDeployment> asyncStorageDeploymentResource = getAsyncStorageDeploymentResource(namespace, workspaceId); if (asyncStorageDeploymentResource.get() == null) { return; } try { deleteAsyncStorageDeployment(asyncStorageDeploymentResource) .get(DELETE_DEPLOYMENT_TIMEOUT_IN_MIN, TimeUnit.MINUTES); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new InfrastructureException( format( "Interrupted while waiting for deployment '%s' removal. " + ex.getMessage(), ASYNC_STORAGE), ex); } catch (ExecutionException ex) { throw new InfrastructureException( format( "Error occurred while waiting for deployment '%s' removal. " + ex.getMessage(), ASYNC_STORAGE), ex); } catch (TimeoutException ex) { throw new InfrastructureException( format("Pod '%s' removal timeout reached " + ex.getMessage(), ASYNC_STORAGE), ex); } } @Inject  AsyncStoragePodInterceptor(
      @Named("che.infra.kubernetes.pvc.strategy") String pvcStrategy,
      KubernetesClientFactory kubernetesClientFactory); void intercept(KubernetesEnvironment k8sEnv, RuntimeIdentity identity);  }
@Test public void shouldDoNothingIfNotCommonStrategy() throws Exception { when(kubernetesClient.pods()).thenReturn(mixedOperationPod); when(mixedOperationPod.inNamespace(NAMESPACE)).thenReturn(namespacePodOperation); when(namespacePodOperation.withName(ASYNC_STORAGE)).thenReturn(podResource); when(podResource.get()).thenReturn(null); AsyncStoragePodInterceptor asyncStoragePodInterceptor = new AsyncStoragePodInterceptor(randomUUID().toString(), clientFactory); asyncStoragePodInterceptor.intercept(kubernetesEnvironment, identity); verifyNoMoreInteractions(clientFactory); verifyNoMoreInteractions(identity); }
@Test public void shouldDoNothingIfEphemeralWorkspace() throws Exception { when(kubernetesEnvironment.getAttributes()).thenReturn(of(PERSIST_VOLUMES_ATTRIBUTE, "false")); asyncStoragePodInterceptor.intercept(kubernetesEnvironment, identity); verifyNoMoreInteractions(clientFactory); verifyNoMoreInteractions(identity); }
@Test public void shouldDoNothingIfWorkspaceConfiguredWithAsyncStorage() throws Exception { when(kubernetesEnvironment.getAttributes()) .thenReturn(of(PERSIST_VOLUMES_ATTRIBUTE, "false", ASYNC_PERSIST_ATTRIBUTE, "true")); asyncStoragePodInterceptor.intercept(kubernetesEnvironment, identity); verifyNoMoreInteractions(clientFactory); verifyNoMoreInteractions(identity); }
@Test public void shouldDoNothingIfPodDoesNotExist() throws InfrastructureException { when(identity.getWorkspaceId()).thenReturn(WORKSPACE_ID); when(identity.getInfrastructureNamespace()).thenReturn(NAMESPACE); when(clientFactory.create(WORKSPACE_ID)).thenReturn(kubernetesClient); when(kubernetesEnvironment.getAttributes()).thenReturn(emptyMap()); when(kubernetesClient.pods()).thenReturn(mixedOperationPod); when(mixedOperationPod.inNamespace(NAMESPACE)).thenReturn(namespacePodOperation); when(namespacePodOperation.withName(ASYNC_STORAGE)).thenReturn(podResource); when(podResource.get()).thenReturn(null); when(kubernetesClient.apps()).thenReturn(apps); when(apps.deployments()).thenReturn(mixedOperation); when(mixedOperation.inNamespace(NAMESPACE)).thenReturn(namespaceOperation); when(namespaceOperation.withName(ASYNC_STORAGE)).thenReturn(deploymentResource); when(deploymentResource.get()).thenReturn(null); asyncStoragePodInterceptor.intercept(kubernetesEnvironment, identity); verifyNoMoreInteractions(clientFactory); verifyNoMoreInteractions(identity); verifyNoMoreInteractions(kubernetesClient); }
@Test public void shouldDoDeletePodIfWorkspaceWithEmptyAttributes() throws InfrastructureException { when(identity.getWorkspaceId()).thenReturn(WORKSPACE_ID); when(identity.getInfrastructureNamespace()).thenReturn(NAMESPACE); when(clientFactory.create(WORKSPACE_ID)).thenReturn(kubernetesClient); when(kubernetesEnvironment.getAttributes()).thenReturn(emptyMap()); when(kubernetesClient.pods()).thenReturn(mixedOperationPod); when(mixedOperationPod.inNamespace(NAMESPACE)).thenReturn(namespacePodOperation); when(namespacePodOperation.withName(ASYNC_STORAGE)).thenReturn(podResource); when(podResource.get()).thenReturn(null); when(kubernetesClient.apps()).thenReturn(apps); when(apps.deployments()).thenReturn(mixedOperation); when(mixedOperation.inNamespace(NAMESPACE)).thenReturn(namespaceOperation); when(namespaceOperation.withName(ASYNC_STORAGE)).thenReturn(deploymentResource); ObjectMeta meta = new ObjectMeta(); meta.setName(ASYNC_STORAGE); Deployment deployment = new Deployment(); deployment.setMetadata(meta); when(deploymentResource.get()).thenReturn(deployment); when(deploymentResource.withPropagationPolicy("Background")).thenReturn(deletable); Watch watch = mock(Watch.class); when(deploymentResource.watch(any())).thenReturn(watch); asyncStoragePodInterceptor.intercept(kubernetesEnvironment, identity); verify(deletable).delete(); verify(watch).close(); }
@Test public void shouldDoDeletePodIfWorkspaceConfigureToPersistentStorage() throws InfrastructureException { when(identity.getWorkspaceId()).thenReturn(WORKSPACE_ID); when(identity.getInfrastructureNamespace()).thenReturn(NAMESPACE); when(clientFactory.create(WORKSPACE_ID)).thenReturn(kubernetesClient); when(kubernetesEnvironment.getAttributes()) .thenReturn(ImmutableMap.of(PERSIST_VOLUMES_ATTRIBUTE, "true")); when(kubernetesClient.pods()).thenReturn(mixedOperationPod); when(mixedOperationPod.inNamespace(NAMESPACE)).thenReturn(namespacePodOperation); when(namespacePodOperation.withName(ASYNC_STORAGE)).thenReturn(podResource); when(podResource.get()).thenReturn(null); when(kubernetesClient.apps()).thenReturn(apps); when(apps.deployments()).thenReturn(mixedOperation); when(mixedOperation.inNamespace(NAMESPACE)).thenReturn(namespaceOperation); when(namespaceOperation.withName(ASYNC_STORAGE)).thenReturn(deploymentResource); ObjectMeta meta = new ObjectMeta(); meta.setName(ASYNC_STORAGE); Deployment deployment = new Deployment(); deployment.setMetadata(meta); when(deploymentResource.get()).thenReturn(deployment); when(deploymentResource.withPropagationPolicy("Background")).thenReturn(deletable); Watch watch = mock(Watch.class); when(deploymentResource.watch(any())).thenReturn(watch); asyncStoragePodInterceptor.intercept(kubernetesEnvironment, identity); verify(deletable).delete(); verify(watch).close(); }