k3s/pkg/util/context.go
Brad Davidson a666b7905c
Some checks failed
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
govulncheck / govulncheck (push) Has been cancelled
Add context to controller event recorders
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>
2026-03-25 15:32:15 -07:00

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
}