-
Notifications
You must be signed in to change notification settings - Fork 364
/
controller.go
1417 lines (1284 loc) · 48.2 KB
/
controller.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwapiv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1"
egcfgv1a1 "github.com/envoyproxy/gateway/api/config/v1alpha1"
"github.com/envoyproxy/gateway/api/config/v1alpha1/validation"
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/envoygateway/config"
"github.com/envoyproxy/gateway/internal/gatewayapi"
"github.com/envoyproxy/gateway/internal/logging"
"github.com/envoyproxy/gateway/internal/message"
"github.com/envoyproxy/gateway/internal/provider/utils"
"github.com/envoyproxy/gateway/internal/status"
"github.com/envoyproxy/gateway/internal/utils/slice"
)
const (
classGatewayIndex = "classGatewayIndex"
gatewayTLSRouteIndex = "gatewayTLSRouteIndex"
gatewayHTTPRouteIndex = "gatewayHTTPRouteIndex"
gatewayGRPCRouteIndex = "gatewayGRPCRouteIndex"
gatewayTCPRouteIndex = "gatewayTCPRouteIndex"
gatewayUDPRouteIndex = "gatewayUDPRouteIndex"
secretGatewayIndex = "secretGatewayIndex"
targetRefGrantRouteIndex = "targetRefGrantRouteIndex"
serviceHTTPRouteIndex = "serviceHTTPRouteIndex"
serviceGRPCRouteIndex = "serviceGRPCRouteIndex"
serviceTLSRouteIndex = "serviceTLSRouteIndex"
serviceTCPRouteIndex = "serviceTCPRouteIndex"
serviceUDPRouteIndex = "serviceUDPRouteIndex"
authenFilterHTTPRouteIndex = "authenHTTPRouteIndex"
rateLimitFilterHTTPRouteIndex = "rateLimitHTTPRouteIndex"
authenFilterGRPCRouteIndex = "authenGRPCRouteIndex"
rateLimitFilterGRPCRouteIndex = "rateLimitGRPCRouteIndex"
)
type gatewayAPIReconciler struct {
client client.Client
log logging.Logger
statusUpdater status.Updater
classController gwapiv1b1.GatewayController
store *kubernetesProviderStore
namespace string
envoyGateway *egcfgv1a1.EnvoyGateway
resources *message.ProviderResources
envoyPatchPolicyStatuses *message.EnvoyPatchPolicyStatuses
extGVKs []schema.GroupVersionKind
}
// newGatewayAPIController
func newGatewayAPIController(mgr manager.Manager, cfg *config.Server, su status.Updater,
resources *message.ProviderResources, eStatuses *message.EnvoyPatchPolicyStatuses) error {
ctx := context.Background()
// Gather additional resources to watch from registered extensions
var extGVKs []schema.GroupVersionKind
if cfg.EnvoyGateway.ExtensionManager != nil {
for _, rsrc := range cfg.EnvoyGateway.ExtensionManager.Resources {
gvk := schema.GroupVersionKind(rsrc)
extGVKs = append(extGVKs, gvk)
}
}
r := &gatewayAPIReconciler{
client: mgr.GetClient(),
log: cfg.Logger,
classController: gwapiv1b1.GatewayController(cfg.EnvoyGateway.Gateway.ControllerName),
namespace: cfg.Namespace,
statusUpdater: su,
resources: resources,
envoyPatchPolicyStatuses: eStatuses,
extGVKs: extGVKs,
store: newProviderStore(),
envoyGateway: cfg.EnvoyGateway,
}
c, err := controller.New("gatewayapi", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
r.log.Info("created gatewayapi controller")
// Subscribe to status updates
r.subscribeAndUpdateStatus(ctx)
// Watch resources
if err := r.watchResources(ctx, mgr, c); err != nil {
return err
}
return nil
}
type resourceMappings struct {
// Map for storing namespaces for Route, Service and Gateway objects.
allAssociatedNamespaces map[string]struct{}
// Map for storing service NamespaceNames referred by various Route objects.
allAssociatedBackendRefs map[types.NamespacedName]struct{}
// Map for storing referenceGrant NamespaceNames for BackendRefs, SecretRefs.
allAssociatedRefGrants map[types.NamespacedName]*gwapiv1a2.ReferenceGrant
// authenFilters is a map of AuthenticationFilters, where the key is the
// namespaced name of the AuthenticationFilter.
authenFilters map[types.NamespacedName]*egv1a1.AuthenticationFilter
// rateLimitFilters is a map of RateLimitFilters, where the key is the
// namespaced name of the RateLimitFilter.
rateLimitFilters map[types.NamespacedName]*egv1a1.RateLimitFilter
// extensionRefFilters is a map of filters managed by an extension.
// The key is the namespaced name of the filter and the value is the
// unstructured form of the resource.
extensionRefFilters map[types.NamespacedName]unstructured.Unstructured
}
func newResourceMapping() *resourceMappings {
return &resourceMappings{
allAssociatedNamespaces: map[string]struct{}{},
allAssociatedBackendRefs: map[types.NamespacedName]struct{}{},
allAssociatedRefGrants: map[types.NamespacedName]*gwapiv1a2.ReferenceGrant{},
authenFilters: map[types.NamespacedName]*egv1a1.AuthenticationFilter{},
rateLimitFilters: map[types.NamespacedName]*egv1a1.RateLimitFilter{},
extensionRefFilters: map[types.NamespacedName]unstructured.Unstructured{},
}
}
func (r *gatewayAPIReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
r.log.WithName(request.Name).Info("reconciling object", "namespace", request.Namespace, "name", request.Name)
var gatewayClasses gwapiv1b1.GatewayClassList
if err := r.client.List(ctx, &gatewayClasses); err != nil {
return reconcile.Result{}, fmt.Errorf("error listing gatewayclasses: %v", err)
}
var cc controlledClasses
for _, gwClass := range gatewayClasses.Items {
gwClass := gwClass
if gwClass.Spec.ControllerName == r.classController {
// The gatewayclass was marked for deletion and the finalizer removed,
// so clean-up dependents.
if !gwClass.DeletionTimestamp.IsZero() &&
!slice.ContainsString(gwClass.Finalizers, gatewayClassFinalizer) {
r.log.Info("gatewayclass marked for deletion")
cc.removeMatch(&gwClass)
// Delete the gatewayclass from the watchable map.
r.resources.GatewayAPIResources.Delete(gwClass.Name)
continue
}
cc.addMatch(&gwClass)
}
}
// The gatewayclass was already deleted/finalized and there are stale queue entries.
acceptedGC := cc.acceptedClass()
if acceptedGC == nil {
r.log.Info("no accepted gatewayclass")
return reconcile.Result{}, nil
}
// Update status for all gateway classes
for _, gc := range cc.notAcceptedClasses() {
if err := r.gatewayClassUpdater(ctx, gc, false, string(status.ReasonOlderGatewayClassExists),
status.MsgOlderGatewayClassExists); err != nil {
r.resources.GatewayAPIResources.Delete(acceptedGC.Name)
return reconcile.Result{}, err
}
}
// Initialize resource types.
resourceTree := gatewayapi.NewResources()
resourceMap := newResourceMapping()
if err := r.processGateways(ctx, acceptedGC, resourceMap, resourceTree); err != nil {
return reconcile.Result{}, err
}
for serviceNamespaceName := range resourceMap.allAssociatedBackendRefs {
r.log.Info("processing Service", "namespace", serviceNamespaceName.Namespace,
"name", serviceNamespaceName.Name)
service := new(corev1.Service)
err := r.client.Get(ctx, serviceNamespaceName, service)
if err != nil {
r.log.Error(err, "failed to get Service", "namespace", serviceNamespaceName.Namespace,
"name", serviceNamespaceName.Name)
} else {
resourceMap.allAssociatedNamespaces[service.Namespace] = struct{}{}
resourceTree.Services = append(resourceTree.Services, service)
r.log.Info("added Service to resource tree", "namespace", serviceNamespaceName.Namespace,
"name", serviceNamespaceName.Name)
// Retrieve the EndpointSlices associated with the service
endpointSliceList := new(discoveryv1.EndpointSliceList)
opts := []client.ListOption{
client.MatchingLabels(map[string]string{
discoveryv1.LabelServiceName: serviceNamespaceName.Name,
}),
client.InNamespace(serviceNamespaceName.Namespace),
}
if err := r.client.List(ctx, endpointSliceList, opts...); err != nil {
r.log.Error(err, "failed to get EndpointSlices", "namespace", serviceNamespaceName.Namespace,
"service", serviceNamespaceName.Name)
} else {
for _, endpointSlice := range endpointSliceList.Items {
endpointSlice := endpointSlice
r.log.Info("added EndpointSlice to resource tree", "namespace", endpointSlice.Namespace,
"name", endpointSlice.Name)
resourceTree.EndpointSlices = append(resourceTree.EndpointSlices, &endpointSlice)
}
}
}
}
// Add all ReferenceGrants to the resourceTree
for _, referenceGrant := range resourceMap.allAssociatedRefGrants {
resourceTree.ReferenceGrants = append(resourceTree.ReferenceGrants, referenceGrant)
}
// Add all EnvoyPatchPolicies
if r.envoyGateway.ExtensionAPIs != nil && r.envoyGateway.ExtensionAPIs.EnableEnvoyPatchPolicy {
envoyPatchPolicies := egv1a1.EnvoyPatchPolicyList{}
if err := r.client.List(ctx, &envoyPatchPolicies); err != nil {
return reconcile.Result{}, fmt.Errorf("error listing envoypatchpolicies: %v", err)
}
for _, policy := range envoyPatchPolicies.Items {
policy := policy
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
policy.Status = egv1a1.EnvoyPatchPolicyStatus{}
resourceTree.EnvoyPatchPolicies = append(resourceTree.EnvoyPatchPolicies, &policy)
}
}
// For this particular Gateway, and all associated objects, check whether the
// namespace exists. Add to the resourceTree.
for ns := range resourceMap.allAssociatedNamespaces {
namespace, err := r.getNamespace(ctx, ns)
if err != nil {
r.log.Error(err, "unable to find the namespace")
if kerrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
resourceTree.Namespaces = append(resourceTree.Namespaces, namespace)
}
// Process the parametersRef of the accepted GatewayClass.
if acceptedGC.Spec.ParametersRef != nil && acceptedGC.DeletionTimestamp == nil {
if err := r.processParamsRef(ctx, acceptedGC, resourceTree); err != nil {
msg := fmt.Sprintf("%s: %v", status.MsgGatewayClassInvalidParams, err)
if err := r.gatewayClassUpdater(ctx, acceptedGC, false, string(gwapiv1b1.GatewayClassReasonInvalidParameters), msg); err != nil {
r.log.Error(err, "unable to update GatewayClass status")
}
r.log.Error(err, "failed to process parametersRef for gatewayclass", "name", acceptedGC.Name)
return reconcile.Result{}, err
}
}
if err := r.gatewayClassUpdater(ctx, acceptedGC, true, string(gwapiv1b1.GatewayClassReasonAccepted), status.MsgValidGatewayClass); err != nil {
r.log.Error(err, "unable to update GatewayClass status")
return reconcile.Result{}, err
}
// Update finalizer on the gateway class based on the resource tree.
if len(resourceTree.Gateways) == 0 {
r.log.Info("No gateways found for accepted gatewayclass")
// If needed, remove the finalizer from the accepted GatewayClass.
if err := r.removeFinalizer(ctx, acceptedGC); err != nil {
r.log.Error(err, fmt.Sprintf("failed to remove finalizer from gatewayclass %s",
acceptedGC.Name))
return reconcile.Result{}, err
}
} else {
// finalize the accepted GatewayClass.
if err := r.addFinalizer(ctx, acceptedGC); err != nil {
r.log.Error(err, fmt.Sprintf("failed adding finalizer to gatewayclass %s",
acceptedGC.Name))
return reconcile.Result{}, err
}
}
// The Store is triggered even when there are no Gateways associated to the
// GatewayClass. This would happen in case the last Gateway is removed and the
// Store will be required to trigger a cleanup of envoy infra resources.
r.resources.GatewayAPIResources.Store(acceptedGC.Name, resourceTree)
r.log.WithName(request.Name).Info("reconciled gatewayAPI object successfully", "namespace", request.Namespace, "name", request.Name)
return reconcile.Result{}, nil
}
func (r *gatewayAPIReconciler) gatewayClassUpdater(ctx context.Context, gc *gwapiv1b1.GatewayClass, accepted bool, reason, msg string) error {
if r.statusUpdater != nil {
r.statusUpdater.Send(status.Update{
NamespacedName: types.NamespacedName{Name: gc.Name},
Resource: &gwapiv1b1.GatewayClass{},
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
gc, ok := obj.(*gwapiv1b1.GatewayClass)
if !ok {
panic(fmt.Sprintf("unsupported object type %T", obj))
}
return status.SetGatewayClassAccepted(gc.DeepCopy(), accepted, reason, msg)
}),
})
} else {
// this branch makes testing easier by not going through the status.Updater.
duplicate := status.SetGatewayClassAccepted(gc.DeepCopy(), accepted, reason, msg)
if err := r.client.Status().Update(ctx, duplicate); err != nil && !kerrors.IsNotFound(err) {
return fmt.Errorf("error updating status of gatewayclass %s: %w", duplicate.Name, err)
}
}
return nil
}
func (r *gatewayAPIReconciler) getNamespace(ctx context.Context, name string) (*corev1.Namespace, error) {
nsKey := types.NamespacedName{Name: name}
ns := new(corev1.Namespace)
if err := r.client.Get(ctx, nsKey, ns); err != nil {
r.log.Error(err, "unable to get Namespace")
return nil, err
}
return ns, nil
}
func (r *gatewayAPIReconciler) statusUpdateForGateway(ctx context.Context, gtw *gwapiv1b1.Gateway) {
// nil check for unit tests.
if r.statusUpdater == nil {
return
}
// Get deployment
deploy, err := r.envoyDeploymentForGateway(ctx, gtw)
if err != nil {
r.log.Info("failed to get Deployment for gateway",
"namespace", gtw.Namespace, "name", gtw.Name)
}
// Get service
svc, err := r.envoyServiceForGateway(ctx, gtw)
if err != nil {
r.log.Info("failed to get Service for gateway",
"namespace", gtw.Namespace, "name", gtw.Name)
}
// update accepted condition
status.UpdateGatewayStatusAcceptedCondition(gtw, true)
// update address field and programmed condition
status.UpdateGatewayStatusProgrammedCondition(gtw, svc, deploy, r.store.listNodeAddresses()...)
key := utils.NamespacedName(gtw)
// publish status
r.statusUpdater.Send(status.Update{
NamespacedName: key,
Resource: new(gwapiv1b1.Gateway),
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
g, ok := obj.(*gwapiv1b1.Gateway)
if !ok {
panic(fmt.Sprintf("unsupported object type %T", obj))
}
gCopy := g.DeepCopy()
gCopy.Status.Conditions = gtw.Status.Conditions
gCopy.Status.Addresses = gtw.Status.Addresses
gCopy.Status.Listeners = gtw.Status.Listeners
return gCopy
}),
})
}
func (r *gatewayAPIReconciler) findReferenceGrant(ctx context.Context, from, to ObjectKindNamespacedName) (*gwapiv1a2.ReferenceGrant, error) {
refGrantList := new(gwapiv1a2.ReferenceGrantList)
opts := &client.ListOptions{FieldSelector: fields.OneTermEqualSelector(targetRefGrantRouteIndex, to.kind)}
if err := r.client.List(ctx, refGrantList, opts); err != nil {
return nil, fmt.Errorf("failed to list ReferenceGrants: %v", err)
}
for _, refGrant := range refGrantList.Items {
if refGrant.Namespace == to.namespace {
for _, src := range refGrant.Spec.From {
if src.Kind == gwapiv1a2.Kind(from.kind) && string(src.Namespace) == from.namespace {
return &refGrant, nil
}
}
}
}
// No ReferenceGrant found.
return nil, nil
}
func (r *gatewayAPIReconciler) processGateways(ctx context.Context, acceptedGC *gwapiv1b1.GatewayClass, resourceMap *resourceMappings, resourceTree *gatewayapi.Resources) error {
// Find gateways for the acceptedGC
// Find the Gateways that reference this Class.
gatewayList := &gwapiv1b1.GatewayList{}
if err := r.client.List(ctx, gatewayList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(classGatewayIndex, acceptedGC.Name),
}); err != nil {
r.log.Info("no associated Gateways found for GatewayClass", "name", acceptedGC.Name)
return err
}
for _, gtw := range gatewayList.Items {
gtw := gtw
r.log.Info("processing Gateway", "namespace", gtw.Namespace, "name", gtw.Name)
resourceMap.allAssociatedNamespaces[gtw.Namespace] = struct{}{}
for _, listener := range gtw.Spec.Listeners {
listener := listener
// Get Secret for gateway if it exists.
if terminatesTLS(&listener) {
for _, certRef := range listener.TLS.CertificateRefs {
certRef := certRef
if refsSecret(&certRef) {
secret := new(corev1.Secret)
secretNamespace := gatewayapi.NamespaceDerefOr(certRef.Namespace, gtw.Namespace)
err := r.client.Get(ctx,
types.NamespacedName{Namespace: secretNamespace, Name: string(certRef.Name)},
secret,
)
if err != nil && !kerrors.IsNotFound(err) {
r.log.Error(err, "unable to find Secret")
return err
}
r.log.Info("processing Secret", "namespace", secretNamespace, "name", string(certRef.Name))
if secretNamespace != gtw.Namespace {
from := ObjectKindNamespacedName{
kind: gatewayapi.KindGateway,
namespace: gtw.Namespace,
name: gtw.Name,
}
to := ObjectKindNamespacedName{
kind: gatewayapi.KindSecret,
namespace: secretNamespace,
name: string(certRef.Name),
}
refGrant, err := r.findReferenceGrant(ctx, from, to)
switch {
case err != nil:
r.log.Error(err, "failed to find ReferenceGrant")
case refGrant == nil:
r.log.Info("no matching ReferenceGrants found", "from", from.kind,
"from namespace", from.namespace, "target", to.kind, "target namespace", to.namespace)
default:
// RefGrant found
resourceMap.allAssociatedRefGrants[utils.NamespacedName(refGrant)] = refGrant
r.log.Info("added ReferenceGrant to resource map", "namespace", refGrant.Namespace,
"name", refGrant.Name)
}
}
resourceMap.allAssociatedNamespaces[secretNamespace] = struct{}{}
resourceTree.Secrets = append(resourceTree.Secrets, secret)
}
}
}
}
// Route Processing
// Get TLSRoute objects and check if it exists.
if err := r.processTLSRoutes(ctx, utils.NamespacedName(>w).String(), resourceMap, resourceTree); err != nil {
return err
}
// Get HTTPRoute objects and check if it exists.
if err := r.processHTTPRoutes(ctx, utils.NamespacedName(>w).String(), resourceMap, resourceTree); err != nil {
return err
}
// Get GRPCRoute objects and check if it exists.
if err := r.processGRPCRoutes(ctx, utils.NamespacedName(>w).String(), resourceMap, resourceTree); err != nil {
return err
}
// Get TCPRoute objects and check if it exists.
if err := r.processTCPRoutes(ctx, utils.NamespacedName(>w).String(), resourceMap, resourceTree); err != nil {
return err
}
// Get UDPRoute objects and check if it exists.
if err := r.processUDPRoutes(ctx, utils.NamespacedName(>w).String(), resourceMap, resourceTree); err != nil {
return err
}
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
gtw.Status = gwapiv1b1.GatewayStatus{}
resourceTree.Gateways = append(resourceTree.Gateways, >w)
}
return nil
}
func addReferenceGrantIndexers(ctx context.Context, mgr manager.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1a2.ReferenceGrant{}, targetRefGrantRouteIndex, func(rawObj client.Object) []string {
refGrant := rawObj.(*gwapiv1a2.ReferenceGrant)
var referredServices []string
for _, target := range refGrant.Spec.To {
referredServices = append(referredServices, string(target.Kind))
}
return referredServices
}); err != nil {
return err
}
return nil
}
// addHTTPRouteIndexers adds indexing on HTTPRoute.
// - For Service objects that are referenced in HTTPRoute objects via `.spec.rules.backendRefs`.
// This helps in querying for HTTPRoutes that are affected by a particular Service CRUD.
// - For AuthenticationFilter and RateLimitFilter objects that are referenced in HTTPRoute objects via
// `.spec.rules[].filters`. This helps in querying for HTTPRoutes that are affected by a
// particular AuthenticationFilter CRUD.
func addHTTPRouteIndexers(ctx context.Context, mgr manager.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1b1.HTTPRoute{}, gatewayHTTPRouteIndex, gatewayHTTPRouteIndexFunc); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1b1.HTTPRoute{}, serviceHTTPRouteIndex, serviceHTTPRouteIndexFunc); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1b1.HTTPRoute{}, authenFilterHTTPRouteIndex, authenFilterHTTPRouteIndexFunc); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1b1.HTTPRoute{}, rateLimitFilterHTTPRouteIndex, rateLimitFilterHTTPRouteIndexFunc); err != nil {
return err
}
return nil
}
func authenFilterHTTPRouteIndexFunc(rawObj client.Object) []string {
httproute := rawObj.(*gwapiv1b1.HTTPRoute)
var filters []string
for _, rule := range httproute.Spec.Rules {
for i := range rule.Filters {
filter := rule.Filters[i]
if gatewayapi.IsAuthnHTTPFilter(&filter) {
if err := gatewayapi.ValidateHTTPRouteFilter(&filter); err == nil {
filters = append(filters,
types.NamespacedName{
Namespace: httproute.Namespace,
Name: string(filter.ExtensionRef.Name),
}.String(),
)
}
}
}
}
return filters
}
func rateLimitFilterHTTPRouteIndexFunc(rawObj client.Object) []string {
httproute := rawObj.(*gwapiv1b1.HTTPRoute)
var filters []string
for _, rule := range httproute.Spec.Rules {
for i := range rule.Filters {
filter := rule.Filters[i]
if gatewayapi.IsRateLimitHTTPFilter(&filter) {
if err := gatewayapi.ValidateHTTPRouteFilter(&filter); err == nil {
filters = append(filters,
types.NamespacedName{
Namespace: httproute.Namespace,
Name: string(filter.ExtensionRef.Name),
}.String(),
)
}
}
}
}
return filters
}
func gatewayHTTPRouteIndexFunc(rawObj client.Object) []string {
httproute := rawObj.(*gwapiv1b1.HTTPRoute)
var gateways []string
for _, parent := range httproute.Spec.ParentRefs {
if parent.Kind == nil || string(*parent.Kind) == gatewayapi.KindGateway {
// If an explicit Gateway namespace is not provided, use the HTTPRoute namespace to
// lookup the provided Gateway Name.
gateways = append(gateways,
types.NamespacedName{
Namespace: gatewayapi.NamespaceDerefOr(parent.Namespace, httproute.Namespace),
Name: string(parent.Name),
}.String(),
)
}
}
return gateways
}
func serviceHTTPRouteIndexFunc(rawObj client.Object) []string {
httproute := rawObj.(*gwapiv1b1.HTTPRoute)
var services []string
for _, rule := range httproute.Spec.Rules {
for _, backend := range rule.BackendRefs {
if backend.Kind == nil || string(*backend.Kind) == gatewayapi.KindService {
// If an explicit Service namespace is not provided, use the HTTPRoute namespace to
// lookup the provided Gateway Name.
services = append(services,
types.NamespacedName{
Namespace: gatewayapi.NamespaceDerefOr(backend.Namespace, httproute.Namespace),
Name: string(backend.Name),
}.String(),
)
}
}
}
return services
}
// addGRPCRouteIndexers adds indexing on GRPCRoute, for Service objects that are
// referenced in GRPCRoute objects via `.spec.rules.backendRefs`. This helps in
// querying for GRPCRoutes that are affected by a particular Service CRUD.
func addGRPCRouteIndexers(ctx context.Context, mgr manager.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1a2.GRPCRoute{}, gatewayGRPCRouteIndex, gatewayGRPCRouteIndexFunc); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1a2.GRPCRoute{}, serviceGRPCRouteIndex, serviceGRPCRouteIndexFunc); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1a2.GRPCRoute{}, authenFilterGRPCRouteIndex, authenFilterGRPCRouteIndexFunc); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1a2.GRPCRoute{}, rateLimitFilterGRPCRouteIndex, rateLimitFilterGRPCRouteIndexFunc); err != nil {
return err
}
return nil
}
func gatewayGRPCRouteIndexFunc(rawObj client.Object) []string {
grpcroute := rawObj.(*gwapiv1a2.GRPCRoute)
var gateways []string
for _, parent := range grpcroute.Spec.ParentRefs {
if parent.Kind == nil || string(*parent.Kind) == gatewayapi.KindGateway {
// If an explicit Gateway namespace is not provided, use the GRPCRoute namespace to
// lookup the provided Gateway Name.
gateways = append(gateways,
types.NamespacedName{
Namespace: gatewayapi.NamespaceDerefOr(parent.Namespace, grpcroute.Namespace),
Name: string(parent.Name),
}.String(),
)
}
}
return gateways
}
func serviceGRPCRouteIndexFunc(rawObj client.Object) []string {
grpcroute := rawObj.(*gwapiv1a2.GRPCRoute)
var services []string
for _, rule := range grpcroute.Spec.Rules {
for _, backend := range rule.BackendRefs {
if backend.Kind == nil || string(*backend.Kind) == gatewayapi.KindService {
// If an explicit Service namespace is not provided, use the GRPCRoute namespace to
// lookup the provided Gateway Name.
services = append(services,
types.NamespacedName{
Namespace: gatewayapi.NamespaceDerefOr(backend.Namespace, grpcroute.Namespace),
Name: string(backend.Name),
}.String(),
)
}
}
}
return services
}
func authenFilterGRPCRouteIndexFunc(rawObj client.Object) []string {
grpcroute := rawObj.(*gwapiv1a2.GRPCRoute)
var filters []string
for _, rule := range grpcroute.Spec.Rules {
for i := range rule.Filters {
filter := rule.Filters[i]
if gatewayapi.IsAuthnGRPCFilter(&filter) {
if err := gatewayapi.ValidateGRPCRouteFilter(&filter); err == nil {
filters = append(filters,
types.NamespacedName{
Namespace: grpcroute.Namespace,
Name: string(filter.ExtensionRef.Name),
}.String(),
)
}
}
}
}
return filters
}
func rateLimitFilterGRPCRouteIndexFunc(rawObj client.Object) []string {
grpcroute := rawObj.(*gwapiv1a2.GRPCRoute)
var filters []string
for _, rule := range grpcroute.Spec.Rules {
for i := range rule.Filters {
filter := rule.Filters[i]
if gatewayapi.IsRateLimitGRPCFilter(&filter) {
if err := gatewayapi.ValidateGRPCRouteFilter(&filter); err == nil {
filters = append(filters,
types.NamespacedName{
Namespace: grpcroute.Namespace,
Name: string(filter.ExtensionRef.Name),
}.String(),
)
}
}
}
}
return filters
}
// addTLSRouteIndexers adds indexing on TLSRoute, for Service objects that are
// referenced in TLSRoute objects via `.spec.rules.backendRefs`. This helps in
// querying for TLSRoutes that are affected by a particular Service CRUD.
func addTLSRouteIndexers(ctx context.Context, mgr manager.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1a2.TLSRoute{}, gatewayTLSRouteIndex, func(rawObj client.Object) []string {
tlsRoute := rawObj.(*gwapiv1a2.TLSRoute)
var gateways []string
for _, parent := range tlsRoute.Spec.ParentRefs {
if string(*parent.Kind) == gatewayapi.KindGateway {
// If an explicit Gateway namespace is not provided, use the TLSRoute namespace to
// lookup the provided Gateway Name.
gateways = append(gateways,
types.NamespacedName{
Namespace: gatewayapi.NamespaceDerefOrAlpha(parent.Namespace, tlsRoute.Namespace),
Name: string(parent.Name),
}.String(),
)
}
}
return gateways
}); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1a2.TLSRoute{}, serviceTLSRouteIndex, serviceTLSRouteIndexFunc); err != nil {
return err
}
return nil
}
func serviceTLSRouteIndexFunc(rawObj client.Object) []string {
tlsroute := rawObj.(*gwapiv1a2.TLSRoute)
var services []string
for _, rule := range tlsroute.Spec.Rules {
for _, backend := range rule.BackendRefs {
if backend.Kind == nil || string(*backend.Kind) == gatewayapi.KindService {
// If an explicit Service namespace is not provided, use the TLSRoute namespace to
// lookup the provided Gateway Name.
services = append(services,
types.NamespacedName{
Namespace: gatewayapi.NamespaceDerefOrAlpha(backend.Namespace, tlsroute.Namespace),
Name: string(backend.Name),
}.String(),
)
}
}
}
return services
}
// addTCPRouteIndexers adds indexing on TCPRoute, for Service objects that are
// referenced in TCPRoute objects via `.spec.rules.backendRefs`. This helps in
// querying for TCPRoutes that are affected by a particular Service CRUD.
func addTCPRouteIndexers(ctx context.Context, mgr manager.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1a2.TCPRoute{}, gatewayTCPRouteIndex, func(rawObj client.Object) []string {
tcpRoute := rawObj.(*gwapiv1a2.TCPRoute)
var gateways []string
for _, parent := range tcpRoute.Spec.ParentRefs {
if string(*parent.Kind) == gatewayapi.KindGateway {
// If an explicit Gateway namespace is not provided, use the TCPRoute namespace to
// lookup the provided Gateway Name.
gateways = append(gateways,
types.NamespacedName{
Namespace: gatewayapi.NamespaceDerefOrAlpha(parent.Namespace, tcpRoute.Namespace),
Name: string(parent.Name),
}.String(),
)
}
}
return gateways
}); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1a2.TCPRoute{}, serviceTCPRouteIndex, serviceTCPRouteIndexFunc); err != nil {
return err
}
return nil
}
func serviceTCPRouteIndexFunc(rawObj client.Object) []string {
tcpRoute := rawObj.(*gwapiv1a2.TCPRoute)
var services []string
for _, rule := range tcpRoute.Spec.Rules {
for _, backend := range rule.BackendRefs {
if backend.Kind == nil || string(*backend.Kind) == gatewayapi.KindService {
// If an explicit Service namespace is not provided, use the TCPRoute namespace to
// lookup the provided Gateway Name.
services = append(services,
types.NamespacedName{
Namespace: gatewayapi.NamespaceDerefOrAlpha(backend.Namespace, tcpRoute.Namespace),
Name: string(backend.Name),
}.String(),
)
}
}
}
return services
}
// addUDPRouteIndexers adds indexing on UDPRoute, for Service objects that are
// referenced in UDPRoute objects via `.spec.rules.backendRefs`. This helps in
// querying for UDPRoutes that are affected by a particular Service CRUD.
func addUDPRouteIndexers(ctx context.Context, mgr manager.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1a2.UDPRoute{}, gatewayUDPRouteIndex, func(rawObj client.Object) []string {
udpRoute := rawObj.(*gwapiv1a2.UDPRoute)
var gateways []string
for _, parent := range udpRoute.Spec.ParentRefs {
if string(*parent.Kind) == gatewayapi.KindGateway {
// If an explicit Gateway namespace is not provided, use the UDPRoute namespace to
// lookup the provided Gateway Name.
gateways = append(gateways,
types.NamespacedName{
Namespace: gatewayapi.NamespaceDerefOrAlpha(parent.Namespace, udpRoute.Namespace),
Name: string(parent.Name),
}.String(),
)
}
}
return gateways
}); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1a2.UDPRoute{}, serviceUDPRouteIndex, serviceUDPRouteIndexFunc); err != nil {
return err
}
return nil
}
func serviceUDPRouteIndexFunc(rawObj client.Object) []string {
udproute := rawObj.(*gwapiv1a2.UDPRoute)
var services []string
for _, rule := range udproute.Spec.Rules {
for _, backend := range rule.BackendRefs {
if backend.Kind == nil || string(*backend.Kind) == gatewayapi.KindService {
// If an explicit Service namespace is not provided, use the UDPRoute namespace to
// lookup the provided Gateway Name.
services = append(services,
types.NamespacedName{
Namespace: gatewayapi.NamespaceDerefOrAlpha(backend.Namespace, udproute.Namespace),
Name: string(backend.Name),
}.String(),
)
}
}
}
return services
}
// addGatewayIndexers adds indexing on Gateway, for Secret objects that are
// referenced in Gateway objects. This helps in querying for Gateways that are
// affected by a particular Secret CRUD.
func addGatewayIndexers(ctx context.Context, mgr manager.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1b1.Gateway{}, secretGatewayIndex, secretGatewayIndexFunc); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1b1.Gateway{}, classGatewayIndex, func(rawObj client.Object) []string {
gateway := rawObj.(*gwapiv1b1.Gateway)
return []string{string(gateway.Spec.GatewayClassName)}
}); err != nil {
return err
}
return nil
}
func secretGatewayIndexFunc(rawObj client.Object) []string {
gateway := rawObj.(*gwapiv1b1.Gateway)
var secretReferences []string
for _, listener := range gateway.Spec.Listeners {
if listener.TLS == nil || *listener.TLS.Mode != gwapiv1b1.TLSModeTerminate {
continue
}
for _, cert := range listener.TLS.CertificateRefs {
if *cert.Kind == gatewayapi.KindSecret {
// If an explicit Secret namespace is not provided, use the Gateway namespace to
// lookup the provided Secret Name.
secretReferences = append(secretReferences,
types.NamespacedName{
Namespace: gatewayapi.NamespaceDerefOr(cert.Namespace, gateway.Namespace),
Name: string(cert.Name),
}.String(),
)
}
}
}
return secretReferences
}
// removeFinalizer removes the gatewayclass finalizer from the provided gc, if it exists.
func (r *gatewayAPIReconciler) removeFinalizer(ctx context.Context, gc *gwapiv1b1.GatewayClass) error {
if slice.ContainsString(gc.Finalizers, gatewayClassFinalizer) {
base := client.MergeFrom(gc.DeepCopy())
gc.Finalizers = slice.RemoveString(gc.Finalizers, gatewayClassFinalizer)
if err := r.client.Patch(ctx, gc, base); err != nil {
return fmt.Errorf("failed to remove finalizer from gatewayclass %s: %w", gc.Name, err)
}
}
return nil
}
// addFinalizer adds the gatewayclass finalizer to the provided gc, if it doesn't exist.
func (r *gatewayAPIReconciler) addFinalizer(ctx context.Context, gc *gwapiv1b1.GatewayClass) error {
if !slice.ContainsString(gc.Finalizers, gatewayClassFinalizer) {
base := client.MergeFrom(gc.DeepCopy())
gc.Finalizers = append(gc.Finalizers, gatewayClassFinalizer)
if err := r.client.Patch(ctx, gc, base); err != nil {
return fmt.Errorf("failed to add finalizer to gatewayclass %s: %w", gc.Name, err)
}
}
return nil
}
// subscribeAndUpdateStatus subscribes to gateway API object status updates and
// writes it into the Kubernetes API Server.
func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context) {
// Gateway object status updater
go func() {
message.HandleSubscription(r.resources.GatewayStatuses.Subscribe(ctx),
func(update message.Update[types.NamespacedName, *gwapiv1b1.GatewayStatus]) {
// skip delete updates.
if update.Delete {
return
}
// Get gateway object
gtw := new(gwapiv1b1.Gateway)
if err := r.client.Get(ctx, update.Key, gtw); err != nil {
r.log.Error(err, "gateway not found", "namespace", gtw.Namespace, "name", gtw.Name)
return
}
// Set the updated Status and call the status update
gtw.Status = *update.Value
r.statusUpdateForGateway(ctx, gtw)
},
)
r.log.Info("gateway status subscriber shutting down")
}()
// HTTPRoute object status updater
go func() {
message.HandleSubscription(r.resources.HTTPRouteStatuses.Subscribe(ctx),
func(update message.Update[types.NamespacedName, *gwapiv1b1.HTTPRouteStatus]) {
// skip delete updates.
if update.Delete {
return
}
key := update.Key
val := update.Value
r.statusUpdater.Send(status.Update{
NamespacedName: key,
Resource: new(gwapiv1b1.HTTPRoute),
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
h, ok := obj.(*gwapiv1b1.HTTPRoute)
if !ok {
panic(fmt.Sprintf("unsupported object type %T", obj))