/
registrar.go
277 lines (244 loc) Β· 7.41 KB
/
registrar.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
/*
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 watch
import (
"context"
"fmt"
"sort"
"sync"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/event"
)
type vitals struct {
gvk schema.GroupVersionKind
registrars map[*Registrar]bool
}
type vitalsByGVK map[schema.GroupVersionKind]vitals
func (w *vitals) merge(wv vitals) vitals {
if w == nil {
return wv
}
registrars := make(map[*Registrar]bool)
for r := range w.registrars {
registrars[r] = true
}
for r := range wv.registrars {
registrars[r] = true
}
return vitals{
gvk: w.gvk,
registrars: registrars,
}
}
// recordKeeper holds the source of truth for the intended state of the manager
// This is essentially a read/write lock on the wrapped map (the `intent` variable).
type recordKeeper struct {
// map[registrarName][kind]
intent map[string]vitalsByGVK
intentMux sync.RWMutex
registrars map[string]*Registrar
mgr *Manager
metrics *reporter
}
func (r *recordKeeper) NewRegistrar(parentName string, events chan<- event.GenericEvent) (*Registrar, error) {
r.intentMux.Lock()
defer r.intentMux.Unlock()
if _, ok := r.registrars[parentName]; ok {
return nil, fmt.Errorf("registrar for %s already exists", parentName)
}
out := &Registrar{
parentName: parentName,
mgr: r.mgr,
managedKinds: r,
events: events,
}
r.registrars[parentName] = out
return out, nil
}
// RemoveRegistrar removes a registrar and all its watches.
func (r *recordKeeper) RemoveRegistrar(parentName string) error {
r.intentMux.Lock()
registrar := r.registrars[parentName]
r.intentMux.Unlock()
if registrar == nil {
return nil
}
if err := registrar.ReplaceWatch(context.Background(), nil); err != nil {
return err
}
r.intentMux.Lock()
defer r.intentMux.Unlock()
delete(r.registrars, parentName)
return nil
}
func (r *recordKeeper) Update(parentName string, m vitalsByGVK) {
r.intentMux.Lock()
defer r.intentMux.Unlock()
defer func() {
if err := r.metrics.reportGvkIntentCount(int64(r.count())); err != nil {
log.Error(err, "while reporting gvk intent count metric")
}
}()
if _, ok := r.intent[parentName]; !ok {
r.intent[parentName] = make(vitalsByGVK)
}
for gvk, v := range m {
r.intent[parentName][gvk] = v
}
}
// ReplaceRegistrarRoster replaces the desired set of watches for the specified registrar using provided roster.
// Ownership is taken over roster - it is not currently deep-copied.
func (r *recordKeeper) ReplaceRegistrarRoster(reg *Registrar, roster map[schema.GroupVersionKind]vitals) {
r.intentMux.Lock()
defer r.intentMux.Unlock()
defer func() {
if err := r.metrics.reportGvkIntentCount(int64(r.count())); err != nil {
log.Error(err, "while reporting gvk intent count metric")
}
}()
r.intent[reg.parentName] = roster
}
// Watching returns whether a GVK is being watched by a given registrar.
func (r *recordKeeper) Watching(parentName string, gvk schema.GroupVersionKind) bool {
r.intentMux.RLock()
defer r.intentMux.RUnlock()
_, ok := r.intent[parentName][gvk]
return ok
}
// Remove removes the intent-to-watch a particular resource kind.
func (r *recordKeeper) Remove(parentName string, gvk schema.GroupVersionKind) {
r.intentMux.Lock()
defer r.intentMux.Unlock()
defer func() {
if err := r.metrics.reportGvkIntentCount(int64(r.count())); err != nil {
log.Error(err, "while reporting gvk intent count metric")
}
}()
delete(r.intent[parentName], gvk)
}
// Get returns all managed vitals, merged across registrars.
func (r *recordKeeper) Get() vitalsByGVK {
r.intentMux.RLock()
defer r.intentMux.RUnlock()
cpy := make(map[string]vitalsByGVK)
for k := range r.intent {
cpy[k] = make(vitalsByGVK)
for k2, v := range r.intent[k] {
cpy[k][k2] = v
}
}
managedKinds := make(vitalsByGVK)
for _, registrar := range cpy {
for gvk, v := range registrar {
if mk, ok := managedKinds[gvk]; ok {
merged := mk.merge(v)
managedKinds[gvk] = merged
} else {
managedKinds[gvk] = v
}
}
}
return managedKinds
}
// count returns total gvk count across all registrars.
func (r *recordKeeper) count() int {
managedKinds := make(map[schema.GroupVersionKind]bool)
for _, registrar := range r.intent {
for gvk := range registrar {
managedKinds[gvk] = true
}
}
return len(managedKinds)
}
// GetGVK returns all managed kinds, merged across registrars.
func (r *recordKeeper) GetGVK() []schema.GroupVersionKind {
var gvks []schema.GroupVersionKind
g := r.Get()
for gvk := range g {
gvks = append(gvks, gvk)
}
sort.Slice(gvks, func(i, j int) bool {
return gvks[i].String() < gvks[j].String()
})
return gvks
}
func newRecordKeeper() (*recordKeeper, error) {
metrics, err := newStatsReporter()
if err != nil {
return nil, err
}
return &recordKeeper{
intent: make(map[string]vitalsByGVK),
registrars: make(map[string]*Registrar),
metrics: metrics,
}, nil
}
// A Registrar allows a parent to add/remove child watches.
type Registrar struct {
parentName string
mgr *Manager
managedKinds *recordKeeper
events chan<- event.GenericEvent
mux sync.RWMutex
}
// AddWatch registers a watch for the given kind.
//
// AddWatch will only block if all of the following are true:
// - The registrar is joining an existing watch
// - The registrar's event channel does not have sufficient capacity to receive existing resources
// - The consumer of the channel does not receive any unbuffered events.
//
// XXXX also may block if the watch manager has not been started.
func (r *Registrar) AddWatch(ctx context.Context, gvk schema.GroupVersionKind) error {
r.mux.Lock()
defer r.mux.Unlock()
wv := vitals{
gvk: gvk,
registrars: map[*Registrar]bool{r: true},
}
r.managedKinds.Update(r.parentName, vitalsByGVK{gvk: wv})
return r.mgr.addWatch(ctx, r, gvk)
}
// ReplaceWatch replaces the set of watched resources.
func (r *Registrar) ReplaceWatch(ctx context.Context, gvks []schema.GroupVersionKind) error {
r.mux.Lock()
defer r.mux.Unlock()
roster := make(vitalsByGVK)
for _, gvk := range gvks {
wv := vitals{
gvk: gvk,
registrars: map[*Registrar]bool{r: true},
}
roster[gvk] = wv
}
r.managedKinds.ReplaceRegistrarRoster(r, roster)
return r.mgr.replaceWatches(ctx, r)
}
// RemoveWatch removes a watch for the given kind.
// Ignores the request if the kind was not previously watched.
func (r *Registrar) RemoveWatch(ctx context.Context, gvk schema.GroupVersionKind) error {
r.mux.Lock()
defer r.mux.Unlock()
r.managedKinds.Remove(r.parentName, gvk)
return r.mgr.removeWatch(ctx, r, gvk)
}
// IfWatching executes the passed function if the provided GVK is being watched
// by the registrar, ignoring it if not. It returns whether the function was
// executed and any errors returned by the executed function.
func (r *Registrar) IfWatching(gvk schema.GroupVersionKind, fn func() error) (bool, error) {
r.mux.RLock()
defer r.mux.RUnlock()
if r.managedKinds.Watching(r.parentName, gvk) {
return true, fn()
}
return false, nil
}