Skip to content

Commit b0957fc

Browse files
u-kairlymbur
andauthored
Fix missing changes in PR#709 (#715)
* Fix missing changes in PR#709 * Fix controller name --------- Co-authored-by: Ryan Lymburner <[email protected]>
1 parent 104b8e0 commit b0957fc

File tree

6 files changed

+69
-144
lines changed

6 files changed

+69
-144
lines changed

pkg/controllers/eventhandlers/gateway.go

+12-37
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"time"
66

7+
"github.com/aws/aws-application-networking-k8s/pkg/k8s"
78
"github.com/aws/aws-application-networking-k8s/pkg/model/core"
89
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
910

@@ -17,8 +18,6 @@ import (
1718
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1819

1920
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
20-
21-
"github.com/aws/aws-application-networking-k8s/pkg/config"
2221
)
2322

2423
type enqueueRequestsForGatewayEvent struct {
@@ -74,48 +73,24 @@ func (h *enqueueRequestsForGatewayEvent) enqueueImpactedRoutes(ctx context.Conte
7473
}
7574

7675
for _, route := range routes {
77-
if len(route.Spec().ParentRefs()) <= 0 {
78-
h.log.Debugf(ctx, "Ignoring Route with no parentRef %s-%s", route.Name(), route.Namespace())
79-
continue
80-
}
81-
82-
// find the parent gw object
83-
var gwNamespace = route.Namespace()
84-
if route.Spec().ParentRefs()[0].Namespace != nil {
85-
gwNamespace = string(*route.Spec().ParentRefs()[0].Namespace)
86-
}
87-
88-
gwName := types.NamespacedName{
89-
Namespace: gwNamespace,
90-
Name: string(route.Spec().ParentRefs()[0].Name),
91-
}
92-
93-
gw := &gwv1.Gateway{}
94-
if err := h.client.Get(ctx, gwName, gw); err != nil {
95-
h.log.Debugf(ctx, "Ignoring Route with unknown parentRef %s-%s", route.Name(), route.Namespace())
96-
continue
97-
}
98-
99-
// find the parent gateway class name
100-
gwClass := &gwv1.GatewayClass{}
101-
gwClassName := types.NamespacedName{
102-
Namespace: "default",
103-
Name: string(gw.Spec.GatewayClassName),
104-
}
105-
106-
if err := h.client.Get(ctx, gwClassName, gwClass); err != nil {
107-
h.log.Debugf(ctx, "Ignoring Route with unknown Gateway %s-%s", route.Name(), route.Namespace())
108-
continue
109-
}
110-
111-
if gwClass.Spec.ControllerName == config.LatticeGatewayControllerName {
76+
parents, err := k8s.FindControlledParents(ctx, h.client, route)
77+
// If there is one or more parents, even if an error occurs,
78+
// it is not an error related to the parent controlled by the Lattice Controller, so enqueue the route
79+
if len(parents) > 0 {
80+
// parents are controlled by lattice gateway controller, so enqueue the route
11281
h.log.Debugf(ctx, "Adding Route %s-%s to queue due to Gateway event", route.Name(), route.Namespace())
11382
queue.Add(reconcile.Request{
11483
NamespacedName: types.NamespacedName{
11584
Namespace: route.Namespace(),
11685
Name: route.Name(),
11786
},
11887
})
88+
continue
89+
}
90+
if err != nil {
91+
h.log.Debugf(ctx, "Ignoring Route with unknown parentRef %s-%s", route.Name(), route.Namespace())
92+
continue
11993
}
94+
h.log.Debugf(ctx, "Ignoring Route %s-%s with no controlled parent", route.Name(), route.Namespace())
12095
}
12196
}

pkg/controllers/gateway_controller.go

+8-32
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
corev1 "k8s.io/api/core/v1"
3737
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3838
"k8s.io/apimachinery/pkg/runtime"
39-
"k8s.io/apimachinery/pkg/types"
4039
"k8s.io/client-go/tools/record"
4140
ctrl "sigs.k8s.io/controller-runtime"
4241
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -146,19 +145,8 @@ func (r *gatewayReconciler) reconcile(ctx context.Context, req ctrl.Request) err
146145
return client.IgnoreNotFound(err)
147146
}
148147

