diff --git a/internal/mode/static/state/dataplane/configuration_test.go b/internal/mode/static/state/dataplane/configuration_test.go index 273bd2e07..83d5873ce 100644 --- a/internal/mode/static/state/dataplane/configuration_test.go +++ b/internal/mode/static/state/dataplane/configuration_test.go @@ -36,10 +36,6 @@ func getNormalBackendRef() graph.BackendRef { } } -func getModifiedBackendRef(mod func(ref graph.BackendRef) graph.BackendRef) graph.BackendRef { - return mod(getNormalBackendRef()) -} - func getExpectedConfiguration() Configuration { return Configuration{ BaseHTTPConfig: BaseHTTPConfig{HTTP2: true, IPFamily: Dual}, @@ -183,9 +179,7 @@ func TestBuildConfiguration(t *testing.T) { fakeResolver := &resolverfakes.FakeServiceResolver{} fakeResolver.ResolveReturns(fooEndpoints, nil) - validBackendRef := getModifiedBackendRef(func(backend graph.BackendRef) graph.BackendRef { - return backend - }) + validBackendRef := getNormalBackendRef() expValidBackend := Backend{ UpstreamName: fooUpstreamName, @@ -837,14 +831,12 @@ func TestBuildConfiguration(t *testing.T) { }, { graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph { - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "invalid-listener", - Source: invalidListener, - Valid: false, - ResolvedSecret: &secret1NsName, - }, - }...) + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "invalid-listener", + Source: invalidListener, + Valid: false, + ResolvedSecret: &secret1NsName, + }) g.Routes = map[graph.RouteKey]*graph.L7Route{ graph.CreateRouteKey(httpsHR1): httpsRouteHR1, graph.CreateRouteKey(httpsHR2): httpsRouteHR2, @@ -862,17 +854,15 @@ func TestBuildConfiguration(t *testing.T) { }, { graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph { - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-80-1", - Source: listener80, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{ - graph.CreateRouteKey(hr1): routeHR1, - graph.CreateRouteKey(hr2): routeHR2, - }, + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-80-1", + Source: listener80, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{ + graph.CreateRouteKey(hr1): routeHR1, + graph.CreateRouteKey(hr2): routeHR2, }, - }...) + }) g.Routes = map[graph.RouteKey]*graph.L7Route{ graph.CreateRouteKey(hr1): routeHR1, graph.CreateRouteKey(hr2): routeHR2, @@ -925,16 +915,14 @@ func TestBuildConfiguration(t *testing.T) { }, { graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph { - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-80-1", - Source: listener80, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{ - graph.CreateRouteKey(gr): routeGR, - }, + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-80-1", + Source: listener80, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{ + graph.CreateRouteKey(gr): routeGR, }, - }...) + }) g.Routes[graph.CreateRouteKey(gr)] = routeGR return g }), @@ -1411,16 +1399,14 @@ func TestBuildConfiguration(t *testing.T) { { graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph { g.GatewayClass.Valid = false - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-80-1", - Source: listener80, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{ - graph.CreateRouteKey(hr1): routeHR1, - }, + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-80-1", + Source: listener80, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{ + graph.CreateRouteKey(hr1): routeHR1, }, - }...) + }) g.Routes = map[graph.RouteKey]*graph.L7Route{ graph.CreateRouteKey(hr1): routeHR1, } @@ -1432,16 +1418,14 @@ func TestBuildConfiguration(t *testing.T) { { graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph { g.GatewayClass.Valid = false - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-80-1", - Source: listener80, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{ - graph.CreateRouteKey(hr1): routeHR1, - }, + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-80-1", + Source: listener80, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{ + graph.CreateRouteKey(hr1): routeHR1, }, - }...) + }) g.Routes = map[graph.RouteKey]*graph.L7Route{ graph.CreateRouteKey(hr1): routeHR1, } @@ -1460,16 +1444,14 @@ func TestBuildConfiguration(t *testing.T) { }, { graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph { - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-80-1", - Source: listener80, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{ - graph.CreateRouteKey(hr5): routeHR5, - }, + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-80-1", + Source: listener80, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{ + graph.CreateRouteKey(hr5): routeHR5, }, - }...) + }) g.Routes = map[graph.RouteKey]*graph.L7Route{ graph.CreateRouteKey(hr5): routeHR5, } @@ -1599,16 +1581,14 @@ func TestBuildConfiguration(t *testing.T) { }, { graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph { - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-80-1", - Source: listener80, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{ - graph.CreateRouteKey(hr7): routeHR7, - }, + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-80-1", + Source: listener80, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{ + graph.CreateRouteKey(hr7): routeHR7, }, - }...) + }) g.Routes = map[graph.RouteKey]*graph.L7Route{ graph.CreateRouteKey(hr7): routeHR7, } @@ -1731,17 +1711,15 @@ func TestBuildConfiguration(t *testing.T) { }, { graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph { - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-443", - Source: listener443, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{ - graph.CreateRouteKey(httpsHR8): httpsRouteHR8, - }, - ResolvedSecret: &secret1NsName, + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-443", + Source: listener443, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{ + graph.CreateRouteKey(httpsHR8): httpsRouteHR8, }, - }...) + ResolvedSecret: &secret1NsName, + }) g.Routes = map[graph.RouteKey]*graph.L7Route{ graph.CreateRouteKey(httpsHR8): httpsRouteHR8, } @@ -1792,17 +1770,15 @@ func TestBuildConfiguration(t *testing.T) { }, { graph: getModifiedGraph(func(g *graph.Graph) *graph.Graph { - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-443", - Source: listener443, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{ - graph.CreateRouteKey(httpsHR9): httpsRouteHR9, - }, - ResolvedSecret: &secret1NsName, + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-443", + Source: listener443, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{ + graph.CreateRouteKey(httpsHR9): httpsRouteHR9, }, - }...) + ResolvedSecret: &secret1NsName, + }) g.Routes = map[graph.RouteKey]*graph.L7Route{ graph.CreateRouteKey(httpsHR9): httpsRouteHR9, } @@ -1857,14 +1833,12 @@ func TestBuildConfiguration(t *testing.T) { Name: "gw", Namespace: "ns", } - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-80-1", - Source: listener80, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{}, - }, - }...) + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-80-1", + Source: listener80, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{}, + }) g.NginxProxy = nginxProxy return g }), @@ -1890,14 +1864,12 @@ func TestBuildConfiguration(t *testing.T) { Name: "gw", Namespace: "ns", } - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-80-1", - Source: listener80, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{}, - }, - }...) + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-80-1", + Source: listener80, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{}, + }) g.NginxProxy = &graph.NginxProxy{ Valid: false, Source: &ngfAPI.NginxProxy{ @@ -2077,14 +2049,12 @@ func TestBuildConfiguration(t *testing.T) { Name: "gw", Namespace: "ns", } - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-80-1", - Source: listener80, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{}, - }, - }...) + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-80-1", + Source: listener80, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{}, + }) g.NginxProxy = nginxProxyIPv4 return g }), @@ -2102,14 +2072,12 @@ func TestBuildConfiguration(t *testing.T) { Name: "gw", Namespace: "ns", } - g.Gateway.Listeners = append(g.Gateway.Listeners, []*graph.Listener{ - { - Name: "listener-80-1", - Source: listener80, - Valid: true, - Routes: map[graph.RouteKey]*graph.L7Route{}, - }, - }...) + g.Gateway.Listeners = append(g.Gateway.Listeners, &graph.Listener{ + Name: "listener-80-1", + Source: listener80, + Valid: true, + Routes: map[graph.RouteKey]*graph.L7Route{}, + }) g.NginxProxy = nginxProxyIPv6 return g }), diff --git a/internal/mode/static/state/graph/backend_refs.go b/internal/mode/static/state/graph/backend_refs.go index 200dc71a0..2b79569dc 100644 --- a/internal/mode/static/state/graph/backend_refs.go +++ b/internal/mode/static/state/graph/backend_refs.go @@ -154,7 +154,7 @@ func createBackendRef( ns = string(*ref.Namespace) } svcNsName := types.NamespacedName{Name: string(ref.BackendRef.Name), Namespace: ns} - svcIPFamily, svcPort, err := getServiceAndPortFromRef(ref.BackendRef, sourceNamespace, services, refPath) + svcIPFamily, svcPort, err := getIPFamilyAndPortFromRef(ref.BackendRef, svcNsName, services, refPath) if err != nil { backendRef = BackendRef{ Weight: weight, @@ -293,22 +293,16 @@ func findBackendTLSPolicyForService( return beTLSPolicy, err } -// getServiceAndPortFromRef extracts the NamespacedName of the Service and the port from a BackendRef. +// getIPFamilyAndPortFromRef extracts the IPFamily of the Service and the port from a BackendRef. // It can return an error and an empty v1.ServicePort in two cases: // 1. The Service referenced from the BackendRef does not exist in the cluster/state. // 2. The Port on the BackendRef does not match any of the ServicePorts on the Service. -func getServiceAndPortFromRef( +func getIPFamilyAndPortFromRef( ref gatewayv1.BackendRef, - routeNamespace string, + svcNsName types.NamespacedName, services map[types.NamespacedName]*v1.Service, refPath *field.Path, ) ([]v1.IPFamily, v1.ServicePort, error) { - ns := routeNamespace - if ref.Namespace != nil { - ns = string(*ref.Namespace) - } - - svcNsName := types.NamespacedName{Name: string(ref.Name), Namespace: ns} svc, ok := services[svcNsName] if !ok { return []v1.IPFamily{}, v1.ServicePort{}, field.NotFound(refPath.Child("name"), ref.Name) @@ -334,14 +328,16 @@ func verifyIPFamily(npCfg *NginxProxy, svcIPFamily []v1.IPFamily) error { if slices.Contains(svcIPFamily, v1.IPv6Protocol) { // capitalizing error message to match the rest of the error messages associated with a condition return errors.New( //nolint: stylecheck - "Service configured with IPv6 family but NginxProxy is configured with IPv4") + "Service configured with IPv6 family but NginxProxy is configured with IPv4", + ) } } if *npIPFamily == ngfAPI.IPv6 { if slices.Contains(svcIPFamily, v1.IPv4Protocol) { // capitalizing error message to match the rest of the error messages associated with a condition return errors.New( //nolint: stylecheck - "Service configured with IPv4 family but NginxProxy is configured with IPv6") + "Service configured with IPv4 family but NginxProxy is configured with IPv6", + ) } } diff --git a/internal/mode/static/state/graph/backend_refs_test.go b/internal/mode/static/state/graph/backend_refs_test.go index caafae5f2..c260408c4 100644 --- a/internal/mode/static/state/graph/backend_refs_test.go +++ b/internal/mode/static/state/graph/backend_refs_test.go @@ -256,7 +256,7 @@ func TestValidateWeight(t *testing.T) { } } -func TestGetServiceAndPortFromRef(t *testing.T) { +func TestGetIPFamilyAndPortFromRef(t *testing.T) { svc1 := &v1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: "service1", @@ -280,8 +280,8 @@ func TestGetServiceAndPortFromRef(t *testing.T) { } tests := []struct { - expSvc *v1.Service ref gatewayv1.BackendRef + svcNsName types.NamespacedName expSvcIPFamily []v1.IPFamily name string expServicePort v1.ServicePort @@ -292,6 +292,7 @@ func TestGetServiceAndPortFromRef(t *testing.T) { ref: getNormalRef(), expServicePort: v1.ServicePort{Port: 80}, expSvcIPFamily: []v1.IPFamily{v1.IPv4Protocol}, + svcNsName: types.NamespacedName{Namespace: "test", Name: "service1"}, }, { name: "service does not exist", @@ -301,12 +302,8 @@ func TestGetServiceAndPortFromRef(t *testing.T) { }), expErr: true, expServicePort: v1.ServicePort{}, - expSvc: &v1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Name: "does-not-exist", Namespace: "test", - }, - }, expSvcIPFamily: []v1.IPFamily{}, + svcNsName: types.NamespacedName{Namespace: "test", Name: "does-not-exist"}, }, { name: "no matching port for service and port", @@ -316,12 +313,8 @@ func TestGetServiceAndPortFromRef(t *testing.T) { }), expErr: true, expServicePort: v1.ServicePort{}, - expSvc: &v1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Name: "service1", Namespace: "test", - }, - }, expSvcIPFamily: []v1.IPFamily{}, + svcNsName: types.NamespacedName{Namespace: "test", Name: "service1"}, }, } @@ -336,7 +329,7 @@ func TestGetServiceAndPortFromRef(t *testing.T) { t.Run(test.name, func(t *testing.T) { g := NewWithT(t) - svcIPFamily, servicePort, err := getServiceAndPortFromRef(test.ref, "test", services, refPath) + svcIPFamily, servicePort, err := getIPFamilyAndPortFromRef(test.ref, test.svcNsName, services, refPath) g.Expect(err != nil).To(Equal(test.expErr)) g.Expect(servicePort).To(Equal(test.expServicePort)) @@ -404,7 +397,6 @@ func TestVerifyIPFamily(t *testing.T) { }, { name: "Valid - When NginxProxy is nil", - npCfg: &NginxProxy{}, svcIPFamily: []v1.IPFamily{v1.IPv4Protocol}, }, } @@ -767,7 +759,7 @@ func TestAddBackendRefsToRulesTest(t *testing.T) { t.Run(test.name, func(t *testing.T) { g := NewWithT(t) resolver := newReferenceGrantResolver(nil) - addBackendRefsToRules(test.route, resolver, services, test.policies, &NginxProxy{}) + addBackendRefsToRules(test.route, resolver, services, test.policies, nil) var actual []BackendRef if test.route.Spec.Rules != nil { diff --git a/site/content/how-to/monitoring/troubleshooting.md b/site/content/how-to/monitoring/troubleshooting.md index a7c15ed8c..e15bee6a1 100644 --- a/site/content/how-to/monitoring/troubleshooting.md +++ b/site/content/how-to/monitoring/troubleshooting.md @@ -418,7 +418,7 @@ If you `describe` your HTTPRoute and see the following error: Status: True Type: Accepted Last Transition Time: 2024-07-14T23:36:37Z - Message: service configured with IPv4 family but NginxProxy is configured with IPv6 + Message: Service configured with IPv4 family but NginxProxy is configured with IPv6 Observed Generation: 1 Reason: InvalidServiceIPFamily Status: False