/
reconciler.go
186 lines (155 loc) · 4.8 KB
/
reconciler.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package reconciler
import (
"context"
"fmt"
"log/slog"
"time"
"github.com/cilium/hive/cell"
"github.com/cilium/hive/job"
"github.com/cilium/statedb"
"github.com/cilium/statedb/index"
)
// Register creates a new reconciler and registers to the application
// lifecycle. To be used with cell.Invoke when the API of the reconciler
// is not needed.
func Register[Obj comparable](cfg Config[Obj], params Params) error {
_, err := New(cfg, params)
return err
}
// New creates and registers a new reconciler.
func New[Obj comparable](cfg Config[Obj], p Params) (Reconciler[Obj], error) {
if err := cfg.validate(); err != nil {
return nil, err
}
metrics := cfg.Metrics
if metrics == nil {
if p.DefaultMetrics == nil {
metrics = NewUnpublishedExpVarMetrics()
} else {
metrics = p.DefaultMetrics
}
}
idx := cfg.Table.PrimaryIndexer()
objectToKey := func(o any) index.Key {
return idx.ObjectToKey(o.(Obj))
}
r := &reconciler[Obj]{
Params: p,
Config: cfg,
metrics: metrics,
retries: newRetries(cfg.RetryBackoffMinDuration, cfg.RetryBackoffMaxDuration, objectToKey),
externalFullTrigger: make(chan struct{}, 1),
primaryIndexer: idx,
}
g := p.Jobs.NewGroup(p.Health)
g.Add(job.OneShot("reconciler-loop", r.loop))
p.Lifecycle.Append(g)
return r, nil
}
type Params struct {
cell.In
Lifecycle cell.Lifecycle
Log *slog.Logger
DB *statedb.DB
Jobs job.Registry
ModuleID cell.FullModuleID
Health cell.Health
DefaultMetrics Metrics `optional:"true"`
}
type reconciler[Obj comparable] struct {
Params
Config Config[Obj]
metrics Metrics
retries *retries
externalFullTrigger chan struct{}
primaryIndexer statedb.Indexer[Obj]
changes *statedb.ChangeIterator[Obj]
}
func (r *reconciler[Obj]) TriggerFullReconciliation() {
select {
case r.externalFullTrigger <- struct{}{}:
default:
}
}
// WaitForReconciliation blocks until all objects have been reconciled or the context
// has cancelled.
func WaitForReconciliation[Obj any](ctx context.Context, db *statedb.DB, table statedb.Table[Obj], statusIndex statedb.Index[Obj, StatusKind]) error {
for {
txn := db.ReadTxn()
// See if there are any pending or error'd objects.
_, _, watchPending, okPending := table.GetWatch(txn, statusIndex.Query(StatusKindPending))
_, _, watchError, okError := table.GetWatch(txn, statusIndex.Query(StatusKindError))
if !okPending && !okError {
return nil
}
// Wait for updates before checking again.
select {
case <-ctx.Done():
return ctx.Err()
case <-watchPending:
case <-watchError:
}
}
}
func (r *reconciler[Obj]) loop(ctx context.Context, health cell.Health) error {
var fullReconTickerChan <-chan time.Time
if r.Config.FullReconcilationInterval > 0 {
fullReconTicker := time.NewTicker(r.Config.FullReconcilationInterval)
defer fullReconTicker.Stop()
fullReconTickerChan = fullReconTicker.C
}
// Create the change iterator to watch for inserts and deletes to the table.
wtxn := r.DB.WriteTxn(r.Config.Table)
changes, err := r.Config.Table.Changes(wtxn)
txn := wtxn.Commit()
if err != nil {
return fmt.Errorf("watching for changes failed: %w", err)
}
tableWatchChan := closedWatchChannel
fullReconciliation := false
for {
if r.Config.RateLimiter != nil {
r.Config.RateLimiter.Wait(ctx)
}
// Wait for trigger
select {
case <-ctx.Done():
return ctx.Err()
case <-fullReconTickerChan:
fullReconciliation = true
case <-r.externalFullTrigger:
fullReconciliation = true
case <-r.retries.Wait():
// Object(s) are ready to be retried
case <-tableWatchChan:
// Table has changed
}
// Perform incremental reconciliation and retries of previously failed
// objects.
errs := r.incremental(ctx, txn, changes)
// Refresh the transaction to read the new changes.
txn = r.DB.ReadTxn()
tableWatchChan = changes.Watch(txn)
if fullReconciliation && r.Config.Table.Initialized(txn) {
// Time to perform a full reconciliation. An incremental reconciliation
// has been performed prior to this, so the assumption is that everything
// is up to date (provided incremental reconciliation did not fail). We
// report full reconciliation disparencies as they're indicative of something
// interfering with Cilium operations.
// Clear full reconciliation even if there's errors. Individual objects
// will be retried via the retry queue.
fullReconciliation = false
errs = append(errs, r.full(ctx, txn)...)
}
if len(errs) == 0 {
health.OK(
fmt.Sprintf("OK, %d objects", r.Config.Table.NumObjects(txn)))
} else {
health.Degraded(
fmt.Sprintf("%d failure(s)", len(errs)),
joinErrors(errs))
}
}
}