Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the params in step replace other fields in step that are not in stepaction #7755

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/apis/pipeline/v1alpha1/stepaction_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ type StepActionSpec struct {
VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty" patchStrategy:"merge" patchMergeKey:"mountPath" protobuf:"bytes,9,rep,name=volumeMounts"`
}

// ToStep converts the StepActionSpec to a Step struct
func (ss *StepActionSpec) ToStep() *v1.Step {
return &v1.Step{
Image: ss.Image,
Command: ss.Command,
Args: ss.Args,
WorkingDir: ss.WorkingDir,
Script: ss.Script,
Env: ss.Env,
VolumeMounts: ss.VolumeMounts,
SecurityContext: ss.SecurityContext,
Results: ss.Results,
}
}

// StepActionObject is implemented by StepAction
type StepActionObject interface {
apis.Defaultable
Expand Down
38 changes: 20 additions & 18 deletions pkg/reconciler/taskrun/resources/taskspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,33 @@ func GetStepActionsData(ctx context.Context, taskSpec v1.TaskSpec, taskRun *v1.T
stepActionSpec := stepAction.StepActionSpec()
stepActionSpec.SetDefaults(ctx)

s.Image = stepActionSpec.Image
s.SecurityContext = stepActionSpec.SecurityContext
if len(stepActionSpec.Command) > 0 {
s.Command = stepActionSpec.Command
stepFromStepAction := stepActionSpec.ToStep()
if err := validateStepHasStepActionParameters(s.Params, stepActionSpec.Params); err != nil {
return nil, err
}
if len(stepActionSpec.Args) > 0 {
s.Args = stepActionSpec.Args
stepFromStepAction = applyStepActionParameters(stepFromStepAction, &taskSpec, taskRun, s.Params, stepActionSpec.Params)

s.Image = stepFromStepAction.Image
s.SecurityContext = stepFromStepAction.SecurityContext
if len(stepFromStepAction.Command) > 0 {
s.Command = stepFromStepAction.Command
}
if stepActionSpec.Script != "" {
s.Script = stepActionSpec.Script
if len(stepFromStepAction.Args) > 0 {
s.Args = stepFromStepAction.Args
}
s.WorkingDir = stepActionSpec.WorkingDir
if stepActionSpec.Env != nil {
s.Env = stepActionSpec.Env
if stepFromStepAction.Script != "" {
s.Script = stepFromStepAction.Script
}
if len(stepActionSpec.VolumeMounts) > 0 {
s.VolumeMounts = stepActionSpec.VolumeMounts
s.WorkingDir = stepFromStepAction.WorkingDir
if stepFromStepAction.Env != nil {
s.Env = stepFromStepAction.Env
}
if len(stepActionSpec.Results) > 0 {
s.Results = stepActionSpec.Results
if len(stepFromStepAction.VolumeMounts) > 0 {
s.VolumeMounts = stepFromStepAction.VolumeMounts
}
if err := validateStepHasStepActionParameters(s.Params, stepActionSpec.Params); err != nil {
return nil, err
if len(stepFromStepAction.Results) > 0 {
s.Results = stepFromStepAction.Results
}
s = applyStepActionParameters(s, &taskSpec, taskRun, s.Params, stepActionSpec.Params)
s.Params = nil
s.Ref = nil
steps = append(steps, *s)
Expand Down
55 changes: 55 additions & 0 deletions pkg/reconciler/taskrun/resources/taskspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,61 @@ func TestGetStepActionsData(t *testing.T) {
Image: "myimage",
Args: []string{"taskrun string param", "taskspec", "array", "taskspec", "array", "param", "taskrun key", "taskspec key2", "step action key3"},
}},
}, {
name: "params in step propagated to stepaction only",
tr: &v1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "mytaskrun",
Namespace: "default",
},
Spec: v1.TaskRunSpec{
Params: v1.Params{{
Name: "stringparam",
Value: v1.ParamValue{
Type: v1.ParamTypeString,
StringVal: "taskrun string param",
},
}},
TaskSpec: &v1.TaskSpec{
Steps: []v1.Step{{
Ref: &v1.Ref{
Name: "stepAction",
},
Params: v1.Params{{
Name: "stringparam",
Value: v1.ParamValue{
Type: v1.ParamTypeString,
StringVal: "step string param",
},
}},
OnError: v1.OnErrorType("$(params.stringparam)"),
StdoutConfig: &v1.StepOutputConfig{Path: "$(params.stringparam)"},
StderrConfig: &v1.StepOutputConfig{Path: "$(params.stringparam)"},
}},
},
},
},
stepAction: &v1alpha1.StepAction{
ObjectMeta: metav1.ObjectMeta{
Name: "stepAction",
Namespace: "default",
},
Spec: v1alpha1.StepActionSpec{
Image: "myimage",
Args: []string{"echo", "$(params.stringparam)"},
Params: v1.ParamSpecs{{
Name: "stringparam",
Type: v1.ParamTypeString,
}},
},
},
want: []v1.Step{{
Image: "myimage",
Args: []string{"echo", "step string param"},
OnError: v1.OnErrorType("$(params.stringparam)"),
StdoutConfig: &v1.StepOutputConfig{Path: "$(params.stringparam)"},
StderrConfig: &v1.StepOutputConfig{Path: "$(params.stringparam)"},
}},
}, {
name: "propagating step result substitution strings into step actions",
tr: &v1.TaskRun{
Expand Down
41 changes: 41 additions & 0 deletions pkg/reconciler/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3693,6 +3693,47 @@ spec:
Args: []string{"taskrun string param", "taskrun", "array", "taskrun", "array", "param", "taskrun object param"},
Name: "step1",
}},
}, {
name: "params with the same name propagated from taskrun and step",
taskRun: parse.MustParseV1TaskRun(t, `
metadata:
name: taskrun-with-string-params
namespace: foo
spec:
params:
- name: stringparam
value: "taskrun string param"
taskSpec:
steps:
- ref:
name: stepAction
name: step1
stdoutConfig:
path: $(params.stringparam)
params:
- name: stringparam
value: "step string param"
`),
stepAction: parse.MustParseV1alpha1StepAction(t, `
metadata:
name: stepAction
namespace: foo
spec:
params:
- name: stringparam
image: myImage
command: ["echo"]
args: ["$(params.stringparam)"]
`),
want: []v1.Step{{
Image: "myImage",
Command: []string{"echo"},
Args: []string{"step string param"},
Name: "step1",
StdoutConfig: &v1.StepOutputConfig{
Path: "taskrun string param",
},
}},
}, {
name: "step results",
taskRun: parse.MustParseV1TaskRun(t, `
Expand Down