-
Notifications
You must be signed in to change notification settings - Fork 2
/
executor.go
250 lines (210 loc) · 6.75 KB
/
executor.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package executor
import (
"context"
"fmt"
"sync"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/darkowlzz/operator-toolkit/constant"
eventv1 "github.com/darkowlzz/operator-toolkit/event/v1"
"github.com/darkowlzz/operator-toolkit/operator/v1/operand"
"github.com/darkowlzz/operator-toolkit/telemetry"
)
// Name of the instrumentation.
const instrumentationName = constant.LibraryName + "/operator/executor"
// ExecutionStrategy is the operands execution strategy of an operator.
type ExecutionStrategy int
const (
Parallel ExecutionStrategy = iota
Serial
)
// Executor is an operand executor. It is used to configure how the operands
// are executed. The event recorder is used to broadcast an event right after
// executing an operand.
type Executor struct {
execStrategy ExecutionStrategy
recorder record.EventRecorder
inst *telemetry.Instrumentation
}
// NewExecutor initializes and returns an Executor.
func NewExecutor(e ExecutionStrategy, r record.EventRecorder) *Executor {
return &Executor{
execStrategy: e,
recorder: r,
inst: telemetry.NewInstrumentation(instrumentationName),
}
}
// ExecuteOperands executes operands in a given OperandOrder by calling a given
// OperandRunCall function on each of the operands. The OperandRunCall can be a
// call to Ensure or Delete.
func (exe *Executor) ExecuteOperands(
order operand.OperandOrder,
call operand.OperandRunCall,
ctx context.Context,
obj client.Object,
ownerRef metav1.OwnerReference,
) (result ctrl.Result, rerr error) {
ctx, span, _, _ := exe.inst.Start(ctx, "execute")
defer span.End()
span.SetAttributes(attribute.Int("order-length", len(order)))
span.AddEvent("Start operand execution")
// Iterate through the order steps and run the operands in the steps as per
// the execution strategy.
for _, ops := range order {
// Error in the current execution step.
var execErr error
// res is the Result of the step.
// TODO: Change the type of res to something that reflects that a
// change took place. The value of Result is not propagated to the
// caller.
var res *ctrl.Result
requeueStrategy := operand.StepRequeueStrategy(ops)
span.AddEvent(
"Execute operands",
trace.WithAttributes(
attribute.Int("requeue-strategy", int(requeueStrategy)),
),
)
switch exe.execStrategy {
case Serial:
// Run the operands serially.
res, execErr = exe.serialExec(ops, call, ctx, obj, ownerRef)
case Parallel:
// Run the operands concurrently.
res, execErr = exe.concurrentExec(ops, call, ctx, obj, ownerRef)
default:
rerr = fmt.Errorf("unknown operands execution strategy: %v", exe.execStrategy)
return
}
if execErr != nil {
result = ctrl.Result{Requeue: true}
rerr = execErr
break
}
// If a change was made with a Result received after the execution and
// the RequeueStrategy is RequeueAlways, set a requeued result.
if res != nil && requeueStrategy == operand.RequeueAlways {
result = ctrl.Result{Requeue: true}
break
}
}
span.AddEvent("Finish operand execution")
return
}
// serialExec runs the given set of operands serially with the given call
// function. An event is used to know if a change was applied. When an event is
// found, a result object is returned, else nil.
func (exe *Executor) serialExec(
ops []operand.Operand,
call operand.OperandRunCall,
ctx context.Context,
obj client.Object,
ownerRef metav1.OwnerReference,
) (result *ctrl.Result, rerr error) {
ctx, span, _, _ := exe.inst.Start(ctx, "serial-exec")
defer span.End()
result = nil
span.AddEvent(
"Execute serially",
trace.WithAttributes(attribute.Int("operand-count", len(ops))),
)
for _, op := range ops {
span.AddEvent(
"Executing operand",
trace.WithAttributes(attribute.String("operand-name", op.Name())),
)
// Call the run call function. Since this is serial execution, return
// if an error occurs.
event, err := call(op)(ctx, obj, ownerRef)
if err != nil {
rerr = kerrors.NewAggregate([]error{rerr, err})
return
}
if event != nil {
event.Record(exe.recorder)
result = &ctrl.Result{}
}
}
span.AddEvent("Finish serial execution")
return
}
// concurrentExec runs the operands concurrently, collecting the errors from
// the operand executions and returns them.
func (exe *Executor) concurrentExec(
ops []operand.Operand,
call operand.OperandRunCall,
ctx context.Context,
obj client.Object,
ownerRef metav1.OwnerReference,
) (result *ctrl.Result, rerr error) {
ctx, span, _, _ := exe.inst.Start(ctx, "concurrent-exec")
defer span.End()
result = nil
// Wait group to synchronize the go routines.
var wg sync.WaitGroup
totalOperands := len(ops)
// resultChan is used to collect the result returned from the concurrent
// execution of the operands.
var resultChan chan ctrl.Result = make(chan ctrl.Result, totalOperands)
// Error buffered channel to collect all the errors from the go routines.
var errChan chan error = make(chan error, totalOperands)
span.AddEvent(
"Execute concurrently",
trace.WithAttributes(attribute.Int("operand-count", len(ops))),
)
wg.Add(totalOperands)
for _, op := range ops {
span.AddEvent(
"Executing operand",
trace.WithAttributes(attribute.String("operand-name", op.Name())),
)
go exe.operateWithWaitGroup(&wg, resultChan, errChan, call(op), ctx, obj, ownerRef)
}
wg.Wait()
close(errChan)
// Check if any errors were encountere.
for err := range errChan {
rerr = kerrors.NewAggregate([]error{rerr, err})
}
// Check the result channel, if it contains any result, return a result
// object.
foundResult := false
if len(resultChan) > 0 {
foundResult = true
}
if foundResult {
result = &ctrl.Result{}
}
span.AddEvent("Finish concurrent execution")
return
}
// operateWithWaitGroup runs the given function f and calls done on the wait
// group at the end. This is a goroutine function used for running the operands
// concurrently. The result from events and errors from the execution are
// communicated via the respective channels.
func (exe *Executor) operateWithWaitGroup(
wg *sync.WaitGroup,
resultChan chan ctrl.Result,
errChan chan error,
f func(context.Context, client.Object, metav1.OwnerReference) (eventv1.ReconcilerEvent, error),
ctx context.Context,
obj client.Object,
ownerRef metav1.OwnerReference,
) {
defer wg.Done()
event, err := f(ctx, obj, ownerRef)
if err != nil {
errChan <- err
}
// Event is used to determine if a change took place. Send a result to the
// result channel when an event is received.
if event != nil {
event.Record(exe.recorder)
resultChan <- ctrl.Result{}
}
}