Skip to content

Commit

Permalink
Allow randomizing cloud selection
Browse files Browse the repository at this point in the history
When multiple Docker clouds are defined, the plugin will always create
agents from the first available cloud, not allowing to distribute the
load on all the clouds that are defined.

Possible solutions to this issue are:
- Docker Swarm Standalone, which is deprecated
- Docker Engine Swarm API, which is not supported by docker-plugin.

This changes adds a setting that enables randomizing the order in which
Docker clouds are tried when creating a new agent.
  • Loading branch information
cavedon committed Nov 13, 2024
1 parent 19bde0a commit fe793d4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
29 changes: 29 additions & 0 deletions src/main/java/io/jenkins/docker/DockerGlobalConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.jenkins.docker;

import jenkins.model.GlobalConfiguration;
import org.kohsuke.stapler.DataBoundSetter;
import hudson.Extension;
import hudson.ExtensionList;

@Extension
public class DockerGlobalConfiguration extends GlobalConfiguration {
public static DockerGlobalConfiguration get() {
return ExtensionList.lookupSingleton(DockerGlobalConfiguration.class);
}

private boolean randomizeCloudsOrder = false;

public DockerGlobalConfiguration() {
load();
}

public boolean getRandomizeCloudsOrder() {
return randomizeCloudsOrder;
}

@DataBoundSetter
public void setRandomizeCloudsOrder(boolean value) {
randomizeCloudsOrder = value;
save();
}
}
21 changes: 17 additions & 4 deletions src/main/java/io/jenkins/docker/FastNodeProvisionerStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static hudson.slaves.NodeProvisioner.StrategyDecision.PROVISIONING_COMPLETED;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.FINEST;
import static java.util.logging.Level.WARNING;

import com.nirima.jenkins.plugins.docker.DockerCloud;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand All @@ -16,7 +17,9 @@
import hudson.model.queue.QueueListener;
import hudson.slaves.Cloud;
import hudson.slaves.NodeProvisioner;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Logger;
import jenkins.model.Jenkins;

Expand All @@ -36,12 +39,22 @@ public StrategyDecision apply(@NonNull NodeProvisioner.StrategyState state) {
if (Jenkins.get().isQuietingDown()) {
return CONSULT_REMAINING_STRATEGIES;
}
ArrayList<DockerCloud> dockerClouds = new ArrayList<DockerCloud>();
for (Cloud cloud : Jenkins.get().clouds) {
if (cloud instanceof DockerCloud) {
final StrategyDecision decision = applyToCloud(state, (DockerCloud) cloud);
if (decision == PROVISIONING_COMPLETED) {
return decision;
}
dockerClouds.add((DockerCloud) cloud);
}
}
if (DockerGlobalConfiguration.get().getRandomizeCloudsOrder()) {
LOGGER.log(WARNING, "Randomizing cloud order");
Collections.shuffle(dockerClouds);
} else {
LOGGER.log(WARNING, "Not randomizing cloud order");
}
for (DockerCloud cloud : dockerClouds) {
final StrategyDecision decision = applyToCloud(state, cloud);
if (decision == PROVISIONING_COMPLETED) {
return decision;
}
}
return CONSULT_REMAINING_STRATEGIES;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:section title="Docker Plugin Configuration">
<f:entry title="Randomize clouds order" field="randomizeCloudsOrder"
description="During node provisioning, randomize the order clouds are tried.">
<f:checkbox/>
</f:entry>
</f:section>
</j:jelly>

0 comments on commit fe793d4

Please sign in to comment.