-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit events for BuildRun objects #824
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import ( | |
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/tools/record" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
@@ -46,6 +47,7 @@ type ReconcileBuildRun struct { | |
client client.Client | ||
scheme *runtime.Scheme | ||
setOwnerReferenceFunc setOwnerReferenceFunc | ||
eventRecorder record.EventRecorder | ||
} | ||
|
||
// NewReconciler returns a new reconcile.Reconciler | ||
|
@@ -56,6 +58,7 @@ func NewReconciler(ctx context.Context, c *config.Config, mgr manager.Manager, o | |
client: mgr.GetClient(), | ||
scheme: mgr.GetScheme(), | ||
setOwnerReferenceFunc: ownerRef, | ||
eventRecorder: mgr.GetEventRecorderFor("buildruns"), | ||
} | ||
} | ||
|
||
|
@@ -111,11 +114,8 @@ func (r *ReconcileBuildRun) Reconcile(request reconcile.Request) (reconcile.Resu | |
// stop reconciling and mark the BuildRun as Failed | ||
// we only reconcile again if the status.Update call fails | ||
message := fmt.Sprintf("the Build is not registered correctly, build: %s, registered status: %s, reason: %s", build.Name, build.Status.Registered, build.Status.Reason) | ||
if updateErr := resources.UpdateConditionWithFalseStatus(ctx, r.client, buildRun, message, resources.ConditionBuildRegistrationFailed); updateErr != nil { | ||
return reconcile.Result{}, updateErr | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
updateErr := resources.UpdateConditionWithFalseStatus(ctx, r.client, buildRun, message, resources.ConditionBuildRegistrationFailed) | ||
return r.handleError(buildRun, updateErr) | ||
} | ||
|
||
// Ensure the build-related labels on the BuildRun | ||
|
@@ -126,6 +126,7 @@ func (r *ReconcileBuildRun) Reconcile(request reconcile.Request) (reconcile.Resu | |
// Set OwnerReference for Build and BuildRun only when build.shipwright.io/build-run-deletion is set "true" | ||
if build.GetAnnotations()[buildv1alpha1.AnnotationBuildRunDeletion] == "true" && !resources.IsOwnedByBuild(build, buildRun.OwnerReferences) { | ||
if err := r.setOwnerReferenceFunc(build, buildRun, r.scheme); err != nil { | ||
// TODO - why are we setting this on the Build object instead of the BuildRun object? | ||
build.Status.Reason = buildv1alpha1.SetOwnerReferenceFailed | ||
build.Status.Message = fmt.Sprintf("unexpected error when trying to set the ownerreference: %v", err) | ||
if err := r.client.Status().Update(ctx, build); err != nil { | ||
|
@@ -161,30 +162,18 @@ func (r *ReconcileBuildRun) Reconcile(request reconcile.Request) (reconcile.Resu | |
// Choose a service account to use | ||
svcAccount, err := resources.RetrieveServiceAccount(ctx, r.client, build, buildRun) | ||
if err != nil { | ||
if !resources.IsClientStatusUpdateError(err) && buildRun.Status.IsFailed(buildv1alpha1.Succeeded) { | ||
return reconcile.Result{}, nil | ||
} | ||
// system call failure, reconcile again | ||
return reconcile.Result{}, err | ||
return r.handleError(buildRun, err) | ||
} | ||
|
||
strategy, err := r.getReferencedStrategy(ctx, build, buildRun) | ||
if err != nil { | ||
if !resources.IsClientStatusUpdateError(err) && buildRun.Status.IsFailed(buildv1alpha1.Succeeded) { | ||
return reconcile.Result{}, nil | ||
} | ||
return reconcile.Result{}, err | ||
return r.handleError(buildRun, err) | ||
} | ||
|
||
// Create the TaskRun, this needs to be the last step in this block to be idempotent | ||
generatedTaskRun, err := r.createTaskRun(ctx, svcAccount, strategy, build, buildRun) | ||
if err != nil { | ||
if !resources.IsClientStatusUpdateError(err) && buildRun.Status.IsFailed(buildv1alpha1.Succeeded) { | ||
ctxlog.Info(ctx, "taskRun generation failed", namespace, request.Namespace, name, request.Name) | ||
return reconcile.Result{}, nil | ||
} | ||
// system call failure, reconcile again | ||
return reconcile.Result{}, err | ||
return r.handleError(buildRun, err) | ||
} | ||
|
||
ctxlog.Info(ctx, "creating TaskRun from BuildRun", namespace, request.Namespace, name, generatedTaskRun.GenerateName, "BuildRun", buildRun.Name) | ||
|
@@ -267,6 +256,7 @@ func (r *ReconcileBuildRun) Reconcile(request reconcile.Request) (reconcile.Resu | |
if buildRun.Status.StartTime == nil && lastTaskRun.Status.StartTime != nil { | ||
buildRun.Status.StartTime = lastTaskRun.Status.StartTime | ||
|
||
r.recordBuildRunStarted(buildRun) | ||
// Report the buildrun established duration (time between the creation of the buildrun and the start of the buildrun) | ||
buildmetrics.BuildRunEstablishObserve( | ||
buildRun.Status.BuildSpec.StrategyName(), | ||
|
@@ -280,6 +270,7 @@ func (r *ReconcileBuildRun) Reconcile(request reconcile.Request) (reconcile.Resu | |
if lastTaskRun.Status.CompletionTime != nil && buildRun.Status.CompletionTime == nil { | ||
buildRun.Status.CompletionTime = lastTaskRun.Status.CompletionTime | ||
|
||
r.recordBuildRunCompleted(buildRun) | ||
// buildrun completion duration (total time between the creation of the buildrun and the buildrun completion) | ||
buildmetrics.BuildRunCompletionObserve( | ||
buildRun.Status.BuildSpec.StrategyName(), | ||
|
@@ -318,6 +309,7 @@ func (r *ReconcileBuildRun) Reconcile(request reconcile.Request) (reconcile.Resu | |
pod.CreationTimestamp.Time.Sub(lastTaskRun.CreationTimestamp.Time), | ||
) | ||
} | ||
|
||
} | ||
|
||
ctxlog.Info(ctx, "updating buildRun status", namespace, request.Namespace, name, request.Name) | ||
|
@@ -332,6 +324,43 @@ func (r *ReconcileBuildRun) Reconcile(request reconcile.Request) (reconcile.Resu | |
return reconcile.Result{}, nil | ||
} | ||
|
||
// handleError determines the appropriate reconciliation result for the provided error. | ||
// It also ensures the build success/failure event is recorded. | ||
func (r *ReconcileBuildRun) handleError(buildRun *buildv1alpha1.BuildRun, err error) (reconcile.Result, error) { | ||
if resources.IsClientStatusUpdateError(err) { | ||
return reconcile.Result{}, err | ||
} | ||
if !buildRun.Status.IsFailed(buildv1alpha1.Succeeded) { | ||
return reconcile.Result{}, err | ||
} | ||
r.recordBuildRunCompleted(buildRun) | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// recordBuildRunCompleted records the appropriate event if the provided BuildRun has completed. | ||
func (r *ReconcileBuildRun) recordBuildRunCompleted(buildRun *buildv1alpha1.BuildRun) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My few cents: the function names sound generic and not related to events. The metrics calls could therefore also go in here. What I tend to prefer more is that the event creation code is moved to a separate file. |
||
if buildRun.Status.CompletionTime == nil { | ||
return | ||
} | ||
successStatus := buildRun.Status.GetCondition(buildv1alpha1.Succeeded) | ||
if successStatus == nil { | ||
return | ||
} | ||
if successStatus.Status == corev1.ConditionTrue { | ||
r.eventRecorder.Event(buildRun, "Normal", "Succeeded", "Completed with no errors") | ||
} | ||
if successStatus.Status == corev1.ConditionFalse { | ||
r.eventRecorder.Eventf(buildRun, "Warning", "Failed", "Failure reason: %s", successStatus.Reason) | ||
} | ||
} | ||
|
||
func (r *ReconcileBuildRun) recordBuildRunStarted(buildRun *buildv1alpha1.BuildRun) { | ||
if buildRun.Status.StartTime == nil { | ||
return | ||
} | ||
r.eventRecorder.Event(buildRun, "Normal", "Started", "") | ||
} | ||
|
||
// GetBuildRunObject retrieves an existing BuildRun based on a name and namespace | ||
func (r *ReconcileBuildRun) GetBuildRunObject(ctx context.Context, objectName string, objectNS string, buildRun *buildv1alpha1.BuildRun) error { | ||
if err := r.client.Get(ctx, types.NamespacedName{Name: objectName, Namespace: objectNS}, buildRun); err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally prefer that issues are opened instead of adding TODOs in unrelated pull requests that nobody will work on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened #827 - I'll drop this comment