Skip to content

Commit f747ba8

Browse files
twoGiantstekton-robot
authored andcommitted
refactor: improve names and fix typos
While working on TEP-0056 small sets of typo fixes and minor refactorings are extracted as small separated PRs to reduce the cognitive load for the actual TEP-0056 code changes in following PRs. Issues #8760, #7166. Signed-off-by: Stanislav Jakuschevskij <[email protected]>
1 parent fcc364d commit f747ba8

File tree

9 files changed

+105
-61
lines changed

9 files changed

+105
-61
lines changed

pkg/reconciler/pipelinerun/pipelinerun.go

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -332,29 +332,49 @@ func (c *Reconciler) finishReconcileUpdateEmitEvents(ctx context.Context, pr *v1
332332
return errs
333333
}
334334

335-
// resolvePipelineState will attempt to resolve each referenced task in the pipeline's spec and all of the resources
335+
// resolvePipelineState will attempt to resolve each referenced pipeline task in the pipeline's spec and all of the resources
336336
// specified by those tasks.
337337
func (c *Reconciler) resolvePipelineState(
338338
ctx context.Context,
339-
tasks []v1.PipelineTask,
339+
pipelineTasks []v1.PipelineTask,
340340
pipelineMeta *metav1.ObjectMeta,
341341
pr *v1.PipelineRun,
342342
pst resources.PipelineRunState,
343343
) (resources.PipelineRunState, error) {
344344
ctx, span := c.tracerProvider.Tracer(TracerName).Start(ctx, "resolvePipelineState")
345345
defer span.End()
346-
// Resolve each task individually because they each could have a different reference context (remote or local).
347-
for _, task := range tasks {
346+
// Resolve each pipeline task individually because they each could have a different reference context (remote or local).
347+
for _, pipelineTask := range pipelineTasks {
348348
// We need the TaskRun name to ensure that we don't perform an additional remote resolution request for a PipelineTask
349349
// in the TaskRun reconciler.
350-
trName := resources.GetTaskRunName(pr.Status.ChildReferences, task.Name, pr.Name)
350+
trName := resources.GetTaskRunName(
351+
pr.Status.ChildReferences,
352+
pipelineTask.Name,
353+
pr.Name,
354+
)
351355

352356
// list VerificationPolicies for trusted resources
353357
vp, err := c.verificationPolicyLister.VerificationPolicies(pr.Namespace).List(labels.Everything())
354358
if err != nil {
355359
return nil, fmt.Errorf("failed to list VerificationPolicies from namespace %s with error %w", pr.Namespace, err)
356360
}
357-
fn := tresources.GetTaskFunc(ctx, c.KubeClientSet, c.PipelineClientSet, c.resolutionRequester, pr, task.TaskRef, trName, pr.Namespace, pr.Spec.TaskRunTemplate.ServiceAccountName, vp)
361+
362+
getTaskFunc := tresources.GetTaskFunc(
363+
ctx,
364+
c.KubeClientSet,
365+
c.PipelineClientSet,
366+
c.resolutionRequester,
367+
pr,
368+
pipelineTask.TaskRef,
369+
trName,
370+
pr.Namespace,
371+
pr.Spec.TaskRunTemplate.ServiceAccountName,
372+
vp,
373+
)
374+
375+
getTaskRunFunc := func(name string) (*v1.TaskRun, error) {
376+
return c.taskRunLister.TaskRuns(pr.Namespace).Get(name)
377+
}
358378

359379
getCustomRunFunc := func(name string) (*v1beta1.CustomRun, error) {
360380
r, err := c.customRunLister.CustomRuns(pr.Namespace).Get(name)
@@ -366,12 +386,10 @@ func (c *Reconciler) resolvePipelineState(
366386

367387
resolvedTask, err := resources.ResolvePipelineTask(ctx,
368388
*pr,
369-
fn,
370-
func(name string) (*v1.TaskRun, error) {
371-
return c.taskRunLister.TaskRuns(pr.Namespace).Get(name)
372-
},
389+
getTaskFunc,
390+
getTaskRunFunc,
373391
getCustomRunFunc,
374-
task,
392+
pipelineTask,
375393
pst,
376394
)
377395
if err != nil {
@@ -393,8 +411,9 @@ func (c *Reconciler) resolvePipelineState(
393411
}
394412
return nil, controller.NewPermanentError(err)
395413
}
414+
396415
if resolvedTask.ResolvedTask != nil && resolvedTask.ResolvedTask.VerificationResult != nil {
397-
cond, err := conditionFromVerificationResult(resolvedTask.ResolvedTask.VerificationResult, pr, task.Name)
416+
cond, err := conditionFromVerificationResult(resolvedTask.ResolvedTask.VerificationResult, pr, pipelineTask.Name)
398417
pr.Status.SetCondition(cond)
399418
if err != nil {
400419
pr.Status.MarkFailed(v1.PipelineRunReasonResourceVerificationFailed.String(), err.Error())
@@ -403,6 +422,7 @@ func (c *Reconciler) resolvePipelineState(
403422
}
404423
pst = append(pst, resolvedTask)
405424
}
425+
406426
return pst, nil
407427
}
408428

@@ -549,7 +569,7 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1.PipelineRun, getPipel
549569
}
550570

551571
resources.ApplyParametersToWorkspaceBindings(ctx, pr)
552-
// Make a deep copy of the Pipeline and its Tasks before value substution.
572+
// Make a deep copy of the Pipeline and its Tasks before value substitution.
553573
// This is used to find referenced pipeline-level params at each PipelineTask when validate param enum subset requirement
554574
originalPipeline := pipelineSpec.DeepCopy()
555575
originalTasks := originalPipeline.Tasks
@@ -571,9 +591,9 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1.PipelineRun, getPipel
571591
return controller.NewPermanentError(err)
572592
}
573593

574-
// pipelineState holds a list of pipeline tasks after fetching their resolved Task specs.
575-
// pipelineState also holds a taskRun for each pipeline task after the taskRun is created
576-
// pipelineState is instantiated and updated on every reconcile cycle
594+
// pipelineRunState holds a list of pipeline tasks after fetching their resolved Task specs.
595+
// pipelineRunState also holds a taskRun for each pipeline task after the taskRun is created
596+
// pipelineRunState is instantiated and updated on every reconcile cycle
577597
// Resolve the set of tasks (and possibly task runs).
578598
tasks := pipelineSpec.Tasks
579599
if len(pipelineSpec.Finally) > 0 {
@@ -584,7 +604,7 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1.PipelineRun, getPipel
584604
// - those with a completed (Task|Custom)Run reference (i.e. those that finished running)
585605
// - those without a (Task|Custom)Run reference
586606
// We resolve the status for the former first, to collect all results available at this stage
587-
// We know that tasks in progress or completed have had their fan-out alteady calculated so
607+
// We know that tasks in progress or completed have had their fan-out already calculated so
588608
// they can be safely processed in the first iteration. The underlying assumption is that if
589609
// a PipelineTask has at least one TaskRun associated, then all its TaskRuns have been
590610
// created already.
@@ -604,9 +624,9 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1.PipelineRun, getPipel
604624
notStartedTasks = append(notStartedTasks, task)
605625
}
606626
}
627+
607628
// First iteration
608-
pst := resources.PipelineRunState{}
609-
pipelineRunState, err := c.resolvePipelineState(ctx, ranOrRunningTasks, pipelineMeta.ObjectMeta, pr, pst)
629+
pipelineRunState, err := c.resolvePipelineState(ctx, ranOrRunningTasks, pipelineMeta.ObjectMeta, pr, resources.PipelineRunState{})
610630
switch {
611631
case errors.Is(err, remote.ErrRequestInProgress):
612632
message := fmt.Sprintf("PipelineRun %s/%s awaiting remote resource", pr.Namespace, pr.Name)
@@ -692,7 +712,7 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1.PipelineRun, getPipel
692712
}
693713
}
694714

695-
// check if pipeline run is not gracefully cancelled and there are active task runs, which require cancelling
715+
// check if pipeline run is gracefully cancelled and there are active pipeline task runs, which require cancelling
696716
if pr.IsGracefullyCancelled() && pipelineRunFacts.IsRunning() {
697717
// If the pipelinerun is cancelled, cancel tasks, but run finally
698718
err := gracefullyCancelPipelineRun(ctx, logger, pr, c.PipelineClientSet)
@@ -781,6 +801,7 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1.PipelineRun, getPipel
781801
}
782802
}
783803
}
804+
784805
if err := c.runNextSchedulableTask(ctx, pr, pipelineRunFacts); err != nil {
785806
return err
786807
}
@@ -811,14 +832,18 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1.PipelineRun, getPipel
811832
pr.Status.ChildReferences = pipelineRunFacts.GetChildReferences()
812833

