-
Notifications
You must be signed in to change notification settings - Fork 419
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
apply triggertemplate param default value if body/header based triggerbinding param missing from body/header #761
apply triggertemplate param default value if body/header based triggerbinding param missing from body/header #761
Conversation
8dca2ad
to
5df7ef2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding sample example to examples to show usage of this or any doc update which provide this information that there is a support of default values if not found from binding
pkg/template/event.go
Outdated
if err != nil { | ||
return nil, fmt.Errorf("failed to ApplyEventValuesToParams: %w", err) | ||
} | ||
|
||
return MergeInDefaultParams(out, ttParams), nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we still need to call MergeInDefaultParams
since we are now adding default values in applyEventValuesToParams
itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we do. Unit tests fail without it:
using default defaultVal since failed to replace JSONPath value for param p2: $(body.p2): error: p2 is not found
--- FAIL: TestResolveParams (0.00s)
--- FAIL: TestResolveParams/add_default_values_for_params_with_missing_values (0.00s)
event_test.go:351: didn't get expected params -want + got: []v1alpha1.Param{
{Name: "p1", Value: "val1"},
- {Name: "p2", Value: "defaultVal"},
}
FAIL
FAIL github.com/tektoncd/triggers/pkg/template 0.062s
FAIL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, interesting. I think that makes sense. But makes me wonder if we can simplify this code somehow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok @dibyom I've pushed a separate commit that consolidates applyEventValuesToParams
and mergeInDefaultParams
it is possible the changes pulled in with the rebase helped things (the method was minimally switched from public to package only)
otherwise, the biggest takeaway is that an explicit for loop on the default
array has been replaced with a golang map lookup.... that is positive.
If you like this approach better I'll squash the commits.
pkg/template/event.go
Outdated
// if the header or body was not supplied or was malformed, go with a default if it exists | ||
for _, ps := range defaults { | ||
if ps.Name == p.Name && ps.Default != nil { | ||
fmt.Fprintf(os.Stdout, "using default %s since failed to replace JSONPath value for param %s: %s: error: %s\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is useful information, but I'm not sure we need to print it out. If we do, maybe we can pass through the logger that we for other logging instead of using fmt.Fprintf?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I looked at that and found it a bit onerous to pass the logger through all the call levels to get it here.
But if this is not an tenable compromise, then sure. I can go one way or the other. I don't have a strong opinion. Will it be feasible for a user to figure out that if the default is used vs. the body/header reference, there was an issue with how it was constructed?
You got a final call here @dibyom ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think its fine to remove the log statement here given that this is documented/expected behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok just pushed removal of log statement as part of rebasing this PR on top of recent changes
Per #568 (comment) the existing example could be tweaked to leverage this, where the fact that the default is getting used is highlighted. Would that be amenable vs. creating a separate example? I did scan the docs and was unclear on where to update. Suggestions on how to cross reference https://github.com/tektoncd/triggers/blob/master/docs/triggertemplates.md with https://github.com/tektoncd/triggers/blob/master/docs/triggerbindings.md to connect the dots @savitaashture @dibyom ? |
Sure as long as we have an example that shows that we substitute the default value when one from the event is missing, I'm ok!
In the parameters section of TriggerTemplates, we can add something about the Similarly, in the TriggerBindings docs, we can add a something about the behavior maybe, as sub-section inside the Event Variable Interpolation section -- if we fail to resolve the param value from the event, we'll look to see if the trigger template param has a default value. If it doesn't we throw an error. |
5df7ef2
to
300539d
Compare
bfe8d6c
to
69d7a97
Compare
OK pushed commit 64735d2 to try and capture this |
doc updates following these recommendations in 774fb5c tests have been passing previously, so I'm assuming this doc only change will not affect things PTAL @dibyom @savitaashture thanks!! |
Nice cleanup! Would you mind squashing up the commits? |
/approve |
[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 |
…rbinding param missing from body/header
774fb5c
to
f1ea098
Compare
commits squashed @dibyom |
/lgtm |
Changes
Fixes #568
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
/assign @dibyom