149-
gwClass := &gwv1.GatewayClass{}
150-
gwClassName := types.NamespacedName{
151-
Namespace: defaultNamespace,
152-
Name: string(gw.Spec.GatewayClassName),
153-
}
154-
155-
if err := r.client.Get(ctx, gwClassName, gwClass); err != nil {
156-
r.log.Infow(ctx, "GatewayClass is not found", "name", req.Name, "gwclass", gwClassName)
157-
return client.IgnoreNotFound(err)
158-
}
159-
160-
if gwClass.Spec.ControllerName != config.LatticeGatewayControllerName {
161-
r.log.Infow(ctx, "GatewayClass is not recognized", "name", req.Name, "gwClassControllerName", gwClass.Spec.ControllerName)
148+
if !k8s.IsControlledByLatticeGatewayController(ctx, r.client, gw) {
149+
r.log.Infow(ctx, "Gateway is not controlled by AWS Gateway API Controller", "name", req.Name)
162150
return nil
163151
}
164152

@@ -176,27 +164,15 @@ func (r *gatewayReconciler) reconcileDelete(ctx context.Context, gw *gwv1.Gatewa
176164
}
177165

178166
for _, route := range routes {
179-
if len(route.Spec().ParentRefs()) <= 0 {
180-
continue
181-
}
182-
gwNamespace := route.Namespace()
183-
if route.Spec().ParentRefs()[0].Namespace != nil {
184-
gwNamespace = string(*route.Spec().ParentRefs()[0].Namespace)
185-
}
186-
gwName := types.NamespacedName{
187-
Namespace: gwNamespace,
188-
Name: string(route.Spec().ParentRefs()[0].Name),
189-
}
190-
191-
httpGw := &gwv1.Gateway{}
192-
if err := r.client.Get(ctx, gwName, httpGw); err != nil {
193-
continue
194-
}
195-
196-
if httpGw.Name == gw.Name && httpGw.Namespace == gw.Namespace {
167+
parents, err := k8s.FindControlledParents(ctx, r.client, route)
168+
if len(parents) > 0 {
169+
gw := parents[0]
197170
return fmt.Errorf("cannot delete gateway %s/%s - found referencing route %s/%s",
198171
gw.Namespace, gw.Name, route.Namespace(), route.Name())
199172
}
173+
if err != nil {
174+
continue
175+
}
200176
}
201177

202178
err = r.finalizerManager.RemoveFinalizers(ctx, gw, gatewayFinalizer)

pkg/controllers/route_controller.go

+4-37
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ 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-
gws, err := findControlledParents(ctx, k8sClient, route)
219+
gws, err := k8s.FindControlledParents(ctx, k8sClient, route)
220220
if len(gws) <= 0 {
221221
return fmt.Errorf("failed to get gateway for route %s: %w", route.Name(), err)
222222
}
@@ -233,43 +233,10 @@ func (r *routeReconciler) isRouteRelevant(ctx context.Context, route core.Route)
233233
}
234234
// if route has gateway parentRef that is controlled by lattice gateway controller,
235235
// then it is relevant
236-
gws, _ := findControlledParents(ctx, r.client, route)
236+
gws, _ := k8s.FindControlledParents(ctx, r.client, route)
237237
return len(gws) > 0
238238
}
239239

