Skip to content

Commit

Permalink
fix(kubernetes/job): Fix an edge case where a job can have no pods
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Zheng committed Jul 16, 2024
1 parent 170ab7c commit 7043a0e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ public KubernetesJobStatus collectJob(String account, String location, String id
})
.collect(Collectors.toList());

V1Pod mostRecentPod = typedPods.get(typedPods.size() - 1);
jobStatus.setMostRecentPodName(
mostRecentPod.getMetadata() != null ? mostRecentPod.getMetadata().getName() : "");
// Handle an edge case where a Job may not have any pods, for example
// if a webhook explicitly denies the creation of a pod
if (typedPods.size() != 0) {
V1Pod mostRecentPod = typedPods.get(typedPods.size() - 1);
jobStatus.setMostRecentPodName(
mostRecentPod.getMetadata() != null ? mostRecentPod.getMetadata().getName() : "");
}

jobStatus.setPods(
typedPods.stream().map(KubernetesJobStatus.PodStatus::new).collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.netflix.spinnaker.clouddriver.kubernetes.provider.view;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -145,6 +146,28 @@ void testFailedJobWithoutContainerLogs(boolean detailedPodStatus) {
assertNull(jobStatus.getFailureDetails());
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
void testCollectJobWithoutPod(boolean detailedPodStatus) {
// setup
KubernetesManifest testManifest =
Yaml.loadAs(getResource("base-with-completions.yml"), KubernetesManifest.class);
doReturn(
KubernetesManifestContainer.builder()
.account("mock_account")
.name("a")
.manifest(testManifest)
.build())
.when(mockManifestProvider)
.getManifest(anyString(), anyString(), anyString(), anyBoolean());
doReturn(ImmutableList.of()).when(mockCredentials).list(any(), isNull(), any());

KubernetesJobProvider kubernetesJobProvider =
new KubernetesJobProvider(credentialsProvider, mockManifestProvider, detailedPodStatus);

assertDoesNotThrow(() -> kubernetesJobProvider.collectJob("mock_account", "location", "id"));
}

private String getResource(String name) {
try {
return Resources.toString(
Expand Down

0 comments on commit 7043a0e

Please sign in to comment.