-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
httproute_reconcile.go
132 lines (113 loc) · 3.95 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
// 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"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
"github.com/cilium/cilium/operator/pkg/gateway-api/routechecks"
"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")
}
}()
// check if this cert is allowed to be used by this gateway
grants := &gatewayv1beta1.ReferenceGrantList{}
if err := r.Client.List(ctx, grants); err != nil {
return fail(fmt.Errorf("failed to retrieve reference grants: %w", err))
}
// input for the validators
i := &routechecks.HTTPRouteInput{
Ctx: ctx,
Logger: scopedLog.WithField(logfields.Resource, hr),
Client: r.Client,
Grants: grants,
HTTPRoute: hr,
}
// gateway validators
for _, parent := range hr.Spec.ParentRefs {
// set acceptance to okay, this wil be overwritten in checks if needed
i.SetParentCondition(parent, metav1.Condition{
Type: conditionStatusAccepted,
Status: metav1.ConditionTrue,
Reason: conditionReasonAccepted,
Message: "Accepted HTTPRoute",
})
// set status to okay, this wil be overwritten in checks if needed
i.SetAllParentCondition(metav1.Condition{
Type: string(gatewayv1beta1.RouteConditionResolvedRefs),
Status: metav1.ConditionTrue,
Reason: string(gatewayv1beta1.RouteReasonResolvedRefs),
Message: "Service reference is valid",
})
// run the actual validators
for _, fn := range []routechecks.CheckGatewayFunc{
routechecks.CheckGatewayAllowedForNamespace,
routechecks.CheckGatewayRouteKindAllowed,
routechecks.CheckGatewayMatchingPorts,
routechecks.CheckGatewayMatchingHostnames,
routechecks.CheckGatewayMatchingSection,
} {
continueCheck, err := fn(i, parent)
if err != nil {
return ctrl.Result{}, err
}
if !continueCheck {
break
}
}
}
for _, fn := range []routechecks.CheckRuleFunc{
routechecks.CheckAgainstCrossNamespaceBackendReferences,
routechecks.CheckBackendIsService,
routechecks.CheckBackendIsExistingService,
} {
if continueCheck, err := fn(i); err != nil || !continueCheck {
return ctrl.Result{}, err
}
}
scopedLog.Info("Successfully reconciled HTTPRoute")
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)
}