Skip to content

Commit

Permalink
[TEP-0100] Add functionality to be used in supporting minimal embedde…
Browse files Browse the repository at this point in the history
…d 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 <[email protected]>
  • Loading branch information
abayer authored and chitrangpatel committed May 3, 2022
1 parent 4cae275 commit 2421ceb
Show file tree
Hide file tree
Showing 5 changed files with 437 additions and 99 deletions.
2 changes: 1 addition & 1 deletion pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ func updatePipelineRunStatusFromTaskRuns(logger *zap.SugaredLogger, pr *v1beta1.
// status. This means that the conditions were orphaned, and never added to the
// status. In this case we need to generate a new TaskRun name, that will be used
// to run the TaskRun if the conditions are passed.
taskRunName = resources.GetTaskRunName(pr.Status.TaskRuns, pipelineTaskName, pr.Name)
taskRunName = resources.GetTaskRunName(pr.Status.TaskRuns, pr.Status.ChildReferences, pipelineTaskName, pr.Name)
pr.Status.TaskRuns[taskRunName] = &v1beta1.PipelineRunTaskRunStatus{
PipelineTaskName: pipelineTaskName,
Status: nil,
Expand Down
38 changes: 30 additions & 8 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,14 @@ func ResolvePipelineRunTask(
}
rprt.CustomTask = isCustomTask(ctx, rprt)
if rprt.IsCustomTask() {
rprt.RunName = getRunName(pipelineRun.Status.Runs, task.Name, pipelineRun.Name)
rprt.RunName = getRunName(pipelineRun.Status.Runs, pipelineRun.Status.ChildReferences, task.Name, pipelineRun.Name)
run, err := getRun(rprt.RunName)
if err != nil && !errors.IsNotFound(err) {
return nil, fmt.Errorf("error retrieving Run %s: %w", rprt.RunName, err)
}
rprt.Run = run
} else {
rprt.TaskRunName = GetTaskRunName(pipelineRun.Status.TaskRuns, task.Name, pipelineRun.Name)
rprt.TaskRunName = GetTaskRunName(pipelineRun.Status.TaskRuns, pipelineRun.Status.ChildReferences, task.Name, pipelineRun.Name)

// Find the Task that this PipelineTask is using
var (
Expand Down Expand Up @@ -560,7 +560,7 @@ func ResolvePipelineRunTask(

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

// getConditionCheckName should return a unique name for a `ConditionCheck` if one has not already been defined, and the existing one otherwise.
func getConditionCheckName(taskRunStatus map[string]*v1beta1.PipelineRunTaskRunStatus, trName, conditionRegisterName string) string {
func getConditionCheckName(taskRunStatus map[string]*v1beta1.PipelineRunTaskRunStatus, childRefs []v1beta1.ChildStatusReference, trName, conditionRegisterName string) string {
for _, cr := range childRefs {
if cr.Name == trName {
for _, cc := range cr.ConditionChecks {
if cc.ConditionName == conditionRegisterName {
return cc.ConditionCheckName
}
}
}
}
trStatus, ok := taskRunStatus[trName]
if ok && trStatus.ConditionChecks != nil {
for k, v := range trStatus.ConditionChecks {
Expand All @@ -585,7 +594,13 @@ func getConditionCheckName(taskRunStatus map[string]*v1beta1.PipelineRunTaskRunS
}

// GetTaskRunName should return a unique name for a `TaskRun` if one has not already been defined, and the existing one otherwise.
func GetTaskRunName(taskRunsStatus map[string]*v1beta1.PipelineRunTaskRunStatus, ptName, prName string) string {
func GetTaskRunName(taskRunsStatus map[string]*v1beta1.PipelineRunTaskRunStatus, childRefs []v1beta1.ChildStatusReference, ptName, prName string) string {
for _, cr := range childRefs {
if cr.Kind == "TaskRun" && cr.PipelineTaskName == ptName {
return cr.Name
}
}

for k, v := range taskRunsStatus {
if v.PipelineTaskName == ptName {
return k
Expand All @@ -597,16 +612,23 @@ func GetTaskRunName(taskRunsStatus map[string]*v1beta1.PipelineRunTaskRunStatus,

// getRunName should return a unique name for a `Run` if one has not already
// been defined, and the existing one otherwise.
func getRunName(runsStatus map[string]*v1beta1.PipelineRunRunStatus, ptName, prName string) string {
func getRunName(runsStatus map[string]*v1beta1.PipelineRunRunStatus, childRefs []v1beta1.ChildStatusReference, ptName, prName string) string {
for _, cr := range childRefs {
if cr.Kind == "Run" && cr.PipelineTaskName == ptName {
return cr.Name
}
}

for k, v := range runsStatus {
if v.PipelineTaskName == ptName {
return k
}
}

return kmeta.ChildName(prName, fmt.Sprintf("-%s", ptName))
}

func resolveConditionChecks(pt *v1beta1.PipelineTask, taskRunStatus map[string]*v1beta1.PipelineRunTaskRunStatus, taskRunName string, getTaskRun resources.GetTaskRun, getCondition GetCondition, providedResources map[string]*resourcev1alpha1.PipelineResource) ([]*ResolvedConditionCheck, error) {
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) {
rccs := []*ResolvedConditionCheck{}
for i := range pt.Conditions {
ptc := pt.Conditions[i]
Expand All @@ -619,7 +641,7 @@ func resolveConditionChecks(pt *v1beta1.PipelineTask, taskRunStatus map[string]*
Msg: err.Error(),
}
}
conditionCheckName := getConditionCheckName(taskRunStatus, taskRunName, crName)
conditionCheckName := getConditionCheckName(taskRunStatus, childRefs, taskRunName, crName)
// TODO(#3133): Also handle Custom Task Runs (getRun here)
cctr, err := getTaskRun(conditionCheckName)
if err != nil {
Expand Down
30 changes: 26 additions & 4 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3444,6 +3444,11 @@ func TestGetTaskRunName(t *testing.T) {
PipelineTaskName: "task1",
},
}
childRefs := []v1beta1.ChildStatusReference{{
TypeMeta: runtime.TypeMeta{Kind: "TaskRun"},
Name: "taskrun-for-task1",
PipelineTaskName: "task1",
}}

for _, tc := range []struct {
name string
Expand Down Expand Up @@ -3478,8 +3483,12 @@ func TestGetTaskRunName(t *testing.T) {
if tc.prName != "" {
testPrName = tc.prName
}
gotTrName := GetTaskRunName(taskRunsStatus, tc.ptName, testPrName)
if d := cmp.Diff(tc.wantTrName, gotTrName); d != "" {
trNameFromTRStatus := GetTaskRunName(taskRunsStatus, nil, tc.ptName, testPrName)
if d := cmp.Diff(tc.wantTrName, trNameFromTRStatus); d != "" {
t.Errorf("GetTaskRunName: %s", diff.PrintWantGot(d))
}
trNameFromChildRefs := GetTaskRunName(nil, childRefs, tc.ptName, testPrName)
if d := cmp.Diff(tc.wantTrName, trNameFromChildRefs); d != "" {
t.Errorf("GetTaskRunName: %s", diff.PrintWantGot(d))
}
})
Expand All @@ -3493,6 +3502,11 @@ func TestGetRunName(t *testing.T) {
PipelineTaskName: "task1",
},
}
childRefs := []v1beta1.ChildStatusReference{{
TypeMeta: runtime.TypeMeta{Kind: "Run"},
Name: "run-for-task1",
PipelineTaskName: "task1",
}}

for _, tc := range []struct {
name string
Expand Down Expand Up @@ -3527,8 +3541,16 @@ func TestGetRunName(t *testing.T) {
if tc.prName != "" {
testPrName = tc.prName
}
gotTrName := getRunName(runsStatus, tc.ptName, testPrName)
if d := cmp.Diff(tc.wantTrName, gotTrName); d != "" {
rnFromRunsStatus := getRunName(runsStatus, nil, tc.ptName, testPrName)
if d := cmp.Diff(tc.wantTrName, rnFromRunsStatus); d != "" {
t.Errorf("GetTaskRunName: %s", diff.PrintWantGot(d))
}
rnFromChildRefs := getRunName(nil, childRefs, tc.ptName, testPrName)
if d := cmp.Diff(tc.wantTrName, rnFromChildRefs); d != "" {
t.Errorf("GetTaskRunName: %s", diff.PrintWantGot(d))
}
rnFromBoth := getRunName(runsStatus, childRefs, tc.ptName, testPrName)
if d := cmp.Diff(tc.wantTrName, rnFromBoth); d != "" {
t.Errorf("GetTaskRunName: %s", diff.PrintWantGot(d))
}
})
Expand Down
68 changes: 68 additions & 0 deletions pkg/reconciler/pipelinerun/resources/pipelinerunstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/pkg/apis"
Expand Down Expand Up @@ -256,6 +257,73 @@ func (state PipelineRunState) GetRunsResults() map[string][]v1alpha1.RunResult {
return results
}

// GetChildReferences returns a slice of references, including version, kind, name, and pipeline task name, for all
// TaskRuns and Runs in the state.
func (state PipelineRunState) GetChildReferences(taskRunVersion string, runVersion string) []v1beta1.ChildStatusReference {
var childRefs []v1beta1.ChildStatusReference

for _, rprt := range state {
// If this is for a TaskRun, but there isn't yet a specified TaskRun and we haven't resolved condition checks yet,
// skip this entry.
if !rprt.CustomTask && rprt.TaskRun == nil && rprt.ResolvedConditionChecks == nil {
continue
}

var childAPIVersion string
var childTaskKind string
var childName string
var childConditions []*v1beta1.PipelineRunChildConditionCheckStatus

if rprt.CustomTask {
childName = rprt.RunName
childTaskKind = "Run"

if rprt.Run != nil {
childAPIVersion = rprt.Run.APIVersion
} else {
childAPIVersion = runVersion
}
} else {
childName = rprt.TaskRunName
childTaskKind = "TaskRun"

if rprt.TaskRun != nil {
childAPIVersion = rprt.TaskRun.APIVersion
} else {
childAPIVersion = taskRunVersion
}
if len(rprt.ResolvedConditionChecks) > 0 {
for _, c := range rprt.ResolvedConditionChecks {
condCheck := &v1beta1.PipelineRunChildConditionCheckStatus{
PipelineRunConditionCheckStatus: v1beta1.PipelineRunConditionCheckStatus{
ConditionName: c.ConditionRegisterName,
},
ConditionCheckName: c.ConditionCheckName,
}
if c.ConditionCheck != nil {
condCheck.Status = c.NewConditionCheckStatus()
}

childConditions = append(childConditions, condCheck)
}
}
}

childRefs = append(childRefs, v1beta1.ChildStatusReference{
TypeMeta: runtime.TypeMeta{
APIVersion: childAPIVersion,
Kind: childTaskKind,
},
Name: childName,
PipelineTaskName: rprt.PipelineTask.Name,
WhenExpressions: rprt.PipelineTask.WhenExpressions,
ConditionChecks: childConditions,
})

}
return childRefs
}

// getNextTasks returns a list of tasks which should be executed next i.e.
// a list of tasks from candidateTasks which aren't yet indicated in state to be running and
// a list of cancelled/failed tasks from candidateTasks which haven't exhausted their retries
Expand Down
Loading

0 comments on commit 2421ceb

Please sign in to comment.