Skip to content

Commit

Permalink
Correct and optimize reconciliation on deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaSchwarze0 committed Feb 25, 2021
1 parent f5ab6f5 commit 956be45
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pkg/reconciler/build/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func add(ctx context.Context, mgr manager.Manager, r reconcile.Reconciler) error
return e.MetaOld.GetGeneration() != e.MetaNew.GetGeneration() || buildRunDeletionAnnotation
},
DeleteFunc: func(e event.DeleteEvent) bool {
// Evaluates to false if the object has been confirmed deleted.
return !e.DeleteStateUnknown
// Never reconcile on deletion, there is nothing we have to do
return false
},
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/reconciler/buildrun/buildrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (r *ReconcileBuildRun) GetBuildRunObject(ctx context.Context, objectName st
}

// VerifyRequestName parse a Reconcile request name and looks for an associated BuildRun name
// If the BuildRun object exists, it will update it with an error.
// If the BuildRun object exists and is not yet completed, it will update it with an error.
func (r *ReconcileBuildRun) VerifyRequestName(ctx context.Context, request reconcile.Request, buildRun *buildv1alpha1.BuildRun) {

regxBuildRun, _ := regexp.Compile(generatedNameRegex)
Expand All @@ -319,11 +319,11 @@ func (r *ReconcileBuildRun) VerifyRequestName(ctx context.Context, request recon
if split := regxBuildRun.Split(request.Name, 2); len(split) > 0 {
// Update the related BuildRun
err := r.GetBuildRunObject(ctx, split[0], request.Namespace, buildRun)
if err == nil {
if err == nil && buildRun.Status.CompletionTime == nil {
// We ignore the errors from the following call, because the parent call of this function will always
// return back a reconcile.Result{}, nil. This is done to avoid infinite reconcile loops when a BuildRun
// does not longer exists
r.updateBuildRunErrorStatus(ctx, buildRun, fmt.Sprintf("taskRun %s doesn´t exist", request.Name))
r.updateBuildRunErrorStatus(ctx, buildRun, fmt.Sprintf("taskRun %s doesn't exist", request.Name))
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/reconciler/buildrun/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func add(ctx context.Context, mgr manager.Manager, r reconcile.Reconciler) error
return e.MetaOld.GetGeneration() != e.MetaNew.GetGeneration()
},
DeleteFunc: func(e event.DeleteEvent) bool {
// Evaluates to false if the object has been confirmed deleted.
return !e.DeleteStateUnknown
// Never reconcile on deletion, there is nothing we have to do
return false
},
}

Expand All @@ -90,8 +90,10 @@ func add(ctx context.Context, mgr manager.Manager, r reconcile.Reconciler) error
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
// Evaluates to false if the object has been confirmed deleted.
return !e.DeleteStateUnknown
o := e.Object.(*v1beta1.TaskRun)

// If the TaskRun was deleted before completion, then we reconcile to update the BuildRun to a Failed status
return o.Status.CompletionTime == nil
},
}

Expand Down
58 changes: 58 additions & 0 deletions test/integration/buildruns_to_taskruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,62 @@ var _ = Describe("Integration tests BuildRuns and TaskRuns", func() {
Expect(err).To(BeNil(), fmt.Sprintf("failed to get desired reason; expected %s, got %s", expectedReason, actualReason))
})
})

Context("when a buildrun is created and the taskrun deleted before completion", func() {

BeforeEach(func() {
buildSample = []byte(test.BuildCBSMinimal)
buildRunSample = []byte(test.MinimalBuildRun)
})

It("should reflect a Failed reason", func() {

Expect(tb.CreateBuild(buildObject)).To(BeNil())

buildObject, err = tb.GetBuildTillValidation(buildObject.Name)
Expect(err).To(BeNil())

Expect(tb.CreateBR(buildRunObject)).To(BeNil())

_, err = tb.GetBRTillStartTime(buildRunObject.Name)
Expect(err).To(BeNil())

tr, err := tb.GetTaskRunFromBuildRun(buildRunObject.Name)
Expect(err).To(BeNil())

tb.DeleteTR(tr.Name)

expectedReason := fmt.Sprintf("taskRun %s doesn't exist", tr.Name)
actualReason, err := tb.GetBRTillDesiredReason(buildRunObject.Name, expectedReason)
Expect(err).To(BeNil(), fmt.Sprintf("failed to get desired reason; expected %s, got %s", expectedReason, actualReason))
})
})

Context("when a buildrun is created and the taskrun deleted after successful completion", func() {

It("should reflect a Success reason", func() {
WithCustomClusterBuildStrategy([]byte(test.ClusterBuildStrategyNoOp), func() {
_, _, buildRunObject := setupBuildAndBuildRun([]byte(test.BuildCBSMinimal), []byte(test.MinimalBuildRun), STRATEGY+tb.Namespace+"custom")

_, err = tb.GetBRTillCompletion(buildRunObject.Name)
Expect(err).To(BeNil())

reason, err := tb.GetBRReason(buildRunObject.Name)
Expect(err).To(BeNil())
Expect(reason).To(Equal("Succeeded"))

tr, err := tb.GetTaskRunFromBuildRun(buildRunObject.Name)
Expect(err).To(BeNil())
Expect(err).To(BeNil())

tb.DeleteTR(tr.Name)

// in a test case, it is hard to verify that something (marking the BuildRun failed is not happening)
time.Sleep(5 * time.Second)

reason, err = tb.GetBRReason(buildRunObject.Name)
Expect(reason).To(Equal("Succeeded"))
})
})
})
})
11 changes: 11 additions & 0 deletions test/integration/utils/buildruns.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,14 @@ func (t *TestBuild) GetBRTillDeletion(name string) (bool, error) {

return true, nil
}

// DeleteTR deletes a TaskRun from a desired namespace
func (t *TestBuild) DeleteTR(name string) error {
trInterface := t.PipelineClientSet.TektonV1beta1().TaskRuns(t.Namespace)

if err := trInterface.Delete(context.TODO(), name, metav1.DeleteOptions{}); err != nil {
return err
}

return nil
}

0 comments on commit 956be45

Please sign in to comment.