-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
tlsroute_reconcile.go
135 lines (113 loc) · 4.28 KB
/
tlsroute_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
// 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"
"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/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 *tlsRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
scopedLog := log.WithContext(ctx).WithFields(logrus.Fields{
logfields.Controller: "tlsRoute",
logfields.Resource: req.NamespacedName,
})
scopedLog.Info("Reconciling TLSRoute")
// Fetch the TLSRoute instance
original := &gatewayv1alpha2.TLSRoute{}
if err := r.Client.Get(ctx, req.NamespacedName, original); err != nil {
if k8serrors.IsNotFound(err) {
return success()
}
scopedLog.WithError(err).Error("Unable to fetch TLSRoute")
return fail(err)
}
// Ignore deleted TLSRoute, this can happen when foregroundDeletion is enabled
if original.GetDeletionTimestamp() != nil {
return success()
}
tr := original.DeepCopy()
defer func() {
if err := r.updateStatus(ctx, original, tr); err != nil {
scopedLog.WithError(err).Error("Failed to update TLSRoute 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))
}
// gateway validators
for _, parent := range tr.Spec.ParentRefs {
ns := helpers.NamespaceDerefOr(parent.Namespace, tr.GetNamespace())
gw := &gatewayv1beta1.Gateway{}
if err := r.Client.Get(ctx, client.ObjectKey{Namespace: ns, Name: string(parent.Name)}, gw); err != nil {
// pass the error to the status
mergeTLSRouteStatusConditions(tr, parent, []metav1.Condition{
tlsRouteAcceptedCondition(tr, false, err.Error()),
})
if !k8serrors.IsNotFound(err) {
// if it is not just a not found error, we should return the error as something is bad
return fail(err)
}
// Gateway does not exist skip further checks
continue
}
// run the actual validators
for _, fn := range []gatewayParentValidatonFunc{
checkGatewayAllowedForNamespace,
checkGatewayRouteKindAllowed,
checkMatchingGatewayPorts,
checkMatchingGatewayHostnames,
} {
if res, continueCheck, err := fn(ctx, scopedLog.WithField(logfields.Resource, tr), r.Client, parent, gw, tr); err != nil || !continueCheck {
return res, err
}
}
// Gateway is attachable, update the status for this HTTPRoute
mergeTLSRouteStatusConditions(tr, parent, []metav1.Condition{
tlsRouteAcceptedCondition(tr, true, tlsRouteAcceptedMessage),
})
}
// backend validators
// set status to okay, this wil be overwritten in checks if needed
for _, parent := range tr.Spec.ParentRefs {
mergeTLSRouteStatusConditions(tr, parent, []metav1.Condition{
tlsRouteResolvedRefsOkayCondition(tr, "Service reference is valid"),
})
}
for _, fn := range []backendValidationFunc{
checkAgainstCrossNamespaceReferences,
checkBackendIsService,
checkBackendIsExistingService,
} {
if res, continueCheck, err := fn(ctx, scopedLog.WithField(logfields.Resource, tr), r.Client, grants, tr); err != nil || !continueCheck {
return res, err
}
}
scopedLog.Info("Successfully reconciled TLSRoute")
return success()
}
func (r *tlsRouteReconciler) updateStatus(ctx context.Context, original *gatewayv1alpha2.TLSRoute, new *gatewayv1alpha2.TLSRoute) 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)
}