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

Add custom labels in build request with conda and spack packges #422

Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
04108d2
labels added in spack dockerfile
munishchouhan Mar 12, 2024
4ae6caa
labels added in spack dockerfile
munishchouhan Mar 12, 2024
00c0517
fixed tests
munishchouhan Mar 12, 2024
8503a66
adding labels via kaniko
munishchouhan Mar 12, 2024
2b99d28
adding labels via kaniko
munishchouhan Mar 12, 2024
1d62b72
fixed test
munishchouhan Mar 13, 2024
d3a9e14
corrected label injection in kaniko
munishchouhan Mar 13, 2024
1318e68
tests fixed
munishchouhan Mar 13, 2024
f863e92
labels added to spack singularity file
munishchouhan Mar 13, 2024
a923649
labels added to spack singularity file
munishchouhan Mar 13, 2024
b7d8049
labels added in containerfile and removed from kaniko
munishchouhan Mar 13, 2024
e7b3e39
minor formatting
munishchouhan Mar 13, 2024
12926e1
labels moved to containerconfig
munishchouhan Mar 15, 2024
7559489
minor change
munishchouhan Mar 19, 2024
7566464
Merge branch 'master' into 396-support-custom-labels-when-building-co…
munishchouhan Mar 26, 2024
b0a73dd
changes as per review
munishchouhan Apr 10, 2024
d0f6e05
Merge remote-tracking branch 'origin/master' into 396-support-custom-…
munishchouhan Apr 10, 2024
a1c0dad
changes as per review
munishchouhan Apr 10, 2024
49b16e7
minor change
munishchouhan Apr 10, 2024
317b384
Merge branch 'master' into 396-support-custom-labels-when-building-co…
munishchouhan Apr 11, 2024
efdfc1a
changes made as per review
munishchouhan Apr 11, 2024
7cf792b
minor makeup
marcodelapierre Apr 12, 2024
14a7b94
consistent order for readability: labels after env
marcodelapierre Apr 12, 2024
c4dc428
added doc
munishchouhan Apr 16, 2024
5bcaa86
fixed typo
munishchouhan Apr 16, 2024
ae608d5
Merge remote-tracking branch 'origin/master' into 396-support-custom-…
munishchouhan Apr 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,44 @@ class ContainerBuildServiceImpl implements ContainerBuildService {
}
}

//this method adds labels in singularity container
protected static String addLabels(String containerFile, final BuildRequest req){
final labels = req.containerConfig?.labels
if(labels) {
if (req.formatSingularity()) {
return containerFile + getSingularityLabels(labels)
}
if (req.formatDocker()){
return containerFile + getDockerLabels(labels)
}
}
return containerFile
}

protected static String getSingularityLabels(Map<String, String> labels){
StringBuilder labelsBuilder = new StringBuilder()
labelsBuilder.append("\n%labels\n")
labels.each() { key, value ->
labelsBuilder.append(key)
labelsBuilder.append(" ")
labelsBuilder.append(value)
labelsBuilder.append("\n")
}
return labelsBuilder.toString()
}

protected static String getDockerLabels(Map<String, String> labels){
StringBuilder labelsBuilder = new StringBuilder()
labelsBuilder.append("\nLABEL ")
labels.each() { key, value ->
labelsBuilder.append(key)
labelsBuilder.append("=")
labelsBuilder.append(value)
labelsBuilder.append(" ")
}
return labelsBuilder.toString()
}

protected BuildResult launch(BuildRequest req) {
// launch an external process to build the container
BuildResult resp=null
Expand All @@ -177,7 +215,7 @@ class ContainerBuildServiceImpl implements ContainerBuildService {
catch (FileAlreadyExistsException e) { /* ignore it */ }
// save the dockerfile
final containerFile = req.workDir.resolve('Containerfile')
Files.write(containerFile, containerFile0(req, context, spackConfig).bytes, CREATE, WRITE, TRUNCATE_EXISTING)
Files.write(containerFile, addLabels(containerFile0(req, context, spackConfig), req).bytes, CREATE, WRITE, TRUNCATE_EXISTING)
// save build context
if( req.buildContext ) {
saveBuildContext(req.buildContext, context, req.identity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ class ContainerBuildServiceTest extends Specification {
and:
def layer = new Packer().layer(source)
def context = BuildContext.of(layer)
and:

when:
service.saveBuildContext(context, target, Mock(PlatformId))
Expand Down Expand Up @@ -445,4 +444,59 @@ class ContainerBuildServiceTest extends Specification {
server?.stop(0)
}

def 'should resolve singularity file with labels' () {
given:
def folder = Files.createTempDirectory('test')
def builder = new ContainerBuildServiceImpl()
and:
def context = Path.of('/some/context/dir')
def singularityFile = "Bootstrap: docker \n from alpine"
def REQ = new BuildRequest(singularityFile, folder, 'box:latest', null, null, BuildFormat.SINGULARITY, Mock(PlatformId),null, null, ContainerPlatform.of('amd64'), null, null, null, "", null)
and:
def labels = [
"arch": "arm64",
"packageName": "salmon",
"version": "1.0.0"
]
REQ.withLabels(labels)

when:
def result = builder.addLabels(builder.containerFile0(REQ, context, null), REQ)

then:
result.contains('%labels\n'+
'arch arm64\n' +
'packageName salmon\n' +
'version 1.0.0')

cleanup:
folder?.deleteDir()
}

def 'should resolve docker file with labels' () {
given:
def folder = Files.createTempDirectory('test')
def builder = new ContainerBuildServiceImpl()
and:
def context = Path.of('/some/context/dir')
def dockerFile = "from alpine"
def REQ = new BuildRequest(dockerFile, folder, 'box:latest', null, null, BuildFormat.DOCKER, Mock(PlatformId),null, null, ContainerPlatform.of('amd64'), null, null, null, "", null)
and:
def labels = [
"arch": "arm64",
"packageName": "salmon",
"version": "1.0.0"
]
REQ.withLabels(labels)

when:
def result = builder.addLabels(builder.containerFile0(REQ, context, null), REQ)

then:
result.contains('LABEL arch=arm64 packageName=salmon version=1.0.0')

cleanup:
folder?.deleteDir()
}

}
Loading