-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgateway_controller.go
More file actions
300 lines (270 loc) · 9.06 KB
/
gateway_controller.go
File metadata and controls
300 lines (270 loc) · 9.06 KB
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
package controller
import (
"context"
"fmt"
"reflect"
"github.com/api7/api7-ingress-controller/api/v1alpha1"
"github.com/api7/api7-ingress-controller/internal/controller/config"
"github.com/api7/api7-ingress-controller/internal/controller/indexer"
"github.com/api7/api7-ingress-controller/internal/provider"
"github.com/api7/gopkg/pkg/log"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)
// GatewayReconciler reconciles a Gateway object.
type GatewayReconciler struct { //nolint:revive
client.Client
Scheme *runtime.Scheme
Log logr.Logger
Provider provider.Provider
}
// SetupWithManager sets up the controller with the Manager.
func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(
&gatewayv1.Gateway{},
builder.WithPredicates(
predicate.NewPredicateFuncs(r.checkGatewayClass),
),
).
WithEventFilter(predicate.GenerationChangedPredicate{}).
Watches(
&gatewayv1.GatewayClass{},
handler.EnqueueRequestsFromMapFunc(r.listGatewayForGatewayClass),
builder.WithPredicates(
predicate.NewPredicateFuncs(r.matchesGatewayClass),
),
).
Watches(
&gatewayv1.HTTPRoute{},
handler.EnqueueRequestsFromMapFunc(r.listGatewaysForHTTPRoute),
).
Watches(
&v1alpha1.GatewayProxy{},
handler.EnqueueRequestsFromMapFunc(r.listGatewaysForGatewayProxy),
).
Complete(r)
}
func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
gateway := new(gatewayv1.Gateway)
if err := r.Get(ctx, req.NamespacedName, gateway); err != nil {
if client.IgnoreNotFound(err) == nil {
gateway.Namespace = req.Namespace
gateway.Name = req.Name
gateway.TypeMeta = metav1.TypeMeta{
Kind: KindGateway,
APIVersion: gatewayv1.GroupVersion.String(),
}
if err := r.Provider.Delete(ctx, gateway); err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, err
}
conditionProgrammedStatus, conditionProgrammedMsg := true, "Programmed"
cfg := config.GetFirstGatewayConfig()
var addrs []gatewayv1.GatewayStatusAddress
if len(gateway.Status.Addresses) != len(cfg.Addresses) {
for _, addr := range cfg.Addresses {
if addr == "" {
continue
}
addrs = append(addrs,
gatewayv1.GatewayStatusAddress{
Value: addr,
},
)
}
}
r.Log.Info("gateway has been accepted", "gateway", gateway.GetName())
type status struct {
status bool
msg string
}
acceptStatus := status{
status: true,
msg: acceptedMessage("gateway"),
}
tctx := &provider.TranslateContext{
Secrets: make(map[types.NamespacedName]*corev1.Secret),
}
r.processListenerConfig(tctx, gateway)
if err := r.processInfrastructure(tctx, gateway); err != nil {
acceptStatus = status{
status: false,
msg: err.Error(),
}
}
if err := r.Provider.Update(ctx, tctx, gateway); err != nil {
acceptStatus = status{
status: false,
msg: err.Error(),
}
}
ListenerStatuses, err := getListenerStatus(ctx, r.Client, gateway)
if err != nil {
log.Error(err, "failed to get listener status", "gateway", gateway.GetName())
return ctrl.Result{}, err
}
accepted := SetGatewayConditionAccepted(gateway, acceptStatus.status, acceptStatus.msg)
Programmed := SetGatewayConditionProgrammed(gateway, conditionProgrammedStatus, conditionProgrammedMsg)
if accepted || Programmed || len(addrs) > 0 || len(ListenerStatuses) > 0 {
if len(addrs) > 0 {
gateway.Status.Addresses = addrs
}
if len(ListenerStatuses) > 0 {
gateway.Status.Listeners = ListenerStatuses
}
return ctrl.Result{}, r.Status().Update(ctx, gateway)
}
return ctrl.Result{}, nil
}
func (r *GatewayReconciler) matchesGatewayClass(obj client.Object) bool {
gateway, ok := obj.(*gatewayv1.GatewayClass)
if !ok {
r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to Gateway")
return false
}
return matchesController(string(gateway.Spec.ControllerName))
}
/*
func (r *GatewayReconciler) matchesGatewayForControlPlaneConfig(obj client.Object) bool {
gateway, ok := obj.(*gatewayv1.Gateway)
if !ok {
r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to Gateway")
return false
}
cfg := config.GetControlPlaneConfigByGatewatName(gateway.GetName())
ok = true
if cfg == nil {
ok = false
}
return ok
}
*/
func (r *GatewayReconciler) listGatewayForGatewayClass(ctx context.Context, gatewayClass client.Object) []reconcile.Request {
gatewayList := &gatewayv1.GatewayList{}
if err := r.List(context.Background(), gatewayList); err != nil {
r.Log.Error(err, "failed to list gateways for gateway class",
"gatewayclass", gatewayClass.GetName(),
)
return nil
}
/*
gateways := []gatewayv1.Gateway{}
for _, gateway := range gatewayList.Items {
if cp := config.GetControlPlaneConfigByGatewatName(gateway.GetName()); cp != nil {
gateways = append(gateways, gateway)
}
}
*/
return reconcileGatewaysMatchGatewayClass(gatewayClass, gatewayList.Items)
}
func (r *GatewayReconciler) checkGatewayClass(obj client.Object) bool {
gateway := obj.(*gatewayv1.Gateway)
gatewayClass := &gatewayv1.GatewayClass{}
if err := r.Client.Get(context.Background(), client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil {
r.Log.Error(err, "failed to get gateway class", "gateway", gateway.GetName(), "gatewayclass", gateway.Spec.GatewayClassName)
return false
}
return matchesController(string(gatewayClass.Spec.ControllerName))
}
func (r *GatewayReconciler) listGatewaysForGatewayProxy(ctx context.Context, obj client.Object) []reconcile.Request {
gatewayProxy, ok := obj.(*v1alpha1.GatewayProxy)
if !ok {
r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to GatewayProxy")
return nil
}
namespace := gatewayProxy.GetNamespace()
name := gatewayProxy.GetName()
gatewayList := &gatewayv1.GatewayList{}
if err := r.List(ctx, gatewayList, client.MatchingFields{
indexer.ParametersRef: indexer.GenIndexKey(namespace, name),
}); err != nil {
r.Log.Error(err, "failed to list gateways for gateway proxy", "gatewayproxy", gatewayProxy.GetName())
return nil
}
recs := make([]reconcile.Request, 0, len(gatewayList.Items))
for _, gateway := range gatewayList.Items {
recs = append(recs, reconcile.Request{
NamespacedName: client.ObjectKey{
Namespace: gateway.GetNamespace(),
Name: gateway.GetName(),
},
})
}
return recs
}
func (r *GatewayReconciler) listGatewaysForHTTPRoute(_ context.Context, obj client.Object) []reconcile.Request {
httpRoute, ok := obj.(*gatewayv1.HTTPRoute)
if !ok {
r.Log.Error(
fmt.Errorf("unexpected object type"),
"HTTPRoute watch predicate received unexpected object type",
"expected", "*gatewayapi.HTTPRoute", "found", reflect.TypeOf(obj),
)
return nil
}
recs := []reconcile.Request{}
for _, routeParentStatus := range httpRoute.Status.Parents {
gatewayNamespace := httpRoute.GetNamespace()
parentRef := routeParentStatus.ParentRef
if parentRef.Group != nil && *parentRef.Group != gatewayv1.GroupName {
continue
}
if parentRef.Kind != nil && *parentRef.Kind != "Gateway" {
continue
}
if parentRef.Namespace != nil {
gatewayNamespace = string(*parentRef.Namespace)
}
recs = append(recs, reconcile.Request{
NamespacedName: client.ObjectKey{
Namespace: gatewayNamespace,
Name: string(parentRef.Name),
},
})
}
return recs
}
func (r *GatewayReconciler) processInfrastructure(tctx *provider.TranslateContext, gateway *gatewayv1.Gateway) error {
return ProcessGatewayProxy(r.Client, tctx, gateway)
}
func (r *GatewayReconciler) processListenerConfig(tctx *provider.TranslateContext, gateway *gatewayv1.Gateway) {
listeners := gateway.Spec.Listeners
for _, listener := range listeners {
if listener.TLS == nil || listener.TLS.CertificateRefs == nil {
continue
}
secret := corev1.Secret{}
for _, ref := range listener.TLS.CertificateRefs {
ns := gateway.GetNamespace()
if ref.Namespace != nil {
ns = string(*ref.Namespace)
}
if ref.Kind != nil && *ref.Kind == gatewayv1.Kind("Secret") {
if err := r.Get(context.Background(), client.ObjectKey{
Namespace: ns,
Name: string(ref.Name),
}, &secret); err != nil {
log.Error(err, "failed to get secret", "namespace", ns, "name", string(ref.Name))
SetGatewayListenerConditionProgrammed(gateway, string(listener.Name), false, err.Error())
SetGatewayListenerConditionResolvedRefs(gateway, string(listener.Name), false, err.Error())
break
}
log.Info("Setting secret for listener", "listener", listener.Name, "secret", secret.Name, " namespace", ns)
tctx.Secrets[types.NamespacedName{Namespace: ns, Name: string(ref.Name)}] = &secret
}
}
}
}