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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package controller
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"strconv"
"strings"
"sync"
"time"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
policyv1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/policy/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/policyengine/models"
policyengine "github.com/johnandersen777/socialweb-computer-kcp/internal/policyengine/status"
)
const TriggerActionComputeVM = "ComputeVM"
var ErrNoTriggerActionHandler = errors.New("no action handler is registered for the policy trigger action")
type ComputeVMActionHandler interface {
DispatchComputeVM(ctx context.Context, action policyv1alpha1.PolicyAction, outcome Outcome) error
}
type ComputeVMActionHandlerFunc func(ctx context.Context, action policyv1alpha1.PolicyAction, outcome Outcome) error
func (f ComputeVMActionHandlerFunc) DispatchComputeVM(ctx context.Context, action policyv1alpha1.PolicyAction, outcome Outcome) error {
return f(ctx, action, outcome)
}
type TriggerActionRegistry struct {
mu sync.RWMutex
handlers map[string]ActionHandler
}
func (r *TriggerActionRegistry) Register(handler ActionHandler) error {
if handler == nil {
return errors.New("policy trigger: action handler is required")
}
kind := strings.TrimSpace(handler.Kind())
if kind == "" {
return errors.New("policy trigger: action handler kind is required")
}
r.mu.Lock()
defer r.mu.Unlock()
if r.handlers == nil {
r.handlers = make(map[string]ActionHandler)
}
if _, exists := r.handlers[kind]; exists {
return fmt.Errorf("policy trigger: action handler for %s is already registered", kind)
}
r.handlers[kind] = handler
return nil
}
func (r *TriggerActionRegistry) RegisterComputeVM(handler ComputeVMActionHandler) error {
if handler == nil {
return errors.New("policy trigger: ComputeVM action handler is required")
}
return r.Register(computeVMActionAdapter{handler: handler})
}
func (r *TriggerActionRegistry) Lookup(kind string) (ActionHandler, bool) {
if r == nil {
return nil, false
}
r.mu.RLock()
defer r.mu.RUnlock()
handler, ok := r.handlers[strings.TrimSpace(kind)]
return handler, ok
}
func (r *TriggerActionRegistry) Dispatch(ctx context.Context, action policyv1alpha1.PolicyAction, outcome Outcome) error {
kind := strings.TrimSpace(action.Kind)
handler, ok := r.Lookup(kind)
if !ok {
return fmt.Errorf("%w: %s", ErrNoTriggerActionHandler, kind)
}
return handler.Dispatch(ctx, action.Spec, outcome)
}
func (r *TriggerActionRegistry) Handlers() map[string]ActionHandler {
if r == nil {
return nil
}
r.mu.RLock()
defer r.mu.RUnlock()
out := make(map[string]ActionHandler, len(r.handlers))
maps.Copy(out, r.handlers)
return out
}
type computeVMActionAdapter struct {
handler ComputeVMActionHandler
}
func (computeVMActionAdapter) Kind() string {
return TriggerActionComputeVM
}
func (a computeVMActionAdapter) Dispatch(ctx context.Context, spec json.RawMessage, outcome Outcome) error {
return a.handler.DispatchComputeVM(ctx, policyv1alpha1.PolicyAction{Kind: TriggerActionComputeVM, Spec: spec}, outcome)
}
type TriggerReconciler struct {
Registry *TriggerActionRegistry
Handlers map[string]ActionHandler
Now func() time.Time
}
func (r *TriggerReconciler) ReconcilePolicyTrigger(ctx context.Context, trigger *policyv1alpha1.PolicyTrigger) (TriggerReconcileState, error) {
if trigger == nil {
return TriggerReconcileState{}, fmt.Errorf("policy trigger is required")
}
if trigger.DeletionTimestamp != nil {
state := TriggerReconcileState{}
meta.SetStatusCondition(&state.Conditions, metav1.Condition{
Type: policyengine.ConditionReady,
Status: metav1.ConditionFalse,
Reason: "Terminating",
Message: "the policy trigger is being deleted",
})
return state, nil
}
if trigger.Spec.Suspend {
state := TriggerReconcileState{}
meta.SetStatusCondition(&state.Conditions, metav1.Condition{
Type: policyengine.ConditionSuspended,
Status: metav1.ConditionTrue,
Reason: "Suspended",
Message: "the policy trigger is suspended",
})
return state, nil
}
kind := strings.TrimSpace(trigger.Spec.Action.Kind)
if kind == "" {
return triggerInvalid("action.kind is required"), nil
}
if _, ok := r.lookup(kind); !ok {
return triggerInvalid("no action handler is registered for " + kind), nil
}
switch trigger.Spec.Source.Type {
case policyv1alpha1.PolicyTriggerSourceRecord, policyv1alpha1.PolicyTriggerSourceRelay:
case policyv1alpha1.PolicyTriggerSourceCron:
if trigger.Spec.Source.Cron == nil || strings.TrimSpace(trigger.Spec.Source.Cron.Schedule) == "" {
return triggerInvalid("source.cron.schedule is required"), nil
}
case policyv1alpha1.PolicyTriggerSourceKCP:
if trigger.Spec.Source.KCP == nil || strings.TrimSpace(trigger.Spec.Source.KCP.Resource) == "" {
return triggerInvalid("source.kcp.resource is required"), nil
}
default:
return triggerInvalid("source.type must be Record, Relay, Cron or KCP"), nil
}
state := TriggerReconcileState{}
meta.SetStatusCondition(&state.Conditions, metav1.Condition{
Type: policyengine.ConditionReady,
Status: metav1.ConditionTrue,
Reason: "Ready",
Message: "the policy trigger is ready",
})
return state, nil
}
func (r *TriggerReconciler) Handle(ctx context.Context, trigger *policyv1alpha1.PolicyTrigger, outcome Outcome) (TriggerReconcileState, error) {
if trigger == nil {
return TriggerReconcileState{}, fmt.Errorf("policy trigger is required")
}
if trigger.Spec.Suspend {
return TriggerReconcileState{}, nil
}
kind := strings.TrimSpace(trigger.Spec.Action.Kind)
handler, ok := r.lookup(kind)
if !ok {
return triggerInvalid("no action handler is registered for " + kind), fmt.Errorf("unknown action handler %s", kind)
}
if !MatchesOutputs(outcome.Result.Outputs, trigger.Spec.Match) {
state := TriggerReconcileState{}
meta.SetStatusCondition(&state.Conditions, metav1.Condition{
Type: "Matched",
Status: metav1.ConditionFalse,
Reason: "NoMatch",
Message: "the run outputs did not match the trigger",
})
return state, nil
}
if err := handler.Dispatch(ctx, trigger.Spec.Action.Spec, outcome); err != nil {
state := TriggerReconcileState{Active: 1}
meta.SetStatusCondition(&state.Conditions, metav1.Condition{
Type: "Dispatched",
Status: metav1.ConditionFalse,
Reason: "DispatchFailed",
Message: err.Error(),
})
return state, err
}
now := metav1.NewTime(r.now())
state := TriggerReconcileState{LastRun: &now, Active: 1}
meta.SetStatusCondition(&state.Conditions, metav1.Condition{
Type: "Dispatched",
Status: metav1.ConditionTrue,
Reason: "Matched",
Message: "the run outputs matched and the action was dispatched",
})
return state, nil
}
func (r *TriggerReconciler) lookup(kind string) (ActionHandler, bool) {
kind = strings.TrimSpace(kind)
if r.Registry != nil {
return r.Registry.Lookup(kind)
}
if r.Handlers == nil {
return nil, false
}
handler, ok := r.Handlers[kind]
return handler, ok
}
func (r *TriggerReconciler) now() time.Time {
if r.Now != nil {
return r.Now()
}
return time.Now()
}
func triggerInvalid(message string) TriggerReconcileState {
state := TriggerReconcileState{}
meta.SetStatusCondition(&state.Conditions, metav1.Condition{
Type: policyengine.ConditionReady,
Status: metav1.ConditionFalse,
Reason: "Invalid",
Message: message,
})
return state
}
func MatchesOutputs(outputs models.Outputs, match map[string]string) bool {
if len(match) == 0 {
return true
}
for path, expected := range match {
value, ok := lookupOutput(outputs, path)
if !ok {
return false
}
if !sameValue(value, expected) {
return false
}
}
return true
}
func lookupOutput(outputs models.Outputs, path string) (any, bool) {
var current any = map[string]any(outputs)
for part := range strings.SplitSeq(path, ".") {
switch object := current.(type) {
case map[string]any:
value, exists := object[part]
if !exists {
return nil, false
}
current = value
case map[string]string:
value, exists := object[part]
if !exists {
return nil, false
}
current = value
case models.Outputs:
value, exists := object[part]
if !exists {
return nil, false
}
current = value
default:
return nil, false
}
}
return current, true
}
func sameValue(value any, expected string) bool {
switch typed := value.(type) {
case string:
return typed == expected
case bool:
return strconv.FormatBool(typed) == expected
case nil:
return expected == "" || expected == "null"
default:
data, err := json.Marshal(typed)
if err != nil {
return fmt.Sprintf("%v", typed) == expected
}
return string(data) == expected
}
}