-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinformer.go
378 lines (334 loc) · 11.7 KB
/
informer.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package delta
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/jackc/pgx/v5"
"github.com/riverqueue/river"
"github.com/riverqueue/river/rivertype"
"github.com/chariot-giving/delta/deltatype"
"github.com/chariot-giving/delta/internal/db/sqlc"
"github.com/chariot-giving/delta/internal/middleware"
"github.com/chariot-giving/delta/internal/object"
)
// InformOptions are optional settings for filtering resources during inform.
// These are kept purposefully generic and it's up to the application to define
// what they mean.
type InformOptions struct {
// Labels is a map of key-value pairs that can be used to filter resources
Labels map[string]string
// After allows filtering resources based on a specific time.
// If specified, informers should inform resources that have been created or updated
// after the specified time.
//
// Note that while the logic for which datetime field that this is used against is dependent
// on the external system's API and the expected behavior of the resource/informer.
// This should always be treated as exclusive date/time meaning the resources created or updated
// at the exact time specified should not be included in the inform results.
//
// Note that this is mostly useful as a performance optimization to help keep periodic informer
// jobs from re-informing entire datasets.
After *time.Time
// Limit sets the maximum number of resources to return (0 means no limit)
Limit int
// OrderBy specifies the field and direction for sorting (e.g., "created_at DESC")
OrderBy string
}
// Informer is an interface that provides a way to inform the controller of resources.
type Informer[T Object] interface {
// Inform informs the controller of resources.
//
// // It is important to respect context cancellation to enable
// the delta client to respond to shutdown requests.
// There is no way to cancel a running controller that does not respect
// context cancellation, other than terminating the process.
Inform(ctx context.Context, opts *InformOptions) (<-chan T, error)
// InformTimeout is the maximum amount of time the inform job is allowed to run before
// its context is cancelled. A timeout of zero (the default) means the job
// will inherit the Client-level timeout (defaults to 1 minute).
// A timeout of -1 means the job's context will never time out.
InformTimeout(args *InformArgs[T]) time.Duration
}
// InformerDefaults is an empty struct that can be embedded in your controller
// struct to make it fulfill the Informer interface with default values.
type InformerDefaults[T Object] struct{}
// Timeout returns the inform arg-specific timeout. Override this method to set a
// inform-specific timeout, otherwise the Client-level timeout will be applied.
func (w InformerDefaults[T]) InformTimeout(*InformArgs[T]) time.Duration { return 0 }
type InformArgs[T Object] struct {
// ResourceKind is the kind of resource to inform
// Required parameter
ResourceKind string
// ProcessExisting is used to determine if the informer should process existing resources
// The informer checks existence based on object.Compare() or hash comparison
// Defaults to false (skip existing)
ProcessExisting bool
// RunForeground is used to determine if the informer should run the work in the foreground or background
// Defaults to false (background)
// TODO: implement this
RunForeground bool
// Options are optional settings for filtering resources during inform.
Options *InformOptions
// Object is the resource to inform
object T
}
func (i InformArgs[T]) Kind() string {
return "delta.inform." + i.object.Kind()
}
func (i InformArgs[T]) InsertOpts() river.InsertOpts {
return river.InsertOpts{
MaxAttempts: 10,
Queue: i.Kind(),
UniqueOpts: river.UniqueOpts{
ByQueue: true,
ByState: []rivertype.JobState{
rivertype.JobStateAvailable,
rivertype.JobStatePending,
rivertype.JobStateRetryable,
rivertype.JobStateRunning,
rivertype.JobStateScheduled,
},
},
}
}
// controllerInformer is a worker that informs the controller of resources from external sources.
type controllerInformer[T Object] struct {
factory object.ObjectFactory
informer Informer[T]
river.WorkerDefaults[InformArgs[T]]
}
func (i *controllerInformer[T]) Work(ctx context.Context, job *river.Job[InformArgs[T]]) error {
client, err := ClientFromContextSafely(ctx)
if err != nil {
return err
}
queries := sqlc.New(client.dbPool)
controller, err := queries.ControllerGet(ctx, job.Args.ResourceKind)
if err != nil {
return fmt.Errorf("failed to get controller inform record: %w", err)
}
after := firstNonZero(job.Args.Options.After, &controller.LastInformTime)
metadata := make(map[string]string)
if len(job.Args.Options.Labels) > 0 {
metadata = job.Args.Options.Labels
}
informOpts := InformOptions{
Labels: metadata,
After: after,
OrderBy: firstNonZero(job.Args.Options.OrderBy),
Limit: firstNonZero(job.Args.Options.Limit),
}
informStart := time.Now()
informFunc := func(ctx context.Context) error {
timeout := i.informer.InformTimeout(&job.Args)
if timeout == 0 {
// use the client-level timeout if the resource doesn't specify one
timeout = client.config.ControllerInformTimeout
}
if timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
queue, err := i.informer.Inform(ctx, &informOpts)
if err != nil {
return fmt.Errorf("failed to inform controller: %w", err)
}
numObjects := 0
lastInformTimestamp := informStart
for {
select {
case obj, ok := <-queue:
if !ok {
// happy path: successfully informed all resources
if !job.Args.ProcessExisting {
// only update the controller last inform time if we aren't processing existing resources.
err := queries.ControllerSetLastInformTime(ctx, &sqlc.ControllerSetLastInformTimeParams{
LastInformTime: lastInformTimestamp,
Name: controller.Name,
})
if err != nil {
return err
}
}
return nil
}
if objWithCreatedAt, ok := Object(obj).(ObjectWithCreatedAt); ok {
if objWithCreatedAt.CreatedAt().After(lastInformTimestamp) {
lastInformTimestamp = objWithCreatedAt.CreatedAt()
}
}
numObjects++
if err := i.processObject(ctx, obj, &job.Args); err != nil {
return err
}
case <-ctx.Done():
return fmt.Errorf("work deadline exceeded: %w", ctx.Err())
}
}
}
return informFunc(ctx)
}
func (i *controllerInformer[T]) Timeout(job *river.Job[InformArgs[T]]) time.Duration {
// we enforce our own timeout so we want to remove River's underlying timeout on the job
return -1
}
func (i *controllerInformer[T]) processObject(ctx context.Context, object T, args *InformArgs[T]) error {
client, err := ClientFromContextSafely(ctx)
if err != nil {
return err
}
logger := middleware.LoggerFromContext(ctx)
riverClient, err := river.ClientFromContextSafely[pgx.Tx](ctx)
if err != nil {
return err
}
tx, err := client.dbPool.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
queries := sqlc.New(tx)
resource, err := queries.ResourceGetByObjectIDAndKind(ctx, &sqlc.ResourceGetByObjectIDAndKindParams{
ObjectID: object.ID(),
Kind: object.Kind(),
})
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
objectInformOpts := InformOpts{}
if objectWithOpts, ok := Object(object).(ObjectWithInformOpts); ok {
objectInformOpts = objectWithOpts.InformOpts()
}
namespace := firstNonZero(objectInformOpts.Namespace, namespaceDefault)
tags := objectInformOpts.Tags
if tags == nil {
tags = []string{}
} else {
for _, tag := range tags {
if len(tag) > 255 {
return errors.New("tags should be a maximum of 255 characters long")
}
if !tagRE.MatchString(tag) {
return errors.New("tags should match regex " + tagRE.String())
}
}
}
// TODO: clean this up
if len(objectInformOpts.Metadata) == 0 {
objectInformOpts.Metadata = []byte(`{}`)
}
objBytes, err := json.Marshal(object)
if err != nil {
return err
}
hash := sha256.Sum256(objBytes)
var externalCreatedAt *time.Time
if objectWithCreatedAt, ok := Object(object).(ObjectWithCreatedAt); ok {
createdAt := objectWithCreatedAt.CreatedAt()
if !createdAt.IsZero() {
externalCreatedAt = &createdAt
}
}
maxAttempts := firstNonZero(objectInformOpts.MaxAttempts, int16(10))
res, err := queries.ResourceCreateOrUpdate(ctx, &sqlc.ResourceCreateOrUpdateParams{
ObjectID: object.ID(),
Kind: object.Kind(),
Namespace: namespace,
State: sqlc.DeltaResourceStateScheduled,
Object: objBytes,
Metadata: objectInformOpts.Metadata,
Tags: tags,
Hash: hash[:],
MaxAttempts: maxAttempts,
ExternalCreatedAt: externalCreatedAt,
})
if err != nil {
return fmt.Errorf("failed to create or update resource: %w", err)
}
resourceRow := toResourceRow(&res.DeltaResource)
_, err = riverClient.InsertTx(ctx, tx, Resource[T]{Object: object, ResourceRow: &resourceRow}, &river.InsertOpts{
Queue: object.Kind(),
Tags: resourceRow.Tags,
Metadata: resourceRow.Metadata,
UniqueOpts: river.UniqueOpts{
ByArgs: true,
},
})
if err != nil {
return err
}
return tx.Commit(ctx)
}
return fmt.Errorf("failed to get resource: %w", err)
}
// handle soft-deleted resources
if resource.State == sqlc.DeltaResourceStateDeleted {
logger.WarnContext(ctx, "skipping soft-deleted resource", "id", resource.ID, "kind", resource.Kind, "object_id", resource.ObjectID)
return nil
}
// comparison
compare, err := i.compareObjects(object, toResourceRow(resource))
if err != nil {
return err
}
if compare == 0 && resource.State == sqlc.DeltaResourceStateSynced && !args.ProcessExisting {
// if the objects are the same, the resource is synced, and we don't want to process existing, skip
logger.WarnContext(ctx, "skipping already processed object", "id", resource.ID, "kind", resource.Kind, "object_id", resource.ObjectID)
return nil
}
objBytes, err := json.Marshal(object)
if err != nil {
return fmt.Errorf("failed to marshal object: %w", err)
}
hash := sha256.Sum256(objBytes)
// update the resource to be scheduled
updated, err := queries.ResourceSchedule(ctx, &sqlc.ResourceScheduleParams{
ID: resource.ID,
Object: objBytes,
Hash: hash[:],
})
if err != nil {
return fmt.Errorf("failed to set resource state: %w", err)
}
resourceRow := toResourceRow(updated)
_, err = riverClient.InsertTx(ctx, tx, Resource[T]{Object: object, ResourceRow: &resourceRow}, &river.InsertOpts{
Queue: object.Kind(),
Tags: resourceRow.Tags,
Metadata: resourceRow.Metadata,
UniqueOpts: river.UniqueOpts{
ByArgs: true,
},
})
if err != nil {
return fmt.Errorf("failed to enqueue resource: %w", err)
}
if err = tx.Commit(ctx); err != nil {
return fmt.Errorf("failed to commit transaction: %w", err)
}
return nil
}
func (i *controllerInformer[T]) compareObjects(object T, resource deltatype.ResourceRow) (int, error) {
resourceObject := i.factory.Make(&resource)
if err := resourceObject.UnmarshalResource(); err != nil {
return 0, fmt.Errorf("failed to unmarshal resource: %w", err)
}
if compare, ok := resourceObject.Compare(object); ok {
return compare, nil
}
// check hash
// we lose chronological ordering with the hash (that's why we prefer the object comparison)
// TODO: use deterministic ordering of fields to ensure consistent hash
objBytes, err := json.Marshal(object)
if err != nil {
return 0, err
}
hash := sha256.Sum256(objBytes)
if bytes.Equal(resource.Hash, hash[:]) {
return 0, nil
}
return 1, nil
}