-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathadvice.go
164 lines (137 loc) · 4.52 KB
/
advice.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Copyright 2024 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package reconcilers
import (
"context"
"fmt"
"sync"
"github.com/go-logr/logr"
"reconciler.io/runtime/validation"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var _ SubReconciler[client.Object] = (*Advice[client.Object])(nil)
// Advice is a sub reconciler for advising the lifecycle of another sub reconciler in an aspect
// oriented programming style.
type Advice[Type client.Object] struct {
// Name used to identify this reconciler. Defaults to `Advice`. Ideally unique, but
// not required to be so.
//
// +optional
Name string
// Setup performs initialization on the manager and builder this reconciler
// will run with. It's common to setup field indexes and watch resources.
//
// +optional
Setup func(ctx context.Context, mgr ctrl.Manager, bldr *builder.Builder) error
// Reconciler being advised
Reconciler SubReconciler[Type]
// Before is called preceding Around. A modified context may be returned. Errors are returned
// immediately.
//
// If Before is not defined, there is no effect.
//
// +optional
Before func(ctx context.Context, resource Type) (context.Context, Result, error)
// Around is responsible for invoking the reconciler and returning the result. Implementations
// may choose to not invoke the reconciler, invoke a different reconciler or invoke the
// reconciler multiple times.
//
// If Around is not defined, the Reconciler is invoked once.
//
// +optional
Around func(ctx context.Context, resource Type, reconciler SubReconciler[Type]) (Result, error)
// After is called following Around. The result and error from Around are provided and may be
// modified before returning.
//
// If After is not defined, the result and error are returned directly.
//
// +optional
After func(ctx context.Context, resource Type, result Result, err error) (Result, error)
lazyInit sync.Once
}
func (r *Advice[T]) init() {
r.lazyInit.Do(func() {
if r.Name == "" {
r.Name = "Advice"
}
if r.Before == nil {
r.Before = func(ctx context.Context, resource T) (context.Context, Result, error) {
return nil, Result{}, nil
}
}
if r.Around == nil {
r.Around = func(ctx context.Context, resource T, reconciler SubReconciler[T]) (Result, error) {
return reconciler.Reconcile(ctx, resource)
}
}
if r.After == nil {
r.After = func(ctx context.Context, resource T, result Result, err error) (Result, error) {
return result, err
}
}
})
}
func (r *Advice[T]) SetupWithManager(ctx context.Context, mgr ctrl.Manager, bldr *builder.Builder) error {
r.init()
log := logr.FromContextOrDiscard(ctx).
WithName(r.Name)
ctx = logr.NewContext(ctx, log)
if r.Setup == nil {
return nil
}
if err := r.Validate(ctx); err != nil {
return err
}
if err := r.Setup(ctx, mgr, bldr); err != nil {
return err
}
return r.Reconciler.SetupWithManager(ctx, mgr, bldr)
}
func (r *Advice[T]) Validate(ctx context.Context) error {
r.init()
if r.Reconciler == nil {
return fmt.Errorf("Advice %q must implement Reconciler", r.Name)
}
if r.Before == nil && r.Around == nil && r.After == nil {
return fmt.Errorf("Advice %q must implement at least one of Before, Around or After", r.Name)
}
if validation.IsRecursive(ctx) {
if v, ok := r.Reconciler.(validation.Validator); ok {
if err := v.Validate(ctx); err != nil {
return fmt.Errorf("Advice %q must have a valid Reconciler: %w", r.Name, err)
}
}
}
return nil
}
func (r *Advice[T]) Reconcile(ctx context.Context, resource T) (Result, error) {
r.init()
log := logr.FromContextOrDiscard(ctx).
WithName(r.Name)
ctx = logr.NewContext(ctx, log)
// before phase
beforeCtx, result, err := r.Before(ctx, resource)
if err != nil {
return result, err
}
if beforeCtx != nil {
ctx = beforeCtx
}
// around phase
aroundResult, err := r.Around(ctx, resource, r.Reconciler)
result = AggregateResults(result, aroundResult)
// after phase
return r.After(ctx, resource, result, err)
}