KubernetesDockerRunner implements DockerRunner { @VisibleForTesting void tryCleanupPods() { var pods = client.listPods().getItems(); pods.stream() .map(pod -> runAsync(guard(() -> tryCleanupPod(pod)), executor)) .collect(toList()) .forEach(CompletableFuture::join); tracer.getCurrentSpan().addAnnotation("processed", Map.of("pods", AttributeValue.longAttributeValue(pods.size()))); } @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 shouldNotCleanupNonStyxPod() { createdPod.getMetadata().setAnnotations(Collections.emptyMap()); createdPod.setStatus(podStatus); when(podStatus.getContainerStatuses()).thenReturn(List.of(containerStatus, keepaliveContainerStatus)); when(stateManager.getActiveStates()).thenReturn(Collections.emptyMap()); kdr.tryCleanupPods(); verify(k8sClient, never()).deletePod(any()); }
@Test public void shouldNotCleanupNonStyxPodWithoutRunState() { createdPod.getMetadata().setAnnotations(Collections.emptyMap()); createdPod.setStatus(podStatus); when(podStatus.getContainerStatuses()).thenReturn(List.of(containerStatus, keepaliveContainerStatus)); when(stateManager.getActiveStates()).thenReturn(Collections.emptyMap()); kdr.tryCleanupPods(); verify(k8sClient, never()).deletePod(any()); }
@Test public void shouldHandlePodWithoutAnnotationsWhenPolling() { final Pod pod = Json.OBJECT_MAPPER.convertValue( Map.of("metadata", Map.of("name", "foobar")), Pod.class); assertThat(pod.getMetadata().getAnnotations(), is(nullValue())); when(k8sClient.getPod("foobar")).thenReturn(Optional.of(pod)); when(podList.getItems()).thenReturn(List.of(pod)); assertThat(Try.run(() -> kdr.tryCleanupPods()).isSuccess(), is(true)); }