Skip to content

Commit d89f80e

Browse files
committed
Update route_controller
1 parent 1fe9ea4 commit d89f80e

File tree

3 files changed

+79
-52
lines changed

3 files changed

+79
-52
lines changed

pkg/controllers/route_controller.go

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,10 @@ func (r *routeReconciler) getRoute(ctx context.Context, req ctrl.Request) (core.
216216
}
217217

218218
func updateRouteListenerStatus(ctx context.Context, k8sClient client.Client, route core.Route) error {
219-
gw := &gwv1.Gateway{}
220-
221-
gwNamespace := route.Namespace()
222-
if route.Spec().ParentRefs()[0].Namespace != nil {
223-
gwNamespace = string(*route.Spec().ParentRefs()[0].Namespace)
224-
}
225-
gwName := types.NamespacedName{
226-
Namespace: gwNamespace,
227-
// TODO assume one parent for now and point to service network
228-
Name: string(route.Spec().ParentRefs()[0].Name),
229-
}
230-
231-
if err := k8sClient.Get(ctx, gwName, gw); err != nil {
232-
return fmt.Errorf("update route listener: gw not found, gw: %s, err: %w", gwName, err)
219+
gw, err := findFirstParentGw(ctx, k8sClient, route)
220+
if err != nil {
221+
return fmt.Errorf("failed to get gateway for route %s: %w", route.Name(), err)
233222
}
234-
235223
return UpdateGWListenerStatus(ctx, k8sClient, gw)
236224
}
237225

@@ -240,42 +228,37 @@ func (r *routeReconciler) isRouteRelevant(ctx context.Context, route core.Route)
240228
r.log.Infof(ctx, "Ignore Route which has no ParentRefs gateway %s ", route.Name())
241229
return false
242230
}
231+
// if route has gateway parentRef that is managed by lattice gateway controller,
232+
// then it is relevant
233+
gw, err := findFirstParentGw(ctx, r.client, route)
234+
return gw != nil && err == nil
235+
}
243236

