2019-10-22 15:41:27 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2019-10-24 10:59:58 +02:00
|
|
|
"bytes"
|
2020-07-15 10:09:42 +02:00
|
|
|
"encoding/json"
|
2019-10-24 10:59:58 +02:00
|
|
|
"fmt"
|
2019-10-22 15:41:27 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2020-01-15 13:16:25 +02:00
|
|
|
|
2020-07-15 10:09:42 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
2020-01-15 13:16:25 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/piperenv"
|
2019-10-22 15:41:27 +02:00
|
|
|
|
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepData defines the metadata for a step, like step descriptions, parameters, ...
|
|
|
|
type StepData struct {
|
|
|
|
Metadata StepMetadata `json:"metadata"`
|
|
|
|
Spec StepSpec `json:"spec"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// StepMetadata defines the metadata for a step, like step descriptions, parameters, ...
|
|
|
|
type StepMetadata struct {
|
2020-03-19 18:24:35 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
Aliases []Alias `json:"aliases,omitempty"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
LongDescription string `json:"longDescription,omitempty"`
|
2019-10-22 15:41:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// StepSpec defines the spec details for a step, like step inputs, containers, sidecars, ...
|
|
|
|
type StepSpec struct {
|
2020-01-15 13:16:25 +02:00
|
|
|
Inputs StepInputs `json:"inputs,omitempty"`
|
|
|
|
Outputs StepOutputs `json:"outputs,omitempty"`
|
2019-10-24 10:59:58 +02:00
|
|
|
Containers []Container `json:"containers,omitempty"`
|
|
|
|
Sidecars []Container `json:"sidecars,omitempty"`
|
2019-10-22 15:41:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// StepInputs defines the spec details for a step, like step inputs, containers, sidecars, ...
|
|
|
|
type StepInputs struct {
|
|
|
|
Parameters []StepParameters `json:"params"`
|
|
|
|
Resources []StepResources `json:"resources,omitempty"`
|
|
|
|
Secrets []StepSecrets `json:"secrets,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// StepParameters defines the parameters for a step
|
|
|
|
type StepParameters struct {
|
2020-01-15 13:16:25 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
LongDescription string `json:"longDescription,omitempty"`
|
|
|
|
ResourceRef []ResourceReference `json:"resourceRef,omitempty"`
|
|
|
|
Scope []string `json:"scope"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Mandatory bool `json:"mandatory,omitempty"`
|
|
|
|
Default interface{} `json:"default,omitempty"`
|
2020-04-24 14:13:02 +02:00
|
|
|
PossibleValues []interface{} `json:"possibleValues,omitempty"`
|
2020-01-15 13:16:25 +02:00
|
|
|
Aliases []Alias `json:"aliases,omitempty"`
|
|
|
|
Conditions []Condition `json:"conditions,omitempty"`
|
2020-04-16 14:37:45 +02:00
|
|
|
Secret bool `json:"secret,omitempty"`
|
2020-01-15 13:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceReference defines the parameters of a resource reference
|
|
|
|
type ResourceReference struct {
|
2020-07-22 11:15:48 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Param string `json:"param,omitempty"`
|
2020-09-16 14:50:09 +02:00
|
|
|
Paths []string `json:"paths,omitempty"`
|
2020-07-22 11:15:48 +02:00
|
|
|
Aliases []Alias `json:"aliases,omitempty"`
|
2019-10-29 11:58:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Alias defines a step input parameter alias
|
|
|
|
type Alias struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Deprecated bool `json:"deprecated,omitempty"`
|
2019-10-22 15:41:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// StepResources defines the resources to be provided by the step context, e.g. Jenkins pipeline
|
|
|
|
type StepResources struct {
|
2020-01-15 13:16:25 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Parameters []map[string]interface{} `json:"params,omitempty"`
|
|
|
|
Conditions []Condition `json:"conditions,omitempty"`
|
2019-10-22 15:41:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// StepSecrets defines the secrets to be provided by the step context, e.g. Jenkins pipeline
|
|
|
|
type StepSecrets struct {
|
2020-04-01 20:46:33 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Aliases []Alias `json:"aliases,omitempty"`
|
2019-10-22 15:41:27 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 13:16:25 +02:00
|
|
|
// StepOutputs defines the outputs of a step step, typically one or multiple resources
|
|
|
|
type StepOutputs struct {
|
|
|
|
Resources []StepResources `json:"resources,omitempty"`
|
|
|
|
}
|
2019-10-22 15:41:27 +02:00
|
|
|
|
2019-10-24 10:59:58 +02:00
|
|
|
// Container defines an execution container
|
|
|
|
type Container struct {
|
|
|
|
//ToDo: check dockerOptions, dockerVolumeBind, containerPortMappings, sidecarOptions, sidecarVolumeBind
|
2019-11-19 12:52:34 +02:00
|
|
|
Command []string `json:"command"`
|
|
|
|
EnvVars []EnvVar `json:"env"`
|
|
|
|
Image string `json:"image"`
|
|
|
|
ImagePullPolicy string `json:"imagePullPolicy"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ReadyCommand string `json:"readyCommand"`
|
|
|
|
Shell string `json:"shell"`
|
|
|
|
WorkingDir string `json:"workingDir"`
|
|
|
|
Conditions []Condition `json:"conditions,omitempty"`
|
2020-04-01 20:46:33 +02:00
|
|
|
Options []Option `json:"options,omitempty"`
|
2019-11-19 12:52:34 +02:00
|
|
|
//VolumeMounts []VolumeMount `json:"volumeMounts,omitempty"`
|
2019-11-15 11:25:23 +02:00
|
|
|
}
|
|
|
|
|
2020-04-01 20:46:33 +02:00
|
|
|
// ToDo: Add the missing Volumes part to enable the volume mount completely
|
|
|
|
// VolumeMount defines a mount path
|
2019-11-19 12:52:34 +02:00
|
|
|
// type VolumeMount struct {
|
|
|
|
// MountPath string `json:"mountPath"`
|
|
|
|
// Name string `json:"name"`
|
|
|
|
//}
|
|
|
|
|
|
|
|
// Option defines an docker option
|
|
|
|
type Option struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value string `json:"value"`
|
2019-10-22 15:41:27 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 10:59:58 +02:00
|
|
|
// EnvVar defines an environment variable
|
|
|
|
type EnvVar struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value string `json:"value"`
|
2019-10-22 15:41:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 17:29:39 +02:00
|
|
|
// Condition defines an condition which decides when the parameter, resource or container is valid
|
|
|
|
type Condition struct {
|
|
|
|
ConditionRef string `json:"conditionRef"`
|
|
|
|
Params []Param `json:"params"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Param defines the parameters serving as inputs to the condition
|
|
|
|
type Param struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
2019-10-22 15:41:27 +02:00
|
|
|
// StepFilters defines the filter parameters for the different sections
|
|
|
|
type StepFilters struct {
|
|
|
|
All []string
|
|
|
|
General []string
|
|
|
|
Stages []string
|
|
|
|
Steps []string
|
|
|
|
Parameters []string
|
|
|
|
Env []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadPipelineStepData loads step definition in yaml format
|
|
|
|
func (m *StepData) ReadPipelineStepData(metadata io.ReadCloser) error {
|
|
|
|
defer metadata.Close()
|
|
|
|
content, err := ioutil.ReadAll(metadata)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error reading %v", metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(content, &m)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error unmarshalling: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetParameterFilters retrieves all scope dependent parameter filters
|
|
|
|
func (m *StepData) GetParameterFilters() StepFilters {
|
2020-01-28 00:40:53 +02:00
|
|
|
filters := StepFilters{All: []string{"verbose"}, General: []string{"verbose"}, Steps: []string{"verbose"}, Stages: []string{"verbose"}, Parameters: []string{"verbose"}}
|
2019-10-22 15:41:27 +02:00
|
|
|
for _, param := range m.Spec.Inputs.Parameters {
|
2019-11-04 17:29:39 +02:00
|
|
|
parameterKeys := []string{param.Name}
|
|
|
|
for _, condition := range param.Conditions {
|
|
|
|
for _, dependentParam := range condition.Params {
|
|
|
|
parameterKeys = append(parameterKeys, dependentParam.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filters.All = append(filters.All, parameterKeys...)
|
2019-10-22 15:41:27 +02:00
|
|
|
for _, scope := range param.Scope {
|
|
|
|
switch scope {
|
|
|
|
case "GENERAL":
|
2019-11-04 17:29:39 +02:00
|
|
|
filters.General = append(filters.General, parameterKeys...)
|
2019-10-22 15:41:27 +02:00
|
|
|
case "STEPS":
|
2019-11-04 17:29:39 +02:00
|
|
|
filters.Steps = append(filters.Steps, parameterKeys...)
|
2019-10-22 15:41:27 +02:00
|
|
|
case "STAGES":
|
2019-11-04 17:29:39 +02:00
|
|
|
filters.Stages = append(filters.Stages, parameterKeys...)
|
2019-10-22 15:41:27 +02:00
|
|
|
case "PARAMETERS":
|
2019-11-04 17:29:39 +02:00
|
|
|
filters.Parameters = append(filters.Parameters, parameterKeys...)
|
2019-10-22 15:41:27 +02:00
|
|
|
case "ENV":
|
2019-11-04 17:29:39 +02:00
|
|
|
filters.Env = append(filters.Env, parameterKeys...)
|
2019-10-22 15:41:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filters
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContextParameterFilters retrieves all scope dependent parameter filters
|
|
|
|
func (m *StepData) GetContextParameterFilters() StepFilters {
|
|
|
|
var filters StepFilters
|
2020-07-02 12:08:56 +02:00
|
|
|
contextFilters := []string{}
|
2019-10-22 15:41:27 +02:00
|
|
|
for _, secret := range m.Spec.Inputs.Secrets {
|
2020-07-02 12:08:56 +02:00
|
|
|
contextFilters = append(contextFilters, secret.Name)
|
2019-10-22 15:41:27 +02:00
|
|
|
}
|
2019-10-24 10:59:58 +02:00
|
|
|
|
2020-07-02 12:08:56 +02:00
|
|
|
if len(m.Spec.Inputs.Resources) > 0 {
|
|
|
|
for _, res := range m.Spec.Inputs.Resources {
|
|
|
|
if res.Type == "stash" {
|
|
|
|
contextFilters = append(contextFilters, "stashContent")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 10:59:58 +02:00
|
|
|
if len(m.Spec.Containers) > 0 {
|
2020-07-16 09:10:15 +02:00
|
|
|
parameterKeys := []string{"containerCommand", "containerShell", "dockerEnvVars", "dockerImage", "dockerName", "dockerOptions", "dockerPullImage", "dockerVolumeBind", "dockerWorkspace"}
|
2019-11-04 17:29:39 +02:00
|
|
|
for _, container := range m.Spec.Containers {
|
|
|
|
for _, condition := range container.Conditions {
|
|
|
|
for _, dependentParam := range condition.Params {
|
|
|
|
parameterKeys = append(parameterKeys, dependentParam.Value)
|
2020-01-24 15:30:27 +02:00
|
|
|
parameterKeys = append(parameterKeys, dependentParam.Name)
|
2019-11-04 17:29:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 10:50:35 +02:00
|
|
|
// ToDo: append dependentParam.Value & dependentParam.Name only according to correct parameter scope and not generally
|
2020-07-02 12:08:56 +02:00
|
|
|
contextFilters = append(contextFilters, parameterKeys...)
|
2019-10-24 10:59:58 +02:00
|
|
|
}
|
|
|
|
if len(m.Spec.Sidecars) > 0 {
|
|
|
|
//ToDo: support fallback for "dockerName" configuration property -> via aliasing?
|
2020-07-02 12:08:56 +02:00
|
|
|
contextFilters = append(contextFilters, []string{"containerName", "containerPortMappings", "dockerName", "sidecarEnvVars", "sidecarImage", "sidecarName", "sidecarOptions", "sidecarPullImage", "sidecarReadyCommand", "sidecarVolumeBind", "sidecarWorkspace"}...)
|
2020-01-24 15:30:27 +02:00
|
|
|
//ToDo: add condition param.Value and param.Name to filter as for Containers
|
2019-10-24 10:59:58 +02:00
|
|
|
}
|
2020-09-16 14:50:09 +02:00
|
|
|
|
|
|
|
if m.HasReference("vaultSecret") {
|
2020-10-13 14:14:47 +02:00
|
|
|
contextFilters = append(contextFilters, []string{"vaultAppRoleTokenCredentialsId", "vaultAppRoleSecretTokenCredentialsId"}...)
|
2020-09-16 14:50:09 +02:00
|
|
|
}
|
|
|
|
|
2020-07-02 12:08:56 +02:00
|
|
|
if len(contextFilters) > 0 {
|
|
|
|
filters.All = append(filters.All, contextFilters...)
|
|
|
|
filters.General = append(filters.General, contextFilters...)
|
|
|
|
filters.Steps = append(filters.Steps, contextFilters...)
|
|
|
|
filters.Stages = append(filters.Stages, contextFilters...)
|
|
|
|
filters.Parameters = append(filters.Parameters, contextFilters...)
|
|
|
|
filters.Env = append(filters.Env, contextFilters...)
|
|
|
|
|
2019-10-24 10:59:58 +02:00
|
|
|
}
|
2019-10-22 15:41:27 +02:00
|
|
|
return filters
|
|
|
|
}
|
2019-10-24 10:59:58 +02:00
|
|
|
|
2019-10-29 11:58:24 +02:00
|
|
|
// GetContextDefaults retrieves context defaults like container image, name, env vars, resources, ...
|
2019-10-24 10:59:58 +02:00
|
|
|
// It only supports scenarios with one container and optionally one sidecar
|
|
|
|
func (m *StepData) GetContextDefaults(stepName string) (io.ReadCloser, error) {
|
|
|
|
|
|
|
|
//ToDo error handling empty Containers/Sidecars
|
|
|
|
//ToDo handle empty Command
|
2019-11-04 17:29:39 +02:00
|
|
|
root := map[string]interface{}{}
|
2019-10-24 10:59:58 +02:00
|
|
|
if len(m.Spec.Containers) > 0 {
|
2019-11-04 17:29:39 +02:00
|
|
|
for _, container := range m.Spec.Containers {
|
|
|
|
key := ""
|
2020-05-22 09:48:11 +02:00
|
|
|
conditionParam := ""
|
2019-11-04 17:29:39 +02:00
|
|
|
if len(container.Conditions) > 0 {
|
|
|
|
key = container.Conditions[0].Params[0].Value
|
2020-05-22 09:48:11 +02:00
|
|
|
conditionParam = container.Conditions[0].Params[0].Name
|
2019-11-04 17:29:39 +02:00
|
|
|
}
|
|
|
|
p := map[string]interface{}{}
|
|
|
|
if key != "" {
|
|
|
|
root[key] = p
|
2020-05-22 09:48:11 +02:00
|
|
|
//add default for condition parameter if available
|
|
|
|
for _, inputParam := range m.Spec.Inputs.Parameters {
|
|
|
|
if inputParam.Name == conditionParam {
|
|
|
|
root[conditionParam] = inputParam.Default
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 17:29:39 +02:00
|
|
|
} else {
|
|
|
|
p = root
|
|
|
|
}
|
|
|
|
if len(container.Command) > 0 {
|
|
|
|
p["containerCommand"] = container.Command[0]
|
|
|
|
}
|
|
|
|
p["containerName"] = container.Name
|
|
|
|
p["containerShell"] = container.Shell
|
2020-07-16 09:10:15 +02:00
|
|
|
p["dockerEnvVars"] = EnvVarsAsMap(container.EnvVars)
|
2019-11-04 17:29:39 +02:00
|
|
|
p["dockerImage"] = container.Image
|
|
|
|
p["dockerName"] = container.Name
|
|
|
|
p["dockerPullImage"] = container.ImagePullPolicy != "Never"
|
|
|
|
p["dockerWorkspace"] = container.WorkingDir
|
2020-07-16 09:10:15 +02:00
|
|
|
p["dockerOptions"] = OptionsAsStringSlice(container.Options)
|
2019-11-19 12:52:34 +02:00
|
|
|
//p["dockerVolumeBind"] = volumeMountsAsStringSlice(container.VolumeMounts)
|
2019-11-04 17:29:39 +02:00
|
|
|
|
|
|
|
// Ready command not relevant for main runtime container so far
|
|
|
|
//p[] = container.ReadyCommand
|
2019-10-24 10:59:58 +02:00
|
|
|
}
|
2019-11-04 17:29:39 +02:00
|
|
|
|
2019-10-24 10:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.Spec.Sidecars) > 0 {
|
|
|
|
if len(m.Spec.Sidecars[0].Command) > 0 {
|
2019-11-04 17:29:39 +02:00
|
|
|
root["sidecarCommand"] = m.Spec.Sidecars[0].Command[0]
|
2019-10-24 10:59:58 +02:00
|
|
|
}
|
2020-07-16 09:10:15 +02:00
|
|
|
root["sidecarEnvVars"] = EnvVarsAsMap(m.Spec.Sidecars[0].EnvVars)
|
2019-11-04 17:29:39 +02:00
|
|
|
root["sidecarImage"] = m.Spec.Sidecars[0].Image
|
|
|
|
root["sidecarName"] = m.Spec.Sidecars[0].Name
|
|
|
|
root["sidecarPullImage"] = m.Spec.Sidecars[0].ImagePullPolicy != "Never"
|
|
|
|
root["sidecarReadyCommand"] = m.Spec.Sidecars[0].ReadyCommand
|
|
|
|
root["sidecarWorkspace"] = m.Spec.Sidecars[0].WorkingDir
|
2020-07-16 09:10:15 +02:00
|
|
|
root["sidecarOptions"] = OptionsAsStringSlice(m.Spec.Sidecars[0].Options)
|
2019-11-19 12:52:34 +02:00
|
|
|
//root["sidecarVolumeBind"] = volumeMountsAsStringSlice(m.Spec.Sidecars[0].VolumeMounts)
|
2019-10-24 10:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// not filled for now since this is not relevant in Kubernetes case
|
2019-11-04 17:29:39 +02:00
|
|
|
//root["containerPortMappings"] = m.Spec.Sidecars[0].
|
2019-10-24 10:59:58 +02:00
|
|
|
|
2019-10-29 11:58:24 +02:00
|
|
|
if len(m.Spec.Inputs.Resources) > 0 {
|
2019-11-04 17:29:39 +02:00
|
|
|
keys := []string{}
|
|
|
|
resources := map[string][]string{}
|
2019-10-29 11:58:24 +02:00
|
|
|
for _, resource := range m.Spec.Inputs.Resources {
|
|
|
|
if resource.Type == "stash" {
|
2019-11-04 17:29:39 +02:00
|
|
|
key := ""
|
|
|
|
if len(resource.Conditions) > 0 {
|
|
|
|
key = resource.Conditions[0].Params[0].Value
|
|
|
|
}
|
|
|
|
if resources[key] == nil {
|
|
|
|
keys = append(keys, key)
|
|
|
|
resources[key] = []string{}
|
|
|
|
}
|
|
|
|
resources[key] = append(resources[key], resource.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
if key == "" {
|
|
|
|
root["stashContent"] = resources[""]
|
|
|
|
} else {
|
|
|
|
if root[key] == nil {
|
|
|
|
root[key] = map[string]interface{}{
|
|
|
|
"stashContent": resources[key],
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p := root[key].(map[string]interface{})
|
|
|
|
p["stashContent"] = resources[key]
|
|
|
|
}
|
2019-10-29 11:58:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 10:59:58 +02:00
|
|
|
c := Config{
|
|
|
|
Steps: map[string]map[string]interface{}{
|
2019-11-04 17:29:39 +02:00
|
|
|
stepName: root,
|
2019-10-24 10:59:58 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON, err := yaml.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create context defaults")
|
|
|
|
}
|
|
|
|
|
|
|
|
r := ioutil.NopCloser(bytes.NewReader(JSON))
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:16:25 +02:00
|
|
|
// GetResourceParameters retrieves parameters from a named pipeline resource with a defined path
|
|
|
|
func (m *StepData) GetResourceParameters(path, name string) map[string]interface{} {
|
|
|
|
resourceParams := map[string]interface{}{}
|
|
|
|
|
|
|
|
for _, param := range m.Spec.Inputs.Parameters {
|
|
|
|
for _, res := range param.ResourceRef {
|
|
|
|
if res.Name == name {
|
2020-10-05 15:33:28 +02:00
|
|
|
resourceParams[param.Name] = getParameterValue(path, res, param)
|
2020-01-15 13:16:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceParams
|
|
|
|
}
|
|
|
|
|
2020-10-05 15:33:28 +02:00
|
|
|
func getParameterValue(path string, res ResourceReference, param StepParameters) interface{} {
|
|
|
|
if val := piperenv.GetResourceParameter(path, res.Name, res.Param); len(val) > 0 {
|
2020-07-15 10:09:42 +02:00
|
|
|
if param.Type != "string" {
|
|
|
|
var unmarshalledValue interface{}
|
|
|
|
err := json.Unmarshal([]byte(val), &unmarshalledValue)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().Debugf("Failed to unmarshal: %v", val)
|
|
|
|
}
|
2020-07-15 15:45:36 +02:00
|
|
|
return unmarshalledValue
|
2020-07-15 10:09:42 +02:00
|
|
|
}
|
2020-07-15 15:45:36 +02:00
|
|
|
return val
|
2020-07-15 10:09:42 +02:00
|
|
|
}
|
2020-07-15 15:45:36 +02:00
|
|
|
return nil
|
2020-07-15 10:09:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 11:15:48 +02:00
|
|
|
// GetReference returns the ResourceReference of the given type
|
|
|
|
func (m *StepParameters) GetReference(refType string) *ResourceReference {
|
|
|
|
for _, ref := range m.ResourceRef {
|
|
|
|
if refType == ref.Type {
|
|
|
|
return &ref
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-16 14:50:09 +02:00
|
|
|
// HasReference checks whether StepData contains a parameter that has Reference with the given type
|
|
|
|
func (m *StepData) HasReference(refType string) bool {
|
|
|
|
for _, param := range m.Spec.Inputs.Parameters {
|
|
|
|
if param.GetReference(refType) != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-16 09:10:15 +02:00
|
|
|
// EnvVarsAsMap converts container EnvVars into a map as required by dockerExecute
|
|
|
|
func EnvVarsAsMap(envVars []EnvVar) map[string]string {
|
2020-07-02 12:08:56 +02:00
|
|
|
e := map[string]string{}
|
2019-10-24 10:59:58 +02:00
|
|
|
for _, v := range envVars {
|
2020-07-02 12:08:56 +02:00
|
|
|
e[v.Name] = v.Value
|
2019-10-24 10:59:58 +02:00
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
2019-11-15 11:25:23 +02:00
|
|
|
|
2020-07-16 09:10:15 +02:00
|
|
|
// OptionsAsStringSlice converts container options into a string slice as required by dockerExecute
|
|
|
|
func OptionsAsStringSlice(options []Option) []string {
|
2019-11-15 11:25:23 +02:00
|
|
|
e := []string{}
|
2019-11-19 12:52:34 +02:00
|
|
|
for _, v := range options {
|
|
|
|
e = append(e, fmt.Sprintf("%v %v", v.Name, v.Value))
|
2019-11-15 11:25:23 +02:00
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
2019-11-19 12:52:34 +02:00
|
|
|
|
|
|
|
//ToDo: Enable this when the Volumes part is also implemented
|
|
|
|
//func volumeMountsAsStringSlice(volumeMounts []VolumeMount) []string {
|
|
|
|
// e := []string{}
|
|
|
|
// for _, v := range volumeMounts {
|
|
|
|
// e = append(e, fmt.Sprintf("%v:%v", v.Name, v.MountPath))
|
|
|
|
// }
|
|
|
|
// return e
|
|
|
|
//}
|