25
25
package org .jvnet .hudson .plugins .platformlabeler ;
26
26
27
27
import edu .umd .cs .findbugs .annotations .NonNull ;
28
+ import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
28
29
import hudson .Extension ;
29
30
import hudson .FilePath ;
30
31
import hudson .model .Computer ;
35
36
import hudson .remoting .VirtualChannel ;
36
37
import hudson .slaves .ComputerListener ;
37
38
import java .io .IOException ;
39
+ import java .util .Arrays ;
38
40
import java .util .Collection ;
39
41
import java .util .Collections ;
40
42
import java .util .HashSet ;
@@ -133,6 +135,26 @@ final void cacheLabels(final Computer computer, final VirtualChannel channel)
133
135
nodePlatformProperties .put (computer , requestComputerPlatformDetails (computer , channel ));
134
136
}
135
137
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
+ }
136
158
/**
137
159
* Update Jenkins' model so that labels for this computer are up to date.
138
160
*
@@ -142,8 +164,19 @@ final void refreshModel(final Computer computer) {
142
164
if (computer != null ) {
143
165
Node node = computer .getNode ();
144
166
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
+ }
147
180
}
148
181
}
149
182
}
0 commit comments