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

Modify spec params to avoid confusion between resourcetemplates and triggertemplate params #589

Merged
merged 1 commit into from
Jun 24, 2020

Conversation

savitaashture
Copy link
Contributor

@savitaashture savitaashture commented May 29, 2020

Changes

PR helps to avoid confusion from TriggerTemplate params to resourceTemplate params
by keeping params of spec as it is and using $(tt.params.) for the substitution.

So, this way we no need to handle the backward compatibility as per the discussion over the comments

Fixes #508

Tried to do tag configurable, given the way ObjectDefaulter and SetDefaults work, neither could accomplish the switch properly. ObjectDefaulter could have done it, but the way the tests are configured do not cause the Defaults to be executed since the objects aren't actually decoded.

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

See the contribution guide for more details.

Release Notes

The substitution for params modified from $(params.gitrevision) to $(tt.params.gitrevision) .
And the the examples

apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerTemplate
metadata:
  name: github-template
spec:
  params:
    - name: gitrevision
    - name: gitrepositoryurl
  resourcetemplates:
    - apiVersion: tekton.dev/v1alpha1
      kind: TaskRun
      metadata:
        generateName: github-run-
      spec:
        taskSpec:
          inputs:
            resources:
              - name: source
                type: git
          steps:
            - image: ubuntu
              script: |
                #! /bin/bash
                ls -al $(inputs.resources.source.path)
        inputs:
          resources:
            - name: source
              resourceSpec:
                type: git
                params:
                  - name: revision
                    value: $(tt.params.gitrevision)
                  - name: url
                    value: $(tt.params.gitrepositoryurl)

For a release or two $(params) supported and later it will be deprecated.
Created issue to track the same

cc @dibyom

@tekton-robot tekton-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label May 29, 2020
@tekton-robot
Copy link

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

File Old Coverage New Coverage Delta
pkg/apis/triggers/v1alpha1/trigger_template_validation.go 97.2% 97.5% 0.3

@dibyom
Copy link
Member

dibyom commented Jun 1, 2020

Thanks for opening this @savitaashture I won't get to this today but will put in on my list for tomorrow!

@dibyom
Copy link
Member

dibyom commented Jun 3, 2020

params are now deprecated and will be removed in future but for now TriggerTemplate spec can support param as shown below

Might be worth rewording as - The params field in TriggerTemplates has been deperecated in favor of the ttparams field. And the the examples

Copy link
Member

@dibyom dibyom left a comment

Choose a reason for hiding this comment

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

Thanks @savitaashture .Code looks fine to me. I'll try this out on my env. Given that this is a pretty far reaching API change, let's discuss it once in next week's WG before merging?

