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

TEP-0156 : when-in-steps #1147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

ericzzzzzzz
Copy link
Contributor

@ericzzzzzzz ericzzzzzzz commented Apr 15, 2024

Description

This pull request proposes the ability to define When Expressions directly within Steps. This allows for fine-grained control over step execution based on conditions like the status of previous steps, enabling more dynamic and adaptable workflows.

Overview

  • Steps accept an optional when field with a list of When Expressions.
  • When Expressions are evaluated at the entrypoint level, allowing access to previous step results.
  • Skipped steps due to When Expressions have an exit code of 0 and a TerminationReason of Skipped.
  • Both Operator-based and CEL expressions are supported.
  • Variable substitution within When Expressions includes access to Task Results, Params, Workspaces, and Step Results.

@tekton-robot tekton-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Apr 15, 2024
@ericzzzzzzz ericzzzzzzz changed the title tep: add-when-in-steps TEP : when-in-steps Apr 15, 2024
@ericzzzzzzz ericzzzzzzz force-pushed the step-when branch 3 times, most recently from 42b9464 to f841b49 Compare April 15, 2024 19:25
Copy link
Contributor

@khrm khrm left a comment

Choose a reason for hiding this comment

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

/kind tep

@tekton-robot tekton-robot added the kind/tep Categorizes issue or PR as related to a TEP (or needs a TEP). label Apr 16, 2024
@chitrangpatel
Copy link
Member

/kind tep

@ericzzzzzzz ericzzzzzzz changed the title TEP : when-in-steps TEP-0155 : when-in-steps Apr 22, 2024
@chitrangpatel
Copy link
Member

/assign


### Supported Expression Languages

Just like when expressions in Task Level, when expressions in Step Level will also be supporting both Operator-based expressions and CEL(Common Expression Language) expressions.
Copy link
Member

Choose a reason for hiding this comment

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

May be also add a paragraph on which fields are we applying when expressions on?

  • all task based fields(e.g. Task Results, Task Conditions etc.)
  • Workspaces
  • Params
  • StepResults
  • StepStatus(?): TerminationReasonFields: Error, Continued, Skipped, TimeOut, Cancelled etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a Variable Substitution section to explains which fields can be referenced in step.when.

StepStatus(?)

It seems that we haven't StepStatus implemented yet, should we include in this tep? This seems a different scope.

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 ok keeping it out for now.

Copy link

Choose a reason for hiding this comment

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

IMHO, we should include some way to allow conditional execution on step status. Otherwise usage of When expression in guarding a step will be limited. I think one major advantage of a When expression guarding a step is to check the status of preview steps. Otherwise we are forcing the user to "mimic" this functionality by exporting a pseudo status of step to workspace/result to mimic this functionality.

Secondly, is it possible to check attributes of files in a workspace in When expressions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, @haroonc
Thanks for the review! I've added step status variable substitution when evaluating When expressions in the design.
For checking file attributes, as far as i know Tekton doesn't support that directly. But, you can use a script in a step to get attributes and store them as results. Then, use those results in When expressions in later steps.

@tekton-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: chitrangpatel

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 Apr 23, 2024
@chitrangpatel
Copy link
Member

@ericzzzzzzz can you please leave a short description of the TEP in the opening comment?

@chitrangpatel
Copy link
Member

@tektoncd/core-maintainers PTAL 🙏

@ericzzzzzzz
Copy link
Contributor Author

@ericzzzzzzz can you please leave a short description of the TEP in the opening comment?

done! Thanks

@afrittoli
Copy link
Member

/assign

@tekton-robot tekton-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 7, 2024
@tekton-robot tekton-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 7, 2024
@ericzzzzzzz ericzzzzzzz changed the title TEP-0155 : when-in-steps TEP-0156 : when-in-steps May 7, 2024
@ericzzzzzzz ericzzzzzzz force-pushed the step-when branch 4 times, most recently from 7ca83ad to 7b6f7d5 Compare May 7, 2024 21:01
@chitrangpatel
Copy link
Member

/assign @vdemeester
PTAL since you had suggested opening this. 🙏

- End-to-end tests covering common use cases and error conditions.

## Alternatives
An alternative approach would be to evaluate When Expressions before creating the pods for the steps. If a When Expression evaluates to false, the corresponding step container would not be created at all.
Copy link
Contributor

@khrm khrm May 16, 2024

Choose a reason for hiding this comment

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

