Skip to content

Commit

Permalink
Avoid excessive Closures (#1653)
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick authored Feb 26, 2025
1 parent a9b4fe2 commit 5b846cf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 45 deletions.
32 changes: 30 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
<jenkins.host.address />
<slaveAgentPort />
<jenkins.baseline>2.479</jenkins.baseline>
<jenkins.version>${jenkins.baseline}.1</jenkins.version>
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
<!-- TODO until in BOM -->
<pipeline-model-definition.version>2.2234.v4a_b_13b_8cd590</pipeline-model-definition.version>
<no-test-jar>false</no-test-jar>
<useBeta>true</useBeta>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
Expand All @@ -58,7 +60,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>4051.v78dce3ce8b_d6</version>
<version>4228.v0a_71308d905b_</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -68,6 +70,32 @@
<!-- transitive dep of io.fabric8:kubernetes-server-mock, needs alignment with okhttp-api -->
<version>4.11.0</version>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-api</artifactId>
<version>${pipeline-model-definition.version}</version>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<version>${pipeline-model-definition.version}</version>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<version>${pipeline-model-definition.version}</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-extensions</artifactId>
<version>${pipeline-model-definition.version}</version>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-stage-tags-metadata</artifactId>
<version>${pipeline-model-definition.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,58 @@
package org.csanchez.jenkins.plugins.kubernetes.pipeline

import org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript2
import org.jenkinsci.plugins.workflow.cps.CpsScript

public class KubernetesDeclarativeAgentScript extends DeclarativeAgentScript<KubernetesDeclarativeAgent> {
public class KubernetesDeclarativeAgentScript extends DeclarativeAgentScript2<KubernetesDeclarativeAgent> {
public KubernetesDeclarativeAgentScript(CpsScript s, KubernetesDeclarativeAgent a) {
super(s, a)
}

@Override
public Closure run(Closure body) {
return {
if ((describable.getYamlFile() != null) && (describable.hasScmContext(script))) {
describable.setYaml(script.readTrusted(describable.getYamlFile()))
}
if (describable.getInheritFrom() == null) {
// Do not implicitly inherit from parent template context for declarative Kubernetes agent declaration
describable.setInheritFrom("")
}
if (describable.labelExpression != null) {
script.echo '[WARNING] label option is deprecated. To use a static pod template, use the \'inheritFrom\' option.'
}
if (describable.containerTemplate != null) {
script.echo '[WARNING] containerTemplate option is deprecated, use yaml syntax to define containers.'
}
script.podTemplate(describable.asArgs) {
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'
public void run(Closure body) {
if ((describable.getYamlFile() != null) && (describable.hasScmContext(script))) {
describable.setYaml(script.readTrusted(describable.getYamlFile()))
}
if (describable.getInheritFrom() == null) {
// Do not implicitly inherit from parent template context for declarative Kubernetes agent declaration
describable.setInheritFrom("")
}
if (describable.labelExpression != null) {
script.echo '[WARNING] label option is deprecated. To use a static pod template, use the \'inheritFrom\' option.'
}
if (describable.containerTemplate != null) {
script.echo '[WARNING] containerTemplate option is deprecated, use yaml syntax to define containers.'
}
script.podTemplate(describable.asArgs) {
Closure run = {
script.node(describable.labelExpression ?: script.POD_LABEL) {
CheckoutScript.doCheckout2(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) {
body.call()
}
}
}.call()
// 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)
}
}
}
if (describable.retries > 1) {
script.retry(count: describable.retries, conditions: [script.kubernetesAgent(), script.nonresumable()]) {
run.call()
}
} else {
}
if (describable.retries > 1) {
script.retry(count: describable.retries, conditions: [script.kubernetesAgent(), script.nonresumable()]) {
run.call()
}
} else {
run.call()
}
}
}
Expand Down

0 comments on commit 5b846cf

Please sign in to comment.