Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving uniqueness of templateID string (via template name) #971

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.github.dockerjava.api.command.PushImageCmd;
import com.github.dockerjava.api.command.StartContainerCmd;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Version;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -602,31 +603,47 @@ public synchronized void removeTemplate(DockerTemplate t) {
}
}

/**
* @deprecated use countContainersInDocker(String, String) instead
* @see DockerCloud#countContainersInDocker(String, String)
*/
@Deprecated
public int countContainersInDocker(final String imageName) throws Exception {
return countContainersInDocker(imageName, null);
}

/**
* Counts the number of instances currently running in Docker that are using
* the specified image.
* the specified image and template.<br>
* Returns all containers matching the given filter parameters.
*
* <p>
* <b>WARNING:</b> This method can be slow so it should be called sparingly.
*
* @param imageName
* If null, then all instances belonging to this Jenkins instance
* are counted. Otherwise, only those started with the specified
* image are counted.
* filter for imageName. If null, no filter is active for imageName.
* @param templateName
* filter for templateName. If null, no filter is active for templateName.
*
* @return The number of containers.
* @throws Exception if anything went wrong.
*/
public int countContainersInDocker(final String imageName) throws Exception {
public int countContainersInDocker(final String imageName, final String templateName) throws Exception {
final Map<String, String> labelFilter = new HashMap<>();
labelFilter.put(
DockerContainerLabelKeys.JENKINS_INSTANCE_ID,
DockerTemplateBase.getJenkinsInstanceIdForContainerLabel());
if (imageName != null) {
labelFilter.put(DockerContainerLabelKeys.CONTAINER_IMAGE, imageName);
}
final List<?> containers;
if (templateName != null) {
labelFilter.put(DockerContainerLabelKeys.TEMPLATE_NAME, templateName);
}
final List<Container> containers;
try (final DockerClient client = dockerApi.getClient()) {
containers = client.listContainersCmd().withLabelFilter(labelFilter).exec();
}

final int count = containers.size();
return count;
}
Expand All @@ -636,14 +653,15 @@ public int countContainersInDocker(final String imageName) throws Exception {
*/
private boolean canAddProvisionedAgent(DockerTemplate t) throws Exception {
final String templateImage = t.getImage();
final String templateName = t.getName();
final int templateContainerCap = t.instanceCap;
final int cloudContainerCap = getContainerCap();

final boolean haveCloudContainerCap = cloudContainerCap > 0 && cloudContainerCap != Integer.MAX_VALUE;
final boolean haveTemplateContainerCap = templateContainerCap > 0 && templateContainerCap != Integer.MAX_VALUE;
final int estimatedTotalAgents;
if (haveCloudContainerCap) {
final int totalContainersInCloud = countContainersInDocker(null);
final int totalContainersInCloud = countContainersInDocker(null, null);
final int containersInProgress = countContainersInProgress();
estimatedTotalAgents = totalContainersInCloud + containersInProgress;
if (estimatedTotalAgents >= cloudContainerCap) {
Expand All @@ -659,7 +677,7 @@ private boolean canAddProvisionedAgent(DockerTemplate t) throws Exception {
}
final int estimatedTemplateAgents;
if (haveTemplateContainerCap) {
final int totalContainersOfThisTemplateInCloud = countContainersInDocker(templateImage);
final int totalContainersOfThisTemplateInCloud = countContainersInDocker(templateImage, templateName);
final int containersInProgress = countContainersInProgress(t);
estimatedTemplateAgents = totalContainersOfThisTemplateInCloud + containersInProgress;
if (estimatedTemplateAgents >= templateContainerCap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
Jenkins will append a unique ID to this name in order to create individual node names.
<p>
If blank or just whitespace, a default of "docker" will be used.
<p/>
<p>
When using the same docker image in multiple docker agent templates, make sure to set an individual name for each template.<br/>
This is needed for capping by number of container instance by template to work correctly.
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private boolean dockerIsStillBusy() throws Exception {
return true;
}
}
final int containersInDocker = cloud.countContainersInDocker(null);
final int containersInDocker = cloud.countContainersInDocker(null, null);
if (containersInDocker > 0) {
return true;
}
Expand Down