-
Notifications
You must be signed in to change notification settings - Fork 364
/
status.go
232 lines (203 loc) · 5.96 KB
/
status.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
// 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.
// This file contains code derived from Contour,
// https://github.com/projectcontour/contour
// from the source file
// https://github.com/projectcontour/contour/blob/main/internal/k8s/status.go
// and is provided here subject to the following:
// Copyright Project Contour Authors
// SPDX-License-Identifier: Apache-2.0
package status
import (
"context"
"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/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwapiv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1"
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
)
// 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
sendUpdates chan struct{}
updateChannel chan Update
}
func NewUpdateHandler(log logr.Logger, client client.Client) *UpdateHandler {
return &UpdateHandler{
log: log,
client: client,
sendUpdates: make(chan struct{}),
updateChannel: make(chan Update, 100),
}
}
func (u *UpdateHandler) apply(update Update) {
if err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
obj := update.Resource
// 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")
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)
}
}
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.
close(u.sendUpdates)
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{
enabled: u.sendUpdates,
updateChannel: u.updateChannel,
}
}
// 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 {
enabled <-chan struct{}
updateChannel chan<- Update
}
// Send sends the given Update off to the update channel for writing by the UpdateHandler.
func (u *UpdateWriter) Send(update Update) {
// Non-blocking receive to see if we should pass along update.
select {
case <-u.enabled:
u.updateChannel <- update
default:
}
}
// isStatusEqual checks if two objects have equivalent status.
//
// Supported objects:
//
// GatewayClasses
// Gateway
// HTTPRoute
// TLSRoute
// TCPRoute
// UDPRoute
// GRPCRoute
// EnvoyPatchPolicy
// ClientTrafficPolicy
func isStatusEqual(objA, objB interface{}) bool {
opts := cmpopts.IgnoreFields(metav1.Condition{}, "LastTransitionTime")
switch a := objA.(type) {
case *gwapiv1b1.GatewayClass:
if b, ok := objB.(*gwapiv1b1.GatewayClass); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *gwapiv1b1.Gateway:
if b, ok := objB.(*gwapiv1b1.Gateway); ok {
if cmp.Equal(a.Status, b.Status, opts) {
return true
}
}
case *gwapiv1b1.HTTPRoute:
if b, ok := objB.(*gwapiv1b1.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 *gwapiv1a2.GRPCRoute:
if b, ok := objB.(*gwapiv1a2.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
}
}
}
return false
}