-
Notifications
You must be signed in to change notification settings - Fork 289
/
predicates.go
549 lines (466 loc) · 17.4 KB
/
predicates.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// 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"
"fmt"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"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"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwapiv1a3 "sigs.k8s.io/gateway-api/apis/v1alpha3"
mcsapi "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/gatewayapi"
"github.com/envoyproxy/gateway/internal/utils"
)
// nolint: gosec
const oidcHMACSecretName = "envoy-oidc-hmac"
// hasMatchingController returns true if the provided object is a GatewayClass
// with a Spec.Controller string matching this Envoy Gateway's controller string,
// or false otherwise.
func (r *gatewayAPIReconciler) hasMatchingController(gc *gwapiv1.GatewayClass) bool {
if gc.Spec.ControllerName == r.classController {
r.log.Info("gatewayclass has matching controller name, processing", "name", gc.Name)
return true
}
r.log.Info("bypassing reconciliation due to controller name", "controller", gc.Spec.ControllerName)
return false
}
// hasMatchingNamespaceLabels returns true if the namespace of provided object has
// the provided labels or false otherwise.
func (r *gatewayAPIReconciler) hasMatchingNamespaceLabels(obj client.Object) bool {
ok, err := r.checkObjectNamespaceLabels(obj)
if err != nil {
r.log.Error(
err, "failed to get Namespace",
"object", obj.GetObjectKind().GroupVersionKind().String(),
"name", obj.GetName())
return false
}
return ok
}
type NamespaceGetter interface {
GetNamespace() string
}
// checkObjectNamespaceLabels checks if labels of namespace of the object is a subset of namespaceLabels
func (r *gatewayAPIReconciler) checkObjectNamespaceLabels(obj metav1.Object) (bool, error) {
var nsString string
// TODO: it requires extra condition validate cluster resources or resources without namespace?
if nsString = obj.GetNamespace(); len(nsString) == 0 {
return false, nil
}
ns := &corev1.Namespace{}
if err := r.client.Get(
context.Background(),
client.ObjectKey{
Namespace: "", // Namespace object should have an empty Namespace
Name: nsString,
},
ns,
); err != nil {
return false, err
}
return matchLabelsAndExpressions(r.namespaceLabel, ns.Labels), nil
}
// matchLabelsAndExpressions extracts information from a given label selector and checks whether
// the provided object labels match the selector criteria.
// If the label selector is nil, it returns true, indicating a match.
// It returns false if there is an error while converting the label selector or if the labels do not match.
func matchLabelsAndExpressions(ls *metav1.LabelSelector, objLabels map[string]string) bool {
if ls == nil {
return true
}
selector, err := metav1.LabelSelectorAsSelector(ls)
if err != nil {
return false
}
return selector.Matches(labels.Set(objLabels))
}
// validateGatewayForReconcile returns true if the provided object is a Gateway
// using a GatewayClass matching the configured gatewayclass controller name.
func (r *gatewayAPIReconciler) validateGatewayForReconcile(obj client.Object) bool {
gw, ok := obj.(*gwapiv1.Gateway)
if !ok {
r.log.Info("unexpected object type, bypassing reconciliation", "object", obj)
return false
}
gc := &gwapiv1.GatewayClass{}
key := types.NamespacedName{Name: string(gw.Spec.GatewayClassName)}
if err := r.client.Get(context.Background(), key, gc); err != nil {
r.log.Error(err, "failed to get gatewayclass", "name", gw.Spec.GatewayClassName)
return false
}
if gc.Spec.ControllerName != r.classController {
r.log.Info("gatewayclass controller name", string(gc.Spec.ControllerName), "class controller name", string(r.classController))
r.log.Info("gatewayclass name for gateway doesn't match configured name",
"namespace", gw.Namespace, "name", gw.Name)
return false
}
return true
}
// validateSecretForReconcile checks whether the Secret belongs to a valid Gateway.
func (r *gatewayAPIReconciler) validateSecretForReconcile(obj client.Object) bool {
secret, ok := obj.(*corev1.Secret)
if !ok {
r.log.Info("unexpected object type, bypassing reconciliation", "object", obj)
return false
}
nsName := utils.NamespacedName(secret)
if r.isGatewayReferencingSecret(&nsName) {
return true
}
if r.isSecurityPolicyReferencingSecret(&nsName) {
return true
}
if r.isCtpReferencingSecret(&nsName) {
return true
}
if r.isOIDCHMACSecret(&nsName) {
return true
}
return false
}
func (r *gatewayAPIReconciler) isGatewayReferencingSecret(nsName *types.NamespacedName) bool {
gwList := &gwapiv1.GatewayList{}
if err := r.client.List(context.Background(), gwList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(secretGatewayIndex, nsName.String()),
}); err != nil {
r.log.Error(err, "unable to find associated Gateways")
return false
}
if len(gwList.Items) == 0 {
return false
}
for _, gw := range gwList.Items {
gw := gw
if !r.validateGatewayForReconcile(&gw) {
return false
}
}
return true
}
func (r *gatewayAPIReconciler) isSecurityPolicyReferencingSecret(nsName *types.NamespacedName) bool {
spList := &egv1a1.SecurityPolicyList{}
if err := r.client.List(context.Background(), spList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(secretSecurityPolicyIndex, nsName.String()),
}); err != nil {
r.log.Error(err, "unable to find associated SecurityPolicies")
return false
}
return len(spList.Items) > 0
}
func (r *gatewayAPIReconciler) isCtpReferencingSecret(nsName *types.NamespacedName) bool {
ctpList := &egv1a1.ClientTrafficPolicyList{}
if err := r.client.List(context.Background(), ctpList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(secretCtpIndex, nsName.String()),
}); err != nil {
r.log.Error(err, "unable to find associated ClientTrafficPolicies")
return false
}
return len(ctpList.Items) > 0
}
func (r *gatewayAPIReconciler) isOIDCHMACSecret(nsName *types.NamespacedName) bool {
oidcHMACSecret := types.NamespacedName{
Namespace: r.namespace,
Name: oidcHMACSecretName,
}
return *nsName == oidcHMACSecret
}
// validateServiceForReconcile tries finding the owning Gateway of the Service
// if it exists, finds the Gateway's Deployment, and further updates the Gateway
// status Ready condition. All Services are pushed for reconciliation.
func (r *gatewayAPIReconciler) validateServiceForReconcile(obj client.Object) bool {
ctx := context.Background()
svc, ok := obj.(*corev1.Service)
if !ok {
r.log.Info("unexpected object type, bypassing reconciliation", "object", obj)
return false
}
labels := svc.GetLabels()
// Check if the Service belongs to a Gateway, if so, update the Gateway status.
gtw := r.findOwningGateway(ctx, labels)
if gtw != nil {
r.updateStatusForGateway(ctx, gtw)
return false
}
// Merged gateways will have only this label, update status of all Gateways under found GatewayClass.
gcName, ok := labels[gatewayapi.OwningGatewayClassLabel]
if ok && r.mergeGateways.Has(gcName) {
if err := r.updateStatusForGatewaysUnderGatewayClass(ctx, gcName); err != nil {
r.log.Info("no Gateways found under GatewayClass", "name", gcName)
return false
}
return false
}
nsName := utils.NamespacedName(svc)
if r.isRouteReferencingBackend(&nsName) {
return true
}
if r.isSecurityPolicyReferencingBackend(&nsName) {
return true
}
return r.isEnvoyExtensionPolicyReferencingBackend(&nsName)
}
func (r *gatewayAPIReconciler) isSecurityPolicyReferencingBackend(nsName *types.NamespacedName) bool {
spList := &egv1a1.SecurityPolicyList{}
if err := r.client.List(context.Background(), spList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(backendSecurityPolicyIndex, nsName.String()),
}); err != nil {
r.log.Error(err, "unable to find associated SecurityPolicies")
return false
}
return len(spList.Items) > 0
}
// validateServiceImportForReconcile tries finding the owning Gateway of the ServiceImport
// if it exists, finds the Gateway's Deployment, and further updates the Gateway
// status Ready condition. All Services are pushed for reconciliation.
func (r *gatewayAPIReconciler) validateServiceImportForReconcile(obj client.Object) bool {
svcImport, ok := obj.(*mcsapi.ServiceImport)
if !ok {
r.log.Info("unexpected object type, bypassing reconciliation", "object", obj)
return false
}
nsName := utils.NamespacedName(svcImport)
return r.isRouteReferencingBackend(&nsName)
}
// isRouteReferencingBackend returns true if the backend(service and serviceImport) is referenced by any of the xRoutes
// in the system, else returns false.
func (r *gatewayAPIReconciler) isRouteReferencingBackend(nsName *types.NamespacedName) bool {
ctx := context.Background()
httpRouteList := &gwapiv1.HTTPRouteList{}
if err := r.client.List(ctx, httpRouteList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(backendHTTPRouteIndex, nsName.String()),
}); err != nil {
r.log.Error(err, "unable to find associated HTTPRoutes")
return false
}
grpcRouteList := &gwapiv1.GRPCRouteList{}
if err := r.client.List(ctx, grpcRouteList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(backendGRPCRouteIndex, nsName.String()),
}); err != nil {
r.log.Error(err, "unable to find associated GRPCRoutes")
return false
}
tlsRouteList := &gwapiv1a2.TLSRouteList{}
if err := r.client.List(ctx, tlsRouteList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(backendTLSRouteIndex, nsName.String()),
}); err != nil {
r.log.Error(err, "unable to find associated TLSRoutes")
return false
}
tcpRouteList := &gwapiv1a2.TCPRouteList{}
if err := r.client.List(ctx, tcpRouteList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(backendTCPRouteIndex, nsName.String()),
}); err != nil {
r.log.Error(err, "unable to find associated TCPRoutes")
return false
}
udpRouteList := &gwapiv1a2.UDPRouteList{}
if err := r.client.List(ctx, udpRouteList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(backendUDPRouteIndex, nsName.String()),
}); err != nil {
r.log.Error(err, "unable to find associated UDPRoutes")
return false
}
// Check how many Route objects refer this Backend
allAssociatedRoutes := len(httpRouteList.Items) +
len(grpcRouteList.Items) +
len(tlsRouteList.Items) +
len(tcpRouteList.Items) +
len(udpRouteList.Items)
return allAssociatedRoutes != 0
}
// validateEndpointSliceForReconcile returns true if the endpointSlice references
// a service that is referenced by a xRoute
func (r *gatewayAPIReconciler) validateEndpointSliceForReconcile(obj client.Object) bool {
ep, ok := obj.(*discoveryv1.EndpointSlice)
if !ok {
r.log.Info("unexpected object type, bypassing reconciliation", "object", obj)
return false
}
svcName, ok := ep.GetLabels()[discoveryv1.LabelServiceName]
multiClusterSvcName, isMCS := ep.GetLabels()[mcsapi.LabelServiceName]
if !ok && !isMCS {
r.log.Info("endpointslice is missing kubernetes.io/service-name or multicluster.kubernetes.io/service-name label", "object", obj)
return false
}
nsName := types.NamespacedName{
Namespace: obj.GetNamespace(),
Name: svcName,
}
if isMCS {
nsName.Name = multiClusterSvcName
}
if r.isRouteReferencingBackend(&nsName) {
return true
}
if r.isSecurityPolicyReferencingBackend(&nsName) {
return true
}
return r.isEnvoyExtensionPolicyReferencingBackend(&nsName)
}
// validateDeploymentForReconcile tries finding the owning Gateway of the Deployment
// if it exists, finds the Gateway's Service, and further updates the Gateway
// status Ready condition. No Deployments are pushed for reconciliation.
func (r *gatewayAPIReconciler) validateDeploymentForReconcile(obj client.Object) bool {
ctx := context.Background()
deployment, ok := obj.(*appsv1.Deployment)
if !ok {
r.log.Info("unexpected object type, bypassing reconciliation", "object", obj)
return false
}
labels := deployment.GetLabels()
// Only deployments in the configured namespace should be reconciled.
if deployment.Namespace == r.namespace {
// Check if the deployment belongs to a Gateway, if so, update the Gateway status.
gtw := r.findOwningGateway(ctx, labels)
if gtw != nil {
r.updateStatusForGateway(ctx, gtw)
return false
}
}
// Merged gateways will have only this label, update status of all Gateways under found GatewayClass.
gcName, ok := labels[gatewayapi.OwningGatewayClassLabel]
if ok && r.mergeGateways.Has(gcName) {
if err := r.updateStatusForGatewaysUnderGatewayClass(ctx, gcName); err != nil {
r.log.Info("no Gateways found under GatewayClass", "name", gcName)
return false
}
return false
}
// There is no need to reconcile the Deployment any further.
return false
}
// envoyDeploymentForGateway returns the Envoy Deployment, returning nil if the Deployment doesn't exist.
func (r *gatewayAPIReconciler) envoyDeploymentForGateway(ctx context.Context, gateway *gwapiv1.Gateway) (*appsv1.Deployment, error) {
key := types.NamespacedName{
Namespace: r.namespace,
Name: infraName(gateway, r.mergeGateways.Has(string(gateway.Spec.GatewayClassName))),
}
deployment := new(appsv1.Deployment)
if err := r.client.Get(ctx, key, deployment); err != nil {
if kerrors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return deployment, nil
}
// envoyServiceForGateway returns the Envoy service, returning nil if the service doesn't exist.
func (r *gatewayAPIReconciler) envoyServiceForGateway(ctx context.Context, gateway *gwapiv1.Gateway) (*corev1.Service, error) {
key := types.NamespacedName{
Namespace: r.namespace,
Name: infraName(gateway, r.mergeGateways.Has(string(gateway.Spec.GatewayClassName))),
}
svc := new(corev1.Service)
if err := r.client.Get(ctx, key, svc); err != nil {
if kerrors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return svc, nil
}
// findOwningGateway attempts finds a Gateway using "labels".
func (r *gatewayAPIReconciler) findOwningGateway(ctx context.Context, labels map[string]string) *gwapiv1.Gateway {
gwName, ok := labels[gatewayapi.OwningGatewayNameLabel]
if !ok {
return nil
}
gwNamespace, ok := labels[gatewayapi.OwningGatewayNamespaceLabel]
if !ok {
return nil
}
gatewayKey := types.NamespacedName{Namespace: gwNamespace, Name: gwName}
gtw := new(gwapiv1.Gateway)
if err := r.client.Get(ctx, gatewayKey, gtw); err != nil {
r.log.Info("gateway not found", "namespace", gtw.Namespace, "name", gtw.Name)
return nil
}
return gtw
}
// updateStatusForGatewaysUnderGatewayClass updates status of all Gateways under the GatewayClass.
func (r *gatewayAPIReconciler) updateStatusForGatewaysUnderGatewayClass(ctx context.Context, gatewayClassName string) error {
gateways := new(gwapiv1.GatewayList)
if err := r.client.List(ctx, gateways, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(classGatewayIndex, gatewayClassName),
}); err != nil {
return err
}
if len(gateways.Items) == 0 {
return fmt.Errorf("no gateways found for gatewayclass: %s", gatewayClassName)
}
for _, gateway := range gateways.Items {
gateway := gateway
r.updateStatusForGateway(ctx, &gateway)
}
return nil
}
func (r *gatewayAPIReconciler) handleNode(obj client.Object) bool {
ctx := context.Background()
node, ok := obj.(*corev1.Node)
if !ok {
r.log.Info("unexpected object type, bypassing reconciliation", "object", obj)
return false
}
key := types.NamespacedName{Name: node.Name}
if err := r.client.Get(ctx, key, node); err != nil {
if kerrors.IsNotFound(err) {
r.store.removeNode(node)
return true
}
r.log.Error(err, "unable to find node", "name", node.Name)
return false
}
r.store.addNode(node)
return true
}
// validateConfigMapForReconcile checks whether the ConfigMap belongs to a valid ClientTrafficPolicy.
func (r *gatewayAPIReconciler) validateConfigMapForReconcile(obj client.Object) bool {
configMap, ok := obj.(*corev1.ConfigMap)
if !ok {
r.log.Info("unexpected object type, bypassing reconciliation", "object", obj)
return false
}
ctpList := &egv1a1.ClientTrafficPolicyList{}
if err := r.client.List(context.Background(), ctpList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(configMapCtpIndex, utils.NamespacedName(configMap).String()),
}); err != nil {
r.log.Error(err, "unable to find associated ClientTrafficPolicy")
return false
}
if len(ctpList.Items) == 0 {
return false
}
btlsList := &gwapiv1a3.BackendTLSPolicyList{}
if err := r.client.List(context.Background(), btlsList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(configMapBtlsIndex, utils.NamespacedName(configMap).String()),
}); err != nil {
r.log.Error(err, "unable to find associated BackendTLSPolicy")
return false
}
if len(btlsList.Items) == 0 {
return false
}
return true
}
func (r *gatewayAPIReconciler) isEnvoyExtensionPolicyReferencingBackend(nsName *types.NamespacedName) bool {
spList := &egv1a1.EnvoyExtensionPolicyList{}
if err := r.client.List(context.Background(), spList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(backendEnvoyExtensionPolicyIndex, nsName.String()),
}); err != nil {
r.log.Error(err, "unable to find associated EnvoyExtensionPolicies")
return false
}
return len(spList.Items) > 0
}