-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
httproute.go
174 lines (155 loc) · 5.55 KB
/
httproute.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
// 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"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
"github.com/cilium/cilium/operator/pkg/gateway-api/helpers"
"github.com/cilium/cilium/pkg/logging/logfields"
)
const (
backendServiceIndex = "backendServiceIndex"
gatewayIndex = "gatewayIndex"
)
// httpRouteReconciler reconciles a HTTPRoute object
type httpRouteReconciler struct {
client.Client
Scheme *runtime.Scheme
Model *internalModel
}
// SetupWithManager sets up the controller with the Manager.
func (r *httpRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &gatewayv1beta1.HTTPRoute{}, backendServiceIndex,
func(rawObj client.Object) []string {
hr, ok := rawObj.(*gatewayv1beta1.HTTPRoute)
if !ok {
return nil
}
var backendServices []string
for _, rule := range hr.Spec.Rules {
for _, backend := range rule.BackendRefs {
if !helpers.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(), &gatewayv1beta1.HTTPRoute{}, gatewayIndex,
func(rawObj client.Object) []string {
hr := rawObj.(*gatewayv1beta1.HTTPRoute)
var gateways []string
for _, parent := range hr.Spec.ParentRefs {
if !helpers.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 HTTPRoute
For(&gatewayv1beta1.HTTPRoute{}).
// Watch for changes to Backend services
Watches(&corev1.Service{}, r.enqueueRequestForBackendService()).
// Watch for changes to Reference Grants
Watches(&gatewayv1beta1.ReferenceGrant{}, r.enqueueRequestForReferenceGrant()).
// Watch for changes to Gateways and enqueue HTTPRoutes that reference them,
Watches(&gatewayv1beta1.Gateway{}, r.enqueueRequestForGateway(),
builder.WithPredicates(
predicate.NewPredicateFuncs(hasMatchingController(context.Background(), mgr.GetClient(), controllerName)))).
Complete(r)
}
// enqueueRequestForBackendService makes sure that HTTP Routes are reconciled
// if the backend services are updated.
func (r *httpRouteReconciler) enqueueRequestForBackendService() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(r.enqueueFromIndex(backendServiceIndex))
}
// enqueueRequestForReferenceGrant makes sure that all HTTP Routes are reconciled
// if a ReferenceGrant changes
func (r *httpRouteReconciler) enqueueRequestForReferenceGrant() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(r.enqueueAll())
}
func (r *httpRouteReconciler) enqueueRequestForGateway() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(r.enqueueFromIndex(gatewayIndex))
}
func (r *httpRouteReconciler) enqueueFromIndex(index string) handler.MapFunc {
return func(ctx context.Context, o client.Object) []reconcile.Request {
scopedLog := log.WithFields(logrus.Fields{
logfields.Controller: "httpRoute",
logfields.Resource: client.ObjectKeyFromObject(o),
})
hrList := &gatewayv1beta1.HTTPRouteList{}
if err := r.Client.List(ctx, hrList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(index, client.ObjectKeyFromObject(o).String()),
}); err != nil {
scopedLog.WithError(err).Error("Failed to get related HTTPRoutes")
return []reconcile.Request{}
}
requests := make([]reconcile.Request, 0, len(hrList.Items))
for _, item := range hrList.Items {
route := client.ObjectKey{
Namespace: item.GetNamespace(),
Name: item.GetName(),
}
requests = append(requests, reconcile.Request{
NamespacedName: route,
})
scopedLog.WithField("httpRoute", route).Info("Enqueued HTTPRoute for resource")
}
return requests
}
}
func (r *httpRouteReconciler) enqueueAll() handler.MapFunc {
return func(ctx context.Context, o client.Object) []reconcile.Request {
scopedLog := log.WithFields(logrus.Fields{
logfields.Controller: "httpRoute",
logfields.Resource: client.ObjectKeyFromObject(o),
})
hrList := &gatewayv1beta1.HTTPRouteList{}
if err := r.Client.List(ctx, hrList, &client.ListOptions{}); err != nil {
scopedLog.WithError(err).Error("Failed to get HTTPRoutes")
return []reconcile.Request{}
}
requests := make([]reconcile.Request, 0, len(hrList.Items))
for _, item := range hrList.Items {
route := client.ObjectKey{
Namespace: item.GetNamespace(),
Name: item.GetName(),
}
requests = append(requests, reconcile.Request{
NamespacedName: route,
})
scopedLog.WithField("httpRoute", route).Info("Enqueued HTTPRoute for resource")
}
return requests
}
}