813834
pr.Status.SkippedTasks = pipelineRunFacts.GetSkippedTasks()
814-
815-
taskStatus := pipelineRunFacts.GetPipelineTaskStatus()
816-
finalTaskStatus := pipelineRunFacts.GetPipelineFinalTaskStatus()
817-
taskStatus = kmap.Union(taskStatus, finalTaskStatus)
835+
pipelineTaskStatus := pipelineRunFacts.GetPipelineTaskStatus()
836+
finalPipelineTaskStatus := pipelineRunFacts.GetPipelineFinalTaskStatus()
837+
pipelineTaskStatus = kmap.Union(pipelineTaskStatus, finalPipelineTaskStatus)
818838

819839
if after.Status == corev1.ConditionTrue || after.Status == corev1.ConditionFalse {
820-
pr.Status.Results, err = resources.ApplyTaskResultsToPipelineResults(ctx, pipelineSpec.Results,
821-
pipelineRunFacts.State.GetTaskRunsResults(), pipelineRunFacts.State.GetRunsResults(), taskStatus)
840+
pr.Status.Results, err = resources.ApplyTaskResultsToPipelineResults(
841+
ctx,
842+
pipelineSpec.Results,
843+
pipelineRunFacts.State.GetTaskRunsResults(),
844+
pipelineRunFacts.State.GetRunsResults(),
845+
pipelineTaskStatus,
846+
)
822847
if err != nil {
823848
pr.Status.MarkFailed(v1.PipelineRunReasonCouldntGetPipelineResult.String(),
824849
"Failed to get PipelineResult from TaskRun Results for PipelineRun %s: %s",
@@ -944,6 +969,7 @@ func (c *Reconciler) runNextSchedulableTask(ctx context.Context, pr *v1.Pipeline
944969
}
945970
}
946971
}
972+
947973
return nil
948974
}
949975

