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 enum validation with multiple param references #7481

Merged
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
29 changes: 27 additions & 2 deletions docs/pipelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,10 @@ spec:
If the `Param` value passed in by `PipelineRun` is **NOT** in the predefined `enum` list, the `PipelineRun` will fail with reason `InvalidParamValue`.

If a `PipelineTask` references a `Task` with `enum`, the `enums` specified in the Pipeline `spec.params` (pipeline-level `enum`) must be
a **subset** of the `enums` specified in the referenced `Task` (task-level `enum`). Note that an empty pipeline-level `enum` is invalid
in this scenario since an empty `enum` set indicates a "universal set" which allows all possible values. In the below example, the referenced `Task` accepts `v1` and `v2` as valid values, the `Pipeline` further restricts the valid values to `v1`.
a **subset** of the `enums` specified in the referenced `Task` (task-level `enum`). An empty pipeline-level `enum` is invalid
in this scenario since an empty `enum` set indicates a "universal set" which allows all possible values. The same rules apply to `Pipelines` with embbeded `Tasks`.

In the below example, the referenced `Task` accepts `v1` and `v2` as valid values, the `Pipeline` further restricts the valid value to `v1`.

``` yaml
apiVersion: tekton.dev/v1
Expand Down Expand Up @@ -339,6 +341,29 @@ spec:
name: param-enum-demo
```

Note that this subset restriction only applies to the task-level `params` with a **direct single** reference to pipeline-level `params`. If a task-level `param` references multiple pipeline-level `params`, the subset validation is not applied.

``` yaml
apiVersion: tekton.dev/v1
kind: Pipeline
...
spec:
params:
- name: message1
enum: ["v1"]
- name: message2
enum: ["v2"]
tasks:
- name: task1
params:
- name: message
value: "$(params.message1) and $(params.message2)"
taskSpec:
params: message
enum: [...] # the message enum is not required to be a subset of message1 or message2
Comment on lines +351 to +363
Copy link
Member

Choose a reason for hiding this comment

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

I'm curious, if for the task, we have an enum that requires a url to beabc/foo, and the pipeline passes a $(params.url-part1)/$(params.url-part2) param to the task's param.
The PR will skip the validation right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really. There are 2 kinds of check we do

  • Validate user input against enums. At pipeline level, we will validate message1 and message2 individually; at task-level, we validate the value of message is in it own enum (the compound value). This PR does not change this behavior.
  • Subset validation: prior to the change, we require that the pipeline-level param enum must be a subset of the task-level enum referencing it. If a task-level param referenes a single pipeline-level param, it is easy and we still validate it. However, if it references multiple pipeline-level params, like this one, we will skip the subset validation.

Copy link
Member

Choose a reason for hiding this comment

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

Oh this is the subset validaition, I thought it is the first one

...
```

Tekton validates user-provided values in a `PipelineRun` against the `enum` specified in the `PipelineSpec.params`. Tekton also validates
any resolved `param` value against the `enum` specified in each `PipelineTask` before creating the `TaskRun`.

Expand Down
9 changes: 4 additions & 5 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,14 +818,13 @@ func ValidateParamEnumSubset(pipelineTaskParams []v1.Param, pipelineParamSpecs [
for _, p := range pipelineTaskParams {
// calculate referenced param enums
res, present, errString := substitution.ExtractVariablesFromString(p.Value.StringVal, "params")
if !present {
continue
}
if errString != "" {
return fmt.Errorf("unexpected error in ExtractVariablesFromString: %s", errString)
}
if len(res) != 1 {
return fmt.Errorf("unexpected resolved param in ExtractVariablesFromString, expect 1 but got %v", len(res))

// if multiple params are extracted, that means the task-level param is a compounded value, skip subset validation
if !present || len(res) > 1 {
continue
}

// resolve pipeline-level and pipelineTask-level enums
Expand Down
32 changes: 32 additions & 0 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5153,6 +5153,38 @@ func TestValidateParamEnumSubset_Valid(t *testing.T) {
},
},
},
}, {
name: "compound task param with enum - pass",
params: []v1.Param{
{
Name: "resolved-task-p1",
Value: v1.ParamValue{
StringVal: "$(params.p1) and $(params.p2)",
},
},
},
pipelinePs: []v1.ParamSpec{
{
Name: "p1",
Type: v1.ParamTypeString,
Enum: []string{"v1", "v2"},
},
{
Name: "p2",
Type: v1.ParamTypeString,
Enum: []string{"v3", "v4"},
},
},
rt: &resources.ResolvedTask{
TaskSpec: &v1.TaskSpec{
Params: v1.ParamSpecs{
{
Name: "resolved-task-p1",
Enum: []string{"e1", "e2"},
},
},
},
},
},
}

Expand Down
Loading