1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

add initContainer capabilities (#2820)

* set init container capabilities

* set init container capabilities

* correct parameter definition + change assertThat

Co-authored-by: Adnan Awan <adnan.awan@sap.com>
Co-authored-by: Thorsten Duda <thorsten.duda@sap.com>
This commit is contained in:
Jesse Awan 2021-06-01 09:59:49 +02:00 committed by GitHub
parent a830a35800
commit 3b2e7dc53d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 4 deletions

View File

@ -676,7 +676,6 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
def expectedSecurityContext = [runAsUser: 1000, fsGroup: 1000] def expectedSecurityContext = [runAsUser: 1000, fsGroup: 1000]
nullScript.commonPipelineEnvironment.configuration = [general: [jenkinsKubernetes: [ nullScript.commonPipelineEnvironment.configuration = [general: [jenkinsKubernetes: [
securityContext: expectedSecurityContext]]] securityContext: expectedSecurityContext]]]
stepRule.step.dockerExecuteOnKubernetes( stepRule.step.dockerExecuteOnKubernetes(
script: nullScript, script: nullScript,
juStabUtils: utils, juStabUtils: utils,
@ -803,11 +802,12 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
script: nullScript, script: nullScript,
juStabUtils: utils, juStabUtils: utils,
containerName: 'mycontainer', containerName: 'mycontainer',
initContainerImage: 'ppiper/cf-cli',
dockerImage: 'maven:3.5-jdk-8-alpine', dockerImage: 'maven:3.5-jdk-8-alpine',
containerMountPath: '/opt', containerMountPath: '/opt',
) { bodyExecuted = true } ) { bodyExecuted = true }
def containerSpec = podSpec.spec.containers.find{it.image == "maven:3.5-jdk-8-alpine"} def containerSpec = podSpec.spec.containers.find{it.image == "maven:3.5-jdk-8-alpine"}
def initContainerSpec = podSpec.spec.initContainers[0]
assertTrue(bodyExecuted) assertTrue(bodyExecuted)
assertEquals( assertEquals(
[[ [[
@ -819,6 +819,46 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
"name" : "volume", "name" : "volume",
"mountPath": "/opt" "mountPath": "/opt"
]], containerSpec.volumeMounts) ]], containerSpec.volumeMounts)
assertEquals(
[[
"name" : "volume",
"mountPath": "/opt"
]], initContainerSpec.volumeMounts)
}
@Test
void testInitContainerDefaultWithParameters() {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
containerName: 'mycontainer',
initContainerImage: 'ppiper/cf-cli@sha256:latest',
initContainerCommand: 'cp /usr/local/bin/cf7 /opt/bin/cf',
dockerImage: 'maven:3.5-jdk-8-alpine',
containerMountPath: '/opt',
) { bodyExecuted = true }
def initContainer = podSpec.spec.initContainers[0]
def expectedCommandOutput = ["sh", "-c", "cp /usr/local/bin/cf7 /opt/bin/cf"]
assertTrue(bodyExecuted)
assertEquals("ppiper-cf-cli-sha256-latest", initContainer.name)
assertEquals("ppiper/cf-cli@sha256:latest", initContainer.image)
assertThat(initContainer.command, is(equalTo(expectedCommandOutput)))
}
@Test
void testInitContainerWithoutContainerCommand() {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
containerName: 'mycontainer',
initContainerImage: 'ppiper/cf-cli@sha256:latest',
dockerImage: 'maven:3.5-jdk-8-alpine',
containerMountPath: '/opt',
) { bodyExecuted = true }
def initContainer = podSpec.spec.initContainers[0]
def expectedCommandOutput = ["/usr/bin/tail", "-f", "/dev/null"]
assertTrue(bodyExecuted)
assertThat(initContainer.command, is(equalTo(expectedCommandOutput)))
} }
private container(options, body) { private container(options, body) {

View File

@ -198,6 +198,15 @@ import hudson.AbortException
* use this volume in an initContainer. * use this volume in an initContainer.
*/ */
'containerMountPath', 'containerMountPath',
/**
* The docker image to run as initContainer.
*/
'initContainerImage',
/**
* Command executed inside the init container shell. Please enter command without providing any "sh -c" prefix. For example for an echo message, simply enter: echo `HelloWorld`
*/
'initContainerCommand',
]) ])
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS.minus([ @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS.minus([
'stashIncludes', 'stashIncludes',
@ -336,6 +345,7 @@ private String generatePodSpec(Map config) {
spec : [:] spec : [:]
] ]
podSpec.spec += getAdditionalPodProperties(config) podSpec.spec += getAdditionalPodProperties(config)
podSpec.spec.initContainers = getInitContainerList(config)
podSpec.spec.containers = getContainerList(config) podSpec.spec.containers = getContainerList(config)
podSpec.spec.securityContext = getSecurityContext(config) podSpec.spec.securityContext = getSecurityContext(config)
@ -349,7 +359,6 @@ private String generatePodSpec(Map config) {
return new JsonUtils().groovyObjectToPrettyJsonString(podSpec) return new JsonUtils().groovyObjectToPrettyJsonString(podSpec)
} }
private String stashWorkspace(config, prefix, boolean chown = false, boolean stashBack = false) { private String stashWorkspace(config, prefix, boolean chown = false, boolean stashBack = false) {
def stashName = "${prefix}-${config.uniqueId}" def stashName = "${prefix}-${config.uniqueId}"
try { try {
@ -416,6 +425,36 @@ private void unstashWorkspace(config, prefix) {
} }
} }
private List getInitContainerList(config){
def initContainerSpecList = []
if (config.initContainerImage && config.containerMountPath) {
// regex [\W_] matches any non-word character equivalent to [^a-zA-Z0-9_]
def initContainerName = config.initContainerImage.toLowerCase().replaceAll(/[\W_]/,"-" )
def initContainerSpec = [
name : initContainerName,
image : config.initContainerImage
]
if (config.containerMountPath) {
initContainerSpec.volumeMounts = [[name: "volume", mountPath: config.containerMountPath]]
}
if (config.initContainerCommand == null) {
initContainerSpec['command'] = [
'/usr/bin/tail',
'-f',
'/dev/null'
]
} else {
initContainerSpec['command'] = [
'sh',
'-c',
config.initContainerCommand
]
}
initContainerSpecList.push(initContainerSpec)
}
return initContainerSpecList
}
private List getContainerList(config) { private List getContainerList(config) {
//If no custom jnlp agent provided as default jnlp agent (jenkins/jnlp-slave) as defined in the plugin, see https://github.com/jenkinsci/kubernetes-plugin#pipeline-support //If no custom jnlp agent provided as default jnlp agent (jenkins/jnlp-slave) as defined in the plugin, see https://github.com/jenkinsci/kubernetes-plugin#pipeline-support
@ -497,7 +536,6 @@ private List getContainerList(config) {
env : getContainerEnvs(config, config.sidecarImage, config.sidecarEnvVars, config.sidecarWorkspace), env : getContainerEnvs(config, config.sidecarImage, config.sidecarEnvVars, config.sidecarWorkspace),
command : [] command : []
] ]
def resources = getResources(sideCarContainerName, config) def resources = getResources(sideCarContainerName, config)
if(resources) { if(resources) {
containerSpec.resources = resources containerSpec.resources = resources