-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathcontroller.go
1409 lines (1266 loc) · 49.7 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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/discovery"
"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"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwapiv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1"
mcsapi "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/api/v1alpha1/validation"
"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/status"
"github.com/envoyproxy/gateway/internal/utils"
"github.com/envoyproxy/gateway/internal/utils/slice"
)
type gatewayAPIReconciler struct {
client client.Client
log logging.Logger
statusUpdater status.Updater
classController gwapiv1.GatewayController
store *kubernetesProviderStore
namespace string
namespaceLabel *metav1.LabelSelector
envoyGateway *egv1a1.EnvoyGateway
mergeGateways sets.Set[string]
resources *message.ProviderResources
extGVKs []schema.GroupVersionKind
}
// newGatewayAPIController
func newGatewayAPIController(mgr manager.Manager, cfg *config.Server, su status.Updater,
resources *message.ProviderResources) 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)
}
}
byNamespaceSelector := cfg.EnvoyGateway.Provider != nil &&
cfg.EnvoyGateway.Provider.Kubernetes != nil &&
cfg.EnvoyGateway.Provider.Kubernetes.Watch != nil &&
cfg.EnvoyGateway.Provider.Kubernetes.Watch.Type == egv1a1.KubernetesWatchModeTypeNamespaceSelector &&
(cfg.EnvoyGateway.Provider.Kubernetes.Watch.NamespaceSelector.MatchLabels != nil ||
len(cfg.EnvoyGateway.Provider.Kubernetes.Watch.NamespaceSelector.MatchExpressions) > 0)
r := &gatewayAPIReconciler{
client: mgr.GetClient(),
log: cfg.Logger,
classController: gwapiv1.GatewayController(cfg.EnvoyGateway.Gateway.ControllerName),
namespace: cfg.Namespace,
statusUpdater: su,
resources: resources,
extGVKs: extGVKs,
store: newProviderStore(),
envoyGateway: cfg.EnvoyGateway,
mergeGateways: sets.New[string](),
}
if byNamespaceSelector {
r.namespaceLabel = cfg.EnvoyGateway.Provider.Kubernetes.Watch.NamespaceSelector
}
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 backendRefs' NamespaceNames referred by various Route objects.
allAssociatedBackendRefs map[gwapiv1.BackendObjectReference]struct{}
// 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[gwapiv1.BackendObjectReference]struct{}{},
extensionRefFilters: map[types.NamespacedName]unstructured.Unstructured{},
}
}
// Reconcile handles reconciling all resources in a single call. Any resource event should enqueue the
// same reconcile.Request containing the gateway controller name. This allows multiple resource updates to
// be handled by a single call to Reconcile. The reconcile.Request DOES NOT map to a specific resource.
func (r *gatewayAPIReconciler) Reconcile(ctx context.Context, _ reconcile.Request) (reconcile.Result, error) {
var (
managedGCs []*gwapiv1.GatewayClass
err error
)
r.log.Info("reconciling gateways")
// Get the GatewayClasses managed by the Envoy Gateway Controller.
managedGCs, err = r.managedGatewayClasses(ctx)
if err != nil {
return reconcile.Result{}, err
}
// The gatewayclass was already deleted/finalized and there are stale queue entries.
if managedGCs == nil {
r.resources.GatewayAPIResources.Delete(string(r.classController))
r.log.Info("no accepted gatewayclass")
return reconcile.Result{}, nil
}
// Collect all the Gateway API resources, Envoy Gateway customized resources,
// and their referenced resources for the managed GatewayClasses, and store
// them per GatewayClass.
// For example:
// - Gateway API resources: Gateways, xRoutes ...
// - Envoy Gateway customized resources: EnvoyPatchPolicies, ClientTrafficPolicies, BackendTrafficPolicies ...
// - Referenced resources: Services, ServiceImports, EndpointSlices, Secrets, ConfigMaps ...
gwcResources := make(gatewayapi.ControllerResources, 0, len(managedGCs))
for _, managedGC := range managedGCs {
// Initialize resource types.
managedGC := managedGC
gwcResource := gatewayapi.NewResources()
gwcResource.GatewayClass = managedGC
gwcResources = append(gwcResources, gwcResource)
resourceMappings := newResourceMapping()
// Add all Gateways, their associated Routes, and referenced resources to the resourceTree
if err = r.processGateways(ctx, managedGC, resourceMappings, gwcResource); err != nil {
return reconcile.Result{}, err
}
// Add all EnvoyPatchPolicies to the resourceTree
if err = r.processEnvoyPatchPolicies(ctx, gwcResource); err != nil {
return reconcile.Result{}, err
}
// Add all ClientTrafficPolicies and their referenced resources to the resourceTree
if err = r.processClientTrafficPolicies(ctx, gwcResource, resourceMappings); err != nil {
return reconcile.Result{}, err
}
// Add all BackendTrafficPolicies to the resourceTree
if err = r.processBackendTrafficPolicies(ctx, gwcResource); err != nil {
return reconcile.Result{}, err
}
// Add all SecurityPolicies and their referenced resources to the resourceTree
if err = r.processSecurityPolicies(ctx, gwcResource, resourceMappings); err != nil {
return reconcile.Result{}, err
}
// Add all BackendTLSPolies to the resourceTree
if err = r.processBackendTLSPolicies(ctx, gwcResource, resourceMappings); err != nil {
return reconcile.Result{}, err
}
// Add the referenced services, ServiceImports, and EndpointSlices in
// the collected BackendRefs to the resourceTree.
// BackendRefs are referred by various Route objects and the ExtAuth in SecurityPolicies.
r.processBackendRefs(ctx, gwcResource, resourceMappings)
// For this particular Gateway, and all associated objects, check whether the
// namespace exists. Add to the resourceTree.
for ns := range resourceMappings.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
}
gwcResource.Namespaces = append(gwcResource.Namespaces, namespace)
}
// Process the parametersRef of the accepted GatewayClass.
if managedGC.Spec.ParametersRef != nil && managedGC.DeletionTimestamp == nil {
if err := r.processParamsRef(ctx, managedGC, gwcResource); err != nil {
msg := fmt.Sprintf("%s: %v", status.MsgGatewayClassInvalidParams, err)
if err := r.updateStatusForGatewayClass(ctx, managedGC, false, string(gwapiv1.GatewayClassReasonInvalidParameters), msg); err != nil {
r.log.Error(err, "unable to update GatewayClass status")
}
r.log.Error(err, "failed to process parametersRef for gatewayclass", "name", managedGC.Name)
return reconcile.Result{}, err
}
}
if gwcResource.EnvoyProxy != nil && gwcResource.EnvoyProxy.Spec.MergeGateways != nil {
if *gwcResource.EnvoyProxy.Spec.MergeGateways {
r.mergeGateways.Insert(managedGC.Name)
} else {
r.mergeGateways.Delete(managedGC.Name)
}
}
if err := r.updateStatusForGatewayClass(ctx, managedGC, true, string(gwapiv1.GatewayClassReasonAccepted), status.MsgValidGatewayClass); err != nil {
r.log.Error(err, "unable to update GatewayClass status")
return reconcile.Result{}, err
}
if len(gwcResource.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, managedGC); err != nil {
r.log.Error(err, fmt.Sprintf("failed to remove finalizer from gatewayclass %s",
managedGC.Name))
return reconcile.Result{}, err
}
} else {
// finalize the accepted GatewayClass.
if err := r.addFinalizer(ctx, managedGC); err != nil {
r.log.Error(err, fmt.Sprintf("failed adding finalizer to gatewayclass %s",
managedGC.Name))
return reconcile.Result{}, err
}
}
}
// Store the Gateway Resources for the GatewayClass.
// 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(string(r.classController), &gwcResources)
r.log.Info("reconciled gateways successfully")
return reconcile.Result{}, nil
}
// managedGatewayClasses returns a list of GatewayClass objects that are managed by the Envoy Gateway Controller.
func (r *gatewayAPIReconciler) managedGatewayClasses(ctx context.Context) ([]*gwapiv1.GatewayClass, error) {
var gatewayClasses gwapiv1.GatewayClassList
if err := r.client.List(ctx, &gatewayClasses); err != nil {
return nil, fmt.Errorf("error listing gatewayclasses: %w", 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)
continue
}
cc.addMatch(&gwClass)
}
}
return cc.matchedClasses, nil
}
// processBackendRefs adds the referenced resources in BackendRefs to the resourceTree, including:
// - Services
// - ServiceImports
// - EndpointSlices
func (r *gatewayAPIReconciler) processBackendRefs(ctx context.Context, gwcResource *gatewayapi.Resources, resourceMappings *resourceMappings) {
for backendRef := range resourceMappings.allAssociatedBackendRefs {
backendRefKind := gatewayapi.KindDerefOr(backendRef.Kind, gatewayapi.KindService)
r.log.Info("processing Backend", "kind", backendRefKind, "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
var endpointSliceLabelKey string
switch backendRefKind {
case gatewayapi.KindService:
service := new(corev1.Service)
err := r.client.Get(ctx, types.NamespacedName{Namespace: string(*backendRef.Namespace), Name: string(backendRef.Name)}, service)
if err != nil {
r.log.Error(err, "failed to get Service", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
} else {
resourceMappings.allAssociatedNamespaces[service.Namespace] = struct{}{}
gwcResource.Services = append(gwcResource.Services, service)
r.log.Info("added Service to resource tree", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
}
endpointSliceLabelKey = discoveryv1.LabelServiceName
case gatewayapi.KindServiceImport:
serviceImport := new(mcsapi.ServiceImport)
err := r.client.Get(ctx, types.NamespacedName{Namespace: string(*backendRef.Namespace), Name: string(backendRef.Name)}, serviceImport)
if err != nil {
r.log.Error(err, "failed to get ServiceImport", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
} else {
resourceMappings.allAssociatedNamespaces[serviceImport.Namespace] = struct{}{}
gwcResource.ServiceImports = append(gwcResource.ServiceImports, serviceImport)
r.log.Info("added ServiceImport to resource tree", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
}
endpointSliceLabelKey = mcsapi.LabelServiceName
}
// Retrieve the EndpointSlices associated with the service
endpointSliceList := new(discoveryv1.EndpointSliceList)
opts := []client.ListOption{
client.MatchingLabels(map[string]string{
endpointSliceLabelKey: string(backendRef.Name),
}),
client.InNamespace(string(*backendRef.Namespace)),
}
if err := r.client.List(ctx, endpointSliceList, opts...); err != nil {
r.log.Error(err, "failed to get EndpointSlices", "namespace", string(*backendRef.Namespace),
backendRefKind, string(backendRef.Name))
} else {
for _, endpointSlice := range endpointSliceList.Items {
endpointSlice := endpointSlice
r.log.Info("added EndpointSlice to resource tree", "namespace", endpointSlice.Namespace,
"name", endpointSlice.Name)
gwcResource.EndpointSlices = append(gwcResource.EndpointSlices, &endpointSlice)
}
}
}
}
// processSecurityPolicyObjectRefs adds the referenced resources in SecurityPolicies
// to the resourceTree
// - Secrets for OIDC and BasicAuth
// - BackendRefs for ExAuth
func (r *gatewayAPIReconciler) processSecurityPolicyObjectRefs(
ctx context.Context, resourceTree *gatewayapi.Resources, resourceMap *resourceMappings) {
// we don't return errors from this method, because we want to continue reconciling
// the rest of the SecurityPolicies despite that one reference is invalid. This
// allows Envoy Gateway to continue serving traffic even if some SecurityPolicies
// are invalid.
//
// This SecurityPolicy will be marked as invalid in its status when translating
// to IR because the referenced secret can't be found.
for _, policy := range resourceTree.SecurityPolicies {
oidc := policy.Spec.OIDC
// Add the referenced Secrets in OIDC to the resourceTree
if oidc != nil {
if err := r.processSecretRef(
ctx,
resourceMap,
resourceTree,
gatewayapi.KindSecurityPolicy,
policy.Namespace,
policy.Name,
oidc.ClientSecret); err != nil {
r.log.Error(err,
"failed to process OIDC SecretRef for SecurityPolicy",
"policy", policy, "secretRef", oidc.ClientSecret)
}
}
// Add the referenced Secrets in BasicAuth to the resourceTree
basicAuth := policy.Spec.BasicAuth
if basicAuth != nil {
if err := r.processSecretRef(
ctx,
resourceMap,
resourceTree,
gatewayapi.KindSecurityPolicy,
policy.Namespace,
policy.Name,
basicAuth.Users); err != nil {
r.log.Error(err,
"failed to process BasicAuth SecretRef for SecurityPolicy",
"policy", policy, "secretRef", basicAuth.Users)
}
}
// Add the referenced BackendRefs and ReferenceGrants in ExtAuth to Maps for later processing
extAuth := policy.Spec.ExtAuth
if extAuth != nil {
var backendRef gwapiv1.BackendObjectReference
if extAuth.GRPC != nil {
backendRef = extAuth.GRPC.BackendRef
} else {
backendRef = extAuth.HTTP.BackendRef
}
backendNamespace := gatewayapi.NamespaceDerefOr(backendRef.Namespace, policy.Namespace)
resourceMap.allAssociatedBackendRefs[gwapiv1.BackendObjectReference{
Group: backendRef.Group,
Kind: backendRef.Kind,
Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace),
Name: backendRef.Name,
}] = struct{}{}
if backendNamespace != policy.Namespace {
from := ObjectKindNamespacedName{
kind: gatewayapi.KindHTTPRoute,
namespace: policy.Namespace,
name: policy.Name,
}
to := ObjectKindNamespacedName{
kind: gatewayapi.KindDerefOr(backendRef.Kind, gatewayapi.KindService),
namespace: backendNamespace,
name: string(backendRef.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:
resourceTree.ReferenceGrants = append(resourceTree.ReferenceGrants, refGrant)
r.log.Info("added ReferenceGrant to resource map", "namespace", refGrant.Namespace,
"name", refGrant.Name)
}
}
}
}
}
// processOIDCHMACSecret adds the OIDC HMAC Secret to the resourceTree.
// The OIDC HMAC Secret is created by the CertGen job and is used by SecurityPolicy
// to configure OAuth2 filters.
func (r *gatewayAPIReconciler) processOIDCHMACSecret(ctx context.Context, resourceTree *gatewayapi.Resources) {
var (
secret corev1.Secret
err error
)
err = r.client.Get(ctx,
types.NamespacedName{Namespace: r.namespace, Name: oidcHMACSecretName},
&secret,
)
// We don't return an error here, because we want to continue reconciling
// despite that the OIDC HMAC secret can't be found.
// If the OIDC HMAC Secret is missing, the SecurityPolicy with OIDC will be
// marked as invalid in its status when translating to IR.
if err != nil {
r.log.Error(err,
"failed to process OIDC HMAC Secret",
"namespace", r.namespace, "name", oidcHMACSecretName)
return
}
resourceTree.Secrets = append(resourceTree.Secrets, &secret)
r.log.Info("processing OIDC HMAC Secret", "namespace", r.namespace, "name", oidcHMACSecretName)
}
// processSecretRef adds the referenced Secret to the resourceTree if it's valid.
// - If it exists in the same namespace as the owner.
// - If it exists in a different namespace, and there is a ReferenceGrant.
func (r *gatewayAPIReconciler) processSecretRef(
ctx context.Context,
resourceMap *resourceMappings,
resourceTree *gatewayapi.Resources,
ownerKind string,
ownerNS string,
ownerName string,
secretRef gwapiv1b1.SecretObjectReference,
) error {
secret := new(corev1.Secret)
secretNS := gatewayapi.NamespaceDerefOr(secretRef.Namespace, ownerNS)
err := r.client.Get(ctx,
types.NamespacedName{Namespace: secretNS, Name: string(secretRef.Name)},
secret,
)
if err != nil && !kerrors.IsNotFound(err) {
return fmt.Errorf("unable to find the Secret: %s/%s", secretNS, string(secretRef.Name))
}
if secretNS != ownerNS {
from := ObjectKindNamespacedName{
kind: ownerKind,
namespace: ownerNS,
name: ownerName,
}
to := ObjectKindNamespacedName{
kind: gatewayapi.KindSecret,
namespace: secretNS,
name: secret.Name,
}
refGrant, err := r.findReferenceGrant(ctx, from, to)
switch {
case err != nil:
return fmt.Errorf("failed to find ReferenceGrant: %w", err)
case refGrant == nil:
return fmt.Errorf(
"no matching ReferenceGrants found: from %s/%s to %s/%s",
from.kind, from.namespace, to.kind, to.namespace)
default:
// RefGrant found
resourceTree.ReferenceGrants = append(resourceTree.ReferenceGrants, refGrant)
r.log.Info("added ReferenceGrant to resource map", "namespace", refGrant.Namespace,
"name", refGrant.Name)
}
}
resourceMap.allAssociatedNamespaces[secretNS] = struct{}{} // TODO Zhaohuabing do we need this line?
resourceTree.Secrets = append(resourceTree.Secrets, secret)
r.log.Info("processing Secret", "namespace", secretNS, "name", string(secretRef.Name))
return nil
}
// processCtpConfigMapRefs adds the referenced ConfigMaps in ClientTrafficPolicies
// to the resourceTree
func (r *gatewayAPIReconciler) processCtpConfigMapRefs(
ctx context.Context, resourceTree *gatewayapi.Resources, resourceMap *resourceMappings) {
for _, policy := range resourceTree.ClientTrafficPolicies {
tls := policy.Spec.TLS
if tls != nil && tls.ClientValidation != nil {
for _, caCertRef := range tls.ClientValidation.CACertificateRefs {
if caCertRef.Kind != nil && string(*caCertRef.Kind) == gatewayapi.KindConfigMap {
if err := r.processConfigMapRef(
ctx,
resourceMap,
resourceTree,
gatewayapi.KindClientTrafficPolicy,
policy.Namespace,
policy.Name,
caCertRef); err != nil {
// we don't return an error here, because we want to continue
// reconciling the rest of the ClientTrafficPolicies despite that this
// reference is invalid.
// This ClientTrafficPolicy will be marked as invalid in its status
// when translating to IR because the referenced configmap can't be
// found.
r.log.Error(err,
"failed to process CACertificateRef for ClientTrafficPolicy",
"policy", policy, "caCertificateRef", caCertRef.Name)
}
} else if caCertRef.Kind == nil || string(*caCertRef.Kind) == gatewayapi.KindSecret {
if err := r.processSecretRef(
ctx,
resourceMap,
resourceTree,
gatewayapi.KindClientTrafficPolicy,
policy.Namespace,
policy.Name,
caCertRef); err != nil {
r.log.Error(err,
"failed to process CACertificateRef for SecurityPolicy",
"policy", policy, "caCertificateRef", caCertRef.Name)
}
}
}
}
}
}
// processConfigMapRef adds the referenced ConfigMap to the resourceTree if it's valid.
// - If it exists in the same namespace as the owner.
// - If it exists in a different namespace, and there is a ReferenceGrant.
func (r *gatewayAPIReconciler) processConfigMapRef(
ctx context.Context,
resourceMap *resourceMappings,
resourceTree *gatewayapi.Resources,
ownerKind string,
ownerNS string,
ownerName string,
configMapRef gwapiv1b1.SecretObjectReference,
) error {
configMap := new(corev1.ConfigMap)
configMapNS := gatewayapi.NamespaceDerefOr(configMapRef.Namespace, ownerNS)
err := r.client.Get(ctx,
types.NamespacedName{Namespace: configMapNS, Name: string(configMapRef.Name)},
configMap,
)
if err != nil && !kerrors.IsNotFound(err) {
return fmt.Errorf("unable to find the ConfigMap: %s/%s", configMapNS, string(configMapRef.Name))
}
if configMapNS != ownerNS {
from := ObjectKindNamespacedName{
kind: ownerKind,
namespace: ownerNS,
name: ownerName,
}
to := ObjectKindNamespacedName{
kind: gatewayapi.KindConfigMap,
namespace: configMapNS,
name: configMap.Name,
}
refGrant, err := r.findReferenceGrant(ctx, from, to)
switch {
case err != nil:
return fmt.Errorf("failed to find ReferenceGrant: %w", err)
case refGrant == nil:
return fmt.Errorf(
"no matching ReferenceGrants found: from %s/%s to %s/%s",
from.kind, from.namespace, to.kind, to.namespace)
default:
// RefGrant found
resourceTree.ReferenceGrants = append(resourceTree.ReferenceGrants, refGrant)
r.log.Info("added ReferenceGrant to resource map", "namespace", refGrant.Namespace,
"name", refGrant.Name)
}
}
resourceMap.allAssociatedNamespaces[configMapNS] = struct{}{} // TODO Zhaohuabing do we need this line?
resourceTree.ConfigMaps = append(resourceTree.ConfigMaps, configMap)
r.log.Info("processing ConfigMap", "namespace", configMapNS, "name", string(configMapRef.Name))
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) findReferenceGrant(ctx context.Context, from, to ObjectKindNamespacedName) (*gwapiv1b1.ReferenceGrant, error) {
refGrantList := new(gwapiv1b1.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: %w", err)
}
refGrants := refGrantList.Items
if r.namespaceLabel != nil {
var rgs []gwapiv1b1.ReferenceGrant
for _, refGrant := range refGrants {
refGrant := refGrant
if ok, err := r.checkObjectNamespaceLabels(&refGrant); err != nil {
r.log.Error(err, "failed to check namespace labels for ReferenceGrant %s in namespace %s: %w", refGrant.GetName(), refGrant.GetNamespace())
continue
} else if !ok {
continue
}
rgs = append(rgs, refGrant)
}
refGrants = rgs
}
for _, refGrant := range refGrants {
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, managedGC *gwapiv1.GatewayClass, resourceMap *resourceMappings, resourceTree *gatewayapi.Resources) error {
// Find gateways for the managedGC
// Find the Gateways that reference this Class.
gatewayList := &gwapiv1.GatewayList{}
if err := r.client.List(ctx, gatewayList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(classGatewayIndex, managedGC.Name),
}); err != nil {
r.log.Info("no associated Gateways found for GatewayClass", "name", managedGC.Name)
return err
}
for _, gtw := range gatewayList.Items {
gtw := gtw
if r.namespaceLabel != nil {
if ok, err := r.checkObjectNamespaceLabels(>w); err != nil {
r.log.Error(err, "failed to check namespace labels for gateway %s in namespace %s: %w", gtw.GetName(), gtw.GetNamespace())
continue
} else if !ok {
continue
}
}
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) {
if err := r.processSecretRef(
ctx,
resourceMap,
resourceTree,
gatewayapi.KindGateway,
gtw.Namespace,
gtw.Name,
certRef); err != nil {
r.log.Error(err,
"failed to process TLS SecretRef for gateway",
"gateway", gtw, "secretRef", certRef)
}
}
}
}
}
// 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 = gwapiv1.GatewayStatus{}
resourceTree.Gateways = append(resourceTree.Gateways, >w)
}
return nil
}
// processEnvoyPatchPolicies adds EnvoyPatchPolicies to the resourceTree
func (r *gatewayAPIReconciler) processEnvoyPatchPolicies(ctx context.Context, resourceTree *gatewayapi.Resources) error {
envoyPatchPolicies := egv1a1.EnvoyPatchPolicyList{}
if err := r.client.List(ctx, &envoyPatchPolicies); err != nil {
return fmt.Errorf("error listing EnvoyPatchPolicies: %w", 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 = gwapiv1a2.PolicyStatus{}
resourceTree.EnvoyPatchPolicies = append(resourceTree.EnvoyPatchPolicies, &policy)
}
return nil
}
// processClientTrafficPolicies adds ClientTrafficPolicies to the resourceTree
func (r *gatewayAPIReconciler) processClientTrafficPolicies(
ctx context.Context, resourceTree *gatewayapi.Resources, resourceMap *resourceMappings) error {
clientTrafficPolicies := egv1a1.ClientTrafficPolicyList{}
if err := r.client.List(ctx, &clientTrafficPolicies); err != nil {
return fmt.Errorf("error listing ClientTrafficPolicies: %w", err)
}
for _, policy := range clientTrafficPolicies.Items {
policy := policy
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
policy.Status = gwapiv1a2.PolicyStatus{}
resourceTree.ClientTrafficPolicies = append(resourceTree.ClientTrafficPolicies, &policy)
}
r.processCtpConfigMapRefs(ctx, resourceTree, resourceMap)
return nil
}
// processBackendTrafficPolicies adds BackendTrafficPolicies to the resourceTree
func (r *gatewayAPIReconciler) processBackendTrafficPolicies(ctx context.Context, resourceTree *gatewayapi.Resources) error {
backendTrafficPolicies := egv1a1.BackendTrafficPolicyList{}
if err := r.client.List(ctx, &backendTrafficPolicies); err != nil {
return fmt.Errorf("error listing BackendTrafficPolicies: %w", err)
}
for _, policy := range backendTrafficPolicies.Items {
policy := policy
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
policy.Status = gwapiv1a2.PolicyStatus{}
resourceTree.BackendTrafficPolicies = append(resourceTree.BackendTrafficPolicies, &policy)
}
return nil
}
// processSecurityPolicies adds SecurityPolicies and their referenced resources to the resourceTree
func (r *gatewayAPIReconciler) processSecurityPolicies(
ctx context.Context, resourceTree *gatewayapi.Resources, resourceMap *resourceMappings) error {
securityPolicies := egv1a1.SecurityPolicyList{}
if err := r.client.List(ctx, &securityPolicies); err != nil {
return fmt.Errorf("error listing SecurityPolicies: %w", err)
}
for _, policy := range securityPolicies.Items {
policy := policy
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
policy.Status = gwapiv1a2.PolicyStatus{}
resourceTree.SecurityPolicies = append(resourceTree.SecurityPolicies, &policy)
}
// Add the referenced Resources in SecurityPolicies to the resourceTree
r.processSecurityPolicyObjectRefs(ctx, resourceTree, resourceMap)
// Add the OIDC HMAC Secret to the resourceTree
r.processOIDCHMACSecret(ctx, resourceTree)
return nil
}
// processBackendTLSPolicies adds BackendTLSPolicies and their referenced resources to the resourceTree
func (r *gatewayAPIReconciler) processBackendTLSPolicies(
ctx context.Context, resourceTree *gatewayapi.Resources, resourceMap *resourceMappings) error {
backendTLSPolicies := gwapiv1a2.BackendTLSPolicyList{}
if err := r.client.List(ctx, &backendTLSPolicies); err != nil {
return fmt.Errorf("error listing BackendTLSPolicies: %w", err)
}
for _, policy := range backendTLSPolicies.Items {
policy := policy
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
policy.Status = gwapiv1a2.PolicyStatus{}
resourceTree.BackendTLSPolicies = append(resourceTree.BackendTLSPolicies, &policy)
}
// Add the referenced Secrets and ConfigMaps in BackendTLSPolicies to the resourceTree.
r.processBackendTLSPolicyConfigMapRefs(ctx, resourceTree, resourceMap)
return nil
}
// removeFinalizer removes the gatewayclass finalizer from the provided gc, if it exists.
func (r *gatewayAPIReconciler) removeFinalizer(ctx context.Context, gc *gwapiv1.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 *gwapiv1.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
}
// watchResources watches gateway api resources.
func (r *gatewayAPIReconciler) watchResources(ctx context.Context, mgr manager.Manager, c controller.Controller) error {
// trigger a reconcile after getting elected
if err := c.Watch(
NewWatchAndReconcileSource(mgr.Elected(), &gwapiv1.GatewayClass{}),
handler.EnqueueRequestsFromMapFunc(r.enqueueClass)); err != nil {
return err
}
if err := c.Watch(
source.Kind(mgr.GetCache(), &gwapiv1.GatewayClass{}),
handler.EnqueueRequestsFromMapFunc(r.enqueueClass),
predicate.GenerationChangedPredicate{},
predicate.NewPredicateFuncs(r.hasMatchingController),
); err != nil {
return err
}
// Only enqueue EnvoyProxy objects that match this Envoy Gateway's GatewayClass.
epPredicates := []predicate.Predicate{
predicate.GenerationChangedPredicate{},
predicate.ResourceVersionChangedPredicate{},
predicate.NewPredicateFuncs(r.hasManagedClass),
}
if r.namespaceLabel != nil {
epPredicates = append(epPredicates, predicate.NewPredicateFuncs(r.hasMatchingNamespaceLabels))
}
if err := c.Watch(
source.Kind(mgr.GetCache(), &egv1a1.EnvoyProxy{}),
handler.EnqueueRequestsFromMapFunc(r.enqueueClass),
epPredicates...,
); err != nil {
return err
}
// Watch Gateway CRUDs and reconcile affected GatewayClass.
gPredicates := []predicate.Predicate{
predicate.GenerationChangedPredicate{},
predicate.NewPredicateFuncs(r.validateGatewayForReconcile),
}
if r.namespaceLabel != nil {
gPredicates = append(gPredicates, predicate.NewPredicateFuncs(r.hasMatchingNamespaceLabels))
}
if err := c.Watch(
source.Kind(mgr.GetCache(), &gwapiv1.Gateway{}),
handler.EnqueueRequestsFromMapFunc(r.enqueueClass),
gPredicates...,
); err != nil {
return err
}
if err := addGatewayIndexers(ctx, mgr); err != nil {
return err
}
// Watch HTTPRoute CRUDs and process affected Gateways.
httprPredicates := []predicate.Predicate{predicate.GenerationChangedPredicate{}}
if r.namespaceLabel != nil {
httprPredicates = append(httprPredicates, predicate.NewPredicateFuncs(r.hasMatchingNamespaceLabels))
}
if err := c.Watch(
source.Kind(mgr.GetCache(), &gwapiv1.HTTPRoute{}),
handler.EnqueueRequestsFromMapFunc(r.enqueueClass),
httprPredicates...,
); err != nil {
return err
}
if err := addHTTPRouteIndexers(ctx, mgr); err != nil {
return err
}
// Watch GRPCRoute CRUDs and process affected Gateways.
grpcrPredicates := []predicate.Predicate{predicate.GenerationChangedPredicate{}}
if r.namespaceLabel != nil {
grpcrPredicates = append(grpcrPredicates, predicate.NewPredicateFuncs(r.hasMatchingNamespaceLabels))
}
if err := c.Watch(
source.Kind(mgr.GetCache(), &gwapiv1a2.GRPCRoute{}),
handler.EnqueueRequestsFromMapFunc(r.enqueueClass),
grpcrPredicates...,
); err != nil {
return err
}
if err := addGRPCRouteIndexers(ctx, mgr); err != nil {
return err
}
// Watch TLSRoute CRUDs and process affected Gateways.
tlsrPredicates := []predicate.Predicate{predicate.GenerationChangedPredicate{}}
if r.namespaceLabel != nil {
tlsrPredicates = append(tlsrPredicates, predicate.NewPredicateFuncs(r.hasMatchingNamespaceLabels))
}
if err := c.Watch(
source.Kind(mgr.GetCache(), &gwapiv1a2.TLSRoute{}),
handler.EnqueueRequestsFromMapFunc(r.enqueueClass),
tlsrPredicates...,
); err != nil {
return err
}
if err := addTLSRouteIndexers(ctx, mgr); err != nil {