-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
gateway.go
258 lines (227 loc) · 8.44 KB
/
gateway.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
// 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"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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"
)
const (
owningGatewayLabel = "io.cilium.gateway/owning-gateway"
lastTransitionTime = "LastTransitionTime"
)
// gatewayReconciler reconciles a Gateway object
type gatewayReconciler struct {
client.Client
Scheme *runtime.Scheme
SecretsNamespace string
IdleTimeoutSeconds int
controllerName string
Model *internalModel
}
// SetupWithManager sets up the controller with the Manager.
// The reconciler will be triggered by Gateway, or any cilium-managed GatewayClass events
func (r *gatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
hasMatchingControllerFn := hasMatchingController(context.Background(), r.Client, r.controllerName)
return ctrl.NewControllerManagedBy(mgr).
// Watch its own resource
For(&gatewayv1beta1.Gateway{},
builder.WithPredicates(predicate.NewPredicateFuncs(hasMatchingControllerFn))).
// Watch GatewayClass resources, which are linked to Gateway
Watches(&gatewayv1beta1.GatewayClass{},
r.enqueueRequestForOwningGatewayClass(),
builder.WithPredicates(predicate.NewPredicateFuncs(hasMatchingControllerFn))).
// Watch related LB service for status
Watches(&corev1.Service{},
r.enqueueRequestForOwningResource(),
builder.WithPredicates(predicate.NewPredicateFuncs(func(object client.Object) bool {
_, found := object.GetLabels()[owningGatewayLabel]
return found
}))).
// Watch HTTP Route status changes, there is one assumption that any change in spec will
// always update status always at least for observedGeneration value.
Watches(&gatewayv1beta1.HTTPRoute{},
r.enqueueRequestForOwningHTTPRoute(),
builder.WithPredicates(onlyStatusChanged())).
// Watch TLS Route status changes, there is one assumption that any change in spec will
// always update status always at least for observedGeneration value.
Watches(&gatewayv1alpha2.TLSRoute{},
r.enqueueRequestForOwningTLSRoute(),
builder.WithPredicates(onlyStatusChanged())).
// Watch related secrets used to configure TLS
Watches(&corev1.Secret{},
r.enqueueRequestForTLSSecret(),
builder.WithPredicates(predicate.NewPredicateFuncs(r.usedInGateway))).
// Watch related namespace in allowed namespaces
Watches(&corev1.Namespace{},
r.enqueueRequestForAllowedNamespace()).
Complete(r)
}
// enqueueRequestForOwningGatewayClass returns an event handler for all Gateway objects
// belonging to the given GatewayClass.
func (r *gatewayReconciler) enqueueRequestForOwningGatewayClass() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
scopedLog := log.WithFields(logrus.Fields{
logfields.Controller: gateway,
logfields.Resource: a.GetName(),
})
var reqs []reconcile.Request
gwList := &gatewayv1beta1.GatewayList{}
if err := r.Client.List(ctx, gwList); err != nil {
scopedLog.Error("Unable to list Gateways")
return nil
}
for _, gw := range gwList.Items {
if gw.Spec.GatewayClassName != gatewayv1beta1.ObjectName(a.GetName()) {
continue
}
req := reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: gw.Namespace,
Name: gw.Name,
},
}
reqs = append(reqs, req)
scopedLog.WithFields(logrus.Fields{
logfields.K8sNamespace: gw.GetNamespace(),
logfields.Resource: gw.GetName(),
}).Info("Queueing gateway")
}
return reqs
})
}
// enqueueRequestForOwningResource returns an event handler for all Gateway objects having
// owningGatewayLabel
func (r *gatewayReconciler) enqueueRequestForOwningResource() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
scopedLog := log.WithFields(logrus.Fields{
logfields.Controller: "gateway",
logfields.Resource: a.GetName(),
})
key, found := a.GetLabels()[owningGatewayLabel]
if !found {
return nil
}
scopedLog.WithFields(logrus.Fields{
logfields.K8sNamespace: a.GetNamespace(),
logfields.Resource: a.GetName(),
"gateway": key,
}).Info("Enqueued gateway for owning service")
return []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Namespace: a.GetNamespace(),
Name: key,
},
},
}
})
}
// enqueueRequestForOwningHTTPRoute returns an event handler for any changes with HTTP Routes
// belonging to the given Gateway
func (r *gatewayReconciler) enqueueRequestForOwningHTTPRoute() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
hr, ok := a.(*gatewayv1beta1.HTTPRoute)
if !ok {
return nil
}
return getReconcileRequestsForRoute(context.Background(), r.Client, a, hr.Spec.CommonRouteSpec)
})
}
// enqueueRequestForOwningTLSRoute returns an event handler for any changes with TLS Routes
// belonging to the given Gateway
func (r *gatewayReconciler) enqueueRequestForOwningTLSRoute() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
hr, ok := a.(*gatewayv1alpha2.TLSRoute)
if !ok {
return nil
}
return getReconcileRequestsForRoute(context.Background(), r.Client, a, hr.Spec.CommonRouteSpec)
})
}
func getReconcileRequestsForRoute(ctx context.Context, c client.Client, object metav1.Object, route gatewayv1beta1.CommonRouteSpec) []reconcile.Request {
var reqs []reconcile.Request
scopedLog := log.WithFields(logrus.Fields{
logfields.Controller: gateway,
logfields.Resource: types.NamespacedName{
Namespace: object.GetNamespace(),
Name: object.GetName(),
},
})
for _, parent := range route.ParentRefs {
if !IsGateway(parent) {
continue
}
ns := helpers.NamespaceDerefOr(parent.Namespace, object.GetNamespace())
gw := &gatewayv1beta1.Gateway{}
if err := c.Get(ctx, types.NamespacedName{
Namespace: ns,
Name: string(parent.Name),
}, gw); err != nil {
if !k8serrors.IsNotFound(err) {
scopedLog.WithError(err).Error("Failed to get Gateway")
}
continue
}
if !hasMatchingController(ctx, c, controllerName)(gw) {
scopedLog.Debug("Gateway does not have matching controller, skipping")
continue
}
scopedLog.WithFields(logrus.Fields{
logfields.K8sNamespace: ns,
logfields.Resource: parent.Name,
logfields.Route: object.GetName(),
}).Info("Enqueued gateway for Route")
reqs = append(reqs, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: ns,
Name: string(parent.Name),
},
})
}
return reqs
}
// enqueueRequestForOwningTLSCertificate returns an event handler for any changes with TLS secrets
func (r *gatewayReconciler) enqueueRequestForTLSSecret() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
gateways := getGatewaysForSecret(ctx, r.Client, a)
reqs := make([]reconcile.Request, 0, len(gateways))
for _, gw := range gateways {
reqs = append(reqs, reconcile.Request{
NamespacedName: gw,
})
}
return reqs
})
}
// enqueueRequestForAllowedNamespace returns an event handler for any changes
// with allowed namespaces
func (r *gatewayReconciler) enqueueRequestForAllowedNamespace() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, ns client.Object) []reconcile.Request {
gateways := getGatewaysForNamespace(ctx, r.Client, ns)
reqs := make([]reconcile.Request, 0, len(gateways))
for _, gw := range gateways {
reqs = append(reqs, reconcile.Request{
NamespacedName: gw,
})
}
return reqs
})
}
func (r *gatewayReconciler) usedInGateway(obj client.Object) bool {
return len(getGatewaysForSecret(context.Background(), r.Client, obj)) > 0
}