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

support referencing array params in when expression values #4075

Merged
merged 1 commit into from
Aug 5, 2021

Conversation

pritidesai
Copy link
Member

Changes

Array params are referenced using $(params.arrayParam[*]). When expressions values is a list of string and was designed to reference and resolve an array parameter. But the implementation was limiting the array param usage in the when expression values. Adding support for the array params in when expressions.

/kind bug

Closes #4061

Submitter Checklist

As the author of this PR, please check off the items in this checklist:

  • Docs included if any changes are user facing
  • Tests included if any functionality added or changed
  • Follows the commit message standard
  • Meets the Tekton contributor standards (including
    functionality, content, code)
  • Release notes block below has been filled in or deleted (only if no user facing changes)

Release Notes

When expression values can have array parameter reference now, such as, values: [$(params.arrayParam[*])]

@tekton-robot tekton-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. labels Jul 6, 2021
@tekton-robot tekton-robot requested review from dlorenc and a user July 6, 2021 21:38
@tekton-robot tekton-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 6, 2021
Copy link
Member

@jerop jerop left a comment

Choose a reason for hiding this comment

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

thanks for this fix @pritidesai 😁

could we please add a task with array params to the example pipeline with when expressions? also would be great to have an test case invalidating specifying array parameters in the input field

@@ -51,6 +51,13 @@ func TestWhenExpressions_Valid(t *testing.T) {
Operator: selection.In,
Values: []string{""},
}},
}, {
name: "valid input with string variable and values with array variables",
Copy link
Member

Choose a reason for hiding this comment

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

we could also add the array in input test case here

Copy link
Member Author

@pritidesai pritidesai Jul 7, 2021

Choose a reason for hiding this comment

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

the set of tests here is running positive tests. We could add it as one of the failure tests but nothing changed in terms of validating the input.

These tests are validating WhenExpressions.validate() defined here. The purpose of WhenExpressions.validate() is to validate each field for non-empty values and validating task result pattern.

This test is also not necessary here and can be removed.

@pritidesai pritidesai force-pushed the issue-4061 branch 2 times, most recently from a72b11b to 232ec0e Compare July 7, 2021 23:17
@pritidesai
Copy link
Member Author

looks like a flake 😞
Screen Shot 2021-07-07 at 5 00 42 PM

/test tekton-pipeline-unit-tests

Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

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

If we support it for when expression, should we also support this on variable interpolation ? (otherwise might feel a bit inconsistent ).

@pritidesai
Copy link
Member Author

pritidesai commented Jul 8, 2021

If we support it for when expression, should we also support this on variable interpolation ? (otherwise might feel a bit inconsistent ).

I think we do support this on variable interpolations using the same syntax $(params.arrayParam[*]), we are missing support for the array indexing i.e. $(params.arrayParam[2]) in general.

@vdemeester
Copy link
Member

If we support it for when expression, should we also support this on variable interpolation ? (otherwise might feel a bit inconsistent ).

I think we do support this on variable interpolations using the same syntax $(params.arrayParam[*]), we are missing support for the array indexing i.e. $(params.arrayParam[2]) in general.

Ah I see 👌🏼

@pritidesai pritidesai added this to the Pipelines v0.27 milestone Jul 13, 2021
@jerop jerop self-assigned this Jul 13, 2021
// one of the values could be a reference to an array param, such as, $(params.foo[*])
// extract the variable name from the pattern $(params.foo[*]), if the variable name matches with one of the array params
// validate the param as an array variable otherwise, validate it as a string variable
if arrayParamNames.Has(strings.TrimSuffix(strings.TrimPrefix(val, "$("+prefix+"."), "[*])")) {
Copy link
Member

Choose a reason for hiding this comment

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

we're checking if the variable is an array param in two places is slightly different ways, wondering if we can create a common function to check for array param that can be used in both places

if _, ok := arrayReplacements[strings.TrimSuffix(strings.TrimPrefix(val, "$("), "[*])")]; ok {

if arrayParamNames.Has(strings.TrimSuffix(strings.TrimPrefix(val, "$("+prefix+"."), "[*])")) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, it's very similar but the first expression is returning params.arrayParam1 from $(params.arrayParam1[*]) while the second expression is returning arrayParam1 from $(params.arrayParam1[*]).

The arrayReplacements in the first expression hold a list of array parameters with params prefix while the arrayParamNames hold the list of parameters without such prefix.

I have created a common function to return just the parameter name which works for the second expression and added prefix back for the first expression.

Validation and applying replacements works with a different patterns because the apply.go adds the prefix before creating a list of parameter names:

arrayReplacements[fmt.Sprintf("params.%s", p.Name)] = p.Default.ArrayVal

We can refactor this further (in a separate PR) to have a consistent validation and replacements routines.

Copy link
Member

Choose a reason for hiding this comment

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

thanks for the refactor, looks great!

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/param_types.go 96.9% 97.0% 0.1

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/param_types.go 96.9% 97.0% 0.1

@pritidesai
Copy link
Member Author

/retest

@jerop
Copy link
Member

jerop commented Jul 19, 2021

/test pull-tekton-pipeline-integration-tests

Copy link
Member

@jerop jerop left a comment

Choose a reason for hiding this comment

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

thanks @pritidesai!

@tekton-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jerop

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 20, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Requesting one additional unit test case but otherwise this lgtm!

original: &WhenExpression{
Input: "$(params.path)",
Operator: selection.In,
Values: []string{"$(params.branches[*])", "$(params.files[*])"},
Copy link

Choose a reason for hiding this comment

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

One additional test-case here for completeness would be Values with string and array vars mixed together:

original: &WhenExpression{
  Input: "$(params.path)",
  Operator: selection.In,
  Values: []string{"$(params.branches[*])", "$(params.matchPath)", "$(params.files[*])"},
},
replacements: map[string]string{
  "params.path": "readme.md",
  "params.matchPath": "foo.txt",
},
arrayReplacements: map[string][]string{
  "params.branches": {"dev", "stage"},
  "params.files": {"readme.md", "test.go"},
},
expected: &WhenExpression{
  Input: "readme.md",
  Operator: selection.In,
  Values: []string{"dev", "stage", "foo.txt", "readme.md", "test.go"},
},

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks @sbwsg for an additional unit test, done 👍

Array params are referenced using $(params.arrayParam[*]), when
expressions values is a list of string and was designed to
resolve an array parameter. But the implementation was limiting the
array param usage in the values. Adding support for the array
params in when expressions.
@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/param_types.go 96.9% 97.0% 0.1

@ghost
Copy link

ghost commented Aug 5, 2021

/lgtm

@tekton-robot tekton-robot assigned ghost Aug 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot pass array to spec.tasks[0].when[0].values
4 participants