Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add function to get reconcileID from context #2056

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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