KubernetesDockerRunner implements DockerRunner { @VisibleForTesting boolean shouldDeletePodWithRunState(WorkflowInstance workflowInstance, Pod pod, RunState runState) { final Optional<ContainerStatus> containerStatus = getMainContainerStatus(pod); if (containerStatus.isEmpty()) { return false; } if (wantsPod(runState)) { return false; } if (isTerminated(containerStatus.get())) { return shouldDeletePodIfNonDeletePeriodExpired(workflowInstance, pod); } else if (imageError(containerStatus.get()).isPresent()) { return shouldDeletePod(workflowInstance, pod, "Pull image error"); } return false; } @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();  }
@Parameters({"TERMINATED", "FAILED", "ERROR", "DONE"}) @Test public void shouldCleanupPodAfterNonDeletePeriodIfRunStateNotRunning(String stateName) { final State state = State.valueOf(stateName); createdPod.setStatus(podStatus); when(podStatus.getContainerStatuses()).thenReturn(List.of(keepaliveContainerStatus, containerStatus)); 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 runState = RunState.create(WORKFLOW_INSTANCE, state); var shouldDelete = kdr.shouldDeletePodWithRunState(WORKFLOW_INSTANCE, createdPod, runState); assertThat(shouldDelete, is(true)); }
@Parameters({"NEW", "QUEUED", "PREPARE", "SUBMITTING", "SUBMITTED", "RUNNING"}) @Test public void shouldNotDeletePodAfterNonDeletePeriodIfRunStateStillRunning(String stateName) { final State state = State.valueOf(stateName); 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 runState = RunState.create(WORKFLOW_INSTANCE, state); var shouldDelete = kdr.shouldDeletePodWithRunState(WORKFLOW_INSTANCE, createdPod, runState); assertThat(shouldDelete, is(false)); }
@Test public void shouldCleanupPodWhenMissingFinishedAt() { 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); var runState = RunState.create(WORKFLOW_INSTANCE, State.TERMINATED); var shouldDelete = kdr.shouldDeletePodWithRunState(WORKFLOW_INSTANCE, createdPod, runState); assertThat(shouldDelete, is(true)); }
@Test public void shouldNotCleanupPodWhenMissingContainerStatus() { createdPod.setStatus(podStatus); var runState = RunState.create(WORKFLOW_INSTANCE, State.TERMINATED); var shouldDelete = kdr.shouldDeletePodWithRunState(WORKFLOW_INSTANCE, createdPod, runState); assertThat(shouldDelete, is(false)); }
@Test public void shouldCleanupPodWhenPullImageError() { setWaiting(createdPod, "Pending", "ErrImagePull"); var runState = RunState.create(WORKFLOW_INSTANCE, State.TERMINATED); var shouldDelete = kdr.shouldDeletePodWithRunState(WORKFLOW_INSTANCE, createdPod, runState); assertThat(shouldDelete, is(true)); }
@Test public void shouldNotCleanupPodBeforeNonDeletePeriod() { 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 runState = RunState.create(WORKFLOW_INSTANCE, State.TERMINATED); var shouldDelete = kdr.shouldDeletePodWithRunState(WORKFLOW_INSTANCE, createdPod, runState); assertThat(shouldDelete, is(false)); }
@Test public void shouldNotCleanupPodIfNotTerminated() { createdPod.setStatus(podStatus); when(podStatus.getContainerStatuses()).thenReturn(List.of(containerStatus, keepaliveContainerStatus)); when(containerStatus.getName()).thenReturn(MAIN_CONTAINER_NAME); when(containerStatus.getState()).thenReturn(containerState); var runState = RunState.create(WORKFLOW_INSTANCE, State.TERMINATED); var shouldDelete = kdr.shouldDeletePodWithRunState(WORKFLOW_INSTANCE, createdPod, runState); assertThat(shouldDelete, is(false)); }