Skip to content

Commit

Permalink
Apply K8s Pod metadata to Job (#4057) [ci fast]
Browse files Browse the repository at this point in the history

Signed-off-by: Ben Sherman <[email protected]>
  • Loading branch information
bentsherman authored Aug 14, 2023
1 parent 30036db commit 4d91862
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -564,30 +564,18 @@ class PodSpecBuilder {
Map buildAsJob() {
final pod = build()

// job metadata
final metadata = new LinkedHashMap<String,Object>()
metadata.name = this.podName // just use the podName for simplicity, it may be renamed to just `name` or `resourceName` in the future
metadata.namespace = this.namespace ?: 'default'

// job spec
final spec = new LinkedHashMap<String,Object>()
spec.backoffLimit = 0
spec.template = [spec: pod.spec]

if( labels )
metadata.labels = sanitize(labels, MetaType.LABEL)

if( annotations )
metadata.annotations = sanitize(annotations, MetaType.ANNOTATION)

final result = [
apiVersion: 'batch/v1',
kind: 'Job',
metadata: metadata,
spec: spec ]

return result

return [
apiVersion: 'batch/v1',
kind: 'Job',
metadata: pod.metadata,
spec: [
backoffLimit: 0,
template: [
metadata: pod.metadata,
spec: pod.spec
]
]
]
}

@PackageScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ class K8sDriverLauncherTest extends Specification {
driver.@k8sClient = new K8sClient(new ClientConfig(namespace: 'foo', serviceAccount: 'bar'))
driver.@k8sConfig = k8s

and:
def metadata = [name: 'foo-boo', namespace: 'foo', labels: [app: 'nextflow', runName: 'foo-boo']]

when:
def spec = driver.makeLauncherSpec()
then:
Expand All @@ -221,11 +224,12 @@ class K8sDriverLauncherTest extends Specification {
spec == [
apiVersion: 'batch/v1',
kind: 'Job',
metadata: [name: 'foo-boo', namespace: 'foo', labels: [app: 'nextflow', runName: 'foo-boo']],
metadata: metadata,
spec: [
backoffLimit: 0,
template: [
spec: [
backoffLimit: 0,
template: [
metadata: metadata,
spec: [
restartPolicy: 'Never',
containers: [
[
Expand All @@ -249,8 +253,8 @@ class K8sDriverLauncherTest extends Specification {
[name:'vol-1', persistentVolumeClaim:[claimName:'pvc-1']],
[name:'vol-2', configMap:[name:'cfg-2']]
]
]
]
]
]
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,23 +605,22 @@ class K8sTaskHandlerTest extends Specification {
1 * task.getConfig() >> config

result == [
apiVersion: 'batch/v1',
kind: 'Job',
metadata:[name: 'nf-123', namespace: 'default'],
spec:[
backoffLimit: 0,
template: [
spec: [
restartPolicy: 'Never',
containers: [
[
name: 'nf-123',
image: 'debian:latest',
command: ['/bin/bash', '-ue','/some/work/dir/.command.run']
]
]
]
]
apiVersion: 'batch/v1',
kind: 'Job',
metadata: [name: 'nf-123', namespace: 'default'],
spec: [
backoffLimit: 0,
template: [
metadata: [name: 'nf-123', namespace: 'default'],
spec: [
restartPolicy: 'Never',
containers: [[
name: 'nf-123',
image: 'debian:latest',
command: ['/bin/bash', '-ue','/some/work/dir/.command.run']
]]
]
]
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1001,23 +1001,24 @@ class PodSpecBuilderTest extends Specification {
.buildAsJob()

then:
spec == [ apiVersion: 'batch/v1',
kind: 'Job',
metadata: [name:'foo', namespace:'default'],
spec == [
apiVersion: 'batch/v1',
kind: 'Job',
metadata: [name: 'foo', namespace: 'default'],
spec: [
backoffLimit: 0,
template: [
metadata: [name: 'foo', namespace: 'default'],
spec: [
backoffLimit: 0,
template: [
spec: [
restartPolicy:'Never',
containers:[
[name:'foo',
image:'busybox',
command:['echo', 'hello'],
]
]
]
]
restartPolicy: 'Never',
containers: [[
name: 'foo',
image: 'busybox',
command: ['echo', 'hello'],
]]
]
]
]
]
}

Expand All @@ -1035,37 +1036,40 @@ class PodSpecBuilderTest extends Specification {
.withAnnotations([anno2: "val2", anno3: "val3"])
.buildAsJob()

def metadata = [
name: 'foo',
namespace: 'default',
labels: [
app: 'someApp',
runName: 'someName',
version: '3.8.1'
],
annotations: [
anno1: "val1",
anno2: "val2",
anno3: "val3"
]
]

then:
spec == [ apiVersion: 'batch/v1',
kind: 'Job',
metadata: [
name:'foo',
namespace:'default',
labels: [
app: 'someApp',
runName: 'someName',
version: '3.8.1'
],
annotations: [
anno1: "val1",
anno2: "val2",
anno3: "val3"
]
],
spec: [
backoffLimit: 0,
template: [
spec: [
restartPolicy:'Never',
containers:[
[name:'foo',
image:'busybox',
command:['echo', 'hello'],
]
]
]
]
]
spec == [
apiVersion: 'batch/v1',
kind: 'Job',
metadata: metadata,
spec: [
backoffLimit: 0,
template: [
metadata: metadata,
spec: [
restartPolicy: 'Never',
containers: [[
name: 'foo',
image: 'busybox',
command: ['echo', 'hello'],
]]
]
]
]
]
}

Expand Down Expand Up @@ -1099,36 +1103,4 @@ class PodSpecBuilderTest extends Specification {

}

def 'should create job spec with activeDeadlineSeconds' () {

when:
def spec = new PodSpecBuilder()
.withPodName('foo')
.withImageName('busybox')
.withCommand(['echo', 'hello'])
.withActiveDeadline(100)
.buildAsJob()

then:
spec == [ apiVersion: 'batch/v1',
kind: 'Job',
metadata: [name:'foo', namespace:'default'],
spec: [
backoffLimit: 0,
template: [
spec: [
restartPolicy:'Never',
activeDeadlineSeconds: 100,
containers:[
[name:'foo',
image:'busybox',
command:['echo', 'hello'],
]
]
]
]
]
]
}

}

0 comments on commit 4d91862

Please sign in to comment.