Skip to content

Commit af6055a

Browse files
committed
[TEP-0100] Prepare for testing of minimal status implementation
This change is to minimize the size of the actual implementation. We need to change a number of tests in `pkg/reconciler/pipelinerun/pipelinerun_test.go` to be table-based, so that we can test behavior under each possible value for the new `embedded-status` feature flag. Here, we just modify the relevant tests to be table-based and use a parameterized helper function for the actual execution, with a test case for each value. It also adds helper functions for checking the relevant fields in the `PipelineRun` status, which are hard-coded to always handle the current, "full" embedded status approach. This also splits out `TestUpdatePipelineRunStatusFromTaskRuns` and `TestUpdatePipelineRunStatusFromRuns` into a separate file, `pipelinerun_updatestatus_test.go`. When the TEP-0100 implementation lands, this will also contain additional tests for updating via child references. Splitting like this helps keep `pipelinerun_test.go` from getting even more bloated than it is currently. Until the implementation, these table-based tests are purely duplicative - they're going to run the same and check the same things for any value of `embedded-status`, but the implementation PR will be cleaner, only adding the implementation and changing the helper functions to take all of the possible "embedded-status" values into account. The changes which need to be made in the implementation PR are all marked with `// TODO(abayer): ...` in `pipelinerun_test.go` and `pipelinerun_updatestatus_test.go`. Signed-off-by: Andrew Bayer <andrew.bayer@gmail.com>
1 parent 053833c commit af6055a

15 files changed

+3761
-1062
lines changed

docs/install.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ features](#alpha-features) to be used.
389389
- `embedded-status`: set this flag to "full" to enable full embedding of `TaskRun` and `Run` statuses in the
390390
`PipelineRun` status. Set it to "minimal" to populate the `ChildReferences` field in the `PipelineRun` status with
391391
name, kind, and API version information for each `TaskRun` and `Run` in the `PipelineRun` instead. Set it to "both" to
392-
do both. For more information, see [Configuring usage of `TaskRun` and `Run` embedded statuses](pipelineruns.md#configuring-usage-of-taskrun-and-run-embedded-statuses). **NOTE**: This functionality is not yet active.
392+
do both. For more information, see [Configuring usage of `TaskRun` and `Run` embedded statuses](pipelineruns.md#configuring-usage-of-taskrun-and-run-embedded-statuses).
393393

394394
For example:
395395

pkg/apis/pipeline/v1beta1/pipelinerun_types.go

+15
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,21 @@ type ChildStatusReference struct {
422422
WhenExpressions []WhenExpression `json:"whenExpressions,omitempty"`
423423
}
424424

425+
// GetConditionChecksAsMap returns a map representation of this ChildStatusReference's ConditionChecks, in the same form
426+
// as PipelineRunTaskRunStatus.ConditionChecks.
427+
func (cr ChildStatusReference) GetConditionChecksAsMap() map[string]*PipelineRunConditionCheckStatus {
428+
if len(cr.ConditionChecks) == 0 {
429+
return nil
430+
}
431+
ccMap := make(map[string]*PipelineRunConditionCheckStatus)
432+
433+
for _, cc := range cr.ConditionChecks {
434+
ccMap[cc.ConditionCheckName] = &cc.PipelineRunConditionCheckStatus
435+
}
436+
437+
return ccMap
438+
}
439+
425440
// PipelineRunStatusFields holds the fields of PipelineRunStatus' status.
426441
// This is defined separately and inlined so that other types can readily
427442
// consume these fields via duck typing.

pkg/reconciler/pipelinerun/cancel.go

+46-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"strings"
2525
"time"
2626

27+
"github.com/tektoncd/pipeline/pkg/apis/config"
28+
2729
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
2830
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
2931
clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
@@ -99,18 +101,21 @@ func cancelPipelineRun(ctx context.Context, logger *zap.SugaredLogger, pr *v1bet
99101
func cancelPipelineTaskRuns(ctx context.Context, logger *zap.SugaredLogger, pr *v1beta1.PipelineRun, clientSet clientset.Interface) []string {
100102
errs := []string{}
101103

102-
// Loop over the TaskRuns in the PipelineRun status.
103-
// If a TaskRun is not in the status yet we should not cancel it anyways.
104-
for taskRunName := range pr.Status.TaskRuns {
104+
trNames, runNames, err := filterChildObjectsFromPRStatus(ctx, pr.Status)
105+
if err != nil {
106+
errs = append(errs, err.Error())
107+
}
108+
109+
for _, taskRunName := range trNames {
105110
logger.Infof("cancelling TaskRun %s", taskRunName)
106111

107112
if _, err := clientSet.TektonV1beta1().TaskRuns(pr.Namespace).Patch(ctx, taskRunName, types.JSONPatchType, cancelTaskRunPatchBytes, metav1.PatchOptions{}, ""); err != nil {
108113
errs = append(errs, fmt.Errorf("Failed to patch TaskRun `%s` with cancellation: %s", taskRunName, err).Error())
109114
continue
110115
}
111116
}
112-
// Loop over the Runs in the PipelineRun status.
113-
for runName := range pr.Status.Runs {
117+
118+
for _, runName := range runNames {
114119
logger.Infof("cancelling Run %s", runName)
115120

116121
if err := cancelRun(ctx, runName, pr.Namespace, clientSet); err != nil {
@@ -122,6 +127,42 @@ func cancelPipelineTaskRuns(ctx context.Context, logger *zap.SugaredLogger, pr *
122127
return errs
123128
}
124129

130+
// filterChildObjectsFromPRStatus returns taskruns and runs owned by the pipelineRun, based on the value of the embedded status flag.
131+
func filterChildObjectsFromPRStatus(ctx context.Context, prs v1beta1.PipelineRunStatus) ([]string, []string, error) {
132+
cfg := config.FromContextOrDefaults(ctx)
133+
134+
var trNames []string
135+
var runNames []string
136+
unknownChildKinds := make(map[string]string)
137+
138+
if cfg.FeatureFlags.EmbeddedStatus != config.FullEmbeddedStatus {
139+
for _, cr := range prs.ChildReferences {
140+
switch cr.Kind {
141+
case "TaskRun":
142+
trNames = append(trNames, cr.Name)
143+
case "Run":
144+
runNames = append(runNames, cr.Name)
145+
default:
146+
unknownChildKinds[cr.Name] = cr.Kind
147+
}
148+
}
149+
} else {
150+
for trName := range prs.TaskRuns {
151+
trNames = append(trNames, trName)
152+
}
153+
for runName := range prs.Runs {
154+
runNames = append(runNames, runName)
155+
}
156+
}
157+
158+
var err error
159+
if len(unknownChildKinds) > 0 {
160+
err = fmt.Errorf("found child objects of unknown kinds: %v", unknownChildKinds)
161+
}
162+
163+
return trNames, runNames, err
164+
}
165+
125166
// gracefullyCancelPipelineRun marks any non-final resolved TaskRun(s) as cancelled and runs finally.
126167
func gracefullyCancelPipelineRun(ctx context.Context, logger *zap.SugaredLogger, pr *v1beta1.PipelineRun, clientSet clientset.Interface) error {
127168
errs := cancelPipelineTaskRuns(ctx, logger, pr, clientSet)

0 commit comments

Comments
 (0)