Skip to content

Commit d74e0ef

Browse files
committed
feat: add helper function for logging requeues
1 parent 847f4db commit d74e0ef

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pkg/logging/logger.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"context"
99
"fmt"
1010
"strings"
11+
"time"
1112

1213
"github.com/go-logr/logr"
14+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1315
)
1416

1517
type Logger struct {
@@ -252,3 +254,19 @@ func (l Logger) WithNameAndContext(ctx context.Context, name string) (Logger, co
252254
ctx = NewContext(ctx, log)
253255
return log, ctx
254256
}
257+
258+
// LogRequeue takes a reconcile.Result and prints a log message on the desired verbosity if the result indicates a requeue.
259+
// Logs at debug level, unless another verbosity is provided. Any but the first verbosity argument is ignored.
260+
// No message is logged if no requeue is specified in the reconcile.Result.
261+
// Returns the provided reconcile.Result unchanged.
262+
func (l Logger) LogRequeue(rr reconcile.Result, verbosity ...LogLevel) reconcile.Result {
263+
if rr.RequeueAfter > 0 {
264+
v := DEBUG
265+
if len(verbosity) > 0 {
266+
v = verbosity[0]
267+
}
268+
nextRequeueTime := time.Now().Add(rr.RequeueAfter)
269+
l.Log(v, "Requeuing object", "after", rr.RequeueAfter.String(), "at", nextRequeueTime.Format(time.RFC3339))
270+
}
271+
return rr
272+
}

0 commit comments

Comments
 (0)