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 Validation Error Merging StepTemplates with StepRef #7982

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
21 changes: 21 additions & 0 deletions examples/v1/taskruns/beta/stepactions-steptemplate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: tekton.dev/v1beta1
kind: StepAction
metadata:
name: step-action
spec:
image: alpine
command: ["env"]
---
apiVersion: tekton.dev/v1
kind: TaskRun
metadata:
name: task-run
spec:
taskSpec:
steps:
- ref:
name: step-action
stepTemplate:
env:
- name: foo
value: bar
5 changes: 5 additions & 0 deletions pkg/apis/pipeline/v1/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func MergeStepsWithStepTemplate(template *StepTemplate, steps []Step) ([]Step, e
}

for i, s := range steps {
// If the stepaction has not been fetched yet then do not merge.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I'm following here - why does if s.Ref is empty determine if the stepaction has been fetched?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because users can either provide a ref to StepActions (in which case it is not nil) or inline the actionable fields e.g. image, command etc. (in which case ref is nil). There is already a validation check that users cannot provide both ref and image (for example).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And at webhook validation time, we have not yet done the remote resolution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At runtime, after we fetch the StepActions, we inline the content and set ref to nil explicitly. So when we validate at runtime, we can actually do this after the StepAction has been fetched. https://github.com/tektoncd/pipeline/blob/main/pkg/reconciler/taskrun/resources/taskspec.go#L145

// Skip over to the next one
if s.Ref != nil {
continue
}
merged := corev1.Container{}
err := mergeObjWithTemplateBytes(md, s.ToK8sContainer(), &merged)
if err != nil {
Expand Down
27 changes: 22 additions & 5 deletions pkg/apis/pipeline/v1/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ func TestMergeStepsWithStepTemplate(t *testing.T) {
}},
}},
expected: []v1.Step{{
Command: []string{"/somecmd"}, Image: "some-image",
Command: []string{"/somecmd"},
Image: "some-image",
OnError: "foo",
Results: []v1.StepResult{{
Name: "result",
Expand All @@ -155,11 +156,30 @@ func TestMergeStepsWithStepTemplate(t *testing.T) {
}},
}},
}, {
name: "ref-should-not-be-removed",
name: "step-ref-should-not-be-merged-with-steptemplate",
template: &v1.StepTemplate{
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: pointer.Bool(true),
},
VolumeMounts: []corev1.VolumeMount{{
Name: "data",
MountPath: "/workspace/data",
}},
Env: []corev1.EnvVar{{
Name: "KEEP_THIS",
Value: "A_VALUE",
}, {
Name: "SOME_KEY_1",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: "A_KEY",
LocalObjectReference: corev1.LocalObjectReference{Name: "A_NAME"},
},
},
}, {
Name: "SOME_KEY_2",
Value: "VALUE_2",
}},
},
steps: []v1.Step{{
Ref: &v1.Ref{Name: "my-step-action"},
Expand All @@ -172,9 +192,6 @@ func TestMergeStepsWithStepTemplate(t *testing.T) {
}},
}},
expected: []v1.Step{{
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: pointer.Bool(true),
},
Ref: &v1.Ref{Name: "my-step-action"},
OnError: "foo",
Results: []v1.StepResult{{
Expand Down
36 changes: 36 additions & 0 deletions pkg/apis/pipeline/v1/task_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/pointer"
"knative.dev/pkg/apis"
)

Expand Down Expand Up @@ -333,6 +334,41 @@ func TestTaskSpecValidate(t *testing.T) {
hello world`,
}},
},
}, {
name: "step template included in validation with stepaction",
fields: fields{
Steps: []v1.Step{{
Name: "astep",
Ref: &v1.Ref{
Name: "stepAction",
},
}},
StepTemplate: &v1.StepTemplate{
Image: "some-image",
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: pointer.Bool(true),
},
VolumeMounts: []corev1.VolumeMount{{
Name: "data",
MountPath: "/workspace/data",
}},
Env: []corev1.EnvVar{{
Name: "KEEP_THIS",
Value: "A_VALUE",
}, {
Name: "SOME_KEY_1",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: "A_KEY",
LocalObjectReference: corev1.LocalObjectReference{Name: "A_NAME"},
},
},
}, {
Name: "SOME_KEY_2",
Value: "VALUE_2",
}},
},
},
}, {
name: "valid step with parameterized script",
fields: fields{
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/pipeline/v1beta1/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func MergeStepsWithStepTemplate(template *StepTemplate, steps []Step) ([]Step, e
}

for i, s := range steps {
// If the stepaction has not been fetched yet then do not merge.
// Skip over to the next one
if s.Ref != nil {
continue
}
merged := corev1.Container{}
err := mergeObjWithTemplateBytes(md, s.ToK8sContainer(), &merged)
if err != nil {
Expand Down
48 changes: 48 additions & 0 deletions pkg/apis/pipeline/v1beta1/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
v1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/test/diff"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/pointer"
)

func TestMergeStepsWithStepTemplate(t *testing.T) {
Expand Down Expand Up @@ -127,6 +129,52 @@ func TestMergeStepsWithStepTemplate(t *testing.T) {
MountPath: "/workspace/data",
}},
}},
}, {
name: "step-ref-should-not-be-merged-with-steptemplate",
template: &v1beta1.StepTemplate{
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: pointer.Bool(true),
},
VolumeMounts: []corev1.VolumeMount{{
Name: "data",
MountPath: "/workspace/data",
}},
Env: []corev1.EnvVar{{
Name: "KEEP_THIS",
Value: "A_VALUE",
}, {
Name: "SOME_KEY_1",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: "A_KEY",
LocalObjectReference: corev1.LocalObjectReference{Name: "A_NAME"},
},
},
}, {
Name: "SOME_KEY_2",
Value: "VALUE_2",
}},
},
steps: []v1beta1.Step{{
Ref: &v1beta1.Ref{Name: "my-step-action"},
OnError: "foo",
Results: []v1.StepResult{{
Name: "result",
}},
Params: v1beta1.Params{{
Name: "param",
}},
}},
expected: []v1beta1.Step{{
Ref: &v1beta1.Ref{Name: "my-step-action"},
OnError: "foo",
Results: []v1.StepResult{{
Name: "result",
}},
Params: v1beta1.Params{{
Name: "param",
}},
}},
}, {
name: "merge-env-by-step",
template: &v1beta1.StepTemplate{
Expand Down
36 changes: 36 additions & 0 deletions pkg/apis/pipeline/v1beta1/task_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/pointer"
"knative.dev/pkg/apis"
)

Expand Down Expand Up @@ -337,6 +338,41 @@ func TestTaskSpecValidate(t *testing.T) {
hello world`,
}},
},
}, {
name: "step template included in validation with stepaction",
fields: fields{
Steps: []v1beta1.Step{{
Name: "astep",
Ref: &v1beta1.Ref{
Name: "stepAction",
},
}},
StepTemplate: &v1beta1.StepTemplate{
Image: "some-image",
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: pointer.Bool(true),
},
VolumeMounts: []corev1.VolumeMount{{
Name: "data",
MountPath: "/workspace/data",
}},
Env: []corev1.EnvVar{{
Name: "KEEP_THIS",
Value: "A_VALUE",
}, {
Name: "SOME_KEY_1",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: "A_KEY",
LocalObjectReference: corev1.LocalObjectReference{Name: "A_NAME"},
},
},
}, {
Name: "SOME_KEY_2",
Value: "VALUE_2",
}},
},
},
}, {
name: "valid step with parameterized script",
fields: fields{
Expand Down