Skip to content
Permalink
Browse files
[JENKINS-39109] Add a configuration option for what label to use for …
…docker agents

This adds a folder property and a global configuration where the administrator,
or someone configuring a folder, can specify what label expression to use with
a pipeline with agent docker: "" and omitting the label.
The folder property can be specified on any folder in the hierarchy and the closest
parent to the run is consulted first and then up the tree, lastly the global configuration
is consulted for a value.
An extension point is also introduced with specified ordinals for the existing extensions
with ample room to spare in between in case something wants to jump in between.
Lastly a step that returns the value of the extension point that first returns a non null value.
  • Loading branch information
rsandell committed Oct 19, 2016
1 parent 9517f02 commit 5adc8cd8ec379b610e5c3602f8c415ffeb598a27
Showing with 780 additions and 17 deletions.
  1. +5 −0 src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Agent.groovy
  2. +45 −0 src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/config/DockerLabelProvider.java
  3. +112 −0 src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/config/FolderConfig.java
  4. +78 −0 src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/config/GlobalConfig.java
  5. +86 −0 src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/steps/DockerLabelStep.java
  6. +26 −0 src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/Messages.properties
  7. +10 −0 src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy
  8. +32 −0 src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/config/FolderConfig/config.groovy
  9. +30 −0 ...esources/org/jenkinsci/plugins/pipeline/modeldefinition/config/FolderConfig/help-dockerLabel.html
  10. +32 −0 src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/config/GlobalConfig/config.groovy
  11. +30 −0 ...esources/org/jenkinsci/plugins/pipeline/modeldefinition/config/GlobalConfig/help-dockerLabel.html
  12. +53 −17 src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/AbstractModelDefTest.java
  13. +127 −0 src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/steps/DockerLabelStepTest.java
  14. +44 −0 src/test/resources/agentDockerEnvSpecLabel.groovy
  15. +44 −0 src/test/resources/agentDockerEnvTest.groovy
  16. +26 −0 src/test/resources/dockerLabel.groovy
@@ -74,6 +74,11 @@ public class Agent implements Serializable {
return any || docker != null || label != null
}

@Whitelisted
public boolean hasDocker() {
return docker != null
}

// TODO: Rewrite as an extension point and get this by extension discovery, but we knew that already.
public static List<String> agentConfigKeys() {
return ["docker", "label", "dockerArgs"]
@@ -0,0 +1,45 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.config;

import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.Run;

import javax.annotation.CheckForNull;

/**
* Provider of label expressions to use for {@code agent: docker} when no other label is provided.
*/
public abstract class DockerLabelProvider implements ExtensionPoint {

@CheckForNull
public abstract String getLabel(Run run);

public static ExtensionList<DockerLabelProvider> all() {
return ExtensionList.lookup(DockerLabelProvider.class);
}
}
@@ -0,0 +1,112 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.config;

import com.cloudbees.hudson.plugins.folder.AbstractFolder;
import com.cloudbees.hudson.plugins.folder.AbstractFolderProperty;
import com.cloudbees.hudson.plugins.folder.AbstractFolderPropertyDescriptor;
import hudson.Extension;
import hudson.model.Describable;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.Run;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.modeldefinition.Messages;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import javax.annotation.Nonnull;

/**
* Provides folder level configuration.
*/
public class FolderConfig extends AbstractFolderProperty<AbstractFolder<?>> {
private String dockerLabel;

@DataBoundConstructor
public FolderConfig() {
}

/**
* For testing
*
* @param dockerLabel the docker label to use
*/
public FolderConfig(String dockerLabel) {
this.dockerLabel = dockerLabel;
}

public String getDockerLabel() {
return dockerLabel;
}

@DataBoundSetter
public void setDockerLabel(String dockerLabel) {
this.dockerLabel = dockerLabel;
}

@Extension @Symbol("pmd")
public static class DescriptorImpl extends AbstractFolderPropertyDescriptor {

@Nonnull
@Override
public String getDisplayName() {
return Messages.PipelineModelDefinition_DisplayName();
}
}

@Extension(ordinal = 10000) //First to be asked
public static class FolderDockerLabelProvider extends DockerLabelProvider {

@Override
public String getLabel(Run run) {
Job job = run.getParent();
ItemGroup parent = job.getParent();
while(parent != null) {

if (parent instanceof AbstractFolder) {
AbstractFolder folder = (AbstractFolder)parent;
FolderConfig config = (FolderConfig)folder.getProperties().get(FolderConfig.class);
if (config != null) {
String label = config.getDockerLabel();
if (!StringUtils.isBlank(label)) {
return label;
}
}
}

if (parent instanceof Item) {
parent = ((Item)parent).getParent();
} else {
parent = null;
}
}
return null;
}
}
}
@@ -0,0 +1,78 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.config;

import com.google.inject.Inject;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.Util;
import hudson.model.Run;
import jenkins.model.GlobalConfiguration;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;

/**
* The system config.
*
* For example the system level {@link DockerLabelProvider}.
*/
@Extension @Symbol("pmd")
public class GlobalConfig extends GlobalConfiguration {
private String dockerLabel;

public String getDockerLabel() {
return Util.fixEmpty(dockerLabel);
}

@DataBoundSetter
public void setDockerLabel(String dockerLabel) {
this.dockerLabel = dockerLabel;
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
req.bindJSON(this, json);
save();
return true;
}

public static GlobalConfig get() {
return ExtensionList.lookup(GlobalConfiguration.class).get(GlobalConfig.class);
}

@Extension(ordinal = -10000) //Last one to be asked
public static final class GlobalConfigDockerLabelProvider extends DockerLabelProvider {
@Inject
GlobalConfig config;

@Override
public String getLabel(Run run) {
return config.getDockerLabel();
}
}
}
@@ -0,0 +1,86 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.steps;

import hudson.Extension;
import hudson.model.TaskListener;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.pipeline.modeldefinition.config.DockerLabelProvider;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.Serializable;

/**
* The node label expression to use for running docker.
*
* @see org.jenkinsci.plugins.pipeline.modeldefinition.config.DockerLabelProvider
*/
public class DockerLabelStep extends AbstractStepImpl implements Serializable {
private static final long serialVersionUID = 1L;

@DataBoundConstructor
public DockerLabelStep() {
}

@Extension
public static final class DescriptorImpl extends AbstractStepDescriptorImpl {

public DescriptorImpl() {
super(StepExecutionImpl.class);
}

@Override
public String getFunctionName() {
return "dockerLabel";
}
}


public static class StepExecutionImpl extends AbstractSynchronousNonBlockingStepExecution<String> {

@StepContextParameter
transient TaskListener listener;

@StepContextParameter
transient WorkflowRun run;

@Override
protected String run() throws Exception {
for (DockerLabelProvider provider : DockerLabelProvider.all()) {
String label = provider.getLabel(run);
if (!StringUtils.isBlank(label)) {
return label;
}
}
return null;
}
}
}
@@ -0,0 +1,26 @@
#
# The MIT License
#
# Copyright (c) 2016, CloudBees, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#

PipelineModelDefinition.DisplayName=Pipeline Model Definition
@@ -270,6 +270,16 @@ public class ModelInterpreter implements Serializable {
}
}
} else {
if (agent?.hasDocker()) {
String dl = script.dockerLabel()?.trim()
if (dl) {
return {
script.node(dl) {
body.call()
}
}
}
}
return {
script.node {
body.call()

0 comments on commit 5adc8cd

Please sign in to comment.