Skip to content

Commit 07df00c

Browse files
[JENKINS-72224] Trigger node updates when refreshing platform labels (#1389)
* JENKINS-72224 Ensure to trigger node update when refreshing platform labeler model * More logging of node save steps Need to watch for performance impact with many nodes --------- Co-authored-by: Mark Waite <mark.earl.waite@gmail.com>
1 parent cc0971d commit 07df00c

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/main/java/org/jvnet/hudson/plugins/platformlabeler/NodeLabelCache.java

+35-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package org.jvnet.hudson.plugins.platformlabeler;
2626

2727
import edu.umd.cs.findbugs.annotations.NonNull;
28+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2829
import hudson.Extension;
2930
import hudson.FilePath;
3031
import hudson.model.Computer;
@@ -35,6 +36,7 @@
3536
import hudson.remoting.VirtualChannel;
3637
import hudson.slaves.ComputerListener;
3738
import java.io.IOException;
39+
import java.util.Arrays;
3840
import java.util.Collection;
3941
import java.util.Collections;
4042
import java.util.HashSet;
@@ -133,6 +135,26 @@ final void cacheLabels(final Computer computer, final VirtualChannel channel)
133135
nodePlatformProperties.put(computer, requestComputerPlatformDetails(computer, channel));
134136
}
135137

138+
@SuppressFBWarnings(value = "CRLF_INJECTION_LOGS", justification = "CRLF not allowed in label display names")
139+
private void logUpdateNodeException(Node node, IOException e) {
140+
LOGGER.log(
141+
Level.FINE,
142+
String.format("Exception updating node '%s' during label refresh", node.getDisplayName()),
143+
e);
144+
}
145+
146+
@SuppressFBWarnings(value = "CRLF_INJECTION_LOGS", justification = "CRLF not allowed in label display names")
147+
private void logUpdateNodeResult(
148+
boolean result, Node node, Collection<LabelAtom> labels, Set<LabelAtom> assignedLabels) {
149+
LOGGER.log(
150+
Level.FINEST,
151+
String.format(
152+
"Update of node '%s' %s with labels %s and assigned labels %s",
153+
node.getDisplayName(),
154+
result ? "succeeded" : "failed",
155+
Arrays.toString(labels.toArray()),
156+
Arrays.toString(assignedLabels.toArray())));
157+
}
136158
/**
137159
* Update Jenkins' model so that labels for this computer are up to date.
138160
*
@@ -142,8 +164,19 @@ final void refreshModel(final Computer computer) {
142164
if (computer != null) {
143165
Node node = computer.getNode();
144166
if (node != null) {
145-
nodeLabels.put(node, getLabelsForNode(node));
146-
node.getAssignedLabels();
167+
Collection<LabelAtom> labels = getLabelsForNode(node);
168+
nodeLabels.put(node, labels);
169+
Set<LabelAtom> assignedLabels = node.getAssignedLabels();
170+
try {
171+
// Save the node to ensure label will see the node updated when platform details are added (or
172+
// updated).
173+
// This will ensure a node has the same state if we were adding labels via the UI.
174+
// See JENKINS-72224
175+
boolean result = Jenkins.get().updateNode(node);
176+
logUpdateNodeResult(result, node, labels, assignedLabels);
177+
} catch (IOException e) {
178+
logUpdateNodeException(node, e);
179+
}
147180
}
148181
}
149182
}

0 commit comments

Comments
 (0)