Skip to content

Commit

Permalink
Merge branch 'truncate_labels_for_k8s' of https://github.com/ebi-gene…
Browse files Browse the repository at this point in the history
…-expression-group/nextflow into ebi-gene-expression-group-truncate_labels_for_k8s
  • Loading branch information
pditommaso committed May 10, 2020
2 parents 2784cdb + a2cc3f8 commit 6434b9d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
Expand Up @@ -24,12 +24,15 @@ import groovy.transform.CompileStatic
import groovy.transform.PackageScope
import nextflow.executor.res.AcceleratorResource
import nextflow.util.MemoryUnit
import groovy.util.logging.Slf4j

/**
* Object build for a K8s pod specification
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@CompileStatic
@Slf4j
class PodSpecBuilder {

static @PackageScope AtomicInteger VOLUMES = new AtomicInteger()
Expand Down Expand Up @@ -146,12 +149,12 @@ class PodSpecBuilder {
}

PodSpecBuilder withLabel( String name, String value ) {
this.labels.put(name, value)
this.labels.put(name, normalizeLabel(name, value))
return this
}

PodSpecBuilder withLabels(Map labels) {
this.labels.putAll(labels)
this.labels.putAll(normalizeLabels(labels))
return this
}

Expand Down Expand Up @@ -216,6 +219,24 @@ class PodSpecBuilder {
return this
}

String normalizeLabel( String name, String value ) {
if( !name || !value ) {
log.debug "K8s invalid label name=$name value=$value"
}
else if( value.size() > 63 ) {
log.debug "K8s label exceeds allowed size: 63 -- offending name=$name value=$value"
value=value.substring(0,63)
}
return value
}

Map<String, String> normalizeLabels(Map<String, String> labels) {
for( Map.Entry<String,String> entry : labels ) {
labels.put(entry.key, normalizeLabel(entry.key, entry.value))
}
return labels
}

PodSpecBuilder withPodOptions(PodOptions opts) {
// -- pull policy
if( opts.imagePullPolicy )
Expand All @@ -239,7 +260,7 @@ class PodSpecBuilder {
def keys = opts.labels.keySet()
if( 'app' in keys ) throw new IllegalArgumentException("Invalid pod label -- `app` is a reserved label")
if( 'runName' in keys ) throw new IllegalArgumentException("Invalid pod label -- `runName` is a reserved label")
labels.putAll( opts.labels )
labels.putAll(normalizeLabels(opts.labels))
}
// - annotations
if( opts.annotations ) {
Expand Down
Expand Up @@ -103,6 +103,55 @@ class PodSpecBuilderTest extends Specification {
]
]
}

def 'should truncate labels longer than 63 chars' () {

when:
def spec = new PodSpecBuilder()
.withPodName('foo')
.withImageName('busybox')
.withWorkDir('/some/work/dir')
.withCommand(['sh', '-c', 'echo hello'])
.withNamespace('xyz')
.withLabel('app','myApp')
.withLabel('runName','something')
.withLabel('tag','somethingreallylonggggggggggggggggggggggggggggggggggggggggggendEXTRABIT')
.withLabels([tag2: 'somethingreallylonggggggggggggggggggggggggggggggggggggggggggendEXTRABIT', tag3: 'somethingreallylonggggggggggggggggggggggggggggggggggggggggggendEXTRABIT'])
.withAnnotation("anno1", "value1")
.withAnnotations([anno2: "value2", anno3: "value3"])
.build()

then:
spec == [ apiVersion: 'v1',
kind: 'Pod',
metadata: [
name:'foo',
namespace:'xyz',
labels: [
app: 'myApp',
runName: 'something',
tag: 'somethingreallylonggggggggggggggggggggggggggggggggggggggggggend',
tag2: 'somethingreallylonggggggggggggggggggggggggggggggggggggggggggend',
tag3: 'somethingreallylonggggggggggggggggggggggggggggggggggggggggggend'
],
annotations: [
anno1: "value1",
anno2: "value2",
anno3: "value3"
]
],
spec: [
restartPolicy:'Never',
containers:[
[name:'foo',
image:'busybox',
command: ['sh', '-c', 'echo hello'],
workingDir:'/some/work/dir'
]
]
]
]
}


def 'should set resources and env' () {
Expand Down

0 comments on commit 6434b9d

Please sign in to comment.