-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.go
More file actions
59 lines (49 loc) · 1.86 KB
/
Copy pathcontext.go
File metadata and controls
59 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ctrlfwk
import (
"context"
"sigs.k8s.io/controller-runtime/pkg/client"
corev1 "k8s.io/api/core/v1"
)
type Context[K client.Object] interface {
context.Context
ImplementsCustomResource[K]
}
type baseContext[K client.Object] struct {
context.Context
CustomResource[K]
}
// NewContext creates a new Context for the given reconciler and base context.
// K is the type of the custom resource being reconciled.
// You can use it as such:
//
// func (reconciler *SecretReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// logger := logf.FromContext(ctx)
// context := ctrlfwk.NewContext(ctx, reconciler)
func NewContext[K client.Object](ctx context.Context, reconciler Reconciler[K]) Context[K] {
return &baseContext[K]{
Context: ctx,
CustomResource: CustomResource[K]{},
}
}
var _ Context[*corev1.Secret] = &baseContext[*corev1.Secret]{}
// ContextWithData is a context that holds additional data of type D along with the base context.
// K is the type of the custom resource being reconciled.
// D is the type of the additional data to be stored in the context.
type ContextWithData[K client.Object, D any] struct {
Context[K]
Data D
}
// NewContextWithData creates a new ContextWithData for the given reconciler, base context, and data.
// K is the type of the custom resource being reconciled.
// D is the type of the additional data to be stored in the context.
// You can use it as such:
//
// func (reconciler *TestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// logger := logf.FromContext(ctx)
// context := ctrlfwk.NewContextWithData(ctx, reconciler, &MyDataType{})
func NewContextWithData[K client.Object, D any](ctx context.Context, reconciler Reconciler[K], data D) *ContextWithData[K, D] {
return &ContextWithData[K, D]{
Context: &baseContext[K]{Context: ctx},
Data: data,
}
}