-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathstatus_updater.go
357 lines (322 loc) · 9.08 KB
/
status_updater.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
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package kubernetes
import (
"context"
"sync"
"time"
"github.com/go-logr/logr"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwapiv1a3 "sigs.k8s.io/gateway-api/apis/v1alpha3"
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/gatewayapi/resource"
"github.com/envoyproxy/gateway/internal/metrics"
)
// Update contains an all the information needed to update an object's status.
// Send down a channel to the goroutine that actually writes the changes back.
type Update struct {
NamespacedName types.NamespacedName
Resource client.Object
Mutator Mutator
}
// Mutator is an interface to hold mutator functions for status updates.
type Mutator interface {
Mutate(obj client.Object) client.Object
}
// MutatorFunc is a function adaptor for Mutators.
type MutatorFunc func(client.Object) client.Object
// Mutate adapts the MutatorFunc to fit through the Mutator interface.
func (m MutatorFunc) Mutate(old client.Object) client.Object {
if m == nil {
return nil
}
return m(old)
}
// UpdateHandler holds the details required to actually write an Update back to the referenced object.
type UpdateHandler struct {
log logr.Logger
client client.Client
updateChannel chan Update
wg *sync.WaitGroup
}
func NewUpdateHandler(log logr.Logger, client client.Client) *UpdateHandler {
u := &UpdateHandler{
log: log,
client: client,
updateChannel: make(chan Update, 1000),
wg: new(sync.WaitGroup),
}
u.wg.Add(1)
return u
}
func (u *UpdateHandler) apply(update Update) {
var (
startTime = time.Now()
obj = update.Resource
objKind = kindOf(obj)
)
defer func() {
updateDuration := time.Since(startTime)
statusUpdateDurationSeconds.With(kindLabel.Value(objKind)).Record(updateDuration.Seconds())
}()
if err := retry.OnError(retry.DefaultBackoff, func(err error) bool {
if kerrors.IsConflict(err) {
statusUpdateTotal.WithFailure(metrics.ReasonConflict, kindLabel.Value(objKind)).Increment()
return true
}
return false
}, func() error {
// Get the resource.
if err := u.client.Get(context.Background(), update.NamespacedName, obj); err != nil {
if kerrors.IsNotFound(err) {
return nil
}
return err
}
newObj := update.Mutator.Mutate(obj)
if isStatusEqual(obj, newObj) {
u.log.WithName(update.NamespacedName.Name).
WithName(update.NamespacedName.Namespace).
Info("status unchanged, bypassing update")
statusUpdateTotal.WithStatus(statusNoAction, kindLabel.Value(objKind)).Increment()
return nil
}
newObj.SetUID(obj.GetUID())
return u.client.Status().Update(context.Background(), newObj)
}); err != nil {
u.log.Error(err, "unable to update status", "name", update.NamespacedName.Name,
"namespace", update.NamespacedName.Namespace)
statusUpdateTotal.WithFailure(metrics.ReasonError, kindLabel.Value(objKind)).Increment()
} else {
statusUpdateTotal.WithSuccess(kindLabel.Value(objKind)).Increment()
}
}
func (u *UpdateHandler) NeedLeaderElection() bool {
return true
}
// Start runs the goroutine to perform status writes.
func (u *UpdateHandler) Start(ctx context.Context) error {
u.log.Info("started status update handler")
defer u.log.Info("stopped status update handler")
// Enable Updaters to start sending updates to this handler.
u.wg.Done()
for {
select {
case <-ctx.Done():
return nil
case update := <-u.updateChannel:
u.log.Info("received a status update", "namespace", update.NamespacedName.Namespace,
"name", update.NamespacedName.Name)
u.apply(update)
}
}
}
// Writer retrieves the interface that should be used to write to the UpdateHandler.
func (u *UpdateHandler) Writer() Updater {
return &UpdateWriter{
updateChannel: u.updateChannel,
wg: u.wg,
}
}
// Updater describes an interface to send status updates somewhere.
type Updater interface {
Send(u Update)
}
// UpdateWriter takes status updates and sends these to the UpdateHandler via a channel.
type UpdateWriter struct {
updateChannel chan<- Update
wg *sync.WaitGroup
}
// Send sends the given Update off to the update channel for writing by the UpdateHandler.
func (u *UpdateWriter) Send(update Update) {
// Wait until updater is ready
u.wg.Wait()
u.updateChannel <- update
}
// isStatusEqual checks if two objects have equivalent status.
//
// Supported objects:
//
// GatewayClasses
// Gateway
// HTTPRoute
// TLSRoute
// TCPRoute
// UDPRoute
// GRPCRoute
// EnvoyPatchPolicy
// ClientTrafficPolicy
// BackendTrafficPolicy
// SecurityPolicy
// BackendTLSPolicy
// EnvoyExtensionPolicy
// Unstructured (for server extension policies)
func isStatusEqual(objA, objB interface{}) bool {
opts := cmp.Options{
cmpopts.IgnoreFields(metav1.Condition{}, "LastTransitionTime"),
cmpopts.IgnoreMapEntries(func(k string, _ any) bool {
return k == "lastTransitionTime"
}),
}
switch a := objA.(type) {
case *gwapiv1.GatewayClass:
if b, ok := objB.(*gwapiv1.GatewayClass); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *gwapiv1.Gateway:
if b, ok := objB.(*gwapiv1.Gateway); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *gwapiv1.HTTPRoute:
if b, ok := objB.(*gwapiv1.HTTPRoute); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *gwapiv1a2.TLSRoute:
if b, ok := objB.(*gwapiv1a2.TLSRoute); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *gwapiv1a2.TCPRoute:
if b, ok := objB.(*gwapiv1a2.TCPRoute); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *gwapiv1a2.UDPRoute:
if b, ok := objB.(*gwapiv1a2.UDPRoute); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *gwapiv1.GRPCRoute:
if b, ok := objB.(*gwapiv1.GRPCRoute); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *egv1a1.EnvoyPatchPolicy:
if b, ok := objB.(*egv1a1.EnvoyPatchPolicy); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *egv1a1.ClientTrafficPolicy:
if b, ok := objB.(*egv1a1.ClientTrafficPolicy); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *egv1a1.BackendTrafficPolicy:
if b, ok := objB.(*egv1a1.BackendTrafficPolicy); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *egv1a1.SecurityPolicy:
if b, ok := objB.(*egv1a1.SecurityPolicy); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *gwapiv1a3.BackendTLSPolicy:
if b, ok := objB.(*gwapiv1a3.BackendTLSPolicy); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *egv1a1.EnvoyExtensionPolicy:
if b, ok := objB.(*egv1a1.EnvoyExtensionPolicy); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *unstructured.Unstructured:
if b, ok := objB.(*unstructured.Unstructured); ok {
if cmp.Equal(a.Object["status"], b.Object["status"], opts) {
return true
}
}
case *egv1a1.Backend:
if b, ok := objB.(*egv1a1.Backend); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
}
return false
}
// kindOf returns the known kind string for the given Kubernetes object,
// returns Unknown for the unsupported object.
//
// Supported objects:
//
// GatewayClasses
// Gateway
// HTTPRoute
// TLSRoute
// TCPRoute
// UDPRoute
// GRPCRoute
// EnvoyPatchPolicy
// ClientTrafficPolicy
// BackendTrafficPolicy
// SecurityPolicy
// BackendTLSPolicy
// EnvoyExtensionPolicy
// Unstructured (for Extension Policies)
func kindOf(obj interface{}) string {
var kind string
switch o := obj.(type) {
case *gwapiv1.GatewayClass:
kind = resource.KindGatewayClass
case *gwapiv1.Gateway:
kind = resource.KindGateway
case *gwapiv1.HTTPRoute:
kind = resource.KindHTTPRoute
case *gwapiv1a2.TLSRoute:
kind = resource.KindTLSRoute
case *gwapiv1a2.TCPRoute:
kind = resource.KindTCPRoute
case *gwapiv1a2.UDPRoute:
kind = resource.KindUDPRoute
case *gwapiv1.GRPCRoute:
kind = resource.KindGRPCRoute
case *egv1a1.EnvoyPatchPolicy:
kind = resource.KindEnvoyPatchPolicy
case *egv1a1.ClientTrafficPolicy:
kind = resource.KindClientTrafficPolicy
case *egv1a1.BackendTrafficPolicy:
kind = resource.KindBackendTrafficPolicy
case *egv1a1.SecurityPolicy:
kind = resource.KindSecurityPolicy
case *egv1a1.EnvoyExtensionPolicy:
kind = resource.KindEnvoyExtensionPolicy
case *gwapiv1a3.BackendTLSPolicy:
kind = resource.KindBackendTLSPolicy
case *unstructured.Unstructured:
kind = o.GetKind()
case *egv1a1.Backend:
kind = resource.KindBackend
default:
kind = "Unknown"
}
return kind
}