Skip to content

Commit

Permalink
Merge pull request #2056 from zbindenren/request-id-from-context
Browse files Browse the repository at this point in the history
✨ Add function to get reconcileID from context
  • Loading branch information
k8s-ci-robot committed Nov 24, 2022
2 parents e7b2d45 + 0070bb2 commit 92611cb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/controller/controller.go
Expand Up @@ -153,3 +153,6 @@ func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller
RecoverPanic: options.RecoverPanic,
}, nil
}

// ReconcileIDFromContext gets the reconcileID from the current context.
var ReconcileIDFromContext = controller.ReconcileIDFromContext
23 changes: 22 additions & 1 deletion pkg/internal/controller/controller.go
Expand Up @@ -24,6 +24,7 @@ import (
"time"

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

log := c.LogConstructor(&req)
reconcileID := uuid.NewUUID()

log = log.WithValues("reconcileID", uuid.NewUUID())
log = log.WithValues("reconcileID", reconcileID)
ctx = logf.IntoContext(ctx, log)
ctx = addReconcileID(ctx, reconcileID)

// RunInformersAndControllers the syncHandler, passing it the Namespace/Name string of the
// resource to be synced.
Expand Down Expand Up @@ -358,3 +361,21 @@ func (c *Controller) InjectFunc(f inject.Func) error {
func (c *Controller) updateMetrics(reconcileTime time.Duration) {
ctrlmetrics.ReconcileTime.WithLabelValues(c.Name).Observe(reconcileTime.Seconds())
}

// ReconcileIDFromContext gets the reconcileID from the current context.
func ReconcileIDFromContext(ctx context.Context) types.UID {
r, ok := ctx.Value(reconcileIDKey{}).(types.UID)
if !ok {
return ""
}

return r
}

// reconcileIDKey is a context.Context Value key. Its associated value should
// be a types.UID.
type reconcileIDKey struct{}

func addReconcileID(ctx context.Context, reconcileID types.UID) context.Context {
return context.WithValue(ctx, reconcileIDKey{}, reconcileID)
}
17 changes: 17 additions & 0 deletions pkg/internal/controller/controller_test.go
Expand Up @@ -873,6 +873,23 @@ var _ = Describe("controller", func() {
})
})

var _ = Describe("ReconcileIDFromContext function", func() {
It("should return an empty string if there is nothing in the context", func() {
ctx := context.Background()
reconcileID := ReconcileIDFromContext(ctx)

Expect(reconcileID).To(Equal(types.UID("")))
})

It("should return the correct reconcileID from context", func() {
const expectedReconcileID = types.UID("uuid")
ctx := addReconcileID(context.Background(), expectedReconcileID)
reconcileID := ReconcileIDFromContext(ctx)

Expect(reconcileID).To(Equal(expectedReconcileID))
})
})

type DelegatingQueue struct {
workqueue.RateLimitingInterface
mu sync.Mutex
Expand Down

0 comments on commit 92611cb

Please sign in to comment.