Skip to content

Commit b5daf3e

Browse files
committed
Fix route/path naming for conflicts
1 parent 1952ccf commit b5daf3e

File tree

8 files changed

+22
-50
lines changed

8 files changed

+22
-50
lines changed

internal/mode/static/state/dataplane/convert_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func TestConvertHTTPMirrorFilter(t *testing.T) {
351351
expected: &HTTPRequestMirrorFilter{
352352
Name: helpers.GetPointer("backend"),
353353
Namespace: helpers.GetPointer("namespace"),
354-
Target: helpers.GetPointer("/_ngf-internal-mirror-namespace-backend-0"),
354+
Target: helpers.GetPointer("/_ngf-internal-mirror-namespace/backend-0"),
355355
},
356356
name: "full",
357357
},

internal/mode/static/state/graph/common_filter_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestValidateFilter(t *testing.T) {
103103
RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{},
104104
},
105105
expectErrCount: 0,
106-
name: "valid GRPC filter",
106+
name: "valid GRPC mirror filter",
107107
},
108108
{
109109
filter: Filter{

internal/mode/static/state/graph/grpcroute.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ func buildGRPCMirrorRoutes(
9191

9292
objectMeta := route.ObjectMeta.DeepCopy()
9393
backendRef := filter.RequestMirror.BackendRef
94-
name := mirror.RouteName(route.GetName(), string(backendRef.Name), (*string)(backendRef.Namespace), idx)
94+
namespace := route.GetNamespace()
95+
if backendRef.Namespace != nil {
96+
namespace = string(*backendRef.Namespace)
97+
}
98+
name := mirror.RouteName(route.GetName(), string(backendRef.Name), namespace, idx)
9599
objectMeta.SetName(name)
96100

97101
tmpMirrorRoute := &v1.GRPCRoute{

internal/mode/static/state/graph/grpcroute_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ func TestBuildGRPCRouteWithMirrorRoutes(t *testing.T) {
11401140
Source: &v1.GRPCRoute{
11411141
ObjectMeta: metav1.ObjectMeta{
11421142
Namespace: "test",
1143-
Name: mirror.RouteName("gr", "mirror-backend", nil, 0),
1143+
Name: mirror.RouteName("gr", "mirror-backend", "test", 0),
11441144
},
11451145
Spec: v1.GRPCRouteSpec{
11461146
CommonRouteSpec: gr.Spec.CommonRouteSpec,

internal/mode/static/state/graph/httproute.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ func buildHTTPMirrorRoutes(
8787

8888
objectMeta := route.ObjectMeta.DeepCopy()
8989
backendRef := filter.RequestMirror.BackendRef
90-
name := mirror.RouteName(route.GetName(), string(backendRef.Name), (*string)(backendRef.Namespace), idx)
90+
namespace := route.GetNamespace()
91+
if backendRef.Namespace != nil {
92+
namespace = string(*backendRef.Namespace)
93+
}
94+
name := mirror.RouteName(route.GetName(), string(backendRef.Name), namespace, idx)
9195
objectMeta.SetName(name)
9296

9397
tmpMirrorRoute := &v1.HTTPRoute{

internal/mode/static/state/graph/httproute_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ func TestBuildHTTPRouteWithMirrorRoutes(t *testing.T) {
942942
Source: &gatewayv1.HTTPRoute{
943943
ObjectMeta: metav1.ObjectMeta{
944944
Namespace: "test",
945-
Name: mirror.RouteName("hr", "mirror-backend", nil, 0),
945+
Name: mirror.RouteName("hr", "mirror-backend", "test", 0),
946946
},
947947
Spec: gatewayv1.HTTPRouteSpec{
948948
CommonRouteSpec: hr.Spec.CommonRouteSpec,

internal/mode/static/state/mirror/mirror.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@ import (
1212
// RouteName builds the name for the internal mirror route, using the user route name,
1313
// service namespace/name, and index of the rule.
1414
// The prefix is used to prevent a user from creating a route with a conflicting name.
15-
func RouteName(routeName, service string, namespace *string, idx int) string {
15+
func RouteName(routeName, service, namespace string, idx int) string {
1616
prefix := strings.TrimPrefix(http.InternalMirrorRoutePathPrefix, "/")
17-
if namespace != nil {
18-
return fmt.Sprintf("%s-%s-%s-%s-%d", prefix, routeName, service, *namespace, idx)
19-
}
20-
21-
return fmt.Sprintf("%s-%s-%s-%d", prefix, routeName, service, idx)
17+
return fmt.Sprintf("%s-%s-%s/%s-%d", prefix, routeName, namespace, service, idx)
2218
}
2319

2420
// BackendPath builds the path for the internal mirror location, using the BackendRef.
@@ -35,7 +31,7 @@ func BackendPath(idx int, namespace *string, service string) *string {
3531
var mirrorPath string
3632

3733
if namespace != nil {
38-
mirrorPath = fmt.Sprintf("%s-%s-%s-%d", http.InternalMirrorRoutePathPrefix, *namespace, service, idx)
34+
mirrorPath = fmt.Sprintf("%s-%s/%s-%d", http.InternalMirrorRoutePathPrefix, *namespace, service, idx)
3935
} else {
4036
mirrorPath = fmt.Sprintf("%s-%s-%d", http.InternalMirrorRoutePathPrefix, service, idx)
4137
}

internal/mode/static/state/mirror/mirror_test.go

+5-37
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,10 @@ import (
1111

1212
func TestRouteName(t *testing.T) {
1313
t.Parallel()
14+
g := NewWithT(t)
1415

15-
tests := []struct {
16-
namespace *string
17-
name string
18-
routeName string
19-
service string
20-
expected string
21-
idx int
22-
}{
23-
{
24-
name: "with namespace",
25-
routeName: "route1",
26-
service: "service1",
27-
namespace: helpers.GetPointer("namespace1"),
28-
idx: 1,
29-
expected: "_ngf-internal-mirror-route1-service1-namespace1-1",
30-
},
31-
{
32-
name: "without namespace",
33-
routeName: "route2",
34-
service: "service2",
35-
namespace: nil,
36-
idx: 2,
37-
expected: "_ngf-internal-mirror-route2-service2-2",
38-
},
39-
}
40-
41-
for _, tt := range tests {
42-
t.Run(tt.name, func(t *testing.T) {
43-
t.Parallel()
44-
g := NewWithT(t)
45-
46-
result := RouteName(tt.routeName, tt.service, tt.namespace, tt.idx)
47-
g.Expect(result).To(Equal(tt.expected))
48-
})
49-
}
16+
result := RouteName("route1", "service1", "namespace1", 1)
17+
g.Expect(result).To(Equal("_ngf-internal-mirror-route1-namespace1/service1-1"))
5018
}
5119

5220
func TestPathWithBackendRef(t *testing.T) {
@@ -65,7 +33,7 @@ func TestPathWithBackendRef(t *testing.T) {
6533
Name: "service1",
6634
Namespace: helpers.GetPointer[v1.Namespace]("namespace1"),
6735
},
68-
expected: helpers.GetPointer("/_ngf-internal-mirror-namespace1-service1-1"),
36+
expected: helpers.GetPointer("/_ngf-internal-mirror-namespace1/service1-1"),
6937
},
7038
{
7139
name: "without namespace",
@@ -103,7 +71,7 @@ func TestBackendPath(t *testing.T) {
10371
idx: 1,
10472
namespace: helpers.GetPointer("namespace1"),
10573
service: "service1",
106-
expected: helpers.GetPointer("/_ngf-internal-mirror-namespace1-service1-1"),
74+
expected: helpers.GetPointer("/_ngf-internal-mirror-namespace1/service1-1"),
10775
},
10876
{
10977
name: "Without namespace",

0 commit comments

Comments
 (0)