Skip to content

Commit 513d3d3

Browse files
authored
Merge pull request #414 from jenkinsci/fail-fast
Fail faster if a pod enters in error state during provisioning
2 parents 3a7cdb8 + 083db46 commit 513d3d3

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesLauncher.java

+30-8
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,7 @@ public void launch(SlaveComputer computer, TaskListener listener) {
155155
}
156156
}
157157

158-
if (!terminatedContainers.isEmpty()) {
159-
Map<String, Integer> errors = terminatedContainers.stream().collect(Collectors
160-
.toMap(ContainerStatus::getName, (info) -> info.getState().getTerminated().getExitCode()));
161-
162-
// Print the last lines of failed containers
163-
logLastLines(terminatedContainers, podId, namespace, slave, errors, client);
164-
throw new IllegalStateException("Containers are terminated with exit codes: " + errors);
165-
}
158+
checkTerminatedContainers(terminatedContainers, podId, namespace, slave, client);
166159

167160
if (!allContainersAreReady) {
168161
continue;
@@ -203,6 +196,23 @@ public void launch(SlaveComputer computer, TaskListener listener) {
203196
break;
204197
}
205198

199+
containerStatuses = pod.getStatus().getContainerStatuses();
200+
List<ContainerStatus> terminatedContainers = new ArrayList<>();
201+
for (ContainerStatus info : containerStatuses) {
202+
if (info != null) {
203+
if (info.getState().getTerminated() != null) {
204+
// Container has errored
205+
LOGGER.log(INFO, "Container is terminated {0} [{2}]: {1}",
206+
new Object[]{podId, info.getState().getTerminated(), info.getName()});
207+
logger.printf("Container is terminated %1$s [%3$s]: %2$s%n",
208+
podId, info.getState().getTerminated(), info.getName());
209+
terminatedContainers.add(info);
210+
}
211+
}
212+
}
213+
214+
checkTerminatedContainers(terminatedContainers, podId, namespace, slave, client);
215+
206216
LOGGER.log(INFO, "Waiting for agent to connect ({1}/{2}): {0}",
207217
new Object[]{podId, waitedForSlave, waitForSlaveToConnect});
208218
logger.printf("Waiting for agent to connect (%2$s/%3$s): %1$s%n",
@@ -234,6 +244,18 @@ public void launch(SlaveComputer computer, TaskListener listener) {
234244
}
235245
}
236246

247+
private void checkTerminatedContainers(List<ContainerStatus> terminatedContainers, String podId, String namespace,
248+
KubernetesSlave slave, KubernetesClient client) {
249+
if (!terminatedContainers.isEmpty()) {
250+
Map<String, Integer> errors = terminatedContainers.stream().collect(Collectors
251+
.toMap(ContainerStatus::getName, (info) -> info.getState().getTerminated().getExitCode()));
252+
253+
// Print the last lines of failed containers
254+
logLastLines(terminatedContainers, podId, namespace, slave, errors, client);
255+
throw new IllegalStateException("Containers are terminated with exit codes: " + errors);
256+
}
257+
}
258+
237259
private Pod getPodTemplate(KubernetesClient client, KubernetesSlave slave, PodTemplate template) {
238260
return template == null ? null : template.build(client, slave);
239261
}

0 commit comments

Comments
 (0)