Skip to content

Commit 19b160f

Browse files
authored
Merge pull request #1335 from jglick/KubernetesAgentErrorCondition-diag
2 parents 73d36f3 + a0b2698 commit 19b160f

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesAgentErrorCondition.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import hudson.Extension;
2020
import hudson.ExtensionList;
2121
import hudson.model.Node;
22+
import hudson.model.TaskListener;
2223
import hudson.model.labels.LabelAtom;
2324
import java.io.IOException;
2425
import java.util.HashSet;
@@ -36,10 +37,8 @@
3637
import org.jenkinsci.plugins.workflow.graph.BlockEndNode;
3738
import org.jenkinsci.plugins.workflow.graph.FlowNode;
3839
import org.jenkinsci.plugins.workflow.graphanalysis.LinearBlockHoppingScanner;
39-
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException;
4040
import org.jenkinsci.plugins.workflow.steps.StepContext;
4141
import org.jenkinsci.plugins.workflow.support.steps.AgentErrorCondition;
42-
import org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution;
4342
import org.kohsuke.stapler.DataBoundConstructor;
4443
import org.kohsuke.stapler.DataBoundSetter;
4544

@@ -80,9 +79,12 @@ public boolean test(Throwable t, StepContext context) throws IOException, Interr
8079
LOGGER.fine(() -> "Not a recognized failure: " + t);
8180
return false;
8281
}
82+
TaskListener listener = context.get(TaskListener.class);
8383
FlowNode _origin = ErrorAction.findOrigin(t, context.get(FlowExecution.class));
8484
if (_origin == null) {
85-
LOGGER.fine(() -> "No recognized origin of error: " + t);
85+
if (!handleNonKubernetes) {
86+
listener.getLogger().println("Unable to identify source of error (" + t + ") to see if this was associated with a Kubernetes agent");
87+
}
8688
return handleNonKubernetes;
8789
}
8890
FlowNode origin = _origin instanceof BlockEndNode ? ((BlockEndNode) _origin).getStartNode() : _origin;
@@ -96,27 +98,33 @@ public boolean test(Throwable t, StepContext context) throws IOException, Interr
9698
Node n = Jenkins.get().getNode(node);
9799
if (n != null) {
98100
if (!(n instanceof KubernetesSlave)) {
99-
LOGGER.fine(() -> node + " was not a K8s agent");
101+
if (!handleNonKubernetes) {
102+
listener.getLogger().println(node + " was not a Kubernetes agent");
103+
}
100104
return handleNonKubernetes;
101105
}
102106
} else {
103107
// May have been removed already, but we can look up the labels to see what it was.
104108
Set<LabelAtom> labels = ws.getLabels();
105109
if (labels.stream().noneMatch(l -> Jenkins.get().clouds.stream().anyMatch(c -> c instanceof KubernetesCloud && ((KubernetesCloud) c).getTemplate(l) != null))) {
106-
LOGGER.fine(() -> node + " was not a K8s agent judging by " + labels);
110+
if (!handleNonKubernetes) {
111+
listener.getLogger().println(node + " was not a Kubernetes agent judging by " + labels);
112+
}
107113
return handleNonKubernetes;
108114
}
109115
}
110116
Set<String> terminationReasons = ExtensionList.lookupSingleton(Reaper.class).terminationReasons(node);
111117
if (terminationReasons.stream().anyMatch(r -> IGNORED_CONTAINER_TERMINATION_REASONS.contains(r))) {
112-
LOGGER.fine(() -> "ignored termination reason(s) for " + node + ": " + terminationReasons);
118+
listener.getLogger().println("Ignored termination reason(s) for " + node + " for purposes of retry: " + terminationReasons);
113119
return false;
114120
}
115121
LOGGER.fine(() -> "active on " + node + " (termination reasons: " + terminationReasons + ")");
116122
return true;
117123
}
118124
}
119-
LOGGER.fine(() -> "found no WorkspaceAction starting from " + origin);
125+
if (!handleNonKubernetes) {
126+
listener.getLogger().println("Could not find a node block associated with " + origin.getDisplayFunctionName() + " (source of error)");
127+
}
120128
return handleNonKubernetes;
121129
}
122130

src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesAgentErrorConditionTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public class KubernetesAgentErrorConditionTest {
6464
SemaphoreStep.success("wait/1", null);
6565
s.toComputer().connect(false);
6666
r.assertBuildStatus(Result.FAILURE, r.waitForCompletion(b));
67+
r.assertLogContains(s.getNodeName() + " was not a Kubernetes agent", b);
6768
b = p.scheduleBuild2(0, new ParametersAction(new BooleanParameterValue("HNK", true))).waitForStart();
6869
SemaphoreStep.waitForStart("wait/2", b);
6970
s.toComputer().disconnect(new OfflineCause.UserCause(null, null));
@@ -74,6 +75,7 @@ public class KubernetesAgentErrorConditionTest {
7475
SemaphoreStep.success("wait/3", null);
7576
s.toComputer().connect(false);
7677
r.assertBuildStatusSuccess(r.waitForCompletion(b));
78+
r.assertLogNotContains(s.getNodeName() + " was not a Kubernetes agent", b);
7779
}
7880

7981
}

0 commit comments

Comments
 (0)