@@ -960,11 +986,12 @@ func (c *Reconciler) setFinallyStartedTimeIfNeeded(pr *v1.PipelineRun, facts *re
960986
func (c *Reconciler) createTaskRuns(ctx context.Context, rpt *resources.ResolvedPipelineTask, pr *v1.PipelineRun, facts *resources.PipelineRunFacts) ([]*v1.TaskRun, error) {
961987
ctx, span := c.tracerProvider.Tracer(TracerName).Start(ctx, "createTaskRuns")
962988
defer span.End()
963-
var taskRuns []*v1.TaskRun
989+
964990
var matrixCombinations []v1.Params
965991
if rpt.PipelineTask.IsMatrixed() {
966992
matrixCombinations = rpt.PipelineTask.Matrix.FanOut()
967993
}
994+
968995
// validate the param values meet resolved Task Param Enum requirements before creating TaskRuns
969996
if config.FromContextOrDefaults(ctx).FeatureFlags.EnableParamEnum {
970997
for i := range rpt.TaskRunNames {
@@ -981,6 +1008,8 @@ func (c *Reconciler) createTaskRuns(ctx context.Context, rpt *resources.Resolved
9811008
}
9821009
}
9831010
}
1011+
1012+
var taskRuns []*v1.TaskRun
9841013
for i, taskRunName := range rpt.TaskRunNames {
9851014
var params v1.Params
9861015
if len(matrixCombinations) > i {
@@ -993,6 +1022,7 @@ func (c *Reconciler) createTaskRuns(ctx context.Context, rpt *resources.Resolved
9931022
}
9941023
taskRuns = append(taskRuns, taskRun)
9951024
}
1025+
9961026
return taskRuns, nil
9971027
}
9981028

pkg/reconciler/pipelinerun/pipelinerun_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ func TestReconcile_InvalidPipelineRunNames(t *testing.T) {
13601360
func TestReconcileOnCompletedPipelineRun(t *testing.T) {
13611361
// TestReconcileOnCompletedPipelineRun runs "Reconcile" on a PipelineRun that already reached completion
13621362
// and that does not have the latest status from TaskRuns yet. It checks that the TaskRun status is updated
1363-
// in the PipelineRun status, that the completion status is not altered, that not error is returned and
1363+
// in the PipelineRun status, that the completion status is not altered, that no error is returned and
13641364
// a successful event is triggered
13651365
namespace := "foo"
13661366
taskRunName := "test-pipeline-run-completed-hello-world-task-run"

pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,16 @@ func (e *TaskNotFoundError) Error() string {
6565
type ResolvedPipelineTask struct {
6666
TaskRunNames []string
6767
TaskRuns []*v1.TaskRun
68+
ResolvedTask *resources.ResolvedTask
69+
6870
// If the PipelineTask is a Custom Task, CustomRunName and CustomRun will be set.
6971
CustomTask bool
7072
CustomRunNames []string
7173
CustomRuns []*v1beta1.CustomRun
72-
PipelineTask *v1.PipelineTask
73-
ResolvedTask *resources.ResolvedTask
74-
ResultsCache map[string][]string
74+
75+
PipelineTask *v1.PipelineTask
76+
ResultsCache map[string][]string
77+
7578
// EvaluatedCEL is used to store the results of evaluated CEL expression
7679
EvaluatedCEL map[string]bool
7780
}
@@ -95,7 +98,7 @@ func (t *ResolvedPipelineTask) EvaluateCEL() error {
9598
if iss.Err() != nil {
9699
return iss.Err()
97100
}
98-
// Generate an evaluable instance of the Ast within the environment
101+
// Generate an evaluatable instance of the Ast within the environment
99102
prg, err := env.Program(ast)
100103
if err != nil {
101104
return err
@@ -156,6 +159,7 @@ func (t ResolvedPipelineTask) getReason() string {
156159
return t.CustomRuns[0].Status.Conditions[0].Reason
157160
}
158161
}
162+
159163
if len(t.TaskRuns) == 0 {
160164
return ""
161165
}
@@ -167,6 +171,7 @@ func (t ResolvedPipelineTask) getReason() string {
167171
if len(t.TaskRuns) >= 1 && len(t.TaskRuns[0].Status.Conditions) >= 1 {
168172
return t.TaskRuns[0].Status.Conditions[0].Reason
169173
}
174+
170175
return ""
171176
}
172177

@@ -184,6 +189,7 @@ func (t ResolvedPipelineTask) isSuccessful() bool {
184189
}
185190
return true
186191
}
192+
187193
if len(t.TaskRuns) == 0 {
188194
return false
189195
}
@@ -309,10 +315,11 @@ func (t ResolvedPipelineTask) haveAnyRunsFailed() bool {
309315
if t.IsCustomTask() {
310316
return t.haveAnyCustomRunsFailed()
311317
}
318+
312319
return t.haveAnyTaskRunsFailed()
313320
}
314321

315-
// haveAnyTaskRunsFailed returns true when any of the taskRuns have succeeded condition with status set to false
322+
// haveAnyTaskRunsFailed returns true when any of the TaskRuns have succeeded condition with status set to false
316323
func (t ResolvedPipelineTask) haveAnyTaskRunsFailed() bool {
317324
for _, taskRun := range t.TaskRuns {
318325
if taskRun.IsFailure() {
@@ -322,7 +329,7 @@ func (t ResolvedPipelineTask) haveAnyTaskRunsFailed() bool {
322329
return false
323330
}
324331

325-
// haveAnyCustomRunsFailed returns true when a CustomRun has succeeded condition with status set to false
332+
// haveAnyCustomRunsFailed returns true when any of the CustomRuns have succeeded condition with status set to false
326333
func (t ResolvedPipelineTask) haveAnyCustomRunsFailed() bool {
327334
for _, customRun := range t.CustomRuns {
328335
if customRun.IsFailure() {
@@ -633,6 +640,7 @@ func ResolvePipelineTask(
633640
}
634641
}
635642
}
643+
636644
return &rpt, nil
637645
}
638646

@@ -748,27 +756,27 @@ func getTaskRunNamesFromChildRefs(childRefs []v1.ChildStatusReference, ptName st
748756
}
749757

750758
func getNewRunNames(ptName, prName string, numberOfRuns int) []string {
751-
var taskRunNames []string
759+
var runNames []string
752760
// If it is a singular TaskRun/CustomRun, we only append the ptName
753761
if numberOfRuns == 1 {
754-
taskRunName := kmeta.ChildName(prName, "-"+ptName)
755-
return append(taskRunNames, taskRunName)
762+
runName := kmeta.ChildName(prName, "-"+ptName)
763+
return append(runNames, runName)
756764
}
757-
// For a matrix we append i to then end of the fanned out TaskRuns "matrixed-pr-taskrun-0"
765+
// For a matrix we append i to the end of the fanned out TaskRuns/CustomRun "matrixed-pr-taskrun-0"
758766
for i := range numberOfRuns {
759-
taskRunName := kmeta.ChildName(prName, fmt.Sprintf("-%s-%d", ptName, i))
767+
runName := kmeta.ChildName(prName, fmt.Sprintf("-%s-%d", ptName, i))
760768
// check if the taskRun name ends with a matrix instance count
761-
if !strings.HasSuffix(taskRunName, fmt.Sprintf("-%d", i)) {
762-
taskRunName = kmeta.ChildName(prName, "-"+ptName)
769+
if !strings.HasSuffix(runName, fmt.Sprintf("-%d", i)) {
770+
runName = kmeta.ChildName(prName, "-"+ptName)
763771
// kmeta.ChildName limits the size of a name to max of 63 characters based on k8s guidelines
764-
// truncate the name such that "-<matrix-id>" can be appended to the taskRun name
772+
// truncate the name such that "-<matrix-id>" can be appended to the TaskRun/CustomRun name
765773
longest := 63 - len(fmt.Sprintf("-%d", numberOfRuns))
766-
taskRunName = taskRunName[0:longest]
767-
taskRunName = fmt.Sprintf("%s-%d", taskRunName, i)
774+
runName = runName[0:longest]
775+
runName = fmt.Sprintf("%s-%d", runName, i)
768776
}
769-
taskRunNames = append(taskRunNames, taskRunName)
777+
runNames = append(runNames, runName)
770778
}
771-
return taskRunNames
779+
return runNames
772780
}
773781

774782
// getCustomRunName should return a unique name for a `Run` if one has not already
@@ -866,7 +874,7 @@ func CheckMissingResultReferences(pipelineRunState PipelineRunState, target *Res
866874
}
867875

868876
// createResultsCacheMatrixedTaskRuns creates a cache of results that have been fanned out from a
869-
// referenced matrixed PipelintTask so that you can easily access these results in subsequent Pipeline Tasks
877+
// referenced matrixed PipelineTask so that you can easily access these results in subsequent Pipeline Tasks
870878
func createResultsCacheMatrixedTaskRuns(rpt *ResolvedPipelineTask) (resultsCache map[string][]string) {
871879
if len(rpt.ResultsCache) == 0 {
872880
resultsCache = make(map[string][]string)

0 commit comments

Comments
 (0)