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

[JENKINS-41758] Add a Declarative Agent extension for Kubernetes #127

Merged
merged 9 commits into from
May 4, 2017
Merged
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
71 changes: 61 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,25 @@
<slf4j.version>1.7.7</slf4j.version>

<!-- jenkins plugins versions -->
<jenkins-basic-steps.version>2.1</jenkins-basic-steps.version>
<jenkins-credentials.version>1.24</jenkins-credentials.version>
<jenkins-basic-steps.version>2.3</jenkins-basic-steps.version>
<jenkins-credentials.version>2.1.7</jenkins-credentials.version>
<jenkins-durable-task.version>1.12</jenkins-durable-task.version>
<jenkins-durable-task-step.version>2.7</jenkins-durable-task-step.version>
<jenkins-git.version>2.4.1</jenkins-git.version>
<jenkins-mailer.version>1.16</jenkins-mailer.version>
<jenkins-git.version>2.6.0</jenkins-git.version>
<jenkins-mailer.version>1.17</jenkins-mailer.version>
<jenkins-pipeline-stage-step.version>2.2</jenkins-pipeline-stage-step.version>
<jenkins-ssh-credentials.version>1.11</jenkins-ssh-credentials.version>
<jenkins-structs.version>1.5</jenkins-structs.version>
<jenkins-workflow-cps.version>2.17</jenkins-workflow-cps.version>
<jenkins-workflow-job.version>2.6</jenkins-workflow-job.version>
<jenkins-workflow-step-api.version>2.7</jenkins-workflow-step-api.version>
<jenkins-workflow-support.version>2.12</jenkins-workflow-support.version>
<jenkins-ssh-credentials.version>1.12</jenkins-ssh-credentials.version>
<jenkins-structs.version>1.6</jenkins-structs.version>
<jenkins-workflow-cps.version>2.29</jenkins-workflow-cps.version>
<jenkins-workflow-job.version>2.9</jenkins-workflow-job.version>
<jenkins-workflow-step-api.version>2.9</jenkins-workflow-step-api.version>
<jenkins-workflow-support.version>2.14</jenkins-workflow-support.version>
<jenkins-workflow-api.version>2.11</jenkins-workflow-api.version>
<jenkins-subversion.version>2.5</jenkins-subversion.version>
<jenkins-workflow-scm-step.version>2.4</jenkins-workflow-scm-step.version>
<jenkins-scm-api.version>2.0.7</jenkins-scm-api.version>
<jenkins-declarative.version>1.1.2</jenkins-declarative.version>
<jenkins-script-security.version>1.26</jenkins-script-security.version>

<!-- testing tools versions -->
<powermock.version>1.6.5</powermock.version>
Expand Down Expand Up @@ -108,6 +113,27 @@
<artifactId>workflow-cps</artifactId>
<version>${jenkins-workflow-cps.version}</version>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-extensions</artifactId>
<version>${jenkins-declarative.version}</version>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-api</artifactId>
<version>${jenkins-declarative.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>${jenkins-script-security.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>${jenkins-workflow-support.version}</version>
</dependency>


<!-- for testing -->
<dependency>
Expand Down Expand Up @@ -154,6 +180,30 @@
<version>${jenkins-subversion.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>${jenkins-workflow-scm-step.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>${jenkins-scm-api.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<version>${jenkins-declarative.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>${jenkins-workflow-api.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
Expand All @@ -180,6 +230,7 @@
<version>${jenkins-pipeline-stage-step.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<dependencyManagement>
Expand Down
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;
Copy link
Member

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.

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) {
Copy link
Contributor

@marvinthepa marvinthepa Oct 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that wrapping every call in container(describable.containerTemplate.name){ } is such a good idea.

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:

step {
   container('jnlp') {
      sh 'echo run on the slave'
   }
}

Also, the current example contains an(other) explicit container, see

container('maven') {
.

I would vote for removing the container here, making the default "execution location" the jnlp container (like it is for non-declarative pipeline). That will also enable us to add support for multiple containerTemplates in declarative pipeline.

Or was it a conscious decision to keep the declarative syntax simpler and therefore less powerful?

@carlossg what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw #127 (comment), i.e. multiple containerTemplates may not make sense..

Choose a reason for hiding this comment

The 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).

Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.junit.rules.TemporaryFolder;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRuleNonLocalhost;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.RestartableJenkinsRule;
Expand Down Expand Up @@ -235,12 +237,52 @@ public void evaluate() throws Throwable {
});
}


private String loadPipelineScript(String name) {
try {
return new String(IOUtils.toByteArray(getClass().getResourceAsStream(name)));
} catch (Throwable t) {
throw new RuntimeException("Could not read resource:["+name+"].");
throw new RuntimeException("Could not read resource:[" + name + "].");
}
}

@Issue("JENKINS-41758")
@Test
public void declarative() throws Exception {

// Slaves running in Kubernetes (minikube) need to connect to this server, so localhost does not work
URL url = r.getURL();
URL nonLocalhostUrl = new URL(url.getProtocol(), InetAddress.getLocalHost().getHostAddress(), url.getPort(),
url.getFile());
JenkinsLocationConfiguration.get().setUrl(nonLocalhostUrl.toString());

r.jenkins.clouds.add(cloud);

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("" //
+ "pipeline {\n"
+ " agent {\n"
+ " kubernetes {\n"
+ " cloud 'minikube'\n"
+ " label 'mypod'\n"
+ " containerTemplate {\n"
+ " name 'maven'\n"
+ " image 'maven:3.3.9-jdk-8-alpine'\n"
+ " ttyEnabled true\n"
+ " command 'cat'\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " stages {\n"
+ " stage('Run maven') {\n"
+ " steps {\n"
+ " sh 'mvn -version'\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}\n", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Apache Maven 3.3.9", b);
}
}