-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
tlsroute.go
137 lines (123 loc) · 4.39 KB
/
tlsroute.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package gateway_api
import (
"context"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
"github.com/cilium/cilium/operator/pkg/gateway-api/helpers"
"github.com/cilium/cilium/pkg/logging/logfields"
)
// tlsRouteReconciler reconciles a TLSRoute object
type tlsRouteReconciler struct {
client.Client
Scheme *runtime.Scheme
Model *internalModel
}
// SetupWithManager sets up the controller with the Manager.
func (r *tlsRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &gatewayv1alpha2.TLSRoute{}, backendServiceIndex,
func(rawObj client.Object) []string {
hr, ok := rawObj.(*gatewayv1alpha2.TLSRoute)
if !ok {
return nil
}
var backendServices []string
for _, rule := range hr.Spec.Rules {
for _, backend := range rule.BackendRefs {
if !IsService(backend.BackendObjectReference) {
continue
}
backendServices = append(backendServices,
types.NamespacedName{
Namespace: helpers.NamespaceDerefOr(backend.Namespace, hr.Namespace),
Name: string(backend.Name),
}.String(),
)
}
}
return backendServices
},
); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &gatewayv1alpha2.TLSRoute{}, gatewayIndex,
func(rawObj client.Object) []string {
hr := rawObj.(*gatewayv1alpha2.TLSRoute)
var gateways []string
for _, parent := range hr.Spec.ParentRefs {
if !IsGateway(parent) {
continue
}
gateways = append(gateways,
types.NamespacedName{
Namespace: helpers.NamespaceDerefOr(parent.Namespace, hr.Namespace),
Name: string(parent.Name),
}.String(),
)
}
return gateways
},
); err != nil {
return err
}
return ctrl.NewControllerManagedBy(mgr).
// Watch for changes to TLSRoute
For(&gatewayv1alpha2.TLSRoute{}).
// Watch for changes to Backend services
Watches(&corev1.Service{}, r.enqueueRequestForBackendService()).
// Watch for changes to Gateways and enqueue TLSRoutes that reference them
Watches(&gatewayv1beta1.Gateway{}, r.enqueueRequestForGateway()).
// Watch for changes to Gateways and enqueue TLSRoutes that reference them,
Watches(&gatewayv1beta1.Gateway{}, r.enqueueRequestForGateway(),
builder.WithPredicates(
predicate.NewPredicateFuncs(hasMatchingController(context.Background(), mgr.GetClient(), controllerName)),
)).
Complete(r)
}
// enqueueRequestForBackendService makes sure that TLS Routes are reconciled
// if the backend services are updated.
func (r *tlsRouteReconciler) enqueueRequestForBackendService() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(r.enqueueFromIndex(backendServiceIndex))
}
func (r *tlsRouteReconciler) enqueueRequestForGateway() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(r.enqueueFromIndex(gatewayIndex))
}
func (r *tlsRouteReconciler) enqueueFromIndex(index string) handler.MapFunc {
return func(ctx context.Context, o client.Object) []reconcile.Request {
scopedLog := log.WithFields(logrus.Fields{
logfields.Controller: "tlsRoute",
logfields.Resource: client.ObjectKeyFromObject(o),
})
rList := &gatewayv1alpha2.TLSRouteList{}
if err := r.Client.List(context.Background(), rList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(index, client.ObjectKeyFromObject(o).String()),
}); err != nil {
scopedLog.WithError(err).Error("Failed to get related TLSRoutes")
return []reconcile.Request{}
}
requests := make([]reconcile.Request, 0, len(rList.Items))
for _, item := range rList.Items {
route := client.ObjectKey{
Namespace: item.GetNamespace(),
Name: item.GetName(),
}
requests = append(requests, reconcile.Request{
NamespacedName: route,
})
scopedLog.WithField("tlsRoute", route).Info("Enqueued TLSRoute for resource")
}
return requests
}
}