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-49707] retries option for agent kubernetes #1210

Merged
merged 3 commits into from
Jul 19, 2022
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
27 changes: 27 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<no-test-jar>false</no-test-jar>
<useBeta>true</useBeta>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<pipeline-model-definition-plugin.version>2.2114.v2654ca_721309</pipeline-model-definition-plugin.version> <!-- TODO until in BOM -->
</properties>

<dependencies>
Expand Down Expand Up @@ -257,6 +258,32 @@
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency> <!-- TODO as above -->
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-api</artifactId>
<version>${pipeline-model-definition-plugin.version}</version>
</dependency>
<dependency> <!-- TODO as above -->
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<version>${pipeline-model-definition-plugin.version}</version>
</dependency>
<dependency> <!-- TODO as above -->
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<version>${pipeline-model-definition-plugin.version}</version>
<classifier>tests</classifier>
</dependency>
<dependency> <!-- TODO as above -->
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-extensions</artifactId>
<version>${pipeline-model-definition-plugin.version}</version>
</dependency>
<dependency> <!-- TODO as above -->
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-stage-tags-metadata</artifactId>
<version>${pipeline-model-definition-plugin.version}</version>
</dependency>
<!-- Fix InjectedTest -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.csanchez.jenkins.plugins.kubernetes.pod.yaml.YamlMergeStrategy;
import org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor;
import org.jenkinsci.plugins.variant.OptionalExtension;
import org.kohsuke.accmod.Restricted;
Expand All @@ -30,8 +29,9 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.RetryableDeclarativeAgent;

public class KubernetesDeclarativeAgent extends DeclarativeAgent<KubernetesDeclarativeAgent> {
public class KubernetesDeclarativeAgent extends RetryableDeclarativeAgent<KubernetesDeclarativeAgent> {

private static final Logger LOGGER = Logger.getLogger(KubernetesDeclarativeAgent.class.getName());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler">
<f:entry field="cloud" title="${%Cloud to use}">
<f:select/>
</f:entry>
Expand Down Expand Up @@ -49,5 +49,6 @@
<f:entry title="${%Workspace Volume}" description="${%Volume to use for sharing the workspace}">
<f:dropdownDescriptorSelector field="workspaceVolume" default="${descriptor.defaultWorkspaceVolume}"/>
</f:entry>
<st:include page="retries" class="${descriptor.clazz}"/>
</f:advanced>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,36 @@ public class KubernetesDeclarativeAgentScript extends DeclarativeAgentScript<Kub
script.echo '[WARNING] containerTemplate option is deprecated, use yaml syntax to define containers.'
}
script.podTemplate(describable.asArgs) {
script.node(describable.labelExpression ?: script.POD_LABEL) {
CheckoutScript.doCheckout(script, describable, describable.customWorkspace) {
// what container to use for the main body
def container = describable.defaultContainer ?: 'jnlp'
Closure run = {
script.node(describable.labelExpression ?: script.POD_LABEL) {
CheckoutScript.doCheckout(script, describable, describable.customWorkspace) {
// what container to use for the main body
def container = describable.defaultContainer ?: 'jnlp'

if (describable.containerTemplate != null) {
// run inside the container declared for backwards compatibility
container = describable.containerTemplate.asArgs
}
if (describable.containerTemplate != null) {
// run inside the container declared for backwards compatibility
container = describable.containerTemplate.asArgs
}

// call the main body
if (container == 'jnlp') {
// If default container is not changed by the pipeline user,
// do not enclose the body with a `container` statement.
body.call()
} else {
script.container(container) {
// call the main body
if (container == 'jnlp') {
// If default container is not changed by the pipeline user,
// do not enclose the body with a `container` statement.
body.call()
} else {
script.container(container) {
body.call()
}
}
}
}.call()
}.call()
}
}
if (describable.retries > 1) {
script.retry(count: describable.retries, conditions: [script.kubernetesAgent(), script.nonresumable()]) {
run.call()
}
} else {
run.call()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import hudson.model.Result;
import jenkins.plugins.git.GitSampleRepoRule;
import jenkins.plugins.git.GitStep;
import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.deletePods;
import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.getLabels;
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.OnFailure;
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
import org.jenkinsci.plugins.workflow.actions.ArgumentsAction;
Expand Down Expand Up @@ -194,4 +196,18 @@ public void declarativeShowRawYamlFalse() throws Exception {
// check yaml metadata labels not logged
r.assertLogNotContains("class: KubernetesDeclarativeAgentTest", b);
}

@Issue("JENKINS-49707")
@Test
public void declarativeRetries() throws Exception {
assertNotNull(createJobThenScheduleRun());
r.waitForMessage("+ sleep", b);
deletePods(cloud.connect(), getLabels(this, name), false);
r.waitForMessage("busybox --", b);
r.waitForMessage("jnlp --", b);
r.waitForMessage("was deleted; cancelling node body", b);
r.waitForMessage("Retrying", b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pipeline {
agent {
kubernetes {
yaml '''
spec:
containers:
- name: busybox
image: busybox
command:
- sleep
- 99d
terminationGracePeriodSeconds: 3
'''
defaultContainer 'busybox'
retries 2
}
}
stages {
stage('Run') {
steps {
sh 'echo hello world'
sh 'sleep 15'
}
}
}
}