240-
// findControlledParents returns parent gateways that are controlled by lattice gateway controller
241-
func findControlledParents(
242-
ctx context.Context,
243-
client client.Client,
244-
route core.Route,
245-
) ([]*gwv1.Gateway, error) {
246-
var result []*gwv1.Gateway
247-
gwNamespace := route.Namespace()
248-
misses := []string{}
249-
for _, parentRef := range route.Spec().ParentRefs() {
250-
gw := &gwv1.Gateway{}
251-
if parentRef.Namespace != nil {
252-
gwNamespace = string(*parentRef.Namespace)
253-
}
254-
gwName := types.NamespacedName{
255-
Namespace: gwNamespace,
256-
Name: string(parentRef.Name),
257-
}
258-
if err := client.Get(ctx, gwName, gw); err != nil {
259-
misses = append(misses, gwName.String())
260-
continue
261-
}
262-
if k8s.IsControlledByLatticeGatewayController(ctx, client, gw) {
263-
result = append(result, gw)
264-
}
265-
}
266-
var err error
267-
if len(misses) > 0 {
268-
err = fmt.Errorf("failed to get gateway, name %s", misses)
269-
}
270-
return result, err
271-
}
272-
273240
func (r *routeReconciler) buildAndDeployModel(
274241
ctx context.Context,
275242
route core.Route,
@@ -308,7 +275,7 @@ func (r *routeReconciler) buildAndDeployModel(
308275
}
309276

310277
func (r *routeReconciler) findControlledParentRef(ctx context.Context, route core.Route) (gwv1.ParentReference, error) {
311-
gws, err := findControlledParents(ctx, r.client, route)
278+
gws, err := k8s.FindControlledParents(ctx, r.client, route)
312279
if len(gws) <= 0 {
313280
return gwv1.ParentReference{}, fmt.Errorf("failed to get gateway for route %s: %w", route.Name(), err)
314281
}
@@ -528,7 +495,7 @@ func (r *routeReconciler) validateRouteParentRefs(ctx context.Context, route cor
528495
}
529496

530497
parentStatuses := []gwv1.RouteParentStatus{}
531-
gws, err := findControlledParents(ctx, r.client, route)
498+
gws, err := k8s.FindControlledParents(ctx, r.client, route)
532499
if len(gws) <= 0 {
533500
return nil, fmt.Errorf("failed to get gateway for route %s: %w", route.Name(), err)
534501
}

pkg/k8s/utils.go

+31
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package k8s
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/aws/aws-application-networking-k8s/pkg/config"
8+
"github.com/aws/aws-application-networking-k8s/pkg/model/core"
79
apierrors "k8s.io/apimachinery/pkg/api/errors"
810
"k8s.io/apimachinery/pkg/runtime/schema"
911
"k8s.io/apimachinery/pkg/types"
@@ -67,6 +69,35 @@ func IsControlledByLatticeGatewayController(ctx context.Context, c client.Client
6769
return gwClass.Spec.ControllerName == config.LatticeGatewayControllerName
6870
}
6971

72+
// FindControlledParents returns parent gateways that are controlled by lattice gateway controller
73+
func FindControlledParents(ctx context.Context, client client.Client, route core.Route) ([]*gwv1.Gateway, error) {
74+
var result []*gwv1.Gateway
75+
gwNamespace := route.Namespace()
76+
misses := []string{}
77+
for _, parentRef := range route.Spec().ParentRefs() {
78+
gw := &gwv1.Gateway{}
79+
if parentRef.Namespace != nil {
80+
gwNamespace = string(*parentRef.Namespace)
81+
}
82+
gwName := types.NamespacedName{
83+
Namespace: gwNamespace,
84+
Name: string(parentRef.Name),
85+
}
86+
if err := client.Get(ctx, gwName, gw); err != nil {
87+
misses = append(misses, gwName.String())
88+
continue
89+
}
90+
if IsControlledByLatticeGatewayController(ctx, client, gw) {
91+
result = append(result, gw)
92+
}
93+
}
94+
var err error
95+
if len(misses) > 0 {
96+
err = fmt.Errorf("failed to get gateways, %s", misses)
97+
}
98+
return result, err
99+
}
100+
70101
func ObjExists(ctx context.Context, c client.Client, key types.NamespacedName, obj client.Object) (bool, error) {
71102
err := c.Get(ctx, key, obj)
72103
if err != nil {

pkg/webhook/pod_mutator_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package webhook
22

33
import (
44
"context"
5+
"testing"
6+
57
anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
68
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
79
"github.com/stretchr/testify/assert"
@@ -11,7 +13,6 @@ import (
1113
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
1214
testclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
1315
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
14-
"testing"
1516
)
1617

1718
func Test_ReadinessGateInjection(t *testing.T) {
@@ -1079,8 +1080,7 @@ func Test_ReadinessGateInjection(t *testing.T) {
10791080

10801081
gwClass := &gwv1.GatewayClass{
10811082
ObjectMeta: metav1.ObjectMeta{
1082-
Name: "amazon-vpc-lattice",
1083-
Namespace: "default",
1083+
Name: "amazon-vpc-lattice",
10841084
},
10851085
Spec: gwv1.GatewayClassSpec{
10861086
ControllerName: "application-networking.k8s.aws/gateway-api-controller",

pkg/webhook/pod_readiness_gate_injector.go

+11-35
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package webhook
22

33
import (
44
"context"
5+
56
anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
6-
"github.com/aws/aws-application-networking-k8s/pkg/config"
77
k8sutils "github.com/aws/aws-application-networking-k8s/pkg/k8s"
88
"github.com/aws/aws-application-networking-k8s/pkg/model/core"
99
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
1010
"github.com/pkg/errors"
1111
corev1 "k8s.io/api/core/v1"
1212
"k8s.io/apimachinery/pkg/labels"
13-
"k8s.io/apimachinery/pkg/types"
1413
"sigs.k8s.io/controller-runtime/pkg/client"
1514
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
1615
)
@@ -178,41 +177,18 @@ func (m *PodReadinessGateInjector) routeHasLatticeGateway(ctx context.Context, r
178177
m.log.Debugf(ctx, "Route %s/%s has no parentRefs", route.Namespace(), route.Name())
179178
return false
180179
}
181-
182-
gw := &gwv1.Gateway{}
183-
gwNamespace := route.Namespace()
184-
if route.Spec().ParentRefs()[0].Namespace != nil {
185-
gwNamespace = string(*route.Spec().ParentRefs()[0].Namespace)
186-
}
187-
gwName := types.NamespacedName{
188-
Namespace: gwNamespace,
189-
Name: string(route.Spec().ParentRefs()[0].Name),
190-
}
191-
192-
if err := m.k8sClient.Get(ctx, gwName, gw); err != nil {
193-
m.log.Debugf(ctx, "Unable to retrieve gateway %s/%s for route %s/%s, %s",
194-
gwName.Namespace, gwName.Name, route.Namespace(), route.Name(), err)
195-
return false
196-
}
197-
198-
// make sure gateway is an aws-vpc-lattice
199-
gwClass := &gwv1.GatewayClass{}
200-
gwClassName := types.NamespacedName{
201-
Namespace: "default",
202-
Name: string(gw.Spec.GatewayClassName),
180+
parents, err := k8sutils.FindControlledParents(ctx, m.k8sClient, route)
181+
// If there is at least one parent element and an error exists,
182+
// it is not an error related to the parent controlled by the AWS Gateway API Controller, so return true
183+
if len(parents) > 0 {
184+
gw := parents[0]
185+
m.log.Debugf(ctx, "Gateway %s/%s is a AWS Gateway API Controller", gw.Namespace, gw.Name)
186+
return true
203187
}
204-
205-
if err := m.k8sClient.Get(ctx, gwClassName, gwClass); err != nil {
206-
m.log.Debugf(ctx, "Unable to retrieve gateway class %s/%s for gateway %s/%s, %s",
207-
gwClassName.Namespace, gwClass.Name, gwName.Namespace, gwName.Name, err)
188+
if err != nil {
189+
m.log.Debugf(ctx, "Unable to retrieve controlled parents for route %s/%s, %s", route.Namespace(), route.Name(), err)
208190
return false
209191
}
210-
211-
if gwClass.Spec.ControllerName == config.LatticeGatewayControllerName {
212-
m.log.Debugf(ctx, "Gateway %s/%s is a lattice gateway", gwName.Namespace, gwName.Name)
213-
return true
214-
}
215-
216-
m.log.Debugf(ctx, "Gateway %s/%s is not a lattice gateway", gwName.Namespace, gwName.Name)
192+
m.log.Debugf(ctx, "Route %s/%s has no controlled AWS Gateway API Controller", route.Namespace(), route.Name())
217193
return false
218194
}

0 commit comments

Comments
 (0)