-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
gateway_reconcile.go
342 lines (299 loc) · 10.9 KB
/
gateway_reconcile.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package gateway_api
import (
"context"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"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"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
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/operator/pkg/model"
"github.com/cilium/cilium/operator/pkg/model/ingestion"
translation "github.com/cilium/cilium/operator/pkg/model/translation/gateway-api"
ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/logging/logfields"
)
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile
func (r *gatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
scopedLog := log.WithContext(ctx).WithFields(logrus.Fields{
logfields.Controller: gateway,
logfields.Resource: req.NamespacedName,
})
scopedLog.Info("Reconciling Gateway")
// Step 1: Retrieve the Gateway
original := &gatewayv1beta1.Gateway{}
if err := r.Client.Get(ctx, req.NamespacedName, original); err != nil {
if k8serrors.IsNotFound(err) {
return success()
}
scopedLog.WithError(err).Error("Unable to get Gateway")
return fail(err)
}
// Ignore deleting Gateway, this can happen when foregroundDeletion is enabled
// The reconciliation loop will automatically kick off for related Gateway resources.
if original.GetDeletionTimestamp() != nil {
scopedLog.Info("Gateway is being deleted, doing nothing")
return success()
}
gw := original.DeepCopy()
defer func() {
if err := r.updateStatus(ctx, original, gw); err != nil {
scopedLog.WithError(err).Error("Unable to update Gateway status")
}
}()
setGatewayScheduled(gw, true, "Gateway successfully scheduled")
// Step 2: Gather all required information for the ingestion model
gwc := &gatewayv1beta1.GatewayClass{}
if err := r.Client.Get(ctx, client.ObjectKey{Name: string(gw.Spec.GatewayClassName)}, gwc); err != nil {
scopedLog.WithField(gatewayClass, gw.Spec.GatewayClassName).
WithError(err).
Error("Unable to get GatewayClass")
if k8serrors.IsNotFound(err) {
setGatewayScheduled(gw, false, "GatewayClass does not exist")
return fail(err)
}
setGatewayScheduled(gw, false, "Unable to get GatewayClass")
return fail(err)
}
routeList := &gatewayv1beta1.HTTPRouteList{}
if err := r.Client.List(ctx, routeList); err != nil {
scopedLog.WithError(err).Error("Unable to list HTTPRoutes")
return fail(err)
}
// TODO(tam): Only list the services used by accepted HTTPRoutes
servicesList := &corev1.ServiceList{}
if err := r.Client.List(ctx, servicesList); err != nil {
scopedLog.WithError(err).Error("Unable to list Services")
return fail(err)
}
grants := &gatewayv1alpha2.ReferenceGrantList{}
if err := r.Client.List(ctx, grants); err != nil {
scopedLog.WithError(err).Error("Unable to list ReferenceGrants")
return fail(err)
}
routes := r.filterRoutesByGateway(ctx, gw, routeList.Items)
listeners := ingestion.GatewayAPI(ingestion.Input{
GatewayClass: *gwc,
Gateway: *gw,
HTTPRoutes: routes,
Services: servicesList.Items,
ReferenceGrants: grants.Items,
})
// Step 3: Translate the listeners into Cilium model
cec, svc, ep, err := translation.NewTranslator(r.SecretsNamespace).Translate(&model.Model{HTTP: listeners})
if err != nil {
scopedLog.WithError(err).Error("Unable to translate resources")
setGatewayScheduled(gw, false, "Unable to translate resources")
return fail(err)
}
if err = r.ensureService(ctx, svc); err != nil {
scopedLog.WithError(err).Error("Unable to create Service")
setGatewayScheduled(gw, false, "Unable to create Service resource")
return fail(err)
}
if err = r.ensureEndpoints(ctx, ep); err != nil {
scopedLog.WithError(err).Error("Unable to ensure Endpoints")
setGatewayScheduled(gw, false, "Unable to ensure Endpoints resource")
return fail(err)
}
if err = r.ensureEnvoyConfig(ctx, cec); err != nil {
scopedLog.WithError(err).Error("Unable to ensure CiliumEnvoyConfig")
setGatewayScheduled(gw, false, "Unable to ensure CEC resource")
return fail(err)
}
// Step 4: Update the status of the Gateway
if err = r.setAddressStatus(ctx, gw); err != nil {
scopedLog.WithError(err).Error("Address is not ready")
setGatewayReady(gw, false, "Address is not ready")
return fail(err)
}
if err = r.setListenerStatus(ctx, gw); err != nil {
scopedLog.WithError(err).Error("Unable to set listener status")
setGatewayReady(gw, false, "Unable to set listener status")
return fail(err)
}
setGatewayReady(gw, true, "Gateway successfully reconciled")
scopedLog.Info("Successfully reconciled Gateway")
return success()
}
func (r *gatewayReconciler) ensureService(ctx context.Context, desired *corev1.Service) error {
existing := &corev1.Service{}
err := r.Client.Get(ctx, client.ObjectKeyFromObject(desired), existing)
if err != nil {
if k8serrors.IsNotFound(err) {
return r.Client.Create(ctx, desired)
}
return err
}
temp := existing.DeepCopy()
temp.Spec = desired.Spec
setMergedLabelsAndAnnotations(temp, desired)
return r.Client.Patch(ctx, temp, client.MergeFrom(existing))
}
func (r *gatewayReconciler) ensureEndpoints(ctx context.Context, desired *corev1.Endpoints) error {
existing := &corev1.Endpoints{}
err := r.Client.Get(ctx, client.ObjectKeyFromObject(desired), existing)
if err != nil {
if k8serrors.IsNotFound(err) {
return r.Client.Create(ctx, desired)
}
return err
}
temp := existing.DeepCopy()
temp.Subsets = desired.Subsets
setMergedLabelsAndAnnotations(temp, desired)
return r.Client.Patch(ctx, temp, client.MergeFrom(existing))
}
func (r *gatewayReconciler) ensureEnvoyConfig(ctx context.Context, desired *ciliumv2.CiliumEnvoyConfig) error {
existing := &ciliumv2.CiliumEnvoyConfig{}
err := r.Client.Get(ctx, client.ObjectKeyFromObject(desired), existing)
if err != nil {
if k8serrors.IsNotFound(err) {
return r.Client.Create(ctx, desired)
}
return err
}
temp := existing.DeepCopy()
temp.Spec = desired.Spec
setMergedLabelsAndAnnotations(temp, desired)
return r.Client.Patch(ctx, temp, client.MergeFrom(existing))
}
func (r *gatewayReconciler) updateStatus(ctx context.Context, original *gatewayv1beta1.Gateway, new *gatewayv1beta1.Gateway) error {
oldStatus := original.Status.DeepCopy()
newStatus := new.Status.DeepCopy()
if cmp.Equal(oldStatus, newStatus, cmpopts.IgnoreFields(metav1.Condition{}, lastTransitionTime)) {
return nil
}
return r.Client.Status().Update(ctx, new)
}
func (r *gatewayReconciler) filterRoutesByGateway(ctx context.Context, gw *gatewayv1beta1.Gateway, routes []gatewayv1beta1.HTTPRoute) []gatewayv1beta1.HTTPRoute {
var filtered []gatewayv1beta1.HTTPRoute
for _, route := range routes {
if isAccepted(ctx, gw, route) && isAllowed(ctx, r.Client, gw, &route) && len(computeHosts(gw, &route)) > 0 {
filtered = append(filtered, route)
}
}
return filtered
}
func isAccepted(_ context.Context, gw *gatewayv1beta1.Gateway, route gatewayv1beta1.HTTPRoute) bool {
for _, rps := range route.Status.RouteStatus.Parents {
if namespaceDerefOr(rps.ParentRef.Namespace, route.GetNamespace()) != gw.GetNamespace() ||
string(rps.ParentRef.Name) != gw.GetName() {
continue
}
for _, cond := range rps.Conditions {
if cond.Type == conditionStatusAccepted && cond.Status == metav1.ConditionTrue {
return true
}
}
}
return false
}
func (r *gatewayReconciler) setAddressStatus(ctx context.Context, gw *gatewayv1beta1.Gateway) error {
svcList := &corev1.ServiceList{}
if err := r.Client.List(ctx, svcList, client.MatchingLabels{
owningGatewayLabel: gw.GetName(),
}); err != nil {
return err
}
if len(svcList.Items) == 0 {
return fmt.Errorf("no service found")
}
svc := svcList.Items[0]
if len(svc.Status.LoadBalancer.Ingress) == 0 {
return fmt.Errorf("load balancer status is not ready")
}
var addresses []gatewayv1beta1.GatewayAddress
for _, s := range svc.Status.LoadBalancer.Ingress {
if len(s.IP) != 0 {
addresses = append(addresses, gatewayv1beta1.GatewayAddress{
Type: GatewayAddressTypePtr(gatewayv1beta1.IPAddressType),
Value: s.IP,
})
}
if len(s.Hostname) != 0 {
addresses = append(addresses, gatewayv1beta1.GatewayAddress{
Type: GatewayAddressTypePtr(gatewayv1beta1.HostnameAddressType),
Value: s.Hostname,
})
}
}
gw.Status.Addresses = addresses
return nil
}
func (r *gatewayReconciler) setListenerStatus(ctx context.Context, gw *gatewayv1beta1.Gateway) error {
grants := &gatewayv1alpha2.ReferenceGrantList{}
if err := r.Client.List(ctx, grants); err != nil {
return fmt.Errorf("failed to retrieve reference grants: %w", err)
}
for _, l := range gw.Spec.Listeners {
cond := gatewayListenerReadyCondition(gw, true, "Listener Ready")
if l.TLS != nil {
for _, cert := range l.TLS.CertificateRefs {
if !IsSecret(cert) {
continue
}
if !helpers.IsSecretReferenceAllowed(gw.Namespace, cert, gatewayv1beta1.SchemeGroupVersion.WithKind("Gateway"), grants.Items) {
cond = metav1.Condition{
Type: string(gatewayv1beta1.ListenerConditionResolvedRefs),
Status: metav1.ConditionFalse,
Reason: string(gatewayv1beta1.ListenerReasonRefNotPermitted),
Message: "CertificateRef is not permitted",
LastTransitionTime: metav1.Now(),
}
break
}
secret := &corev1.Secret{}
if err := r.Client.Get(ctx, client.ObjectKey{
Namespace: namespaceDerefOr(cert.Namespace, gw.GetNamespace()),
Name: string(cert.Name),
}, secret); err != nil {
if k8serrors.IsNotFound(err) {
cond = metav1.Condition{
Type: string(gatewayv1beta1.ListenerConditionResolvedRefs),
Status: metav1.ConditionFalse,
Reason: string(gatewayv1beta1.ListenerReasonInvalidCertificateRef),
Message: err.Error(),
LastTransitionTime: metav1.Now(),
}
break
}
return err
}
}
}
found := false
for i := range gw.Status.Listeners {
if l.Name == gw.Status.Listeners[i].Name {
found = true
gw.Status.Listeners[i].Conditions = []metav1.Condition{cond}
break
}
}
if !found {
gw.Status.Listeners = append(gw.Status.Listeners, gatewayv1beta1.ListenerStatus{
Name: l.Name,
SupportedKinds: []gatewayv1beta1.RouteGroupKind{
{
Group: GroupPtr(gatewayv1beta1.GroupName),
Kind: getSupportedKind(l.Protocol),
},
},
Conditions: []metav1.Condition{cond},
})
}
}
return nil
}