/
udproute_util.go
439 lines (366 loc) · 14 KB
/
udproute_util.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package renderer
import (
"fmt"
// "github.com/go-logr/logr"
// apiv1 "k8s.io/api/core/v1"
// "k8s.io/apimachinery/pkg/runtime"
// ctlr "sigs.k8s.io/controller-runtime"
// "sigs.k8s.io/controller-runtime/pkg/manager" corev1 "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
stnrgwv1 "github.com/l7mp/stunner-gateway-operator/api/v1"
"github.com/l7mp/stunner-gateway-operator/internal/config"
"github.com/l7mp/stunner-gateway-operator/internal/store"
)
func (r *Renderer) allUDPRoutes() []*stnrgwv1.UDPRoute {
rs := store.UDPRoutes.GetAll()
for _, uv1a2 := range store.UDPRoutesV1A2.GetAll() {
if isRouteMasked(uv1a2) {
r.log.Info("ignoring gwapiv1a2.UDPRoute masking a stunnerv1.UDPRoute:",
"name", uv1a2.GetName(), "namespace", uv1a2.GetNamespace())
continue
}
rs = append(rs, uv1a2)
}
return rs
}
func (r *Renderer) getUDPRoutes4Listener(gw *gwapiv1.Gateway, l *gwapiv1.Listener) []*stnrgwv1.UDPRoute {
r.log.V(4).Info("getUDPRoutes4Listener", "gateway", store.GetObjectKey(gw), "listener", l.Name)
ret := make([]*stnrgwv1.UDPRoute, 0)
rs := r.allUDPRoutes()
for i := range rs {
ro := rs[i]
r.log.V(4).Info("getUDPRoutes4Listener: considering route for listener", "gateway",
store.GetObjectKey(gw), "listener", l.Name, "route",
store.GetObjectKey(ro))
for j := range ro.Spec.CommonRouteSpec.ParentRefs {
p := ro.Spec.CommonRouteSpec.ParentRefs[j]
found, reason := resolveParentRef(ro, &p, gw, l)
if !found {
r.log.V(4).Info("getUDPRoutes4Listener: parent rejected for listener",
"gateway", store.GetObjectKey(gw), "listener", l.Name,
"route", store.GetObjectKey(ro), "parent", dumpParentRef(&p),
"reason", reason)
continue
}
r.log.V(4).Info("getUDPRoutes4Listener: route found", "gateway",
store.GetObjectKey(gw), "listener", l.Name, "route",
store.GetObjectKey(ro), "V1A2", isRouteV1A2(ro))
// route made it this far: attach!
ret = append(ret, ro)
}
}
return ret
}
func resolveParentRef(ro *stnrgwv1.UDPRoute, p *gwapiv1.ParentReference, gw *gwapiv1.Gateway, l *gwapiv1.Listener) (bool, string) {
if p.Group != nil && *p.Group != gwapiv1.Group(gwapiv1.GroupVersion.Group) {
return false, fmt.Sprintf("parent group %q does not match gateway group %q",
string(*p.Group), gwapiv1.GroupVersion.Group)
}
if p.Kind != nil && *p.Kind != "Gateway" {
return false, fmt.Sprintf("parent kind %q does not match gateway kind %q",
string(*p.Kind), "Gateway")
}
namespace := gwapiv1.Namespace(ro.GetNamespace())
if p.Namespace != nil {
namespace = *p.Namespace
}
if namespace != gwapiv1.Namespace(gw.GetNamespace()) {
return false, fmt.Sprintf("parent namespace %q does not match gateway namespace %q",
string(namespace), gw.GetNamespace())
}
if p.Name != gwapiv1.ObjectName(gw.GetName()) {
return false, fmt.Sprintf("parent name %q does not match gateway name %q",
string(p.Name), gw.GetName())
}
allowed, msg := gatewayAllowsNamespace(ro, gw, l)
if !allowed {
return false, msg
}
if p.SectionName != nil && *p.SectionName != l.Name {
return false, fmt.Sprintf("parent SectionName %q does not match listener name %q",
string(*p.SectionName), l.Name)
}
return true, ""
}
func gatewayAllowsNamespace(ro *stnrgwv1.UDPRoute, gw *gwapiv1.Gateway, l *gwapiv1.Listener) (bool, string) {
// default namespace attachment policy: Same
if l.AllowedRoutes == nil || l.AllowedRoutes.Namespaces == nil || l.AllowedRoutes.Namespaces.From == nil {
return gatewayAllowsSameNamespace(ro, gw)
}
allowedNamespaces := l.AllowedRoutes.Namespaces
switch *allowedNamespaces.From {
case gwapiv1.NamespacesFromAll:
return true, ""
case gwapiv1.NamespacesFromSelector:
if allowedNamespaces.Selector == nil {
return false, fmt.Sprintf("parent %s (namespace attachment policy: Selector): Selector missing",
store.GetObjectKey(gw))
}
selector, err := metav1.LabelSelectorAsSelector(allowedNamespaces.Selector)
if err != nil {
return false, fmt.Sprintf("parent %s (namespace attachment policy: Selector): cannot create selector: %s",
store.GetObjectKey(gw), err.Error())
}
// get the namespace of the route
ns := types.NamespacedName{Name: ro.GetNamespace()}
namespace := store.Namespaces.GetObject(ns)
if namespace == nil {
return false, fmt.Sprintf("parent %s (namespace attachment policy: Selector): cannot "+
"find namespace %q for udproute %q in local storage", store.GetObjectKey(gw),
store.GetObjectKey(ro), ns.String())
}
res := selector.Matches(labels.Set(namespace.Labels))
if !res {
return false, fmt.Sprintf("parent %s (namespace attachment policy: Selector): labels on "+
"namespace %q do not match Selector", store.GetObjectKey(gw), ns.String())
}
return true, ""
default:
// NamespacesFromSame is the default
return gatewayAllowsSameNamespace(ro, gw)
}
}
func gatewayAllowsSameNamespace(ro *stnrgwv1.UDPRoute, gw *gwapiv1.Gateway) (bool, string) {
allowed := gw.GetNamespace() == ro.GetNamespace()
if !allowed {
return false, fmt.Sprintf("parent %q/%q (namespace attachment policy: Same) rejects route %q/%q",
gw.GetName(), gw.GetNamespace(), ro.GetName(), ro.GetNamespace())
}
return true, ""
}
func initRouteStatus(ro *stnrgwv1.UDPRoute) {
ro.Status.Parents = []gwapiv1.RouteParentStatus{}
}
// isParentController returns true if at least one of the parents of the route is controlled by us
func (r *Renderer) isRouteControlled(ro *stnrgwv1.UDPRoute) bool {
gcs := r.getGatewayClasses()
for i := range ro.Spec.ParentRefs {
p := &ro.Spec.ParentRefs[i]
// obtain the parent gw
gw := r.getParentGateway(ro, p)
if gw == nil {
continue
}
// obtain the gatewayclass
for _, gc := range gcs {
if gc.GetName() == string(gw.Spec.GatewayClassName) {
r.log.V(2).Info("route is handled by this controller: accepting",
"route", store.GetObjectKey(ro),
"parent", dumpParentRef(p),
"linked-gateway-class", gw.Spec.GatewayClassName,
)
return true
}
}
}
r.log.V(2).Info("route is handled by another controller: rejecting",
"route", store.GetObjectKey(ro),
)
return false
}
// isParentOutContext returns true if (1) the parent exists and (2) it is NOT included in the
// gateway context being processed (in which case we do not generate a status for the parent)
func (r *Renderer) isParentOutContext(gws *store.GatewayStore, ro *stnrgwv1.UDPRoute, p *gwapiv1.ParentReference) bool {
// find the corresponding gateway
ns := ro.GetNamespace()
if p.Namespace != nil {
ns = string(*p.Namespace)
}
namespacedName := types.NamespacedName{Namespace: ns, Name: string(p.Name)}
ret := store.Gateways.GetObject(namespacedName) != nil && gws.GetObject(namespacedName) == nil
r.log.V(4).Info("isParentOutContext", "route", store.GetObjectKey(ro),
"parent", dumpParentRef(p), "gw-context-length", gws.Len(), "result", ret)
return ret
}
// className == "" means "do not consider classness of parent", this is useful for generating a
// route status that is consistent across rendering contexts
func (r *Renderer) isParentAcceptingRoute(ro *stnrgwv1.UDPRoute, p *gwapiv1.ParentReference, className string) bool {
// r.log.V(4).Info("isParentAcceptingRoute", "route", store.GetObjectKey(ro),
// "parent", dumpParentRef(p))
gw := r.getParentGateway(ro, p)
if gw == nil {
r.log.V(4).Info("no gateway found for Parent", "route",
store.GetObjectKey(ro), "parent", dumpParentRef(p))
return false
}
// does the parent belong to the class we are processing: we don't want to generate routes
// for gateways that link to other classes
if className != "" && gw.Spec.GatewayClassName != gwapiv1.ObjectName(className) {
r.log.V(4).Info("parent links to a gateway that is being managed by another "+
"gateway-class: rejecting", "route", store.GetObjectKey(ro), "parent",
dumpParentRef(p), "linked-gateway-class", gw.Spec.GatewayClassName,
"current-gateway-class", className)
return false
}
// is there a listener that accepts us?
for i := range gw.Spec.Listeners {
l := gw.Spec.Listeners[i]
found, msg := resolveParentRef(ro, p, gw, &l)
if found {
r.log.V(3).Info("isParentAcceptingRoute: gateway/listener found for parent",
"route", store.GetObjectKey(ro), "parent", dumpParentRef(p),
"gateway", gw.GetName(), "listener", l.Name)
return true
} else {
r.log.V(4).Info("isParentAcceptingRoute: gateway/listener does not accept route",
"route", store.GetObjectKey(ro), "parent", dumpParentRef(p),
"gateway", gw.GetName(), "listener", l.Name, "message", msg)
}
}
r.log.V(4).Info("isParentAcceptingRoute result", "route", store.GetObjectKey(ro),
"parent", fmt.Sprintf("%#v", *p), "result", "rejected")
return false
}
func (r *Renderer) getParentGateway(ro *stnrgwv1.UDPRoute, p *gwapiv1.ParentReference) *gwapiv1.Gateway {
// find the corresponding gateway
ns := ro.GetNamespace()
if p.Namespace != nil {
ns = string(*p.Namespace)
}
namespacedName := types.NamespacedName{Namespace: ns, Name: string(p.Name)}
return store.Gateways.GetObject(namespacedName)
}
// invalidateMaskedRoutes invalidates a masked GWAPIV1A2 UDPROute
func (r *Renderer) invalidateMaskedRoutes(c *RenderContext) {
for _, ro := range store.UDPRoutesV1A2.GetAll() {
if !isRouteMasked(ro) || !r.isRouteControlled(ro) {
continue
}
initRouteStatus(ro)
for i := range ro.Spec.ParentRefs {
p := ro.Spec.ParentRefs[i]
parentAccept := r.isParentAcceptingRoute(ro, &p, "")
// automatically handles masked routes
setRouteConditionStatus(ro, &p, config.ControllerName, parentAccept, nil)
}
c.update.UpsertQueue.UDPRoutesV1A2.Upsert(ro)
}
}
func setRouteConditionStatus(ro *stnrgwv1.UDPRoute, p *gwapiv1.ParentReference, controllerName string, accepted bool, backendErr error) {
// ns := gwapiv1.Namespace(ro.GetNamespace())
// gr := gwapiv1.Group(gwapiv1.GroupVersion.Group)
// kind := gwapiv1.Kind("Gateway")
pRef := gwapiv1.ParentReference{
Name: p.Name,
}
if p.Group != nil && *p.Group != gwapiv1.Group(gwapiv1.GroupVersion.Group) {
pRef.Group = p.Group
}
if p.Kind != nil && *p.Kind != "Gateway" {
pRef.Kind = p.Kind
}
if p.Namespace != nil {
pRef.Namespace = p.Namespace
}
if p.SectionName != nil {
pRef.SectionName = p.SectionName
}
s := gwapiv1.RouteParentStatus{
ParentRef: pRef,
ControllerName: gwapiv1.GatewayController(controllerName),
Conditions: []metav1.Condition{},
}
if isRouteMasked(ro) {
setRouteAcceptedCondition(ro, &s.Conditions, gwapiv1.RouteReasonPending,
metav1.ConditionFalse, "GW API UDPRoute masked by a STUNnerV1 UDPRoute")
} else {
if accepted {
setRouteAcceptedCondition(ro, &s.Conditions, gwapiv1.RouteReasonAccepted,
metav1.ConditionTrue, "parent accepts the route")
} else {
setRouteAcceptedCondition(ro, &s.Conditions, gwapiv1.RouteReasonNotAllowedByListeners,
metav1.ConditionFalse, "parent rejects the route")
}
}
var resolvedCond metav1.Condition
if backendErr != nil {
var reason gwapiv1.RouteConditionReason
switch {
case IsNonCriticalError(backendErr, InvalidBackendKind), IsNonCriticalError(backendErr, InvalidBackendGroup):
// "RouteReasonInvalidKind" is used with the "ResolvedRefs" condition when
// one of the Route's rules has a reference to an unknown or unsupported
// Group and/or Kind.
reason = gwapiv1.RouteReasonInvalidKind
default:
reason = gwapiv1.RouteReasonBackendNotFound
}
resolvedCond = metav1.Condition{
Type: string(gwapiv1.RouteConditionResolvedRefs),
Status: metav1.ConditionFalse,
ObservedGeneration: ro.Generation,
LastTransitionTime: metav1.Now(),
Reason: string(reason),
Message: "at least one backend reference failed to be successfully resolved",
}
} else {
resolvedCond = metav1.Condition{
Type: string(gwapiv1.RouteConditionResolvedRefs),
Status: metav1.ConditionTrue,
ObservedGeneration: ro.Generation,
LastTransitionTime: metav1.Now(),
Reason: string(gwapiv1.RouteReasonResolvedRefs),
Message: "all backend references successfully resolved",
}
}
meta.SetStatusCondition(&s.Conditions, resolvedCond)
ro.Status.Parents = append(ro.Status.Parents, s)
}
func setRouteAcceptedCondition(ro *stnrgwv1.UDPRoute, s *[]metav1.Condition, reason gwapiv1.RouteConditionReason, status metav1.ConditionStatus, message string) {
meta.SetStatusCondition(s, metav1.Condition{
Type: string(gwapiv1.RouteConditionAccepted),
Status: status,
ObservedGeneration: ro.Generation,
LastTransitionTime: metav1.Now(),
Reason: string(reason),
Message: message,
})
}
func dumpParentRef(p *gwapiv1.ParentReference) string {
g, k, ns, sn := "<NIL>", "<NIL>", "<NIL>", "<NIL>"
if p.Group != nil {
g = string(*p.Group)
}
if p.Kind != nil {
k = string(*p.Kind)
}
if p.Namespace != nil {
ns = string(*p.Namespace)
}
if p.SectionName != nil {
sn = string(*p.SectionName)
}
return fmt.Sprintf("{Group: %s, Kind: %s, Namespace: %s, Name: %s, SectionName: %s}",
g, k, ns, p.Name, sn)
}
func dumpBackendRef(b *stnrgwv1.BackendRef) string {
g, k, ns := "<NIL>", "<NIL>", "<NIL>"
if b.Group != nil {
g = string(*b.Group)
}
if b.Kind != nil {
k = string(*b.Kind)
}
if b.Namespace != nil {
ns = string(*b.Namespace)
}
return fmt.Sprintf("{Group: %s, Kind: %s, Namespace: %s, Name: %s}",
g, k, ns, b.Name)
}
// check by pointer: namespacedname is not unique across stunnerv1 and v1a2 routes
//
//nolint:unused
func isRouteV1(ro client.Object) bool {
return store.UDPRoutes.Get(store.GetNamespacedName(ro)) == ro
}
// check by pointer: namespacedname is not unique across stunnerv1 and v1a2 routes
func isRouteV1A2(ro client.Object) bool {
return store.UDPRoutesV1A2.Get(store.GetNamespacedName(ro)) == ro
}
func isRouteMasked(ro client.Object) bool {
return isRouteV1A2(ro) && store.UDPRoutes.Get(store.GetNamespacedName(ro)) != nil
}