-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
httproute_reconcile.go
175 lines (153 loc) · 5.65 KB
/
httproute_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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package gateway_api
import (
"context"
"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"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
"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 *httpRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
scopedLog := log.WithContext(ctx).WithFields(logrus.Fields{
logfields.Controller: "httpRoute",
logfields.Resource: req.NamespacedName,
})
scopedLog.Info("Reconciling HTTPRoute")
// Fetch the HTTPRoute instance
original := &gatewayv1beta1.HTTPRoute{}
if err := r.Client.Get(ctx, req.NamespacedName, original); err != nil {
if k8serrors.IsNotFound(err) {
return success()
}
scopedLog.WithError(err).Error("Unable to fetch HTTPRoute")
return fail(err)
}
// Ignore deleted HTTPRoute, this can happen when foregroundDeletion is enabled
if original.GetDeletionTimestamp() != nil {
return success()
}
hr := original.DeepCopy()
defer func() {
if err := r.updateStatus(ctx, original, hr); err != nil {
scopedLog.WithError(err).Error("Failed to update HTTPRoute status")
}
}()
for _, fn := range []httpRouteChecker{
validateService,
validateGateway,
} {
if res, err := fn(ctx, r.Client, hr); err != nil {
return res, err
}
}
scopedLog.Info("Successfully reconciled HTTPRoute")
return success()
}
func validateService(ctx context.Context, c client.Client, hr *gatewayv1beta1.HTTPRoute) (ctrl.Result, error) {
scopedLog := log.WithContext(ctx).WithFields(logrus.Fields{
logfields.Controller: "httpRoute",
logfields.Resource: client.ObjectKeyFromObject(hr),
})
for _, rule := range hr.Spec.Rules {
for _, be := range rule.BackendRefs {
ns := namespaceDerefOr(be.Namespace, hr.GetNamespace())
if ns != hr.GetNamespace() {
for _, parent := range hr.Spec.ParentRefs {
mergeHTTPRouteStatusConditions(hr, parent, []metav1.Condition{
httpRefNotPermittedRouteCondition(hr, "Cross namespace references are not allowed"),
})
}
continue
}
if !IsService(be.BackendObjectReference) {
for _, parent := range hr.Spec.ParentRefs {
mergeHTTPRouteStatusConditions(hr, parent, []metav1.Condition{
httpInvalidKindRouteCondition(hr, string("Unsupported backend kind "+*be.Kind)),
})
}
continue
}
svc := &corev1.Service{}
if err := c.Get(ctx, client.ObjectKey{Namespace: ns, Name: string(be.Name)}, svc); err != nil {
if !k8serrors.IsNotFound(err) {
scopedLog.WithError(err).Error("Failed to get Service")
return fail(err)
}
// Service does not exist, update the status for all the parents
// The `Accepted` condition on a route only describes whether
// the route attached successfully to its parent, so no error
// is returned here, so that the next validation can be run.
for _, parent := range hr.Spec.ParentRefs {
mergeHTTPRouteStatusConditions(hr, parent, []metav1.Condition{
httpBackendNotFoundRouteCondition(hr, err.Error()),
})
}
}
}
}
return success()
}
func validateGateway(ctx context.Context, c client.Client, hr *gatewayv1beta1.HTTPRoute) (ctrl.Result, error) {
scopedLog := log.WithContext(ctx).WithFields(logrus.Fields{
logfields.Controller: "httpRoute",
logfields.Resource: client.ObjectKeyFromObject(hr),
})
for _, parent := range hr.Spec.ParentRefs {
ns := namespaceDerefOr(parent.Namespace, hr.GetNamespace())
gw := &gatewayv1beta1.Gateway{}
if err := c.Get(ctx, client.ObjectKey{Namespace: ns, Name: string(parent.Name)}, gw); err != nil {
if !k8serrors.IsNotFound(err) {
mergeHTTPRouteStatusConditions(hr, parent, []metav1.Condition{
httpRouteAcceptedCondition(hr, false, err.Error()),
})
return fail(err)
}
// Gateway does not exist, update the status for this HTTPRoute
mergeHTTPRouteStatusConditions(hr, parent, []metav1.Condition{
httpRouteAcceptedCondition(hr, false, err.Error()),
})
continue
}
if !isAllowed(ctx, c, gw, hr) {
// Gateway is not attachable, update the status for this HTTPRoute
mergeHTTPRouteStatusConditions(hr, parent, []metav1.Condition{
httpRouteAcceptedCondition(hr, false, "HTTPRoute is not allowed"),
})
continue
}
if len(computeHosts(gw, hr)) == 0 {
// No matching host, update the status for this HTTPRoute
mergeHTTPRouteStatusConditions(hr, parent, []metav1.Condition{
httpNoMatchingListenerHostnameRouteCondition(hr, "No matching listener hostname"),
})
continue
}
scopedLog.Debug("HTTPRoute is attachable")
// Gateway is attachable, update the status for this HTTPRoute
mergeHTTPRouteStatusConditions(hr, parent, []metav1.Condition{
httpRouteAcceptedCondition(hr, true, httpRouteAcceptedMessage),
})
}
return success()
}
func (r *httpRouteReconciler) updateStatus(ctx context.Context, original *gatewayv1beta1.HTTPRoute, new *gatewayv1beta1.HTTPRoute) error {
oldStatus := original.Status.DeepCopy()
newStatus := new.Status.DeepCopy()
opts := cmpopts.IgnoreFields(metav1.Condition{}, "LastTransitionTime")
if cmp.Equal(oldStatus, newStatus, opts) {
return nil
}
return r.Client.Status().Update(ctx, new)
}