Skip to content

Commit 1d02b84

Browse files
author
Doyoon Kim
authored
Remove assumption of default namespaces in httproutes (#256)
* Remove assumption of default namespaces in httproutes
1 parent f93a37a commit 1d02b84

File tree

8 files changed

+160
-26
lines changed

8 files changed

+160
-26
lines changed

controllers/gateway_controller.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/golang/glog"
2525
"github.com/pkg/errors"
2626

27-
"github.com/aws/aws-application-networking-k8s/pkg/latticestore"
2827
corev1 "k8s.io/api/core/v1"
2928
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3029
"k8s.io/apimachinery/pkg/runtime"
@@ -36,6 +35,8 @@ import (
3635
"sigs.k8s.io/controller-runtime/pkg/source"
3736
gateway_api "sigs.k8s.io/gateway-api/apis/v1beta1"
3837

38+
"github.com/aws/aws-application-networking-k8s/pkg/latticestore"
39+
3940
"github.com/aws/aws-application-networking-k8s/controllers/eventhandlers"
4041
"github.com/aws/aws-application-networking-k8s/pkg/aws"
4142
"github.com/aws/aws-application-networking-k8s/pkg/config"
@@ -148,8 +149,12 @@ func (r *GatewayReconciler) reconcile(ctx context.Context, req ctrl.Request) err
148149
if len(httpRoute.Spec.ParentRefs) <= 0 {
149150
continue
150151
}
152+
gwNamespace := httpRoute.Namespace
153+
if httpRoute.Spec.ParentRefs[0].Namespace != nil {
154+
gwNamespace = string(*httpRoute.Spec.ParentRefs[0].Namespace)
155+
}
151156
gwName := types.NamespacedName{
152-
Namespace: httpRoute.Namespace,
157+
Namespace: gwNamespace,
153158
Name: string(httpRoute.Spec.ParentRefs[0].Name),
154159
}
155160

@@ -331,7 +336,7 @@ func (r *GatewayReconciler) updateBadStatus(ctx context.Context, message string,
331336
func UpdateHTTPRouteListenerStatus(ctx context.Context, k8sclient client.Client, httproute *gateway_api.HTTPRoute) error {
332337
gw := &gateway_api.Gateway{}
333338

334-
gwNamespace := "default"
339+
gwNamespace := httproute.Namespace
335340
if httproute.Spec.ParentRefs[0].Namespace != nil {
336341
gwNamespace = string(*httproute.Spec.ParentRefs[0].Namespace)
337342
}

pkg/deploy/lattice/target_group_synthesizer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package lattice
33
import (
44
"context"
55
"errors"
6-
"github.com/golang/glog"
76
"strings"
87

8+
"github.com/golang/glog"
9+
910
"k8s.io/apimachinery/pkg/types"
1011
"sigs.k8s.io/controller-runtime/pkg/client"
1112
gateway_api "sigs.k8s.io/gateway-api/apis/v1beta1"
@@ -338,8 +339,7 @@ func (t *targetGroupSynthesizer) isTargetGroupUsedByaHTTPRoute(ctx context.Conte
338339
if string(*httpBackendRef.BackendObjectReference.Kind) != "Service" {
339340
continue
340341
}
341-
namespace := "default"
342-
342+
namespace := httpRoute.Namespace
343343
if httpBackendRef.BackendObjectReference.Namespace != nil {
344344
namespace = string(*httpBackendRef.BackendObjectReference.Namespace)
345345
}

pkg/gateway/model_build_listener.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
78
"github.com/golang/glog"
89

910
latticemodel "github.com/aws/aws-application-networking-k8s/pkg/model/lattice"
@@ -26,8 +27,7 @@ func (t *latticeServiceModelBuildTask) extractListnerInfo(ctx context.Context, p
2627
}
2728

2829
glog.V(6).Infof("Building Listener for HTTPRoute Name %s NameSpace %s\n", t.httpRoute.Name, t.httpRoute.Namespace)
29-
var gwNamespace = "default"
30-
30+
var gwNamespace = t.httpRoute.Namespace
3131
if t.httpRoute.Spec.ParentRefs[0].Namespace != nil {
3232
gwNamespace = string(*t.httpRoute.Spec.ParentRefs[0].Namespace)
3333
}
@@ -124,25 +124,22 @@ func (t *latticeServiceModelBuildTask) buildListener(ctx context.Context) error
124124

125125
var is_import = false
126126
var targetgroupName = ""
127-
var targetgroupNamespace = "default"
127+
var targetgroupNamespace = t.httpRoute.Namespace
128128

129129
if string(*httpBackendRef.Kind) == "Service" {
130130
if httpBackendRef.BackendObjectReference.Namespace != nil {
131131
targetgroupNamespace = string(*httpBackendRef.BackendObjectReference.Namespace)
132132
}
133133
targetgroupName = string(httpBackendRef.BackendObjectReference.Name)
134134
is_import = false
135-
136135
}
137136

138137
if string(*httpBackendRef.Kind) == "ServiceImport" {
139138
is_import = true
140-
targetgroupNamespace = "default"
141139
if httpBackendRef.BackendObjectReference.Namespace != nil {
142140
targetgroupNamespace = string(*httpBackendRef.BackendObjectReference.Namespace)
143141
}
144142
targetgroupName = string(httpBackendRef.BackendObjectReference.Name)
145-
146143
}
147144

148145
action := latticemodel.DefaultAction{

pkg/gateway/model_build_listener_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,13 @@ func Test_ListenerModelBuild(t *testing.T) {
383383

384384
assert.Equal(t, resListener[0].Spec.DefaultAction.BackendServiceName,
385385
string(tt.httpRoute.Spec.Rules[0].BackendRefs[0].BackendRef.Name))
386+
if ns := tt.httpRoute.Spec.Rules[0].BackendRefs[0].BackendRef.Namespace; ns != nil {
387+
assert.Equal(t, resListener[0].Spec.DefaultAction.BackendServiceNamespace, *ns)
388+
} else {
389+
assert.Equal(t, resListener[0].Spec.DefaultAction.BackendServiceNamespace, tt.httpRoute.ObjectMeta.Namespace)
390+
}
386391

387-
if *tt.httpRoute.Spec.Rules[0].BackendRefs[0].Kind == gateway_api.Kind("Service") {
392+
if *tt.httpRoute.Spec.Rules[0].BackendRefs[0].Kind == "Service" {
388393
assert.Equal(t, resListener[0].Spec.DefaultAction.Is_Import, false)
389394
} else {
390395
assert.Equal(t, resListener[0].Spec.DefaultAction.Is_Import, true)

pkg/gateway/model_build_rule.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
78
"github.com/golang/glog"
89

910
"github.com/aws/aws-sdk-go/aws"
1011

12+
gateway_api "sigs.k8s.io/gateway-api/apis/v1beta1"
13+
1114
latticemodel "github.com/aws/aws-application-networking-k8s/pkg/model/lattice"
1215
"github.com/aws/aws-sdk-go/service/vpclattice"
13-
gateway_api "sigs.k8s.io/gateway-api/apis/v1beta1"
1416
)
1517

1618
const (
@@ -139,7 +141,7 @@ func (t *latticeServiceModelBuildTask) buildRules(ctx context.Context) error {
139141
ruleTG := latticemodel.RuleTargetGroup{}
140142

141143
if string(*httpBackendRef.BackendObjectReference.Kind) == "Service" {
142-
namespace := "default"
144+
namespace := t.httpRoute.Namespace
143145
if httpBackendRef.BackendObjectReference.Namespace != nil {
144146
namespace = string(*httpBackendRef.BackendObjectReference.Namespace)
145147
}
@@ -167,7 +169,7 @@ func (t *latticeServiceModelBuildTask) buildRules(ctx context.Context) error {
167169
}
168170
*/
169171
ruleTG.Name = string(httpBackendRef.BackendObjectReference.Name)
170-
ruleTG.Namespace = "default"
172+
ruleTG.Namespace = t.httpRoute.Namespace
171173
if httpBackendRef.BackendObjectReference.Namespace != nil {
172174
ruleTG.Namespace = string(*httpBackendRef.BackendObjectReference.Namespace)
173175
}

pkg/gateway/model_build_rule_test.go

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
"fmt"
77
"testing"
88

9-
"github.com/aws/aws-sdk-go/service/vpclattice"
109
"github.com/golang/mock/gomock"
1110
"github.com/stretchr/testify/assert"
1211

12+
"github.com/aws/aws-sdk-go/service/vpclattice"
13+
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415
gateway_api "sigs.k8s.io/gateway-api/apis/v1beta1"
1516

@@ -30,26 +31,42 @@ func Test_RuleModelBuild(t *testing.T) {
3031
var serviceimportKind gateway_api.Kind = "ServiceImport"
3132
var weight1 = int32(10)
3233
var weight2 = int32(90)
33-
var namespace = gateway_api.Namespace("default")
34+
var namespace = gateway_api.Namespace("testnamespace")
35+
var namespace2 = gateway_api.Namespace("testnamespace2")
3436
var path1 = string("/ver1")
3537
var path2 = string("/ver2")
38+
var path3 = string("/ver3")
3639
var k8sPathMatchExactType = gateway_api.PathMatchExact
3740
var backendRef1 = gateway_api.BackendRef{
3841
BackendObjectReference: gateway_api.BackendObjectReference{
39-
Name: "targetgroup1",
40-
Namespace: &namespace,
41-
Kind: &serviceKind,
42+
Name: "targetgroup1",
43+
Kind: &serviceKind,
4244
},
4345
Weight: &weight1,
4446
}
4547
var backendRef2 = gateway_api.BackendRef{
48+
BackendObjectReference: gateway_api.BackendObjectReference{
49+
Name: "targetgroup2",
50+
Kind: &serviceimportKind,
51+
},
52+
Weight: &weight2,
53+
}
54+
var backendRef1Namespace1 = gateway_api.BackendRef{
4655
BackendObjectReference: gateway_api.BackendObjectReference{
4756
Name: "targetgroup2",
4857
Namespace: &namespace,
4958
Kind: &serviceimportKind,
5059
},
5160
Weight: &weight2,
5261
}
62+
var backendRef1Namespace2 = gateway_api.BackendRef{
63+
BackendObjectReference: gateway_api.BackendObjectReference{
64+
Name: "targetgroup2",
65+
Namespace: &namespace2,
66+
Kind: &serviceimportKind,
67+
},
68+
Weight: &weight2,
69+
}
5370
var backendServiceImportRef = gateway_api.BackendRef{
5471
BackendObjectReference: gateway_api.BackendObjectReference{
5572
Name: "targetgroup1",
@@ -221,6 +238,73 @@ func Test_RuleModelBuild(t *testing.T) {
221238
},
222239
},
223240
},
241+
{
242+
name: "rule, different namespace combination",
243+
gwListenerPort: *PortNumberPtr(80),
244+
wantErrIsNil: true,
245+
k8sGetGatewayCall: true,
246+
k8sGatewayReturnOK: true,
247+
httpRoute: &gateway_api.HTTPRoute{
248+
ObjectMeta: metav1.ObjectMeta{
249+
Name: "service1",
250+
Namespace: "non-default",
251+
},
252+
Spec: gateway_api.HTTPRouteSpec{
253+
CommonRouteSpec: gateway_api.CommonRouteSpec{
254+
ParentRefs: []gateway_api.ParentReference{
255+
{
256+
Name: "mesh1",
257+
SectionName: &httpSectionName,
258+
},
259+
},
260+
},
261+
Rules: []gateway_api.HTTPRouteRule{
262+
{
263+
Matches: []gateway_api.HTTPRouteMatch{
264+
{
265+
Path: &gateway_api.HTTPPathMatch{
266+
Value: &path1,
267+
},
268+
},
269+
},
270+
BackendRefs: []gateway_api.HTTPBackendRef{
271+
{
272+
BackendRef: backendRef1,
273+
},
274+
},
275+
},
276+
{
277+
Matches: []gateway_api.HTTPRouteMatch{
278+
{
279+
Path: &gateway_api.HTTPPathMatch{
280+
Value: &path2,
281+
},
282+
},
283+
},
284+
BackendRefs: []gateway_api.HTTPBackendRef{
285+
{
286+
BackendRef: backendRef1Namespace1,
287+
},
288+
},
289+
},
290+
{
291+
Matches: []gateway_api.HTTPRouteMatch{
292+
{
293+
Path: &gateway_api.HTTPPathMatch{
294+
Value: &path3,
295+
},
296+
},
297+
},
298+
BackendRefs: []gateway_api.HTTPBackendRef{
299+
{
300+
BackendRef: backendRef1Namespace2,
301+
},
302+
},
303+
},
304+
},
305+
},
306+
},
307+
},
224308
}
225309
for _, tt := range tests {
226310
fmt.Printf("Testing >>> %v\n", tt.name)
@@ -288,6 +372,8 @@ func Test_RuleModelBuild(t *testing.T) {
288372
assert.Equal(t, gateway_api.ObjectName(tg.Name), tt.httpRoute.Spec.Rules[i-1].BackendRefs[j].Name)
289373
if tt.httpRoute.Spec.Rules[i-1].BackendRefs[j].Namespace != nil {
290374
assert.Equal(t, gateway_api.Namespace(tg.Namespace), *tt.httpRoute.Spec.Rules[i-1].BackendRefs[j].Namespace)
375+
} else {
376+
assert.Equal(t, tg.Namespace, tt.httpRoute.Namespace)
291377
}
292378

293379
if *tt.httpRoute.Spec.Rules[i-1].BackendRefs[j].Kind == "ServiceImport" {

pkg/gateway/model_build_targetgroup.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,10 @@ func (t *latticeServiceModelBuildTask) buildTargets(ctx context.Context) error {
140140
glog.V(6).Infof("latticeServiceModelBuildTask: ignore service: %v \n", httpBackendRef)
141141
continue
142142
}
143-
backendNamespace := "default"
144143

144+
backendNamespace := t.httpRoute.Namespace
145145
if httpBackendRef.Namespace != nil {
146146
backendNamespace = string(*httpBackendRef.Namespace)
147-
148147
}
149148

150149
targetTask := &latticeTargetsModelBuildTask{
@@ -278,7 +277,7 @@ func (t *latticeServiceModelBuildTask) buildHTTPTargetGroupSpec(ctx context.Cont
278277
if httpBackendRef.BackendRef.BackendObjectReference.Namespace != nil {
279278
namespace = string(*httpBackendRef.BackendRef.BackendObjectReference.Namespace)
280279
} else {
281-
namespace = "default"
280+
namespace = t.httpRoute.Namespace
282281
}
283282

284283
backendKind := string(*httpBackendRef.BackendRef.BackendObjectReference.Kind)

pkg/gateway/model_build_targetgroup_test.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ func Test_TGModelByHTTPRouteImportBuild(t *testing.T) {
473473
},
474474
},
475475
Rules: []gateway_api.HTTPRouteRule{
476-
477476
{
478477
BackendRefs: []gateway_api.HTTPBackendRef{
479478
{
@@ -496,6 +495,43 @@ func Test_TGModelByHTTPRouteImportBuild(t *testing.T) {
496495
wantIsDeleted: false,
497496
wantErrIsNil: true,
498497
},
498+
{
499+
name: "Add LatticeService, implicit namespace",
500+
httpRoute: &gateway_api.HTTPRoute{
501+
ObjectMeta: metav1.ObjectMeta{
502+
Name: "serviceimport1",
503+
Namespace: "tg1-ns2",
504+
},
505+
Spec: gateway_api.HTTPRouteSpec{
506+
CommonRouteSpec: gateway_api.CommonRouteSpec{
507+
ParentRefs: []gateway_api.ParentReference{
508+
{
509+
Name: "gateway1",
510+
},
511+
},
512+
},
513+
Rules: []gateway_api.HTTPRouteRule{
514+
{
515+
BackendRefs: []gateway_api.HTTPBackendRef{
516+
{
517+
BackendRef: gateway_api.BackendRef{
518+
BackendObjectReference: gateway_api.BackendObjectReference{
519+
Name: "service1-tg2",
520+
Kind: kindPtr("ServiceImport"),
521+
},
522+
},
523+
},
524+
},
525+
},
526+
},
527+
},
528+
},
529+
svcImportExist: true,
530+
wantError: nil,
531+
wantName: "service1",
532+
wantIsDeleted: false,
533+
wantErrIsNil: true,
534+
},
499535
{
500536
name: "Delete LatticeService",
501537
httpRoute: &gateway_api.HTTPRoute{
@@ -589,7 +625,11 @@ func Test_TGModelByHTTPRouteImportBuild(t *testing.T) {
589625
// verify data store
590626
for _, httpRules := range tt.httpRoute.Spec.Rules {
591627
for _, httpBackendRef := range httpRules.BackendRefs {
592-
tgName := latticestore.TargetGroupName(string(httpBackendRef.Name), string(*httpBackendRef.Namespace))
628+
ns := tt.httpRoute.Namespace
629+
if httpBackendRef.Namespace != nil {
630+
ns = string(*httpBackendRef.Namespace)
631+
}
632+
tgName := latticestore.TargetGroupName(string(httpBackendRef.Name), ns)
593633

594634
fmt.Printf("httpBacndendRef %s\n", *httpBackendRef.BackendObjectReference.Kind)
595635
if "Service" == *httpBackendRef.BackendObjectReference.Kind {

0 commit comments

Comments
 (0)