From 5ba375250d8fc3a1b93181917aacfea222a6ebb1 Mon Sep 17 00:00:00 2001 From: Sascha Vornheder Date: Mon, 18 Nov 2019 11:14:45 +0100 Subject: [PATCH] removed complexity added volumeMount --- pkg/generator/helper/docuHelper.go | 158 +++++++++++------- pkg/generator/helper/docuHelper_test.go | 15 +- .../helper/piper-context-defaults.yaml | 4 + 3 files changed, 112 insertions(+), 65 deletions(-) diff --git a/pkg/generator/helper/docuHelper.go b/pkg/generator/helper/docuHelper.go index f3a13d080..83e638a40 100644 --- a/pkg/generator/helper/docuHelper.go +++ b/pkg/generator/helper/docuHelper.go @@ -299,7 +299,10 @@ func handleStepParameters(stepData *config.StepData) { //create StepParemeters items for context defaults for k, v := range context { if len(v) > 0 { - stepData.Spec.Inputs.Parameters = append(stepData.Spec.Inputs.Parameters, config.StepParameters{Name: k, Default: v, Mandatory: false, Description: mCD[k]}) + //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]}) + } } } } @@ -340,74 +343,89 @@ func addContainerContent(m *config.StepData, result map[string]string) { bEmptyKey := true for _, container := range m.Spec.Containers { - key := "" - if len(container.Conditions) > 0 { - key = fmt.Sprintf("%v=%v", container.Conditions[0].Params[0].Name, container.Conditions[0].Params[0].Value) - } - - 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 - } - - workingDir := ifThenElse(len(container.WorkingDir) > 0, container.WorkingDir, "\\") - 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]) - } - //ImagePullPolicy > 0 - if len(container.ImagePullPolicy) > 0 { - resources[key+"_dockerPullImage"] = []string{fmt.Sprintf("%v", container.ImagePullPolicy != "Never")} - } - //Different when key is set (Param.Name + Param.Value) - 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+"_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+"_dockerWorkspace"] = append(resources[key+"_dockerWorkspace"], workingDir) - } + rKeys := addContainerValues(container, bEmptyKey, resources) + keys = append(keys, rKeys...) } - for _, key := range keys { - s := strings.Split(key, "_") - if len(strings.Join(resources[key], ", ")) > 1 { - result[s[1]] += fmt.Sprintf("%v
", strings.Join(resources[key], ", ")) - } else if len(strings.Join(resources[key], ", ")) == 1 { - if _, ok := result[s[1]]; !ok { - result[s[1]] = fmt.Sprintf("%v", strings.Join(resources[key], ", ")) - } + 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") + } + 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) + + 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) + + //ContainerCommand > 0 + if len(container.Command) > 0 { + resources[key+"_containerCommand"] = append(resources[key+"_containerCommand"], container.Command[0]) + } + //ImagePullPolicy > 0 + if len(container.ImagePullPolicy) > 0 { + 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, "\\") + 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 { + result[s[1]] += fmt.Sprintf("%v
", strings.Join(resources[key], ", ")) + } else if len(strings.Join(resources[key], ", ")) == 1 { + if _, ok := result[s[1]]; !ok { + result[s[1]] = fmt.Sprintf("%v", strings.Join(resources[key], ", ")) } } } } + 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,
", v.Name, ifThenElse(len(v.MountPath) > 0, v.MountPath, "\\"))) + } else { + e = append(e, fmt.Sprintf("%v:%v", v.Name, ifThenElse(len(v.MountPath) > 0, v.MountPath, "\\"))) + } + } + return e +} + func sortStepParameters(stepData *config.StepData) { if stepData.Spec.Inputs.Parameters != nil { diff --git a/pkg/generator/helper/docuHelper_test.go b/pkg/generator/helper/docuHelper_test.go index 846ac3e96..0159e3de7 100644 --- a/pkg/generator/helper/docuHelper_test.go +++ b/pkg/generator/helper/docuHelper_test.go @@ -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
container2a
container2b
"}, {"containerShell", "shell"}, {"dockerEnvVars", "envar.name0=envar.value0, envar.name1=envar.value1
param.name2a=param.value2a:\\[envar.name2a=envar.value2a\\]
param.name2b=param.value2b:\\[envar.name2b=envar.value2b\\]"}, {"dockerImage", "image, image
param.name2a=param.value2a:image
param.name2b=param.value2b:image"}, {"dockerName", "container0, container1
container2a
container2b
"}, {"dockerPullImage", "true"}, + {"dockerVolumeBind", "mp.2b:mn.2b"}, {"dockerWorkspace", "workingdir, workingdir
param.name2a=param.value2a:workingdir
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
container2a
container2b
"}, {"containerShell", "shell"}, {"dockerEnvVars", "envar.name0=envar.value0, envar.name1=envar.value1
param.name2a=param.value2a:\\[envar.name2a=envar.value2a\\]
param.name2b=param.value2b:\\[envar.name2b=envar.value2b\\]"}, {"dockerImage", "image, image
param.name2a=param.value2a:image
param.name2b=param.value2b:image"}, {"dockerName", "container0, container1
container2a
container2b
"}, {"dockerPullImage", "true"}, + {"dockerVolumeBind", "mp.2b:mn.2b"}, {"dockerWorkspace", "workingdir, workingdir
param.name2a=param.value2a:workingdir
param.name2b=param.value2b:workingdir"}, } for _, c := range cases { diff --git a/pkg/generator/helper/piper-context-defaults.yaml b/pkg/generator/helper/piper-context-defaults.yaml index 058602d21..b989e5605 100644 --- a/pkg/generator/helper/piper-context-defaults.yaml +++ b/pkg/generator/helper/piper-context-defaults.yaml @@ -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