Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
Showing
16 changed files
with
780 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/config/DockerLabelProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/config/FolderConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/config/GlobalConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/steps/DockerLabelStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/Messages.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.