2626public final class KubernetesProvisioningLimits {
2727 private static final Logger LOGGER = Logger .getLogger (KubernetesProvisioningLimits .class .getName ());
2828
29- private static boolean init ;
29+ private boolean init ;
3030
3131 /**
3232 * Tracks current number of kubernetes agents per pod template
@@ -38,25 +38,30 @@ public final class KubernetesProvisioningLimits {
3838 */
3939 private final Map <String , Integer > cloudCounts = new HashMap <>();
4040
41- @ Initializer (after = InitMilestone .SYSTEM_CONFIG_LOADED )
4241 public static void init () {
43- // We don't want anything to be provisioned while we do the initial count.
42+ get ().initInstance ();
43+ }
44+
45+ /**
46+ * Initialize limits counter
47+ * @return whether the instance was already initialized
48+ */
49+ private synchronized boolean initInstance () {
50+ if (init ) {
51+ return true ;
52+ }
4453 Queue .withLock (() -> {
45- if (!init ) {
46- final KubernetesProvisioningLimits instance = get ();
47- synchronized (instance ) {
48- Jenkins .get ().getNodes ()
49- .stream ()
50- .filter (KubernetesSlave .class ::isInstance )
51- .map (KubernetesSlave .class ::cast )
52- .forEach (node -> {
53- instance .cloudCounts .put (node .getCloudName (), instance .getGlobalCount (node .getCloudName ()) + node .getNumExecutors ());
54- instance .podTemplateCounts .put (node .getTemplateId (), instance .getPodTemplateCount (node .getTemplateId ()) + node .getNumExecutors ());
55- });
56- init = true ;
57- }
58- }
54+ Jenkins .get ().getNodes ()
55+ .stream ()
56+ .filter (KubernetesSlave .class ::isInstance )
57+ .map (KubernetesSlave .class ::cast )
58+ .forEach (node -> {
59+ cloudCounts .put (node .getCloudName (), getGlobalCount (node .getCloudName ()) + node .getNumExecutors ());
60+ podTemplateCounts .put (node .getTemplateId (), getPodTemplateCount (node .getTemplateId ()) + node .getNumExecutors ());
61+ });
62+ init = true ;
5963 });
64+ return false ;
6065 }
6166
6267 /**
@@ -73,6 +78,7 @@ public static KubernetesProvisioningLimits get() {
7378 * @param numExecutors the number of executors (pretty much always 1)
7479 */
7580 public synchronized boolean register (@ NonNull KubernetesCloud cloud , @ NonNull PodTemplate podTemplate , int numExecutors ) {
81+ initInstance ();
7682 int newGlobalCount = getGlobalCount (cloud .name ) + numExecutors ;
7783 if (newGlobalCount <= cloud .getContainerCap ()) {
7884 int newPodTemplateCount = getPodTemplateCount (podTemplate .getId ()) + numExecutors ;
@@ -101,19 +107,21 @@ public synchronized boolean register(@NonNull KubernetesCloud cloud, @NonNull Po
101107 * @param numExecutors the number of executors (pretty much always 1)
102108 */
103109 public synchronized void unregister (@ NonNull KubernetesCloud cloud , @ NonNull PodTemplate podTemplate , int numExecutors ) {
104- int newGlobalCount = getGlobalCount (cloud .name ) - numExecutors ;
105- if (newGlobalCount < 0 ) {
106- LOGGER .log (Level .WARNING , "Global count for " + cloud .name + " went below zero. There is likely a bug in kubernetes-plugin" );
107- }
108- cloudCounts .put (cloud .name , Math .max (0 , newGlobalCount ));
109- LOGGER .log (Level .FINEST , () -> cloud .name + " global limit: " + Math .max (0 , newGlobalCount ) + "/" + cloud .getContainerCap ());
110+ if (initInstance ()) {
111+ int newGlobalCount = getGlobalCount (cloud .name ) - numExecutors ;
112+ if (newGlobalCount < 0 ) {
113+ LOGGER .log (Level .WARNING , "Global count for " + cloud .name + " went below zero. There is likely a bug in kubernetes-plugin" );
114+ }
115+ cloudCounts .put (cloud .name , Math .max (0 , newGlobalCount ));
116+ LOGGER .log (Level .FINEST , () -> cloud .name + " global limit: " + Math .max (0 , newGlobalCount ) + "/" + cloud .getContainerCap ());
110117
111- int newPodTemplateCount = getPodTemplateCount (podTemplate .getId ()) - numExecutors ;
112- if (newPodTemplateCount < 0 ) {
113- LOGGER .log (Level .WARNING , "Pod template count for " + podTemplate .getName () + " went below zero. There is likely a bug in kubernetes-plugin" );
118+ int newPodTemplateCount = getPodTemplateCount (podTemplate .getId ()) - numExecutors ;
119+ if (newPodTemplateCount < 0 ) {
120+ LOGGER .log (Level .WARNING , "Pod template count for " + podTemplate .getName () + " went below zero. There is likely a bug in kubernetes-plugin" );
121+ }
122+ podTemplateCounts .put (podTemplate .getId (), Math .max (0 , newPodTemplateCount ));
123+ LOGGER .log (Level .FINEST , () -> podTemplate .getName () + " template limit: " + Math .max (0 , newPodTemplateCount ) + "/" + podTemplate .getInstanceCap ());
114124 }
115- podTemplateCounts .put (podTemplate .getId (), Math .max (0 , newPodTemplateCount ));
116- LOGGER .log (Level .FINEST , () -> podTemplate .getName () + " template limit: " + Math .max (0 , newPodTemplateCount ) + "/" + podTemplate .getInstanceCap ());
117125 }
118126
119127 @ NonNull
@@ -133,14 +141,11 @@ public static class NodeListenerImpl extends NodeListener {
133141 @ Override
134142 protected void onDeleted (@ NonNull Node node ) {
135143 if (node instanceof KubernetesSlave ) {
136- if (KubernetesProvisioningLimits .init ) {
137- KubernetesSlave kubernetesNode = (KubernetesSlave ) node ;
138- PodTemplate template = kubernetesNode .getTemplateOrNull ();
139- if (template != null ) {
140- KubernetesProvisioningLimits .get ().unregister (kubernetesNode .getKubernetesCloud (), template , node .getNumExecutors ());
141- }
142- } else {
143- KubernetesProvisioningLimits .init ();
144+ KubernetesProvisioningLimits instance = KubernetesProvisioningLimits .get ();
145+ KubernetesSlave kubernetesNode = (KubernetesSlave ) node ;
146+ PodTemplate template = kubernetesNode .getTemplateOrNull ();
147+ if (template != null ) {
148+ instance .unregister (kubernetesNode .getKubernetesCloud (), template , node .getNumExecutors ());
144149 }
145150 }
146151 }
0 commit comments