func (tt *TriggerTemplate) SetDefaults(ctx context.Context) {}
func (tt *TriggerTemplate) SetDefaults(ctx context.Context) {
// This is a temporary logic to have a backward compatibility with the original params.
// TODO(savitaashture): remove below logic once after the complete deprecation of Spec.DeprecatedParams
Copy link
Member

Choose a reason for hiding this comment

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

we should open an issue to remove this and link to that issue here instead!

Copy link
Contributor Author

@savitaashture savitaashture Jun 8, 2020

Choose a reason for hiding this comment

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

Sure i will open and update in the code comments

Yes +1 to discuss before merging

Copy link
Contributor Author

@savitaashture savitaashture Jun 12, 2020

Choose a reason for hiding this comment

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

Created issue to track the removal of deprecated tag params

if _, ok := declaredParamNames[templateParamName]; !ok {
// This logic is to get the tag and display in error dynamically for both ttparams and params.
// TODO(savitaashture): remove params once after the complete deprecation of spec.DeprecatedParams
var tag string
Copy link
Member

Choose a reason for hiding this comment

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

Question: Until we start using only $(ttparams) for replacement, we cannot support #525 right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes because till that time we will have validation for params

@dibyom
Copy link
Member

dibyom commented Jun 3, 2020

/approve
/hold

@tekton-robot tekton-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 3, 2020
@tekton-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dibyom

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 Jun 3, 2020
@tekton-robot
Copy link

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

File Old Coverage New Coverage Delta
pkg/apis/triggers/v1alpha1/trigger_template_validation.go 97.2% 97.5% 0.3

@bobcatfish
Copy link
Collaborator

Drive by comment - instead of changing the name of the params field, you could change how the replacement works, e.g. use $(tt.params.foo) when using the field, but still call the section params in the trigger template spec

whaddaya think (ignore me if this has been discussed already)

@savitaashture
Copy link
Contributor Author

Drive by comment - instead of changing the name of the params field, you could change how the replacement works, e.g. use $(tt.params.foo) when using the field, but still call the section params in the trigger template spec

whaddaya think (ignore me if this has been discussed already)

Hi @bobcatfish Its good point
Right now I don't remember that whether we discussed the point which you mentioned.
But i just want to know any reason behind this $(tt.params.foo) suggestion
Or is it because consistency with params

@bobcatfish
Copy link
Collaborator

But i just want to know any reason behind this $(tt.params.foo) suggestion
Or is it because consistency with params

Just consistency! Across tekton we have multiple spec.params - it seems like the right name for the field. The problem is the ambiguity in the usage, feels weird to solve that by changing the name of the field.

Mostly a subjective opinion tho!

@dibyom
Copy link
Member

dibyom commented Jun 17, 2020

I like the $(tt.params) idea. I think we discussed this though there weren't any strong opinions one way or another.
I agree with the consistency issue and that renaming the field feels a bit weird. Calling the field params but using it as tt.params is also weird but less so in my opinion 👼

How do you feel about keeping the params field and using tt.params for substitution? Also if we go ahead with this: I think we should support replacements with both $(params) and $(tt.params) for a release before we drop support for $(params)?

@savitaashture
Copy link
Contributor Author

I like the $(tt.params) idea. I think we discussed this though there weren't any strong opinions one way or another.
I agree with the consistency issue and that renaming the field feels a bit weird. Calling the field params but using it as tt.params is also weird but less so in my opinion

How do you feel about keeping the params field and using tt.params for substitution? Also if we go ahead with this: I think we should support replacements with both $(params) and $(tt.params) for a release before we drop support for $(params)?

@dibyom yes i think we need to keep $(params) and $(tt.params) for a release before we drop support for $(params)

Let me understand clearly one of your point here
How do you feel about keeping the params field and using tt.params for substitution?
Do you mean something like below

spec:
  params:
    - name: gitrevision
    - name: gitrepositoryurl
  resourcetemplates:
    - apiVersion: tekton.dev/v1alpha1
      kind: TaskRun
      metadata:
        generateName: github-run-
      spec:
        taskSpec:
          inputs:
            resources:
              - name: source
                type: git
          steps:
            - image: ubuntu
              script: |
                #! /bin/bash
                ls -al $(inputs.resources.source.path)
        inputs:
          resources:
            - name: source
              resourceSpec:
                type: git
                params:
                  - name: revision
                    value: $(tt.params.gitrevision)
                  - name: url
                    value: $(tt.params.gitrepositoryurl)

If thats the thing i feel it creates bit confusion 🤔

OR

This way

spec:
  tt:
    params:
      - name: gitrevision
      - name: gitrepositoryurl
  resourcetemplates:
    - apiVersion: tekton.dev/v1alpha1
      kind: TaskRun
      metadata:
        generateName: github-run-
      spec:
        taskSpec:
          inputs:
            resources:
              - name: source
                type: git
          steps:
            - image: ubuntu
              script: |
                #! /bin/bash
                ls -al $(inputs.resources.source.path)
        inputs:
          resources:
            - name: source
              resourceSpec:
                type: git
                params:
                  - name: revision
                    value: $(tt.params.gitrevision)
                  - name: url
                    value: $(tt.params.gitrepositoryurl)

@dibyom
Copy link
Member

dibyom commented Jun 18, 2020

Yeah, the first one. I agree it can be a bit confusing 😄 On the other hand calling the field ttparams when everywhere else (e.g. TriggerBindings, Pipelines etc.) we call it params is also a bit inconsistent.

@savitaashture
Copy link
Contributor Author

Yeah, the first one. I agree it can be a bit confusing On the other hand calling the field ttparams when everywhere else (e.g. TriggerBindings, Pipelines etc.) we call it params is also a bit inconsistent.

Ya even i am thinking of confusing part.
Otherwise + point is we no need to do backward compatibility if we keep field params as it is and use $(tt.params) everywhere

@tekton-robot tekton-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 20, 2020
@savitaashture savitaashture changed the title Modify TriggerTemplate spec params to avoid confusion from resourcetemplates params Modify spec params to avoid confusion between resourcetemplates and triggertemplate params Jun 20, 2020
@savitaashture
Copy link
Contributor Author

savitaashture commented Jun 20, 2020

@dibyom Modified as per the discussion

Changes:
kept $(params) field and used $(tt.params) for the substitution

@savitaashture
Copy link
Contributor Author

/retest

@@ -111,7 +111,7 @@ func ApplyParamsToResourceTemplate(params []triggersv1.Param, rt json.RawMessage
// param value substituted for all matching param variables in the template
func applyParamToResourceTemplate(param triggersv1.Param, rt json.RawMessage) json.RawMessage {
// Assume the param is valid
paramVariable := fmt.Sprintf("$(params.%s)", param.Name)
paramVariable := fmt.Sprintf("$(tt.params.%s)", param.Name)
Copy link
Member

Choose a reason for hiding this comment

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

Are we just supporting $(tt.params) for now. Shouldn't we support both $(params) or $(tt.params) for a release or two?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated changes
Please do have a look
Thank you

@tekton-robot tekton-robot removed the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jun 22, 2020
@tekton-robot tekton-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 22, 2020
@tekton-robot
Copy link

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

File Old Coverage New Coverage Delta
pkg/apis/triggers/v1alpha1/trigger_template_validation.go 97.2% 97.5% 0.3

@tekton-robot
Copy link

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

File Old Coverage New Coverage Delta
pkg/apis/triggers/v1alpha1/trigger_template_validation.go 97.2% 97.5% 0.3

// Assume the param is valid
paramVariable := fmt.Sprintf("$(params.%s)", param.Name)
if len(templateParams) == 0 {
paramVariable = fmt.Sprintf("$(tt.params.%s)", param.Name)
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason we cannot attempt to replace with both $(params) as well as $(tt.params)?

Copy link
Contributor Author

@savitaashture savitaashture Jun 23, 2020

Choose a reason for hiding this comment

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

The reason is we will not be getting the information on user provided ResourceTemplate params whether its $(params) or $(tt.params)

So currently with the help of regular expression able to distinguish whether its $(params) or $(tt.params).

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this works if a user mixes both params and tt.params in the same file. It should be safe to do a double pass - e.g.

for _, tag := range []string{ "params", "tt.params"} {
  paramVariable := fmt.Sprintf("$(%s.%s)", tag, param.Name)
  rt = bytes.Replace(rt, []byte(paramVariable), []byte(paramValue), -1)
}

@dibyom dibyom removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 23, 2020
- name: url
value: $(params.gitrepositoryurl)
value: $(tt.params.gitrepositoryurl)
```

`TriggerTemplates` currently support the following [Tekton Pipelines](https://github.com/tektoncd/pipelines) resources:
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@savitaashture savitaashture Jun 23, 2020

Choose a reason for hiding this comment

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

Updated doc 👍
Thank you

@tekton-robot
Copy link

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

File Old Coverage New Coverage Delta
pkg/apis/triggers/v1alpha1/trigger_template_validation.go 97.2% 97.5% 0.3

@savitaashture
Copy link
Contributor Author

/test pull-tekton-triggers-integration-tests

1 similar comment
@dibyom
Copy link
Member

dibyom commented Jun 23, 2020

/test pull-tekton-triggers-integration-tests

Copy link
Member

@wlynch wlynch left a comment

Choose a reason for hiding this comment

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

Almost there! No particular preference on tt.params (only alternative I can think of is template.params, but I don't care that much).

Few minor comments, otherwise SGTM

templateParams := paramsRegexp.FindAllSubmatch(template.RawExtension.Raw, -1)
for _, templateParam := range templateParams {
templateParamName := string(templateParam[1])
templateParamName := string(templateParam[2])
Copy link
Member

Choose a reason for hiding this comment

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

I realize this is following what was set before, but the direct array access is at risk for nil panics. I think we should do 2 things:

  1. Have a length check prior to accessing templateParam[2].
  2. Create a more exhaustive set of regexp checks instead of the more e2e tests we see here. (mainly to give us more confidence that the regexp is matching to what we expect, even in unexpected cases).

Copy link
Contributor Author

@savitaashture savitaashture Jun 24, 2020

Choose a reason for hiding this comment

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

Actually

templateParams := paramsRegexp.FindAllSubmatch(template.RawExtension.Raw, -1)

templateParams length will be 0 in any unexpected cases because of Regular Expression

regexp.MustCompile(`\$\((params|tt.params).([_a-zA-Z][_a-zA-Z0-9.-]*)\)`)

If user provides any such values like below

1. params1.foo
2. tt1.params.foo
3. tt.params1.foo
4. tt.foo
5. .foo

the templateParams itself is 0 and because of that it never enters into for loop

But yes need to add more test cases to ensures the regex will not break.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added test cases to cover few more scenario

@@ -153,18 +153,18 @@ func TestHandleEvent(t *testing.T) {
Kind: "PipelineResource",
},
ObjectMeta: metav1.ObjectMeta{
Name: "$(params.name)",
Name: "$(tt.params.name)",
Copy link
Member

Choose a reason for hiding this comment

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

We should probably leave some of these tests using the old params syntax to ensure backwards compatibility, since this is the first place where we are actually using the value outside of validation.

Copy link
Member

Choose a reason for hiding this comment

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

+1 to having some tests with the old $(params)

// Assume the param is valid
paramVariable := fmt.Sprintf("$(params.%s)", param.Name)
if len(templateParams) == 0 {
paramVariable = fmt.Sprintf("$(tt.params.%s)", param.Name)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this works if a user mixes both params and tt.params in the same file. It should be safe to do a double pass - e.g.

for _, tag := range []string{ "params", "tt.params"} {
  paramVariable := fmt.Sprintf("$(%s.%s)", tag, param.Name)
  rt = bytes.Replace(rt, []byte(paramVariable), []byte(paramValue), -1)
}

@tekton-robot
Copy link

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

File Old Coverage New Coverage Delta
pkg/apis/triggers/v1alpha1/trigger_template_validation.go 97.2% 97.5% 0.3

@tekton-robot
Copy link

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

File Old Coverage New Coverage Delta
pkg/apis/triggers/v1alpha1/trigger_template_validation.go 97.2% 97.5% 0.3

Copy link
Member

@dibyom dibyom left a comment

Choose a reason for hiding this comment

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

/lgtm

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. lgtm Indicates that a PR is ready to be merged. 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.

Confusing params syntax in trigger templates
6 participants