From 5ffd1827518522c01be188cf96abfe5073721106 Mon Sep 17 00:00:00 2001 From: Thomas Morin Date: Wed, 7 Feb 2024 15:15:34 +0100 Subject: [PATCH] round durations to the second in events, errors and status conditions messages Signed-off-by: Thomas Morin --- internal/controller/kustomization_controller.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/controller/kustomization_controller.go b/internal/controller/kustomization_controller.go index 0e313ed2..4b9e081a 100644 --- a/internal/controller/kustomization_controller.go +++ b/internal/controller/kustomization_controller.go @@ -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, @@ -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) @@ -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) } @@ -1101,3 +1101,11 @@ func (r *KustomizationReconciler) patch(ctx context.Context, return nil } + +func formatDurationSince(t time.Time) string { + if (t < time.Second) { + return time.Since(t).Round(time.Millisecond).String() + } else { + return time.Since(t).Round(time.Second).String() + } +}