I think you meant creating step containers inside the pods for the Task. There are no pods for containers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I meant tekton can evaluate 'when expressions' when building pod definition, if the evaluation result is false, then we don't have to add the corresponding step container to the pod 'spec.containers'

## Motivation
Currently, Tekton Tasks execute steps sequentially without the ability to control their execution based on conditions within the Task itself. This limitation restricts the ability to create dynamic workflows that adapt to different scenarios or respond to the outcomes of previous steps.
By introducing When Expressions to Steps, users will be able to:
- **Create conditional workflows**: Execute steps only when specific conditions are met, such as the success or failure of a previous step, the value of a result, or the presence of a specific parameter.
Copy link

Choose a reason for hiding this comment

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

Related with this: the new v2alpha4 formatter from Tekton Chains now is processing all the type-hinted results from StepResults associated with a given PipelineRun or TaskRun. With the new WhenExpressions in Steps feature, I'm guessing we'll need to do a change in Chains to process only the executed steps, and not all of them; processing not executed steps might result in an error (?). With that said I think we could open a new issue in Chains once this TEP is approved to track the needed effort there. @chitrangpatel @ericzzzzzzz please feel free to correct me if I'm wrong with my previous thoughts. Thanks!

@chitrangpatel
Copy link
Member

@vdemeester PTAL when you have a chance 🙏

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.

The lines between Task and Pipeline are bluring more and more with this.
An additionnal question, what happens if a step that write a TaskResult is skipped ?


## Summary
This TEP proposes the addition of When Expressions to individual Steps within a Task. This will allow for fine-grained control over the execution of steps based on conditions like the status of previous step results. This enhancement will provide greater flexibility and control in defining task logic, enabling more complex and dynamic workflows.
## Motivation
Copy link
Member

Choose a reason for hiding this comment

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

Anything described here can be achieved by "relatively" smart scripts in Steps, am I right ?
The "main" difference is in one case it's expressed with the tekton API, and on the other case, it's in "a script" or "in code".

Copy link
Member

Choose a reason for hiding this comment

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

Only for inlined steps. This allows us to control the execution of stepActions where we don't have the ability to edit the scripts.

I think that's why it wasn't needed when we only had inlined steps. It is needed now that we have StepActions.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I can hear that 😛 But one could also "condition" this in the StepAction with a parameter (probably a bit convoluted)

@chitrangpatel
Copy link
Member

The lines between Task and Pipeline are bluring more and more with this. An additionnal question, what happens if a step that write a TaskResult is skipped ?

I think the core difference between the fact that Pipeline can fan-out (Matrix, DAG) vs Task is sequential will always remain. So it might appear that lines are being blurred but what you can do with Pipelines vs Tasks will always be different. If Tasks was a DAG of Steps then I agree that the lines would be truly blurred.

I think the outcome will be the same as if the Step producing a TaskResult errored out. The Task author is controlling the flow of the StepAction/Step that is producing the result so they can choose. In this case, I think that the Task will error with the message that it could not produce a result (whatever the current behavior is if the Task does not generate a result)?

@vdemeester
Copy link
Member

I think the outcome will be the same as if the Step producing a TaskResult errored out. The Task author is controlling the flow of the StepAction/Step that is producing the result so they can choose. In this case, I think that the Task will error with the message that it could not produce a result (whatever the current behavior is if the Task does not generate a result)?

Can we discuss this in the TEP ?

@ericzzzzzzz
Copy link
Contributor Author

I think the outcome will be the same as if the Step producing a TaskResult errored out. The Task author is controlling the flow of the StepAction/Step that is producing the result so they can choose. In this case, I think that the Task will error with the message that it could not produce a result (whatever the current behavior is if the Task does not generate a result)?

Can we discuss this in the TEP ?

I added a section to capture this.

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.

Sorry, one more thing, can we dig a bit more into examples of use case it would fullfill ?

For example

  • having "optional" set of caching steps (using StepAction) if a parameter is the Task
  • having a "report" and fail step if the previous step fails
  • … other use case we envision with this feature.

@ericzzzzzzz
Copy link
Contributor Author

Sorry, one more thing, can we dig a bit more into examples of use case it would fullfill ?

For example

  • having "optional" set of caching steps (using StepAction) if a parameter is the Task
  • having a "report" and fail step if the previous step fails
  • … other use case we envision with this feature.

Hi, I added some examples.

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/tep Categorizes issue or PR as related to a TEP (or needs a TEP). 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.

None yet

9 participants