237+
func findFirstParentGw(
238+
ctx context.Context,
239+
client client.Client,
240+
route core.Route,
241+
) (*gwv1.Gateway, error) {
244242
gw := &gwv1.Gateway{}
245-
246243
gwNamespace := route.Namespace()
247-
if route.Spec().ParentRefs()[0].Namespace != nil {
248-
gwNamespace = string(*route.Spec().ParentRefs()[0].Namespace)
249-
}
250-
gwName := types.NamespacedName{
251-
Namespace: gwNamespace,
252-
Name: string(route.Spec().ParentRefs()[0].Name),
253-
}
254-
255-
if err := r.client.Get(ctx, gwName, gw); err != nil {
256-
r.log.Infof(ctx, "Could not find gateway %s with err %s. Ignoring route %+v whose ParentRef gateway object"+
257-
" is not defined.", gwName.String(), err, route.Spec())
258-
return false
259-
}
260-
261-
// make sure gateway is an aws-vpc-lattice
262-
gwClass := &gwv1.GatewayClass{}
263-
gwClassName := types.NamespacedName{
264-
Name: string(gw.Spec.GatewayClassName),
265-
}
266-
267-
if err := r.client.Get(ctx, gwClassName, gwClass); err != nil {
268-
r.log.Infof(ctx, "Ignore Route not controlled by any GatewayClass %s, %s", route.Name(), route.Namespace())
269-
return false
270-
}
271-
272-
if gwClass.Spec.ControllerName == config.LatticeGatewayControllerName {
273-
r.log.Infof(ctx, "Found aws-vpc-lattice for Route for %s, %s", route.Name(), route.Namespace())
274-
return true
244+
fails := []string{}
245+
for _, parentRef := range route.Spec().ParentRefs() {
246+
if parentRef.Namespace != nil {
247+
gwNamespace = string(*parentRef.Namespace)
248+
}
249+
gwName := types.NamespacedName{
250+
Namespace: gwNamespace,
251+
Name: string(parentRef.Name),
252+
}
253+
if err := client.Get(ctx, gwName, gw); err != nil {
254+
continue
255+
}
256+
if k8s.IsManagedGateway(ctx, client, gw) {
257+
return gw, nil
258+
}
259+
fails = append(fails, gwName.String())
275260
}
276-
277-
r.log.Infof(ctx, "Ignore non aws-vpc-lattice Route %s, %s", route.Name(), route.Namespace())
278-
return false
261+
return nil, fmt.Errorf("failed to get gateway, name %s", fails)
279262
}
280263

281264
func (r *routeReconciler) buildAndDeployModel(
@@ -315,6 +298,19 @@ func (r *routeReconciler) buildAndDeployModel(
315298
return stack, err
316299
}
317300

301+
func (r *routeReconciler) findFirstParentRef(ctx context.Context, route core.Route) (gwv1.ParentReference, error) {
302+
gw, err := findFirstParentGw(ctx, r.client, route)
303+
if err != nil {
304+
return gwv1.ParentReference{}, err
305+
}
306+
for _, parentRef := range route.Spec().ParentRefs() {
307+
if string(parentRef.Name) == gw.Name {
308+
return parentRef, nil
309+
}
310+
}
311+
return gwv1.ParentReference{}, fmt.Errorf("parentRef not found for route %s", route.Name())
312+
}
313+
318314
func (r *routeReconciler) reconcileUpsert(ctx context.Context, req ctrl.Request, route core.Route) error {
319315
r.log.Infow(ctx, "reconcile, adding or updating", "name", req.Name)
320316
r.eventRecorder.Event(route.K8sObject(), corev1.EventTypeNormal,
@@ -336,9 +332,12 @@ func (r *routeReconciler) reconcileUpsert(ctx context.Context, req ctrl.Request,
336332

337333
if backendRefIPFamiliesErr != nil {
338334
httpRouteOld := route.DeepCopy()
335+
parentRef, err := r.findFirstParentRef(ctx, route)
336+
if err != nil {
337+
return err
338+
}
339339

340-
route.Status().UpdateParentRefs(route.Spec().ParentRefs()[0], config.LatticeGatewayControllerName)
341-
340+
route.Status().UpdateParentRefs(parentRef, config.LatticeGatewayControllerName)
342341
route.Status().UpdateRouteCondition(metav1.Condition{
343342
Type: string(gwv1.RouteConditionAccepted),
344343
Status: metav1.ConditionFalse,
@@ -357,7 +356,12 @@ func (r *routeReconciler) reconcileUpsert(ctx context.Context, req ctrl.Request,
357356
if _, err := r.buildAndDeployModel(ctx, route); err != nil {
358357
if services.IsConflictError(err) {
359358
// Stop reconciliation of this route if the route cannot be owned / has conflict
360-
route.Status().UpdateParentRefs(route.Spec().ParentRefs()[0], config.LatticeGatewayControllerName)
359+
parentRef, parentRefErr := r.findFirstParentRef(ctx, route)
360+
if parentRefErr != nil {
361+
// if parentRef not found, we cannot update route status
362+
return parentRefErr
363+
}
364+
route.Status().UpdateParentRefs(parentRef, config.LatticeGatewayControllerName)
361365
route.Status().UpdateRouteCondition(metav1.Condition{
362366
Type: string(gwv1.RouteConditionAccepted),
363367
Status: metav1.ConditionFalse,
@@ -553,7 +557,7 @@ func (r *routeReconciler) validateRouteParentRefs(ctx context.Context, route cor
553557

554558
parentStatus := gwv1.RouteParentStatus{
555559
ParentRef: parentRef,
556-
ControllerName: "application-networking.k8s.aws/gateway-api-controller",
560+
ControllerName: config.LatticeGatewayControllerName,
557561
Conditions: []metav1.Condition{},
558562
}
559563

pkg/controllers/route_controller_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ func TestRouteReconciler_ReconcileCreates(t *testing.T) {
7979
},
8080
},
8181
}
82+
83+
notVpcLattice := &gwv1.Gateway{
84+
ObjectMeta: metav1.ObjectMeta{
85+
Name: "not-vpc-lattice",
86+
Namespace: "ns1",
87+
},
88+
Spec: gwv1.GatewaySpec{
89+
GatewayClassName: "not-amazon-vpc-lattice",
90+
Listeners: []gwv1.Listener{
91+
{
92+
Name: "http",
93+
Protocol: "HTTP",
94+
Port: 80,
95+
},
96+
},
97+
},
98+
}
99+
100+
k8sClient.Create(ctx, notVpcLattice.DeepCopy())
82101
k8sClient.Create(ctx, gw.DeepCopy())
83102

84103
svc := &corev1.Service{
@@ -131,6 +150,10 @@ func TestRouteReconciler_ReconcileCreates(t *testing.T) {
131150
Spec: gwv1.HTTPRouteSpec{
132151
CommonRouteSpec: gwv1.CommonRouteSpec{
133152
ParentRefs: []gwv1.ParentReference{
153+
// if route has multiple parents, we'll only use the managed vpc lattice gateway
154+
{
155+
Name: "not-vpc-lattice",
156+
},
134157
{
135158
Name: "my-gateway",
136159
},

pkg/gateway/model_build_lattice_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (t *latticeServiceModelBuildTask) getACMCertArn(ctx context.Context) (strin
186186

187187
for _, parentRef := range t.route.Spec().ParentRefs() {
188188
if string(parentRef.Name) != gw.Name {
189-
t.log.Debugf(ctx, "Ignore ParentRef of different gateway %s-%s", parentRef.Name, *parentRef.Namespace)
189+
t.log.Debugf(ctx, "Ignore ParentRef of different gateway %s", parentRef.Name)
190190
continue
191191
}
192192

0 commit comments

Comments
 (0)