Skip to content

Commit e802e82

Browse files
committed
address comments
1 parent 9ef4868 commit e802e82

File tree

4 files changed

+106
-150
lines changed

4 files changed

+106
-150
lines changed

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

Lines changed: 90 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ func getNormalBackendRef() graph.BackendRef {
3636
}
3737
}
3838

39-
func getModifiedBackendRef(mod func(ref graph.BackendRef) graph.BackendRef) graph.BackendRef {
40-
return mod(getNormalBackendRef())
41-
}
42-
4339
func getExpectedConfiguration() Configuration {
4440
return Configuration{
4541
BaseHTTPConfig: BaseHTTPConfig{HTTP2: true, IPFamily: Dual},
@@ -183,9 +179,7 @@ func TestBuildConfiguration(t *testing.T) {
183179
fakeResolver := &resolverfakes.FakeServiceResolver{}
184180
fakeResolver.ResolveReturns(fooEndpoints, nil)
185181

186-
validBackendRef := getModifiedBackendRef(func(backend graph.BackendRef) graph.BackendRef {
187-
return backend
188-
})
182+
validBackendRef := getNormalBackendRef()
189183

190184
expValidBackend := Backend{
191185
UpstreamName: fooUpstreamName,
@@ -837,14 +831,12 @@ func TestBuildConfiguration(t *testing.T) {
837831
},
838832
{
839833
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
840-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
841-
{
842-
Name: "invalid-listener",
843-
Source: invalidListener,
844-
Valid: false,
845-
ResolvedSecret: &secret1NsName,
846-
},
847-
}...)
834+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
835+
Name: "invalid-listener",
836+
Source: invalidListener,
837+
Valid: false,
838+
ResolvedSecret: &secret1NsName,
839+
})
848840
g.Routes = map[graph.RouteKey]*graph.L7Route{
849841
graph.CreateRouteKey(httpsHR1): httpsRouteHR1,
850842
graph.CreateRouteKey(httpsHR2): httpsRouteHR2,
@@ -862,17 +854,15 @@ func TestBuildConfiguration(t *testing.T) {
862854
},
863855
{
864856
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
865-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
866-
{
867-
Name: "listener-80-1",
868-
Source: listener80,
869-
Valid: true,
870-
Routes: map[graph.RouteKey]*graph.L7Route{
871-
graph.CreateRouteKey(hr1): routeHR1,
872-
graph.CreateRouteKey(hr2): routeHR2,
873-
},
857+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
858+
Name: "listener-80-1",
859+
Source: listener80,
860+
Valid: true,
861+
Routes: map[graph.RouteKey]*graph.L7Route{
862+
graph.CreateRouteKey(hr1): routeHR1,
863+
graph.CreateRouteKey(hr2): routeHR2,
874864
},
875-
}...)
865+
})
876866
g.Routes = map[graph.RouteKey]*graph.L7Route{
877867
graph.CreateRouteKey(hr1): routeHR1,
878868
graph.CreateRouteKey(hr2): routeHR2,
@@ -925,16 +915,14 @@ func TestBuildConfiguration(t *testing.T) {
925915
},
926916
{
927917
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
928-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
929-
{
930-
Name: "listener-80-1",
931-
Source: listener80,
932-
Valid: true,
933-
Routes: map[graph.RouteKey]*graph.L7Route{
934-
graph.CreateRouteKey(gr): routeGR,
935-
},
918+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
919+
Name: "listener-80-1",
920+
Source: listener80,
921+
Valid: true,
922+
Routes: map[graph.RouteKey]*graph.L7Route{
923+
graph.CreateRouteKey(gr): routeGR,
936924
},
937-
}...)
925+
})
938926
g.Routes[graph.CreateRouteKey(gr)] = routeGR
939927
return g
940928
}),
@@ -1411,16 +1399,14 @@ func TestBuildConfiguration(t *testing.T) {
14111399
{
14121400
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
14131401
g.GatewayClass.Valid = false
1414-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
1415-
{
1416-
Name: "listener-80-1",
1417-
Source: listener80,
1418-
Valid: true,
1419-
Routes: map[graph.RouteKey]*graph.L7Route{
1420-
graph.CreateRouteKey(hr1): routeHR1,
1421-
},
1402+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
1403+
Name: "listener-80-1",
1404+
Source: listener80,
1405+
Valid: true,
1406+
Routes: map[graph.RouteKey]*graph.L7Route{
1407+
graph.CreateRouteKey(hr1): routeHR1,
14221408
},
1423-
}...)
1409+
})
14241410
g.Routes = map[graph.RouteKey]*graph.L7Route{
14251411
graph.CreateRouteKey(hr1): routeHR1,
14261412
}
@@ -1432,16 +1418,14 @@ func TestBuildConfiguration(t *testing.T) {
14321418
{
14331419
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
14341420
g.GatewayClass.Valid = false
1435-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
1436-
{
1437-
Name: "listener-80-1",
1438-
Source: listener80,
1439-
Valid: true,
1440-
Routes: map[graph.RouteKey]*graph.L7Route{
1441-
graph.CreateRouteKey(hr1): routeHR1,
1442-
},
1421+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
1422+
Name: "listener-80-1",
1423+
Source: listener80,
1424+
Valid: true,
1425+
Routes: map[graph.RouteKey]*graph.L7Route{
1426+
graph.CreateRouteKey(hr1): routeHR1,
14431427
},
1444-
}...)
1428+
})
14451429
g.Routes = map[graph.RouteKey]*graph.L7Route{
14461430
graph.CreateRouteKey(hr1): routeHR1,
14471431
}
@@ -1460,16 +1444,14 @@ func TestBuildConfiguration(t *testing.T) {
14601444
},
14611445
{
14621446
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
1463-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
1464-
{
1465-
Name: "listener-80-1",
1466-
Source: listener80,
1467-
Valid: true,
1468-
Routes: map[graph.RouteKey]*graph.L7Route{
1469-
graph.CreateRouteKey(hr5): routeHR5,
1470-
},
1447+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
1448+
Name: "listener-80-1",
1449+
Source: listener80,
1450+
Valid: true,
1451+
Routes: map[graph.RouteKey]*graph.L7Route{
1452+
graph.CreateRouteKey(hr5): routeHR5,
14711453
},
1472-
}...)
1454+
})
14731455
g.Routes = map[graph.RouteKey]*graph.L7Route{
14741456
graph.CreateRouteKey(hr5): routeHR5,
14751457
}
@@ -1599,16 +1581,14 @@ func TestBuildConfiguration(t *testing.T) {
15991581
},
16001582
{
16011583
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
1602-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
1603-
{
1604-
Name: "listener-80-1",
1605-
Source: listener80,
1606-
Valid: true,
1607-
Routes: map[graph.RouteKey]*graph.L7Route{
1608-
graph.CreateRouteKey(hr7): routeHR7,
1609-
},
1584+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
1585+
Name: "listener-80-1",
1586+
Source: listener80,
1587+
Valid: true,
1588+
Routes: map[graph.RouteKey]*graph.L7Route{
1589+
graph.CreateRouteKey(hr7): routeHR7,
16101590
},
1611-
}...)
1591+
})
16121592
g.Routes = map[graph.RouteKey]*graph.L7Route{
16131593
graph.CreateRouteKey(hr7): routeHR7,
16141594
}
@@ -1731,17 +1711,15 @@ func TestBuildConfiguration(t *testing.T) {
17311711
},
17321712
{
17331713
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
1734-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
1735-
{
1736-
Name: "listener-443",
1737-
Source: listener443,
1738-
Valid: true,
1739-
Routes: map[graph.RouteKey]*graph.L7Route{
1740-
graph.CreateRouteKey(httpsHR8): httpsRouteHR8,
1741-
},
1742-
ResolvedSecret: &secret1NsName,
1714+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
1715+
Name: "listener-443",
1716+
Source: listener443,
1717+
Valid: true,
1718+
Routes: map[graph.RouteKey]*graph.L7Route{
1719+
graph.CreateRouteKey(httpsHR8): httpsRouteHR8,
17431720
},
1744-
}...)
1721+
ResolvedSecret: &secret1NsName,
1722+
})
17451723
g.Routes = map[graph.RouteKey]*graph.L7Route{
17461724
graph.CreateRouteKey(httpsHR8): httpsRouteHR8,
17471725
}
@@ -1792,17 +1770,15 @@ func TestBuildConfiguration(t *testing.T) {
17921770
},
17931771
{
17941772
graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph {
1795-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
1796-
{
1797-
Name: "listener-443",
1798-
Source: listener443,
1799-
Valid: true,
1800-
Routes: map[graph.RouteKey]*graph.L7Route{
1801-
graph.CreateRouteKey(httpsHR9): httpsRouteHR9,
1802-
},
1803-
ResolvedSecret: &secret1NsName,
1773+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
1774+
Name: "listener-443",
1775+
Source: listener443,
1776+
Valid: true,
1777+
Routes: map[graph.RouteKey]*graph.L7Route{
1778+
graph.CreateRouteKey(httpsHR9): httpsRouteHR9,
18041779
},
1805-
}...)
1780+
ResolvedSecret: &secret1NsName,
1781+
})
18061782
g.Routes = map[graph.RouteKey]*graph.L7Route{
18071783
graph.CreateRouteKey(httpsHR9): httpsRouteHR9,
18081784
}
@@ -1857,14 +1833,12 @@ func TestBuildConfiguration(t *testing.T) {
18571833
Name: "gw",
18581834
Namespace: "ns",
18591835
}
1860-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
1861-
{
1862-
Name: "listener-80-1",
1863-
Source: listener80,
1864-
Valid: true,
1865-
Routes: map[graph.RouteKey]*graph.L7Route{},
1866-
},
1867-
}...)
1836+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
1837+
Name: "listener-80-1",
1838+
Source: listener80,
1839+
Valid: true,
1840+
Routes: map[graph.RouteKey]*graph.L7Route{},
1841+
})
18681842
g.NginxProxy = nginxProxy
18691843
return g
18701844
}),
@@ -1890,14 +1864,12 @@ func TestBuildConfiguration(t *testing.T) {
18901864
Name: "gw",
18911865
Namespace: "ns",
18921866
}
1893-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
1894-
{
1895-
Name: "listener-80-1",
1896-
Source: listener80,
1897-
Valid: true,
1898-
Routes: map[graph.RouteKey]*graph.L7Route{},
1899-
},
1900-
}...)
1867+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
1868+
Name: "listener-80-1",
1869+
Source: listener80,
1870+
Valid: true,
1871+
Routes: map[graph.RouteKey]*graph.L7Route{},
1872+
})
19011873
g.NginxProxy = &graph.NginxProxy{
19021874
Valid: false,
19031875
Source: &ngfAPI.NginxProxy{
@@ -2077,14 +2049,12 @@ func TestBuildConfiguration(t *testing.T) {
20772049
Name: "gw",
20782050
Namespace: "ns",
20792051
}
2080-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
2081-
{
2082-
Name: "listener-80-1",
2083-
Source: listener80,
2084-
Valid: true,
2085-
Routes: map[graph.RouteKey]*graph.L7Route{},
2086-
},
2087-
}...)
2052+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
2053+
Name: "listener-80-1",
2054+
Source: listener80,
2055+
Valid: true,
2056+
Routes: map[graph.RouteKey]*graph.L7Route{},
2057+
})
20882058
g.NginxProxy = nginxProxyIPv4
20892059
return g
20902060
}),
@@ -2102,14 +2072,12 @@ func TestBuildConfiguration(t *testing.T) {
21022072
Name: "gw",
21032073
Namespace: "ns",
21042074
}
2105-
g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{
2106-
{
2107-
Name: "listener-80-1",
2108-
Source: listener80,
2109-
Valid: true,
2110-
Routes: map[graph.RouteKey]*graph.L7Route{},
2111-
},
2112-
}...)
2075+
g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{
2076+
Name: "listener-80-1",
2077+
Source: listener80,
2078+
Valid: true,
2079+
Routes: map[graph.RouteKey]*graph.L7Route{},
2080+
})
21132081
g.NginxProxy = nginxProxyIPv6
21142082
return g
21152083
}),

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func createBackendRef(
154154
ns = string(*ref.Namespace)
155155
}
156156
svcNsName := types.NamespacedName{Name: string(ref.BackendRef.Name), Namespace: ns}
157-
svcIPFamily, svcPort, err := getServiceAndPortFromRef(ref.BackendRef, sourceNamespace, services, refPath)
157+
svcIPFamily, svcPort, err := getIPFamilyAndPortFromRef(ref.BackendRef, svcNsName, services, refPath)
158158
if err != nil {
159159
backendRef = BackendRef{
160160
Weight: weight,
@@ -293,22 +293,16 @@ func findBackendTLSPolicyForService(
293293
return beTLSPolicy, err
294294
}
295295

296-
// getServiceAndPortFromRef extracts the NamespacedName of the Service and the port from a BackendRef.
296+
// getIPFamilyAndPortFromRef extracts the IPFamily of the Service and the port from a BackendRef.
297297
// It can return an error and an empty v1.ServicePort in two cases:
298298
// 1. The Service referenced from the BackendRef does not exist in the cluster/state.
299299
// 2. The Port on the BackendRef does not match any of the ServicePorts on the Service.
300-
func getServiceAndPortFromRef(
300+
func getIPFamilyAndPortFromRef(
301301
ref gatewayv1.BackendRef,
302-
routeNamespace string,
302+
svcNsName types.NamespacedName,
303303
services map[types.NamespacedName]*v1.Service,
304304
refPath *field.Path,
305305
) ([]v1.IPFamily, v1.ServicePort, error) {
306-
ns := routeNamespace
307-
if ref.Namespace != nil {
308-
ns = string(*ref.Namespace)
309-
}
310-
311-
svcNsName := types.NamespacedName{Name: string(ref.Name), Namespace: ns}
312306
svc, ok := services[svcNsName]
313307
if !ok {
314308
return []v1.IPFamily{}, v1.ServicePort{}, field.NotFound(refPath.Child("name"), ref.Name)
@@ -334,14 +328,16 @@ func verifyIPFamily(npCfg *NginxProxy, svcIPFamily []v1.IPFamily) error {
334328
if slices.Contains(svcIPFamily, v1.IPv6Protocol) {
335329
// capitalizing error message to match the rest of the error messages associated with a condition
336330
return errors.New( //nolint: stylecheck
337-
"Service configured with IPv6 family but NginxProxy is configured with IPv4")
331+
"Service configured with IPv6 family but NginxProxy is configured with IPv4",
332+
)
338333
}
339334
}
340335
if *npIPFamily == ngfAPI.IPv6 {
341336
if slices.Contains(svcIPFamily, v1.IPv4Protocol) {
342337
// capitalizing error message to match the rest of the error messages associated with a condition
343338
return errors.New( //nolint: stylecheck
344-
"Service configured with IPv4 family but NginxProxy is configured with IPv6")
339+
"Service configured with IPv4 family but NginxProxy is configured with IPv6",
340+
)
345341
}
346342
}
347343

0 commit comments

Comments
 (0)