-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[JENKINS-41758] Add a Declarative Agent extension for Kubernetes #127
Changes from all commits
ec766da
5f59820
1feb452
f709e7b
aac3c4f
2831c37
6780d63
018afac
c9c3be3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package org.csanchez.jenkins.plugins.kubernetes.pipeline; | ||
|
||
import hudson.Extension; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate; | ||
import org.jenkinsci.Symbol; | ||
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent; | ||
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
public class KubernetesDeclarativeAgent extends DeclarativeAgent<KubernetesDeclarativeAgent> { | ||
private final String label; | ||
|
||
private String cloud; | ||
private String inheritFrom; | ||
|
||
private int instanceCap; | ||
private String serviceAccount; | ||
private String nodeSelector; | ||
private String workingDir; | ||
|
||
private ContainerTemplate containerTemplate; | ||
|
||
@DataBoundConstructor | ||
public KubernetesDeclarativeAgent(String label, ContainerTemplate containerTemplate) { | ||
this.label = label; | ||
this.containerTemplate = containerTemplate; | ||
} | ||
|
||
public String getLabel() { | ||
return label; | ||
} | ||
|
||
public String getCloud() { | ||
return cloud; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setCloud(String cloud) { | ||
this.cloud = cloud; | ||
} | ||
|
||
public String getInheritFrom() { | ||
return inheritFrom; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setInheritFrom(String inheritFrom) { | ||
this.inheritFrom = inheritFrom; | ||
} | ||
|
||
public int getInstanceCap() { | ||
return instanceCap; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setInstanceCap(int instanceCap) { | ||
this.instanceCap = instanceCap; | ||
} | ||
|
||
public String getServiceAccount() { | ||
return serviceAccount; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setServiceAccount(String serviceAccount) { | ||
this.serviceAccount = serviceAccount; | ||
} | ||
|
||
public String getNodeSelector() { | ||
return nodeSelector; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setNodeSelector(String nodeSelector) { | ||
this.nodeSelector = nodeSelector; | ||
} | ||
|
||
public String getWorkingDir() { | ||
return workingDir; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setWorkingDir(String workingDir) { | ||
this.workingDir = workingDir; | ||
} | ||
|
||
public ContainerTemplate getContainerTemplate() { | ||
return containerTemplate; | ||
} | ||
|
||
public Map<String,Object> getAsArgs() { | ||
Map<String,Object> argMap = new TreeMap<>(); | ||
|
||
argMap.put("label", label); | ||
argMap.put("name", label); | ||
argMap.put("containers", Collections.singletonList(containerTemplate)); | ||
|
||
if (!StringUtils.isEmpty(cloud)) { | ||
argMap.put("cloud", cloud); | ||
} | ||
if (!StringUtils.isEmpty(inheritFrom)) { | ||
argMap.put("inheritFrom", inheritFrom); | ||
} | ||
if (!StringUtils.isEmpty(serviceAccount)) { | ||
argMap.put("serviceAccount", serviceAccount); | ||
} | ||
if (!StringUtils.isEmpty(nodeSelector)) { | ||
argMap.put("nodeSelector", nodeSelector); | ||
} | ||
if (!StringUtils.isEmpty(workingDir)) { | ||
argMap.put("workingDir", workingDir); | ||
} | ||
if (instanceCap > 0) { | ||
argMap.put("instanceCap", instanceCap); | ||
} | ||
|
||
return argMap; | ||
} | ||
|
||
@Extension @Symbol("kubernetes") | ||
public static class DescriptorImpl extends DeclarativeAgentDescriptor<KubernetesDeclarativeAgent> { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,64 @@ | ||||
/* | ||||
* 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.csanchez.jenkins.plugins.kubernetes.pipeline | ||||
|
||||
import hudson.model.Result | ||||
import org.jenkinsci.plugins.pipeline.modeldefinition.SyntheticStageNames | ||||
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript | ||||
import org.jenkinsci.plugins.workflow.cps.CpsScript | ||||
|
||||
|
||||
public class KubernetesDeclarativeAgentScript extends DeclarativeAgentScript<KubernetesDeclarativeAgent> { | ||||
public KubernetesDeclarativeAgentScript(CpsScript s, KubernetesDeclarativeAgent a) { | ||||
super(s, a) | ||||
} | ||||
|
||||
@Override | ||||
public Closure run(Closure body) { | ||||
return { | ||||
try { | ||||
script.podTemplate(describable.asArgs) { | ||||
script.node(describable.label) { | ||||
if (describable.isDoCheckout() && describable.hasScmContext(script)) { | ||||
if (!describable.inStage) { | ||||
script.stage(SyntheticStageNames.checkout()) { | ||||
script.checkout script.scm | ||||
} | ||||
} else { | ||||
// No stage when we're in a nested stage already | ||||
script.checkout script.scm | ||||
} | ||||
} | ||||
script.container(describable.containerTemplate.name) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure that wrapping every call in All commands are executed inside the one single container template that can be specified, while the pod will at least contain a 'jnlp' container in addition. For users to execute scripts in the jnlp container, they need to specify it:
Also, the current example contains an(other) explicit
I would vote for removing the Or was it a conscious decision to keep the declarative syntax simpler and therefore less powerful? @carlossg what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just saw #127 (comment), i.e. multiple containerTemplates may not make sense.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe I know the answer (from following the above thread) but does the current declarative support for this plugin support podTemplate inheritance? Such that you can specify X number of containers you want in the POD? (I think the answer is no). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. multiple containers are now possible with the yaml syntax |
||||
body.call() | ||||
} | ||||
} | ||||
} | ||||
} catch (Exception e) { | ||||
script.getProperty("currentBuild").result = Result.FAILURE | ||||
throw e | ||||
} | ||||
} | ||||
} | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should probably have a centralized config with defaults, same as dockerLabel et.al.
The pipeline dev should not have to know all these things, looks more like admin stuff.