Skip to content

Commit 1fe9ea4

Browse files
committed
Update validate gateway logic and tests
1 parent 21ba0af commit 1fe9ea4

File tree

6 files changed

+208
-147
lines changed

6 files changed

+208
-147
lines changed

pkg/gateway/model_build_lattice_service.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,11 @@ func (t *latticeServiceModelBuildTask) buildLatticeService(ctx context.Context)
134134
t.log.Infof(ctx, "Ignoring route %s because failed to get gateway %s: %v", t.route.Name(), gw.Spec.GatewayClassName, err)
135135
continue
136136
}
137-
gwClass := &gwv1.GatewayClass{}
138-
// GatewayClass is cluster-scoped resource, so we don't need to specify namespace
139-
err = t.client.Get(ctx, client.ObjectKey{Name: string(gw.Spec.GatewayClassName)}, gwClass)
140-
if err != nil {
141-
t.log.Infof(ctx, "Ignoring route %s because failed to get gateway class %s: %v", t.route.Name(), gw.Spec.GatewayClassName, err)
142-
continue
143-
}
144-
if gwClass.Spec.ControllerName != config.LatticeGatewayControllerName {
145-
t.log.Infof(ctx, "Ignoring route %s because gateway class %s is not for a VPCLattice", t.route.Name(), gw.Spec.GatewayClassName)
146-
continue
137+
if k8s.IsManagedGateway(ctx, t.client, gw) {
138+
spec.ServiceNetworkNames = append(spec.ServiceNetworkNames, string(parentRef.Name))
139+
} else {
140+
t.log.Infof(ctx, "Ignoring route %s because gateway %s is not managed by lattice gateway controller", t.route.Name(), gw.Name)
147141
}
148-
spec.ServiceNetworkNames = append(spec.ServiceNetworkNames, string(parentRef.Name))
149142
}
150143
if config.ServiceNetworkOverrideMode {
151144
spec.ServiceNetworkNames = []string{config.DefaultServiceNetwork}
@@ -181,7 +174,9 @@ func (t *latticeServiceModelBuildTask) buildLatticeService(ctx context.Context)
181174

182175
// returns empty string if not found
183176
func (t *latticeServiceModelBuildTask) getACMCertArn(ctx context.Context) (string, error) {
184-
gw, err := t.getGateway(ctx)
177+
// when a service is associate to multiple service network(s), all listener config MUST be same
178+
// so here we are only using the 1st gateway
179+
gw, err := t.getFirstGateway(ctx)
185180
if err != nil {
186181
if apierrors.IsNotFound(err) && !t.route.DeletionTimestamp().IsZero() {
187182
return "", nil // ok if we're deleting the route
@@ -190,9 +185,7 @@ func (t *latticeServiceModelBuildTask) getACMCertArn(ctx context.Context) (strin
190185
}
191186

192187
for _, parentRef := range t.route.Spec().ParentRefs() {
193-
if parentRef.Name != t.route.Spec().ParentRefs()[0].Name {
194-
// when a service is associate to multiple service network(s), all listener config MUST be same
195-
// so here we are only using the 1st gateway
188+
if string(parentRef.Name) != gw.Name {
196189
t.log.Debugf(ctx, "Ignore ParentRef of different gateway %s-%s", parentRef.Name, *parentRef.Namespace)
197190
continue
198191
}

pkg/gateway/model_build_lattice_service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ func Test_LatticeServiceModelBuild(t *testing.T) {
465465
gwv1.Install(k8sSchema)
466466
k8sClient := testclient.NewClientBuilder().WithScheme(k8sSchema).Build()
467467

468-
assert.NoError(t, k8sClient.Create(ctx, &tt.gwClass))
468+
assert.NoError(t, k8sClient.Create(ctx, tt.gwClass.DeepCopy()))
469469
for _, gw := range tt.gws {
470470
assert.NoError(t, k8sClient.Create(ctx, gw.DeepCopy()))
471471
}

pkg/gateway/model_build_listener.go

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77

88
"github.com/aws/aws-sdk-go/aws"
99
"github.com/aws/aws-sdk-go/service/vpclattice"
10-
"k8s.io/apimachinery/pkg/types"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
1111
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
1212

13+
"github.com/aws/aws-application-networking-k8s/pkg/k8s"
1314
model "github.com/aws/aws-application-networking-k8s/pkg/model/lattice"
1415
)
1516

@@ -20,16 +21,13 @@ const (
2021
func (t *latticeServiceModelBuildTask) extractListenerInfo(
2122
ctx context.Context,
2223
parentRef gwv1.ParentReference,
24+
gw *gwv1.Gateway,
2325
) (int64, string, error) {
2426
if parentRef.SectionName != nil {
2527
t.log.Debugf(ctx, "Listener parentRef SectionName is %s", *parentRef.SectionName)
2628
}
2729

2830
t.log.Debugf(ctx, "Building Listener for Route %s-%s", t.route.Name(), t.route.Namespace())
29-
gw, err := t.getGateway(ctx)
30-
if err != nil {
31-
return 0, "", err
32-
}
3331
// If no SectionName is specified, use the first listener port
3432
if parentRef.SectionName == nil {
3533
if len(gw.Spec.Listeners) == 0 {
@@ -58,42 +56,54 @@ func isTLSPassthroughGatewayListener(listener *gwv1.Listener) bool {
5856
return listener.Protocol == gwv1.TLSProtocolType && listener.TLS != nil && listener.TLS.Mode != nil && *listener.TLS.Mode == gwv1.TLSModePassthrough
5957
}
6058

61-
func (t *latticeServiceModelBuildTask) getGateway(ctx context.Context) (*gwv1.Gateway, error) {
62-
var gwNamespace = t.route.Namespace()
63-
if t.route.Spec().ParentRefs()[0].Namespace != nil {
64-
gwNamespace = string(*t.route.Spec().ParentRefs()[0].Namespace)
65-
}
66-
59+
func (t *latticeServiceModelBuildTask) getFirstGateway(ctx context.Context) (*gwv1.Gateway, error) {
6760
gw := &gwv1.Gateway{}
68-
gwName := types.NamespacedName{
69-
Namespace: gwNamespace,
70-
Name: string(t.route.Spec().ParentRefs()[0].Name),
71-
}
61+
gwNamespace := t.route.Namespace()
62+
fails := []string{}
63+
for _, parentRef := range t.route.Spec().ParentRefs() {
64+
if parentRef.Namespace != nil {
65+
gwNamespace = string(*parentRef.Namespace)
66+
}
67+
gwName := client.ObjectKey{
68+
Namespace: gwNamespace,
69+
Name: string(parentRef.Name),
70+
}
71+
if err := t.client.Get(ctx, gwName, gw); err != nil {
72+
t.log.Infof(ctx, "Ignoring route %s because failed to get gateway %s: %v", t.route.Name(), parentRef.Name, err)
73+
continue
74+
}
75+
if k8s.IsManagedGateway(ctx, t.client, gw) {
76+
return gw, nil
77+
}
78+
fails = append(fails, gwName.String())
7279

73-
if err := t.client.Get(ctx, gwName, gw); err != nil {
74-
return nil, fmt.Errorf("failed to get gateway, name %s, err %w", gwName, err)
7580
}
76-
return gw, nil
81+
return nil, fmt.Errorf("failed to get gateway, name %s", fails)
7782
}
7883

7984
func (t *latticeServiceModelBuildTask) buildListeners(ctx context.Context, stackSvcId string) error {
80-
if len(t.route.Spec().ParentRefs()) == 0 {
81-
t.log.Debugf(ctx, "No ParentRefs on route %s-%s, nothing to do", t.route.Name(), t.route.Namespace())
82-
}
8385
if !t.route.DeletionTimestamp().IsZero() {
8486
t.log.Debugf(ctx, "Route %s-%s is deleted, skipping listener build", t.route.Name(), t.route.Namespace())
8587
return nil
8688
}
89+
if len(t.route.Spec().ParentRefs()) == 0 {
90+
t.log.Debugf(ctx, "No ParentRefs on route %s-%s, nothing to do", t.route.Name(), t.route.Namespace())
91+
return nil
92+
}
93+
94+
// when a service is associate to multiple service network(s), all listener config MUST be same
95+
// so here we are only using the 1st gateway
96+
gw, err := t.getFirstGateway(ctx)
97+
if err != nil {
98+
return err
99+
}
87100

88101
for _, parentRef := range t.route.Spec().ParentRefs() {
89-
if parentRef.Name != t.route.Spec().ParentRefs()[0].Name {
90-
// when a service is associate to multiple service network(s), all listener config MUST be same
91-
// so here we are only using the 1st gateway
92-
t.log.Debugf(ctx, "Ignore parentref of different gateway %s-%s", parentRef.Name, *parentRef.Namespace)
102+
if string(parentRef.Name) != gw.Name {
93103
continue
94104
}
95105

96-
port, protocol, err := t.extractListenerInfo(ctx, parentRef)
106+
port, protocol, err := t.extractListenerInfo(ctx, parentRef, gw)
97107
if err != nil {
98108
return err
99109
}

0 commit comments

Comments
 (0)