-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
gateway.go
191 lines (169 loc) · 6.13 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
// 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"
"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"
"sigs.k8s.io/controller-runtime/pkg/source"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
"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
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(&source.Kind{Type: &gatewayv1beta1.GatewayClass{}},
r.enqueueRequestForOwningGatewayClass(),
builder.WithPredicates(predicate.NewPredicateFuncs(hasMatchingControllerFn))).
// Watch related LB service for status
Watches(&source.Kind{Type: &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(&source.Kind{Type: &gatewayv1beta1.HTTPRoute{}},
r.enqueueRequestForOwningHTTPRoute(),
builder.WithPredicates(onlyStatusChanged())).
// Watch related secrets used to configure TLS
Watches(&source.Kind{Type: &corev1.Secret{}}, r.enqueueRequestForTLSSecret(),
builder.WithPredicates(predicate.NewPredicateFuncs(r.usedInGateway))).
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(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(context.Background(), 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(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(a client.Object) []reconcile.Request {
scopedLog := log.WithFields(logrus.Fields{
logfields.Controller: gateway,
logfields.Resource: a.GetName(),
})
var reqs []reconcile.Request
hr, ok := a.(*gatewayv1beta1.HTTPRoute)
if !ok {
return nil
}
for _, parent := range hr.Spec.ParentRefs {
if !IsGateway(parent) {
continue
}
ns := namespaceDerefOr(parent.Namespace, hr.GetNamespace())
scopedLog.WithFields(logrus.Fields{
logfields.K8sNamespace: ns,
logfields.Resource: parent.Name,
httpRoute: hr.GetName(),
}).Info("Enqueued gateway for HTTPRoute")
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(a client.Object) []reconcile.Request {
gateways := getGatewaysForSecret(context.Background(), r.Client, a)
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
}