From 001895a228d4c6a9b976fcb412e200fc1d7e8e09 Mon Sep 17 00:00:00 2001 From: Sascha Vornheder Date: Wed, 20 Nov 2019 09:05:10 +0100 Subject: [PATCH] added dockerOptions to the docu generator --- pkg/generator/helper/docuHelper.go | 53 +++++++++++++------ pkg/generator/helper/docuHelper_test.go | 25 ++++++--- .../helper/piper-context-defaults.yaml | 44 +++++++++++++++ 3 files changed, 99 insertions(+), 23 deletions(-) diff --git a/pkg/generator/helper/docuHelper.go b/pkg/generator/helper/docuHelper.go index 83e638a40..463d8498f 100644 --- a/pkg/generator/helper/docuHelper.go +++ b/pkg/generator/helper/docuHelper.go @@ -39,6 +39,7 @@ type ContextDefaultMetadata struct { type ContextDefaultParameters struct { Name string `json:"name"` Description string `json:"description"` + Scope []string `json:"scope"` } // ReadPipelineContextDefaultData loads step definition in yaml format @@ -51,17 +52,17 @@ func (c *ContextDefaultData) readPipelineContextDefaultData(metadata io.ReadClos } // ReadContextDefaultMap maps the default descriptions into a map -func (c *ContextDefaultData) readContextDefaultMap() map[string]string { - var m map[string]string = make(map[string]string) +func (c *ContextDefaultData) readContextDefaultMap() map[string]interface{} { + var m map[string]interface{} = make(map[string]interface{}) for _, param := range c.Parameters { - m[param.Name] = param.Description + m[param.Name] = param } return m } -func readContextDefaultDescription(contextDefaultPath string) map[string]string { +func readContextDefaultDescription(contextDefaultPath string) map[string]interface{} { //read context default description var ContextDefaultData ContextDefaultData @@ -198,14 +199,14 @@ func docGenConfiguration(stepData config.StepData) string { func createParametersTable(parameters []config.StepParameters) string { - var table = "| name | mandatory | default |\n" - table += "| ---- | --------- | ------- |\n" + var table = "| name | mandatory | default | possible values |\n" + table += "| ------- | --------- | ------- | ------- |\n" m := combineEqualParametersTogether(parameters) for _, param := range parameters { if v, ok := m[param.Name]; ok { - table += fmt.Sprintf(" | %v | %v | %v | \n ", param.Name, ifThenElse(param.Mandatory && param.Default == nil, "Yes", "No"), v) + table += fmt.Sprintf(" | %v | %v | %v | |\n ", param.Name, ifThenElse(param.Mandatory && param.Default == nil, "Yes", "No"), v) delete(m, param.Name) } } @@ -301,7 +302,9 @@ func handleStepParameters(stepData *config.StepData) { 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]}) + + dcp := mCD[k].(ContextDefaultParameters) + stepData.Spec.Inputs.Parameters = append(stepData.Spec.Inputs.Parameters, config.StepParameters{Name: k, Default: v, Mandatory: false, Description: dcp.Description, Scope: dcp.Scope}) } } } @@ -371,6 +374,8 @@ func addContainerValues(container config.Container, bEmptyKey bool, resources ma keys = append(keys, key+"_dockerName") keys = append(keys, key+"_dockerPullImage") keys = append(keys, key+"_dockerWorkspace") + keys = append(keys, key+"_dockerOptions") + keys = append(keys, key+"_dockerVolumeBind") } if len(container.Conditions) == 0 { @@ -403,12 +408,14 @@ func addValuesToMap(container config.Container, key string, resources map[string 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+"_dockerVolumeBind"] = append(resources[key+"_dockerVolumeBind"], fmt.Sprintf("%v:\\[%v\\]", key, strings.Join(volumeMountsAsStringSlice(container.VolumeMounts), ""))) + resources[key+"_dockerOptions"] = 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+"_dockerVolumeBind"] = append(resources[key+"_dockerVolumeBind"], fmt.Sprintf("%v", strings.Join(volumeMountsAsStringSlice(container.VolumeMounts), ""))) + resources[key+"_dockerOptions"] = append(resources[key+"_dockerVolumeBind"], fmt.Sprintf("%v", strings.Join(optionsAsStringSlice(container.Options), ""))) resources[key+"_dockerWorkspace"] = append(resources[key+"_dockerWorkspace"], workingDir) } } @@ -439,7 +446,8 @@ 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["sidecarVolumeBind"] = strings.Join(volumeMountsAsStringSlice(m.Spec.Sidecars[0].VolumeMounts), "") + result["sidecarOptions"] = strings.Join(optionsAsStringSlice(m.Spec.Sidecars[0].Options), "") result["sidecarWorkspace"] = m.Spec.Sidecars[0].WorkingDir } @@ -492,14 +500,27 @@ func envVarsAsStringSlice(envVars []config.EnvVar) []string { return e } -func volumeMountsAsStringSlice(volumeMonts []config.VolumeMount) []string { +//func volumeMountsAsStringSlice(volumeMonts []config.VolumeMount) []string { +// e := []string{} +// c := len(volumeMonts) - 1 +// for k, v := range volumeMonts { +// 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 optionsAsStringSlice(options []config.Option) []string { e := []string{} - c := len(envVars) - 1 - for k, v := range envVars { + c := len(options) - 1 + for k, v := range options { if k < c { - e = append(e, fmt.Sprintf("%v:%v,
", v.Name, ifThenElse(len(v.MountPath) > 0, v.MountPath, "\\"))) + e = append(e, fmt.Sprintf("%v %v,
", v.Name, ifThenElse(len(v.Value) > 0, v.Value, "\\"))) } else { - e = append(e, fmt.Sprintf("%v:%v", v.Name, ifThenElse(len(v.MountPath) > 0, v.MountPath, "\\"))) + e = append(e, fmt.Sprintf("%v %v", v.Name, ifThenElse(len(v.Value) > 0, v.Value, "\\"))) } } return e diff --git a/pkg/generator/helper/docuHelper_test.go b/pkg/generator/helper/docuHelper_test.go index 0159e3de7..8762e0e14 100644 --- a/pkg/generator/helper/docuHelper_test.go +++ b/pkg/generator/helper/docuHelper_test.go @@ -150,8 +150,11 @@ var stepData config.StepData = config.StepData{ {"param.name2b", "param.value2b"}, }}, }, - VolumeMounts: []config.VolumeMount{ - {"mp.2b", "mn.2b"}, + //VolumeMounts: []config.VolumeMount{ + // {"mp.2b", "mn.2b"}, + //}, + Options: []config.Option{ + {"option.name2b", "option.value2b"}, }, }, }, @@ -164,8 +167,11 @@ var stepData config.StepData = config.StepData{ {"param.name0", "param.value0"}, }}, }, - VolumeMounts: []config.VolumeMount{ - {"mp.3b", "mn.3b"}, + //VolumeMounts: []config.VolumeMount{ + // {"mp.3b", "mn.3b"}, + //}, + Options: []config.Option{ + {"option.name3b", "option.value3b"}, }, }, }, @@ -254,7 +260,8 @@ func TestAddContainerContent(t *testing.T) { {"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"}, + //{"dockerVolumeBind", "mp.2b:mn.2b"}, + {"dockerOptions", "option.name2b optnion.value2b"}, {"dockerWorkspace", "workingdir, workingdir
param.name2a=param.value2a:workingdir
param.name2b=param.value2b:workingdir"}, } for _, c := range cases { @@ -281,7 +288,8 @@ func TestAddSidecarContent(t *testing.T) { {"sidecarName", "sidecar0"}, {"sidecarPullImage", "true"}, {"sidecarReadyCommand", "readycommand"}, - {"sidecarVolumeBind", "mp.3b:mn.3b"}, + {"sidecarOptions", "option.name3b optnion.value3b"}, + //{"sidecarVolumeBind", "mp.3b:mn.3b"}, {"sidecarWorkspace", "workingdir"}, } for _, c := range cases { @@ -330,6 +338,8 @@ func TestGetDocuContextDefaults(t *testing.T) { {"sidecarName", "sidecar0"}, {"sidecarPullImage", "true"}, {"sidecarReadyCommand", "readycommand"}, + {"sidecarOptions", "option.name3b optnion.value3b"}, + //{"sidecarVolumeBind", "mp.3b:mn.3b"}, {"sidecarWorkspace", "workingdir"}, {"containerCommand", "command"}, {"containerName", "container0, container1
container2a
container2b
"}, @@ -338,7 +348,8 @@ func TestGetDocuContextDefaults(t *testing.T) { {"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"}, + //{"dockerVolumeBind", "mp.2b:mn.2b"}, + {"dockerOptions", "option.name2b optnion.value2b"}, {"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 b989e5605..ccaec4e87 100644 --- a/pkg/generator/helper/piper-context-defaults.yaml +++ b/pkg/generator/helper/piper-context-defaults.yaml @@ -6,37 +6,81 @@ metadata: params: - name: containerCommand description: 'Kubernetes only: Allows to specify start command for container created with dockerImage parameter to overwrite Piper default (/usr/bin/tail -f /dev/null).' + scope: + - STEPS - name: containerName description: Optional configuration in combination with containerMap to define the container where the commands should be executed in. + scope: + - STEPS - name: containerShell description: Allows to specify the shell to be executed for container with containerName. + scope: + - STEPS - name: dockerEnvVars description: 'Environment variables to set in the container, e.g. [http_proxy: "proxy:8080"].' + scope: + - STEPS - name: dockerName description: 'Kubernetes only: Name of the container launching dockerImage. SideCar only: Name of the container in local network.' + scope: + - STEPS - name: dockerPullImage 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. + scope: + - STEPS - 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. + scope: + - STEPS +- name: dockerOptions + description: Docker options to be set when starting the container. + scope: + - STEPS - name: dockerVolumeBind description: Volumes that should be mounted into the docker container. + scope: + - STEPS - name: dockerWorkspace description: 'Kubernetes only: Specifies a dedicated user home directory for the container which will be passed as value for environment variable HOME.' + scope: + - STEPS - name: sidecarCommand description: Allows to specify a start command for the sidecar container. This parameter is similar to containerCommand + scope: + - STEPS - name: sidecarEnvVars description: A map of environment variables to set in the sidecar container, similar to dockerEnvVars. + scope: + - STEPS - name: sidecarImage description: The name of the docker image of the sidecar container. If empty, no sidecar container is started. + scope: + - STEPS - name: sidecarName description: as dockerName for the sidecar container + scope: + - STEPS - name: sidecarPullImage 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. + scope: + - STEPS - name: sidecarReadyCommand description: Command executed inside the container which returns exit code 0 when the container is ready to be used. + scope: + - STEPS +- name: sidecarOptions + description: Options to be set when starting the sidecar container. + scope: + - STEPS - name: sidecarVolumeBind description: Volumes that should be mounted into the sidecar container. + scope: + - STEPS - name: sidecarWorkspace description: as dockerWorkspace for the sidecar container + scope: + - STEPS - name: stashContent description: Specific stashes that should be considered for the step execution. + scope: + - STEPS