Skip to content

Commit

Permalink
round durations to the second in events, errors and status conditions…
Browse files Browse the repository at this point in the history
… messages

Signed-off-by: Thomas Morin <[email protected]>
  • Loading branch information
tmmorin committed Apr 12, 2024
1 parent 440b9da commit dc082a2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions internal/controller/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
// Log and emit success event.
if conditions.IsReady(obj) {
msg := fmt.Sprintf("Reconciliation finished in %s, next run in %s",
time.Since(reconcileStart).String(),
formatDurationSince(reconcileStart),
obj.Spec.Interval.Duration.String())
log.Info(msg, "revision", obj.Status.LastAttemptedRevision)
r.event(obj, obj.Status.LastAppliedRevision, eventv1.EventSeverityInfo, msg,
Expand Down Expand Up @@ -276,7 +276,7 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
// Broadcast the reconciliation failure and requeue at the specified retry interval.
if reconcileErr != nil {
log.Error(reconcileErr, fmt.Sprintf("Reconciliation failed after %s, next try in %s",
time.Since(reconcileStart).String(),
formatDurationSince(reconcileStart),
obj.GetRetryInterval().String()),
"revision",
artifactSource.GetArtifact().Revision)
Expand Down Expand Up @@ -904,11 +904,11 @@ func (r *KustomizationReconciler) checkHealth(ctx context.Context,
}); err != nil {
conditions.MarkFalse(obj, meta.ReadyCondition, kustomizev1.HealthCheckFailedReason, err.Error())
conditions.MarkFalse(obj, kustomizev1.HealthyCondition, kustomizev1.HealthCheckFailedReason, err.Error())
return fmt.Errorf("health check failed after %s: %w", time.Since(checkStart).String(), err)
return fmt.Errorf("health check failed after %s: %w", formatDurationSince(checkStart), err)
}

// Emit recovery event if the previous health check failed.
msg := fmt.Sprintf("Health check passed in %s", time.Since(checkStart).String())
msg := fmt.Sprintf("Health check passed in %s", formatDurationSince(checkStart))
if !wasHealthy || (isNewRevision && drifted) {
r.event(obj, revision, eventv1.EventSeverityInfo, msg, nil)
}
Expand Down Expand Up @@ -1101,3 +1101,11 @@ func (r *KustomizationReconciler) patch(ctx context.Context,

return nil
}

func formatDurationSince(t time.Time) string {
if (time.Since(t) < time.Second) {
return time.Since(t).Round(time.Millisecond).String()
} else {
return time.Since(t).Round(time.Second).String()
}
}

0 comments on commit dc082a2

Please sign in to comment.