Framework: Provide contextual shared state
What does this MR do?
In the new Task framework, Tasks use the shared state that is carried in RuntimeContext to store information and pass parameters.
The basic interface of SharedState and its map-based implemation are provided. The interface provide two core functions, storing and retrieving state. More advanced queries of the shared state can be built using the visitor pattern.
A few queries are implemented in Framework: Built-in queries for shared state (!91 - merged) based on this.
The shared state and queries provide a basis for dependency injection for Tasks. See the following example:
Example
Let's assume:
- The reconcile function store the
reconcile.Request
in the shared state. - Then
Get
looks up an object and usesRegister
Middleware (see !78 (merged) for a definition of Middleware) to store it. - The next task,
LoadInventory
uses the looked up object to load an inventory of objects. This Task also stores the loaded object in the shared state. - Finally
Apply
uses the objects that are produced from inventory and applies them.
func (r *GitLabReconciler) Reconcile(ctx context.Context, req reconcile.Request) (ctrl.Result, error) {
rtCtx := NewRuntimeContext(ctx)
rtCtx.State.Store("Request", req)
...
// Run workflows
}
var exampleWorkflow := &Workflow{
&Get[*v2alpha1.GitLab]{
References: &state.QueryWithType[reconcile.Request],
Register: Register{
Key: "GitLab",
},
},
&LoadInventory{
Inventory: &TemplateInventory{
// The exact implementation is TBD. But let's assume that the SharedState is passed as a map to the template.
},
Register: Register{
Tags: []string{"example-objects", "inventory"},
},
},
&Apply{
Objects: &state.QueryWithTags[client.Object]{Tags: []string{"example-objects"},
},
}
In this example see how state.Query
are used for injecting parameters (aka dependencies) of Tasks.
Review note
Enough parts of context.go
are copied from !77 (merged) so that this MR works as an independent piece of work.
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)