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

removed complexity added volumeMount

This commit is contained in:
Sascha Vornheder 2019-11-18 11:14:45 +01:00
parent 4c00db7a66
commit 5ba375250d
3 changed files with 112 additions and 65 deletions

View File

@ -299,10 +299,13 @@ func handleStepParameters(stepData *config.StepData) {
//create StepParemeters items for context defaults
for k, v := range context {
if len(v) > 0 {
//containerName only for Step: dockerExecuteOnKubernetes
if k != "containerName" || stepData.Metadata.Name == "dockerExecuteOnKubernetes" {
stepData.Spec.Inputs.Parameters = append(stepData.Spec.Inputs.Parameters, config.StepParameters{Name: k, Default: v, Mandatory: false, Description: mCD[k]})
}
}
}
}
//Sort Parameters
sortStepParameters(stepData)
}
@ -340,42 +343,53 @@ func addContainerContent(m *config.StepData, result map[string]string) {
bEmptyKey := true
for _, container := range m.Spec.Containers {
rKeys := addContainerValues(container, bEmptyKey, resources)
keys = append(keys, rKeys...)
}
loopContainerDefaults(keys, resources, result)
}
}
func addContainerValues(container config.Container, bEmptyKey bool, resources map[string][]string) []string {
keys := []string{}
key := ""
if len(container.Conditions) > 0 {
key = fmt.Sprintf("%v=%v", container.Conditions[0].Params[0].Name, container.Conditions[0].Params[0].Value)
}
//only add the key ones
if bEmptyKey || len(key) > 0 {
if len(container.Command) > 0 {
keys = append(keys, key+"_containerCommand")
}
if m.Metadata.Name == "dockerExecuteOnKubernetes" {
keys = append(keys, key+"_containerName")
}
keys = append(keys, key+"_containerShell")
keys = append(keys, key+"_dockerEnvVars")
keys = append(keys, key+"_dockerImage")
keys = append(keys, key+"_dockerName")
keys = append(keys, key+"_dockerPullImage")
keys = append(keys, key+"_dockerWorkspace")
}
if len(container.Conditions) == 0 {
bEmptyKey = false
}
addValuesToMap(container, key, resources)
workingDir := ifThenElse(len(container.WorkingDir) > 0, container.WorkingDir, "\\<empty\\>")
return keys
}
func addValuesToMap(container config.Container, key string, resources map[string][]string) {
resources[key+"_containerName"] = append(resources[key+"_containerName"], container.Name)
//ContainerShell > 0
if len(container.Shell) > 0 {
resources[key+"_containerShell"] = append(resources[key+"_containerShell"], container.Shell)
}
resources[key+"_dockerName"] = append(resources[key+"_dockerName"], container.Name)
//Only for Step: dockerExecuteOnKubernetes
if m.Metadata.Name == "dockerExecuteOnKubernetes" {
resources[key+"_containerName"] = append(resources[key+"_containerName"], container.Name)
}
//ContainerCommand > 0
if len(container.Command) > 0 {
resources[key+"_containerCommand"] = append(resources[key+"_containerCommand"], container.Command[0])
@ -385,17 +399,21 @@ func addContainerContent(m *config.StepData, result map[string]string) {
resources[key+"_dockerPullImage"] = []string{fmt.Sprintf("%v", container.ImagePullPolicy != "Never")}
}
//Different when key is set (Param.Name + Param.Value)
workingDir := ifThenElse(len(container.WorkingDir) > 0, container.WorkingDir, "\\<empty\\>")
if len(key) > 0 {
resources[key+"_dockerEnvVars"] = append(resources[key+"_dockerEnvVars"], fmt.Sprintf("%v:\\[%v\\]", key, strings.Join(envVarsAsStringSlice(container.EnvVars), "")))
resources[key+"_dockerImage"] = append(resources[key+"_dockerImage"], fmt.Sprintf("%v:%v", key, container.Image))
resources[key+"_dockerVolumeBind"] = append(resources[key+"_dockerVolumeBind"], fmt.Sprintf("%v:\\[%v\\]", key, strings.Join(volumeMountsAsStringSlice(container.VolumeMounts), "")))
resources[key+"_dockerWorkspace"] = append(resources[key+"_dockerWorkspace"], fmt.Sprintf("%v:%v", key, workingDir))
} else {
resources[key+"_dockerEnvVars"] = append(resources[key+"_dockerEnvVars"], fmt.Sprintf("%v", strings.Join(envVarsAsStringSlice(container.EnvVars), "")))
resources[key+"_dockerImage"] = append(resources[key+"_dockerImage"], container.Image)
resources[key+"_dockerVolumeBind"] = append(resources[key+"_dockerVolumeBind"], fmt.Sprintf("%v", strings.Join(volumeMountsAsStringSlice(container.VolumeMounts), "")))
resources[key+"_dockerWorkspace"] = append(resources[key+"_dockerWorkspace"], workingDir)
}
}
func loopContainerDefaults(keys []string, resources map[string][]string, result map[string]string) {
for _, key := range keys {
s := strings.Split(key, "_")
if len(strings.Join(resources[key], ", ")) > 1 {
@ -407,7 +425,7 @@ func addContainerContent(m *config.StepData, result map[string]string) {
}
}
}
}
func addSidecarContent(m *config.StepData, result map[string]string) {
//creates the context defaults for sidecars
if len(m.Spec.Sidecars) > 0 {
@ -421,6 +439,7 @@ func addSidecarContent(m *config.StepData, result map[string]string) {
result["sidecarPullImage"] = fmt.Sprintf("%v", m.Spec.Sidecars[0].ImagePullPolicy != "Never")
}
result["sidecarReadyCommand"] = m.Spec.Sidecars[0].ReadyCommand
result["sidecarVolumeBind"] = strings.Join(volumeMountsAsStringSlice(m.Spec.Sidecars[0].VolumeMounts), "")
result["sidecarWorkspace"] = m.Spec.Sidecars[0].WorkingDir
}
@ -473,6 +492,19 @@ func envVarsAsStringSlice(envVars []config.EnvVar) []string {
return e
}
func volumeMountsAsStringSlice(volumeMonts []config.VolumeMount) []string {
e := []string{}
c := len(envVars) - 1
for k, v := range envVars {
if k < c {
e = append(e, fmt.Sprintf("%v:%v, <br>", v.Name, ifThenElse(len(v.MountPath) > 0, v.MountPath, "\\<empty\\>")))
} else {
e = append(e, fmt.Sprintf("%v:%v", v.Name, ifThenElse(len(v.MountPath) > 0, v.MountPath, "\\<empty\\>")))
}
}
return e
}
func sortStepParameters(stepData *config.StepData) {
if stepData.Spec.Inputs.Parameters != nil {

View File

@ -150,6 +150,9 @@ var stepData config.StepData = config.StepData{
{"param.name2b", "param.value2b"},
}},
},
VolumeMounts: []config.VolumeMount{
{"mp.2b", "mn.2b"},
},
},
},
Sidecars: []config.Container{
@ -161,6 +164,9 @@ var stepData config.StepData = config.StepData{
{"param.name0", "param.value0"},
}},
},
VolumeMounts: []config.VolumeMount{
{"mp.3b", "mn.3b"},
},
},
},
},
@ -236,17 +242,19 @@ func TestAddContainerContent(t *testing.T) {
var m map[string]string = make(map[string]string)
addContainerContent(&stepData, m)
assert.Equal(t, 7, len(m))
assert.Equal(t, 8, len(m))
cases := []struct {
x, want string
}{
{"containerCommand", "command"},
{"containerName", "container0, container1 <br>container2a <br>container2b <br>"},
{"containerShell", "shell"},
{"dockerEnvVars", "envar.name0=envar.value0, envar.name1=envar.value1 <br>param.name2a=param.value2a:\\[envar.name2a=envar.value2a\\] <br>param.name2b=param.value2b:\\[envar.name2b=envar.value2b\\]"},
{"dockerImage", "image, image <br>param.name2a=param.value2a:image <br>param.name2b=param.value2b:image"},
{"dockerName", "container0, container1 <br>container2a <br>container2b <br>"},
{"dockerPullImage", "true"},
{"dockerVolumeBind", "mp.2b:mn.2b"},
{"dockerWorkspace", "workingdir, workingdir <br>param.name2a=param.value2a:workingdir <br>param.name2b=param.value2b:workingdir"},
}
for _, c := range cases {
@ -273,6 +281,7 @@ func TestAddSidecarContent(t *testing.T) {
{"sidecarName", "sidecar0"},
{"sidecarPullImage", "true"},
{"sidecarReadyCommand", "readycommand"},
{"sidecarVolumeBind", "mp.3b:mn.3b"},
{"sidecarWorkspace", "workingdir"},
}
for _, c := range cases {
@ -309,7 +318,7 @@ func TestGetDocuContextDefaults(t *testing.T) {
t.Run("Success Case", func(t *testing.T) {
m := getDocuContextDefaults(&stepData)
assert.Equal(t, 15, len(m))
assert.Equal(t, 16, len(m))
cases := []struct {
x, want string
@ -323,11 +332,13 @@ func TestGetDocuContextDefaults(t *testing.T) {
{"sidecarReadyCommand", "readycommand"},
{"sidecarWorkspace", "workingdir"},
{"containerCommand", "command"},
{"containerName", "container0, container1 <br>container2a <br>container2b <br>"},
{"containerShell", "shell"},
{"dockerEnvVars", "envar.name0=envar.value0, envar.name1=envar.value1 <br>param.name2a=param.value2a:\\[envar.name2a=envar.value2a\\] <br>param.name2b=param.value2b:\\[envar.name2b=envar.value2b\\]"},
{"dockerImage", "image, image <br>param.name2a=param.value2a:image <br>param.name2b=param.value2b:image"},
{"dockerName", "container0, container1 <br>container2a <br>container2b <br>"},
{"dockerPullImage", "true"},
{"dockerVolumeBind", "mp.2b:mn.2b"},
{"dockerWorkspace", "workingdir, workingdir <br>param.name2a=param.value2a:workingdir <br>param.name2b=param.value2b:workingdir"},
}
for _, c := range cases {

View File

@ -18,6 +18,8 @@ params:
description: Set this to 'false' to bypass a docker image pull. Usefull during development process. Allows testing of images which are available in the local registry only.
- name: dockerImage
description: Name of the docker image that should be used. If empty, Docker is not used and the command is executed directly on the Jenkins system.
- name: dockerVolumeBind
description: Volumes that should be mounted into the docker container.
- name: dockerWorkspace
description: 'Kubernetes only: Specifies a dedicated user home directory for the container which will be passed as value for environment variable HOME.'
- name: sidecarCommand
@ -32,6 +34,8 @@ params:
description: Set this to 'false' to bypass a docker image pull. Usefull during development process. Allows testing of images which are available in the local registry only.
- name: sidecarReadyCommand
description: Command executed inside the container which returns exit code 0 when the container is ready to be used.
- name: sidecarVolumeBind
description: Volumes that should be mounted into the sidecar container.
- name: sidecarWorkspace
description: as dockerWorkspace for the sidecar container
- name: stashContent