KubernetesDockerRunner implements DockerRunner { @VisibleForTesting boolean shouldDeletePodWithoutRunState(WorkflowInstance workflowInstance, Pod pod) { if (isTerminated(pod)) { return shouldDeletePodIfNonDeletePeriodExpired(workflowInstance, pod); } else { return shouldDeleteNonTerminatedPodWithoutRunState(workflowInstance, pod.getMetadata().getName()); } } @VisibleForTesting  KubernetesDockerRunner(String id, Fabric8KubernetesClient client, StateManager stateManager, Stats stats,
                         KubernetesGCPServiceAccountSecretManager serviceAccountSecretManager,
                         Debug debug, String styxEnvironment,
                         Set<String> secretWhitelist,
                         int cleanupPodsIntervalSeconds,
                         int podDeletionDelaySeconds,
                         Time time, ScheduledExecutorService scheduledExecutor);   KubernetesDockerRunner(String id, Fabric8KubernetesClient client, StateManager stateManager, Stats stats,
                         KubernetesGCPServiceAccountSecretManager serviceAccountSecretManager,
                         Debug debug, String styxEnvironment, Set<String> secretWhitelist); @Override String start(RunState runState, RunSpec runSpec); @Override void poll(RunState runState); @Override void cleanup(); @Override void close();  }
@Test public void shouldCleanupPodWithoutRunStateIfNotTerminated() { createdPod.setStatus(podStatus); when(podStatus.getContainerStatuses()).thenReturn(List.of(containerStatus, keepaliveContainerStatus)); when(containerStatus.getName()).thenReturn(MAIN_CONTAINER_NAME); when(containerStatus.getState()).thenReturn(containerState); when(k8sClient.getPod(POD_NAME)).thenReturn(Optional.of(createdPod)); var shouldDelete = kdr.shouldDeletePodWithoutRunState(WORKFLOW_INSTANCE, createdPod); assertThat(shouldDelete, is(true)); }
@Test public void shouldNotCleanupTerminatedPodWithoutRunStateBeforeNonDeletePeriod() { createdPod.setStatus(podStatus); when(podStatus.getContainerStatuses()).thenReturn(List.of(containerStatus, keepaliveContainerStatus)); when(containerStatus.getName()).thenReturn(MAIN_CONTAINER_NAME); when(containerStatus.getState()).thenReturn(containerState); when(containerState.getTerminated()).thenReturn(containerStateTerminated); when(containerStateTerminated.getFinishedAt()) .thenReturn(FIXED_INSTANT.minus(Duration.ofMinutes(1)).toString()); var shouldDelete = kdr.shouldDeletePodWithoutRunState(WORKFLOW_INSTANCE, createdPod); assertThat(shouldDelete, is(false)); }
@Test public void shouldNotCleanupRunningPodWithoutRunStateIfTerminatedAfterRefresh() { final ContainerStatus runningMainContainer = new ContainerStatusBuilder() .withName(MAIN_CONTAINER_NAME) .withNewState().withNewRunning().endRunning().endState() .build(); final ContainerStatus terminatedMainContainer = new ContainerStatusBuilder() .withName(MAIN_CONTAINER_NAME) .withNewState() .withNewTerminated() .withFinishedAt(FIXED_INSTANT.minus(Duration.ofDays(1)).toString()) .endTerminated() .endState() .build(); createdPod.setStatus(new PodStatusBuilder() .withContainerStatuses(runningMainContainer, keepaliveContainerStatus) .build()); final Pod refreshedPod = new PodBuilder(createdPod) .withStatus(new PodStatusBuilder() .withContainerStatuses(terminatedMainContainer, keepaliveContainerStatus) .build()) .build(); when(k8sClient.getPod(POD_NAME)).thenReturn(Optional.of(refreshedPod)); var shouldDelete = kdr.shouldDeletePodWithoutRunState(WORKFLOW_INSTANCE, createdPod); assertThat(shouldDelete, is(false)); }
@Test public void shouldCleanupPodWithoutRunStateAfterNonDeletePeriod() { createdPod.setStatus(podStatus); when(podStatus.getContainerStatuses()).thenReturn(List.of(containerStatus, keepaliveContainerStatus)); when(containerStatus.getName()).thenReturn(MAIN_CONTAINER_NAME); when(containerStatus.getState()).thenReturn(containerState); when(containerState.getTerminated()).thenReturn(containerStateTerminated); when(containerStateTerminated.getFinishedAt()) .thenReturn(FIXED_INSTANT.minus(Duration.ofMinutes(5)).toString()); var shouldDelete = kdr.shouldDeletePodWithoutRunState(WORKFLOW_INSTANCE, createdPod); assertThat(shouldDelete, is(true)); }