Skip to content

Commit

Permalink
Refactor ResolvePipelineRunTask
Browse files Browse the repository at this point in the history
Prior to this commit, the logic for resolving a `Task` needed for a
given `PipelineTask` was included within the `ResolvePipelineRunTask`
function.

In this change, we separate that logic into its own function for
reuse and readability. No functional changes made in this commit.
  • Loading branch information
jerop committed Jun 3, 2022
1 parent 2f83789 commit c83104c
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,15 +490,6 @@ func ResolvePipelineRunTask(
} else {
rprt.TaskRunName = GetTaskRunName(pipelineRun.Status.TaskRuns, pipelineRun.Status.ChildReferences, task.Name, pipelineRun.Name)

// Find the Task that this PipelineTask is using
var (
t v1beta1.TaskObject
err error
spec v1beta1.TaskSpec
taskName string
kind v1beta1.TaskKind
)

taskRun, err := getTaskRun(rprt.TaskRunName)
if err != nil {
if !kerrors.IsNotFound(err) {
Expand All @@ -509,30 +500,11 @@ func ResolvePipelineRunTask(
rprt.TaskRun = taskRun
}

if task.TaskRef != nil {
// If the TaskRun has already a store TaskSpec in its status, use it as source of truth
if taskRun != nil && taskRun.Status.TaskSpec != nil {
spec = *taskRun.Status.TaskSpec
taskName = task.TaskRef.Name
} else {
t, err = getTask(ctx, task.TaskRef.Name)
switch {
case errors.Is(err, remote.ErrorRequestInProgress):
return nil, err
case err != nil:
return nil, &TaskNotFoundError{
Name: task.TaskRef.Name,
Msg: err.Error(),
}
default:
spec = t.TaskSpec()
taskName = t.TaskMetadata().Name
}
}
kind = task.TaskRef.Kind
} else {
spec = task.TaskSpec.TaskSpec
spec, taskName, kind, err := resolveTask(ctx, taskRun, getTask, task)
if err != nil {
return nil, err
}

spec.SetDefaults(ctx)
rtr, err := resolvePipelineTaskResources(task, &spec, taskName, kind, providedResources)
if err != nil {
Expand All @@ -553,6 +525,42 @@ func ResolvePipelineRunTask(
return &rprt, nil
}

func resolveTask(ctx context.Context, taskRun *v1beta1.TaskRun, getTask resources.GetTask, task v1beta1.PipelineTask) (v1beta1.TaskSpec, string, v1beta1.TaskKind, error) {
// Find the Task that this PipelineTask is using
var (
t v1beta1.TaskObject
err error
spec v1beta1.TaskSpec
taskName string
kind v1beta1.TaskKind
)
if task.TaskRef != nil {
// If the TaskRun has already a store TaskSpec in its status, use it as source of truth
if taskRun != nil && taskRun.Status.TaskSpec != nil {
spec = *taskRun.Status.TaskSpec
taskName = task.TaskRef.Name
} else {
t, err = getTask(ctx, task.TaskRef.Name)
switch {
case errors.Is(err, remote.ErrorRequestInProgress):
return v1beta1.TaskSpec{}, "", "", err
case err != nil:
return v1beta1.TaskSpec{}, "", "", &TaskNotFoundError{
Name: task.TaskRef.Name,
Msg: err.Error(),
}
default:
spec = t.TaskSpec()
taskName = t.TaskMetadata().Name
}
}
kind = task.TaskRef.Kind
} else {
spec = task.TaskSpec.TaskSpec
}
return spec, taskName, kind, err
}

// 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, childRefs []v1beta1.ChildStatusReference, trName, conditionRegisterName string) string {
for _, cr := range childRefs {
Expand Down

0 comments on commit c83104c

Please sign in to comment.