Skip to content

Commit 2421ceb

Browse files
abayerchitrangpatel
authored andcommitted
[TEP-0100] Add functionality to be used in supporting minimal embedded status
This builds on tektoncd#4694, tektoncd#4734, and tektoncd#4753. It will feed into a revamped tektoncd#4739, all as part of https://github.com/tektoncd/community/blob/main/teps/0100-embedded-taskruns-and-runs-status-in-pipelineruns.md and tektoncd#3140. Specifically, this adds functionality to `pkg/reconciler/pipelinerun/resources` in `pipelinerunresolution.go` and `pipelinerunstate.go` which will be needed for the full implementation. These changes won't have any effects in the current situation, because `pr.Status.ChildReferences` is never populated, so can be made independently of the rest of the implementation, thus also shrinking the size of the rest of the implementation PR(s) for easier review. Signed-off-by: Andrew Bayer <andrew.bayer@gmail.com>
1 parent 4cae275 commit 2421ceb

File tree

5 files changed

+437
-99
lines changed

5 files changed

+437
-99
lines changed

pkg/reconciler/pipelinerun/pipelinerun.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ func updatePipelineRunStatusFromTaskRuns(logger *zap.SugaredLogger, pr *v1beta1.
12521252
// status. This means that the conditions were orphaned, and never added to the
12531253
// status. In this case we need to generate a new TaskRun name, that will be used
12541254
// to run the TaskRun if the conditions are passed.
1255-
taskRunName = resources.GetTaskRunName(pr.Status.TaskRuns, pipelineTaskName, pr.Name)
1255+
taskRunName = resources.GetTaskRunName(pr.Status.TaskRuns, pr.Status.ChildReferences, pipelineTaskName, pr.Name)
12561256
pr.Status.TaskRuns[taskRunName] = &v1beta1.PipelineRunTaskRunStatus{
12571257
PipelineTaskName: pipelineTaskName,
12581258
Status: nil,

pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go

+30-8
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,14 @@ func ResolvePipelineRunTask(
502502
}
503503
rprt.CustomTask = isCustomTask(ctx, rprt)
504504
if rprt.IsCustomTask() {
505-
rprt.RunName = getRunName(pipelineRun.Status.Runs, task.Name, pipelineRun.Name)
505+
rprt.RunName = getRunName(pipelineRun.Status.Runs, pipelineRun.Status.ChildReferences, task.Name, pipelineRun.Name)
506506
run, err := getRun(rprt.RunName)
507507
if err != nil && !errors.IsNotFound(err) {
508508
return nil, fmt.Errorf("error retrieving Run %s: %w", rprt.RunName, err)
509509
}
510510
rprt.Run = run
511511
} else {
512-
rprt.TaskRunName = GetTaskRunName(pipelineRun.Status.TaskRuns, task.Name, pipelineRun.Name)
512+
rprt.TaskRunName = GetTaskRunName(pipelineRun.Status.TaskRuns, pipelineRun.Status.ChildReferences, task.Name, pipelineRun.Name)
513513

514514
// Find the Task that this PipelineTask is using
515515
var (
@@ -560,7 +560,7 @@ func ResolvePipelineRunTask(
560560

561561
// Get all conditions that this pipelineTask will be using, if any
562562
if len(task.Conditions) > 0 {
563-
rcc, err := resolveConditionChecks(&task, pipelineRun.Status.TaskRuns, rprt.TaskRunName, getTaskRun, getCondition, providedResources)
563+
rcc, err := resolveConditionChecks(&task, pipelineRun.Status.TaskRuns, pipelineRun.Status.ChildReferences, rprt.TaskRunName, getTaskRun, getCondition, providedResources)
564564
if err != nil {
565565
return nil, err
566566
}
@@ -571,7 +571,16 @@ func ResolvePipelineRunTask(
571571
}
572572

573573
// getConditionCheckName should return a unique name for a `ConditionCheck` if one has not already been defined, and the existing one otherwise.
574-
func getConditionCheckName(taskRunStatus map[string]*v1beta1.PipelineRunTaskRunStatus, trName, conditionRegisterName string) string {
574+
func getConditionCheckName(taskRunStatus map[string]*v1beta1.PipelineRunTaskRunStatus, childRefs []v1beta1.ChildStatusReference, trName, conditionRegisterName string) string {
575+
for _, cr := range childRefs {
576+
if cr.Name == trName {
577+
for _, cc := range cr.ConditionChecks {
578+
if cc.ConditionName == conditionRegisterName {
579+
return cc.ConditionCheckName
580+
}
581+
}
582+
}
583+
}
575584
trStatus, ok := taskRunStatus[trName]
576585
if ok && trStatus.ConditionChecks != nil {
577586
for k, v := range trStatus.ConditionChecks {
@@ -585,7 +594,13 @@ func getConditionCheckName(taskRunStatus map[string]*v1beta1.PipelineRunTaskRunS
585594
}
586595

587596
// GetTaskRunName should return a unique name for a `TaskRun` if one has not already been defined, and the existing one otherwise.
588-
func GetTaskRunName(taskRunsStatus map[string]*v1beta1.PipelineRunTaskRunStatus, ptName, prName string) string {
597+
func GetTaskRunName(taskRunsStatus map[string]*v1beta1.PipelineRunTaskRunStatus, childRefs []v1beta1.ChildStatusReference, ptName, prName string) string {
598+
for _, cr := range childRefs {
599+
if cr.Kind == "TaskRun" && cr.PipelineTaskName == ptName {
600+
return cr.Name
601+
}
602+
}
603+
589604
for k, v := range taskRunsStatus {
590605
if v.PipelineTaskName == ptName {
591606
return k
@@ -597,16 +612,23 @@ func GetTaskRunName(taskRunsStatus map[string]*v1beta1.PipelineRunTaskRunStatus,
597612

598613
// getRunName should return a unique name for a `Run` if one has not already
599614
// been defined, and the existing one otherwise.
600-
func getRunName(runsStatus map[string]*v1beta1.PipelineRunRunStatus, ptName, prName string) string {
615+
func getRunName(runsStatus map[string]*v1beta1.PipelineRunRunStatus, childRefs []v1beta1.ChildStatusReference, ptName, prName string) string {
616+
for _, cr := range childRefs {
617+
if cr.Kind == "Run" && cr.PipelineTaskName == ptName {
618+
return cr.Name
619+
}
620+
}
621+
601622
for k, v := range runsStatus {
602623
if v.PipelineTaskName == ptName {
603624
return k
604625
}
605626
}
627+
606628
return kmeta.ChildName(prName, fmt.Sprintf("-%s", ptName))
607629
}
608630

609-
func resolveConditionChecks(pt *v1beta1.PipelineTask, taskRunStatus map[string]*v1beta1.PipelineRunTaskRunStatus, taskRunName string, getTaskRun resources.GetTaskRun, getCondition GetCondition, providedResources map[string]*resourcev1alpha1.PipelineResource) ([]*ResolvedConditionCheck, error) {
631+
func resolveConditionChecks(pt *v1beta1.PipelineTask, taskRunStatus map[string]*v1beta1.PipelineRunTaskRunStatus, childRefs []v1beta1.ChildStatusReference, taskRunName string, getTaskRun resources.GetTaskRun, getCondition GetCondition, providedResources map[string]*resourcev1alpha1.PipelineResource) ([]*ResolvedConditionCheck, error) {
610632
rccs := []*ResolvedConditionCheck{}
611633
for i := range pt.Conditions {
612634
ptc := pt.Conditions[i]
@@ -619,7 +641,7 @@ func resolveConditionChecks(pt *v1beta1.PipelineTask, taskRunStatus map[string]*
619641
Msg: err.Error(),
620642
}
621643
}
622-
conditionCheckName := getConditionCheckName(taskRunStatus, taskRunName, crName)
644+
conditionCheckName := getConditionCheckName(taskRunStatus, childRefs, taskRunName, crName)
623645
// TODO(#3133): Also handle Custom Task Runs (getRun here)
624646
cctr, err := getTaskRun(conditionCheckName)
625647
if err != nil {

pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -3444,6 +3444,11 @@ func TestGetTaskRunName(t *testing.T) {
34443444
PipelineTaskName: "task1",
34453445
},
34463446
}
3447+
childRefs := []v1beta1.ChildStatusReference{{
3448+
TypeMeta: runtime.TypeMeta{Kind: "TaskRun"},
3449+
Name: "taskrun-for-task1",
3450+
PipelineTaskName: "task1",
3451+
}}
34473452

34483453
for _, tc := range []struct {
34493454
name string
@@ -3478,8 +3483,12 @@ func TestGetTaskRunName(t *testing.T) {
34783483
if tc.prName != "" {
34793484
testPrName = tc.prName
34803485
}
3481-
gotTrName := GetTaskRunName(taskRunsStatus, tc.ptName, testPrName)
3482-
if d := cmp.Diff(tc.wantTrName, gotTrName); d != "" {
3486+
trNameFromTRStatus := GetTaskRunName(taskRunsStatus, nil, tc.ptName, testPrName)
3487+
if d := cmp.Diff(tc.wantTrName, trNameFromTRStatus); d != "" {
3488+
t.Errorf("GetTaskRunName: %s", diff.PrintWantGot(d))
3489+
}
3490+
trNameFromChildRefs := GetTaskRunName(nil, childRefs, tc.ptName, testPrName)
3491+
if d := cmp.Diff(tc.wantTrName, trNameFromChildRefs); d != "" {
34833492
t.Errorf("GetTaskRunName: %s", diff.PrintWantGot(d))
34843493
}
34853494
})
@@ -3493,6 +3502,11 @@ func TestGetRunName(t *testing.T) {
34933502
PipelineTaskName: "task1",
34943503
},
34953504
}
3505+
childRefs := []v1beta1.ChildStatusReference{{
3506+
TypeMeta: runtime.TypeMeta{Kind: "Run"},
3507+
Name: "run-for-task1",
3508+
PipelineTaskName: "task1",
3509+
}}
34963510

34973511
for _, tc := range []struct {
34983512
name string
@@ -3527,8 +3541,16 @@ func TestGetRunName(t *testing.T) {
35273541
if tc.prName != "" {
35283542
testPrName = tc.prName
35293543
}
3530-
gotTrName := getRunName(runsStatus, tc.ptName, testPrName)
3531-
if d := cmp.Diff(tc.wantTrName, gotTrName); d != "" {
3544+
rnFromRunsStatus := getRunName(runsStatus, nil, tc.ptName, testPrName)
3545+
if d := cmp.Diff(tc.wantTrName, rnFromRunsStatus); d != "" {
3546+
t.Errorf("GetTaskRunName: %s", diff.PrintWantGot(d))
3547+
}
3548+
rnFromChildRefs := getRunName(nil, childRefs, tc.ptName, testPrName)
3549+
if d := cmp.Diff(tc.wantTrName, rnFromChildRefs); d != "" {
3550+
t.Errorf("GetTaskRunName: %s", diff.PrintWantGot(d))
3551+
}
3552+
rnFromBoth := getRunName(runsStatus, childRefs, tc.ptName, testPrName)
3553+
if d := cmp.Diff(tc.wantTrName, rnFromBoth); d != "" {
35323554
t.Errorf("GetTaskRunName: %s", diff.PrintWantGot(d))
35333555
}
35343556
})

pkg/reconciler/pipelinerun/resources/pipelinerunstate.go

+68
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"go.uber.org/zap"
2727
corev1 "k8s.io/api/core/v1"
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/apimachinery/pkg/runtime"
2930
"k8s.io/apimachinery/pkg/util/clock"
3031
"k8s.io/apimachinery/pkg/util/sets"
3132
"knative.dev/pkg/apis"
@@ -256,6 +257,73 @@ func (state PipelineRunState) GetRunsResults() map[string][]v1alpha1.RunResult {
256257
return results
257258
}
258259

260+
// GetChildReferences returns a slice of references, including version, kind, name, and pipeline task name, for all
261+
// TaskRuns and Runs in the state.
262+
func (state PipelineRunState) GetChildReferences(taskRunVersion string, runVersion string) []v1beta1.ChildStatusReference {
263+
var childRefs []v1beta1.ChildStatusReference
264+
265+
for _, rprt := range state {
266+
// If this is for a TaskRun, but there isn't yet a specified TaskRun and we haven't resolved condition checks yet,
267+
// skip this entry.
268+
if !rprt.CustomTask && rprt.TaskRun == nil && rprt.ResolvedConditionChecks == nil {
269+
continue
270+
}
271+
272+
var childAPIVersion string
273+
var childTaskKind string
274+
var childName string
275+
var childConditions []*v1beta1.PipelineRunChildConditionCheckStatus
276+
277+
if rprt.CustomTask {
278+
childName = rprt.RunName
279+
childTaskKind = "Run"
280+
281+
if rprt.Run != nil {
282+
childAPIVersion = rprt.Run.APIVersion
283+
} else {
284+
childAPIVersion = runVersion
285+
}
286+
} else {
287+
childName = rprt.TaskRunName
288+
childTaskKind = "TaskRun"
289+
290+
if rprt.TaskRun != nil {
291+
childAPIVersion = rprt.TaskRun.APIVersion
292+
} else {
293+
childAPIVersion = taskRunVersion
294+
}
295+
if len(rprt.ResolvedConditionChecks) > 0 {
296+
for _, c := range rprt.ResolvedConditionChecks {
297+
condCheck := &v1beta1.PipelineRunChildConditionCheckStatus{
298+
PipelineRunConditionCheckStatus: v1beta1.PipelineRunConditionCheckStatus{
299+
ConditionName: c.ConditionRegisterName,
300+
},
301+
ConditionCheckName: c.ConditionCheckName,
302+
}
303+
if c.ConditionCheck != nil {
304+
condCheck.Status = c.NewConditionCheckStatus()
305+
}
306+
307+
childConditions = append(childConditions, condCheck)
308+
}
309+
}
310+
}
311+
312+
childRefs = append(childRefs, v1beta1.ChildStatusReference{
313+
TypeMeta: runtime.TypeMeta{
314+
APIVersion: childAPIVersion,
315+
Kind: childTaskKind,
316+
},
317+
Name: childName,
318+
PipelineTaskName: rprt.PipelineTask.Name,
319+
WhenExpressions: rprt.PipelineTask.WhenExpressions,
320+
ConditionChecks: childConditions,
321+
})
322+
323+
}
324+
return childRefs
325+
}
326+
259327
// getNextTasks returns a list of tasks which should be executed next i.e.
260328
// a list of tasks from candidateTasks which aren't yet indicated in state to be running and
261329
// a list of cancelled/failed tasks from candidateTasks which haven't exhausted their retries

0 commit comments

Comments
 (0)