Skip to content

Commit 0070bb2

Browse files
committed
✨ Add function to get reconcileID from context
1 parent accd262 commit 0070bb2

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

pkg/controller/controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,6 @@ func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller
153153
RecoverPanic: options.RecoverPanic,
154154
}, nil
155155
}
156+
157+
// ReconcileIDFromContext gets the reconcileID from the current context.
158+
var ReconcileIDFromContext = controller.ReconcileIDFromContext

pkg/internal/controller/controller.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
"github.com/go-logr/logr"
27+
"k8s.io/apimachinery/pkg/types"
2728
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2829
"k8s.io/apimachinery/pkg/util/uuid"
2930
"k8s.io/client-go/util/workqueue"
@@ -311,9 +312,11 @@ func (c *Controller) reconcileHandler(ctx context.Context, obj interface{}) {
311312
}
312313

313314
log := c.LogConstructor(&req)
315+
reconcileID := uuid.NewUUID()
314316

315-
log = log.WithValues("reconcileID", uuid.NewUUID())
317+
log = log.WithValues("reconcileID", reconcileID)
316318
ctx = logf.IntoContext(ctx, log)
319+
ctx = addReconcileID(ctx, reconcileID)
317320

318321
// RunInformersAndControllers the syncHandler, passing it the Namespace/Name string of the
319322
// resource to be synced.
@@ -358,3 +361,21 @@ func (c *Controller) InjectFunc(f inject.Func) error {
358361
func (c *Controller) updateMetrics(reconcileTime time.Duration) {
359362
ctrlmetrics.ReconcileTime.WithLabelValues(c.Name).Observe(reconcileTime.Seconds())
360363
}
364+
365+
// ReconcileIDFromContext gets the reconcileID from the current context.
366+
func ReconcileIDFromContext(ctx context.Context) types.UID {
367+
r, ok := ctx.Value(reconcileIDKey{}).(types.UID)
368+
if !ok {
369+
return ""
370+
}
371+
372+
return r
373+
}
374+
375+
// reconcileIDKey is a context.Context Value key. Its associated value should
376+
// be a types.UID.
377+
type reconcileIDKey struct{}
378+
379+
func addReconcileID(ctx context.Context, reconcileID types.UID) context.Context {
380+
return context.WithValue(ctx, reconcileIDKey{}, reconcileID)
381+
}

pkg/internal/controller/controller_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,23 @@ var _ = Describe("controller", func() {
873873
})
874874
})
875875

876+
var _ = Describe("ReconcileIDFromContext function", func() {
877+
It("should return an empty string if there is nothing in the context", func() {
878+
ctx := context.Background()
879+
reconcileID := ReconcileIDFromContext(ctx)
880+
881+
Expect(reconcileID).To(Equal(types.UID("")))
882+
})
883+
884+
It("should return the correct reconcileID from context", func() {
885+
const expectedReconcileID = types.UID("uuid")
886+
ctx := addReconcileID(context.Background(), expectedReconcileID)
887+
reconcileID := ReconcileIDFromContext(ctx)
888+
889+
Expect(reconcileID).To(Equal(expectedReconcileID))
890+
})
891+
})
892+
876893
type DelegatingQueue struct {
877894
workqueue.RateLimitingInterface
878895
mu sync.Mutex

0 commit comments

Comments
 (0)