Skip to content

Commit

Permalink
Sync the pipelinerun status from the informers
Browse files Browse the repository at this point in the history
When we reconcile a pipelinerun, we should ensure that the
pipelinerun status is always in sync with the actual list of taskruns
that can be provided by the taskrun informer.

The only way to filter taskruns is by labels tekton.dev/pipelinerun.
In case an orphaned taskrun is found, we can use the other labels
on the taskrun to reconstruct the missing entry in the pipelinerun
status, whether it's a missing taskrun or a missing condition check.
  • Loading branch information
afrittoli committed May 14, 2020
1 parent 1fbac2a commit 3dbde91
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 9 deletions.
89 changes: 89 additions & 0 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"

Expand All @@ -45,6 +46,7 @@ import (
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/apis"
"knative.dev/pkg/configmap"
Expand Down Expand Up @@ -204,6 +206,14 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
return err
}

// Make sure that the PipelineRun status is in sync with the actual TaskRuns
err = c.updatePipelineRunStatusFromInformer(pr)
if err != nil {
// This should not fail. Return the error so we can re-try later.
c.Logger.Errorf("Error while syncing the pipelinerun status: %v", err.Error())
return err
}

// Reconcile this copy of the pipelinerun and then write back any status or label
// updates regardless of whether the reconciliation errored out.
if err = c.reconcile(ctx, pr); err != nil {
Expand Down Expand Up @@ -935,3 +945,82 @@ func storePipelineSpec(ctx context.Context, pr *v1beta1.PipelineRun, ps *v1beta1
}
return nil
}

func (c *Reconciler) updatePipelineRunStatusFromInformer(pr *v1beta1.PipelineRun) error {
pipelineRunLabels := getTaskrunLabels(pr, "")
taskRuns, err := c.taskRunLister.TaskRuns(pr.Namespace).List(labels.SelectorFromSet(pipelineRunLabels))
if err != nil {
c.Logger.Errorf("Could not list TaskRuns %#v", err)
return err
}
// Store a list of Condition TaskRuns for each PipelineTask (by name)
conditionTaskRuns := make(map[string][]*v1beta1.TaskRun)
// Map PipelineTask names to TaskRun names that were already in the status
taskRunByPipelineTask := make(map[string]string)
// First loop over all the TaskRuns associated to Tasks
for _, taskrun := range taskRuns {
lbls := taskrun.GetLabels()
pipelineTaskName := lbls[pipeline.GroupName+pipeline.PipelineTaskLabelKey]
if _, ok := lbls[pipeline.GroupName+pipeline.ConditionCheckKey]; ok {
// Save condition for looping over them after this
if _, ok := conditionTaskRuns[pipelineTaskName]; !ok {
// If it's the first condition taskrun, initialise the slice
conditionTaskRuns[pipelineTaskName] = []*v1beta1.TaskRun{}
}
conditionTaskRuns[pipelineTaskName] = append(conditionTaskRuns[pipelineTaskName], taskrun)
continue
}
// Map pipeline task to taskrun name
taskRunByPipelineTask[pipelineTaskName] = taskrun.Name
if _, ok := pr.Status.TaskRuns[taskrun.Name]; !ok {
// This taskrun was missing from the status.
// Add it without conditions, which are handled in the next loop
pr.Status.TaskRuns[taskrun.Name] = &v1beta1.PipelineRunTaskRunStatus{
PipelineTaskName: pipelineTaskName,
Status: &taskrun.Status,
ConditionChecks: nil,
}
}
}
// Then loop by pipelinetask name over all the TaskRuns associated to Conditions
for pipelineTaskName, actualConditionTaskRuns := range conditionTaskRuns {
taskRunName, ok := taskRunByPipelineTask[pipelineTaskName]
if !ok {
// The pipelineTask associated to the conditions was not found in the pipelinerun
// 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)
pr.Status.TaskRuns[taskRunName] = &v1beta1.PipelineRunTaskRunStatus{
PipelineTaskName: pipelineTaskName,
Status: nil,
ConditionChecks: nil,
}
}
// Build the map of condition checks for the taskrun
// If there were no other condition, initialise the map
conditionChecks := pr.Status.TaskRuns[taskRunName].ConditionChecks
if conditionChecks == nil {
conditionChecks = make(map[string]*v1beta1.PipelineRunConditionCheckStatus)
}
for i, foundTaskRun := range actualConditionTaskRuns {
lbls := foundTaskRun.GetLabels()
if _, ok := conditionChecks[foundTaskRun.Name]; !ok {
// The condition check was not found, so we need to add it
// We only add the condition name, the status can now be gathered by the
// normal reconcile process
if conditionName, ok := lbls[pipeline.GroupName+pipeline.ConditionNameKey]; ok {
conditionChecks[foundTaskRun.Name] = &v1beta1.PipelineRunConditionCheckStatus{
ConditionName: fmt.Sprintf("%s-%s", conditionName, strconv.Itoa(i)),
}
} else {
// The condition name label is missing, so we cannot recover this
c.Logger.Warnf("found an orphaned condition taskrun %#v with missing %s label",
foundTaskRun, pipeline.ConditionNameKey)
}
}
}
pr.Status.TaskRuns[taskRunName].ConditionChecks = conditionChecks
}
return nil
}
Loading

0 comments on commit 3dbde91

Please sign in to comment.