Skip to content

Commit c4a6cae

Browse files
committed
Added validation to check if parentRef is managed by vpcLatticeController.
1 parent eb0724e commit c4a6cae

File tree

2 files changed

+87
-57
lines changed

2 files changed

+87
-57
lines changed

pkg/gateway/model_build_lattice_service.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,29 @@ func (t *latticeServiceModelBuildTask) buildLatticeService(ctx context.Context)
124124
}
125125

126126
for _, parentRef := range t.route.Spec().ParentRefs() {
127+
gw := &gwv1.Gateway{}
128+
parentNamespace := t.route.Namespace()
129+
if parentRef.Namespace != nil {
130+
parentNamespace = string(*parentRef.Namespace)
131+
}
132+
err := t.client.Get(ctx, client.ObjectKey{Name: string(parentRef.Name), Namespace: parentNamespace}, gw)
133+
if err != nil {
134+
//TODO: error message
135+
t.log.Infof(ctx, "Ignore %s route because failed to get gateway %s: %v", t.route.Name(), parentRef.Name, err)
136+
continue
137+
}
138+
gwClass := &gwv1.GatewayClass{}
139+
// GatewayClass is cluster-scoped resource, so we don't need to specify namespace
140+
err = t.client.Get(ctx, client.ObjectKey{Name: string(gw.Spec.GatewayClassName)}, gwClass)
141+
if err != nil {
142+
//TODO: error message
143+
t.log.Infof(ctx, "Ignore %s route because failed to get gateway class %s: %v", t.route.Name(), gw.Spec.GatewayClassName, err)
144+
continue
145+
}
146+
if gwClass.Spec.ControllerName != config.LatticeGatewayControllerName {
147+
t.log.Infof(ctx, "Ignore %s route because gateway class %s is not for lattice gateway", t.route.Name(), gw.Spec.GatewayClassName)
148+
continue
149+
}
127150
spec.ServiceNetworkNames = append(spec.ServiceNetworkNames, string(parentRef.Name))
128151
}
129152
if config.ServiceNetworkOverrideMode {

pkg/gateway/model_build_lattice_service_test.go

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -182,27 +182,12 @@ func Test_LatticeServiceModelBuild(t *testing.T) {
182182
},
183183
},
184184
{
185-
// TODO:
186185
name: "Delete LatticeService",
187186
wantIsDeleted: true,
188187
wantErrIsNil: true,
189188
gwClass: vpcLatticeGatewayClass,
190189
gws: []gwv1.Gateway{
191-
{
192-
ObjectMeta: metav1.ObjectMeta{
193-
Name: "gateway2",
194-
Namespace: "ns1",
195-
},
196-
Spec: gwv1.GatewaySpec{
197-
Listeners: []gwv1.Listener{
198-
{
199-
Name: httpSectionName,
200-
Port: 80,
201-
Protocol: "HTTP",
202-
},
203-
},
204-
},
205-
},
190+
vpcLatticeGateway,
206191
},
207192
route: core.NewHTTPRoute(gwv1.HTTPRoute{
208193
ObjectMeta: metav1.ObjectMeta{
@@ -215,7 +200,8 @@ func Test_LatticeServiceModelBuild(t *testing.T) {
215200
CommonRouteSpec: gwv1.CommonRouteSpec{
216201
ParentRefs: []gwv1.ParentReference{
217202
{
218-
Name: "gateway2",
203+
Name: gwv1.ObjectName(vpcLatticeGateway.Name),
204+
Namespace: namespacePtr(vpcLatticeGateway.Namespace),
219205
SectionName: &httpSectionName,
220206
},
221207
},
@@ -240,7 +226,7 @@ func Test_LatticeServiceModelBuild(t *testing.T) {
240226
RouteNamespace: "ns1",
241227
RouteType: core.HttpRouteType,
242228
},
243-
ServiceNetworkNames: []string{"gateway2"},
229+
ServiceNetworkNames: []string{vpcLatticeGateway.Name},
244230
},
245231
},
246232
{
@@ -252,6 +238,7 @@ func Test_LatticeServiceModelBuild(t *testing.T) {
252238
{
253239
ObjectMeta: vpcLatticeGateway.ObjectMeta,
254240
Spec: gwv1.GatewaySpec{
241+
GatewayClassName: gwv1.ObjectName(vpcLatticeGatewayClass.Name),
255242
Listeners: []gwv1.Listener{
256243
{
257244
Name: "tls",
@@ -329,6 +316,7 @@ func Test_LatticeServiceModelBuild(t *testing.T) {
329316
{
330317
ObjectMeta: vpcLatticeGateway.ObjectMeta,
331318
Spec: gwv1.GatewaySpec{
319+
GatewayClassName: gwv1.ObjectName(vpcLatticeGatewayClass.Name),
332320
Listeners: []gwv1.Listener{
333321
{
334322
Name: "tls",
@@ -376,6 +364,15 @@ func Test_LatticeServiceModelBuild(t *testing.T) {
376364
gwClass: vpcLatticeGatewayClass,
377365
gws: []gwv1.Gateway{
378366
vpcLatticeGateway,
367+
{
368+
ObjectMeta: metav1.ObjectMeta{
369+
Name: "gateway2",
370+
Namespace: "ns2",
371+
},
372+
Spec: gwv1.GatewaySpec{
373+
GatewayClassName: gwv1.ObjectName(vpcLatticeGatewayClass.Name),
374+
},
375+
},
379376
},
380377
route: core.NewHTTPRoute(gwv1.HTTPRoute{
381378
ObjectMeta: metav1.ObjectMeta{
@@ -406,45 +403,55 @@ func Test_LatticeServiceModelBuild(t *testing.T) {
406403
ServiceNetworkNames: []string{vpcLatticeGateway.Name, "gateway2"},
407404
},
408405
},
409-
//{
410-
// name: "Multiple service networks with one different controller",
411-
// wantIsDeleted: false,
412-
// wantErrIsNil: true,
413-
// gw: gwv1.Gateway{
414-
// ObjectMeta: metav1.ObjectMeta{
415-
// Name: "gateway1",
416-
// Namespace: "default",
417-
// },
418-
// },
419-
// route: core.NewHTTPRoute(gwv1.HTTPRoute{
420-
// ObjectMeta: metav1.ObjectMeta{
421-
// Name: "service1",
422-
// Namespace: "default",
423-
// },
424-
// Spec: gwv1.HTTPRouteSpec{
425-
// CommonRouteSpec: gwv1.CommonRouteSpec{
426-
// ParentRefs: []gwv1.ParentReference{
427-
// {
428-
// Name: "gateway1",
429-
// Namespace: namespacePtr("default"),
430-
// },
431-
// {
432-
// Name: "not-lattice",
433-
// Namespace: namespacePtr("ns2"),
434-
// },
435-
// },
436-
// },
437-
// },
438-
// }),
439-
// expected: model.ServiceSpec{
440-
// ServiceTagFields: model.ServiceTagFields{
441-
// RouteName: "service1",
442-
// RouteNamespace: "default",
443-
// RouteType: core.HttpRouteType,
444-
// },
445-
// ServiceNetworkNames: []string{"gateway1"},
446-
// },
447-
//},
406+
{
407+
name: "Multiple service networks with one different controller",
408+
wantIsDeleted: false,
409+
wantErrIsNil: true,
410+
gwClass: vpcLatticeGatewayClass,
411+
gws: []gwv1.Gateway{
412+
vpcLatticeGateway,
413+
// managed by different controller gateway
414+
{
415+
ObjectMeta: metav1.ObjectMeta{
416+
Name: "not-lattice",
417+
Namespace: "ns2",
418+
},
419+
Spec: gwv1.GatewaySpec{
420+
GatewayClassName: gwv1.ObjectName("not-lattice-gwClass"),
421+
},
422+
},
423+
},
424+
route: core.NewHTTPRoute(gwv1.HTTPRoute{
425+
ObjectMeta: metav1.ObjectMeta{
426+
Name: "service1",
427+
Namespace: "default",
428+
},
429+
Spec: gwv1.HTTPRouteSpec{
430+
CommonRouteSpec: gwv1.CommonRouteSpec{
431+
// has two parent refs and one is not managed by lattice
432+
ParentRefs: []gwv1.ParentReference{
433+
{
434+
Name: gwv1.ObjectName(vpcLatticeGateway.Name),
435+
Namespace: namespacePtr(vpcLatticeGateway.Namespace),
436+
},
437+
{
438+
Name: "not-lattice",
439+
Namespace: namespacePtr("ns2"),
440+
},
441+
},
442+
},
443+
},
444+
}),
445+
expected: model.ServiceSpec{
446+
ServiceTagFields: model.ServiceTagFields{
447+
RouteName: "service1",
448+
RouteNamespace: "default",
449+
RouteType: core.HttpRouteType,
450+
},
451+
// only the lattice gateway is added
452+
ServiceNetworkNames: []string{vpcLatticeGateway.Name},
453+
},
454+
},
448455
}
449456

450457
for _, tt := range tests {

0 commit comments

Comments
 (0)