mirror of
https://github.com/k3s-io/k3s.git
synced 2026-04-06 09:55:01 -04:00
Fixes issue where RKE2 event recorder events were not logged to console due to lack of logging context. Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
25 lines
538 B
Go
25 lines
538 B
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/go-logr/logr"
|
|
)
|
|
|
|
const DefaultContextDelay = 5 * time.Second
|
|
|
|
// DelayCancel returns a context that will be cancelled
|
|
// with a delay after the parent context has been cancelled.
|
|
func DelayCancel(ctx context.Context, delay time.Duration) context.Context {
|
|
dctx, dcancel := context.WithCancel(context.Background())
|
|
if l, err := logr.FromContext(ctx); err == nil {
|
|
dctx = logr.NewContext(dctx, l)
|
|
}
|
|
go func() {
|
|
<-ctx.Done()
|
|
time.Sleep(delay)
|
|
dcancel()
|
|
}()
|
|
return dctx
|
|
}
|