Framework: Introduce RuntimeContext
What does this MR do?
RuntimeContext
encapsulates the request-scoped information and a shared state
between the Tasks that handle a reconciler request, i.e. reconcile.Request.
The NewRuntimeContext
helps with creation and validation of the
context with various options.
Since RuntimeContext
uses slog
logger, adjustments are made to bridge
between logr.Logger
and slog.Logger
.
Motivation
Current
In its current form we use context.Context
and runtime context creation and usage looks like this:
// Create in Reconciler
rtCtx := rt.NewContext(ctx,
rt.WithClient(r.Client), rt.WithEventRecorder(r.Recorder), rt.WithLogger(logger))
// Pass as context.Context interface and
// use runtime functions to extract values
schema := rt.SchemeFromContext(ctx)
if schema == nil {
return errors.New("no Scheme in context")
}
c := rt.ClientFromContext(ctx)
if c == nil {
return nil, rt.Fail(errors.New("no Client in context"))
}
logger := logr.FromContextOrDiscard(ctx)
We use context.Context
for no good reason.
This change
// Create in Reconciler
rtCtx, err := NewRuntimeContext(ctx, WithManager(mgr))
if err != nil {
// Failed to create the runtime context. Stop Reconciler now.
return Terminate(err)
}
// Pass RuntimeContext structure to framework.
// It can be used as a `context.Context` too.
// The following are safe values.
rtCtx.Logger.Debug("....")
Author's Checklist
For anything in this list which will not be completed, please provide a reason in the MR discussion.
Required
-
Ensure a release milestone is set. -
MR title and description are up to date, accurate, and descriptive. -
MR targeting the appropriate branch. -
MR has a green pipeline on GitLab.com. -
When ready for review, MR is labeled workflowready for review per the MR workflow.
Expected
-
Test plan indicating conditions for success has been posted and passes. -
Documentation is created or updated. -
Tests are added.
Related issues
Related to #66 (closed)
Edited by Clemens Beck