diff --git a/internal/mode/static/state/graph/graph.go b/internal/mode/static/state/graph/graph.go index a08d00de86..af60f41a20 100644 --- a/internal/mode/static/state/graph/graph.go +++ b/internal/mode/static/state/graph/graph.go @@ -103,9 +103,9 @@ func (g *Graph) IsReferenced(resourceType client.Object, nsname types.Namespaced // Service Namespace should be the same Namespace as the EndpointSlice _, exists := g.ReferencedServices[types.NamespacedName{Namespace: nsname.Namespace, Name: svcName}] return exists - // Similar to Namespace above, NginxProxy reference exists if it once was or currently is linked to a GatewayClass. + // NginxProxy reference exists if it is linked to a GatewayClass. case *ngfAPI.NginxProxy: - return isNginxProxyReferenced(nsname, g) + return isNginxProxyReferenced(nsname, g.GatewayClass) default: return false } diff --git a/internal/mode/static/state/graph/graph_test.go b/internal/mode/static/state/graph/graph_test.go index 180372f23e..12bfb4f3ca 100644 --- a/internal/mode/static/state/graph/graph_test.go +++ b/internal/mode/static/state/graph/graph_test.go @@ -625,24 +625,18 @@ func TestIsReferenced(t *testing.T) { }, } - npInGraph := &ngfAPI.NginxProxy{ + npNotInGatewayClass := &ngfAPI.NginxProxy{ ObjectMeta: metav1.ObjectMeta{ Name: "nginx-proxy", }, } - npNotInGraphButInGatewayClass := &ngfAPI.NginxProxy{ + npInGatewayClass := &ngfAPI.NginxProxy{ ObjectMeta: metav1.ObjectMeta{ Name: "nginx-proxy-in-gc", }, } - npNotInGraph := &ngfAPI.NginxProxy{ - ObjectMeta: metav1.ObjectMeta{ - Name: "nginx-proxy-not-referenced", - }, - } - graph := &Graph{ Gateway: gw, ReferencedSecrets: map[types.NamespacedName]*Secret{ @@ -662,7 +656,6 @@ func TestIsReferenced(t *testing.T) { CACert: []byte(caBlock), }, }, - NginxProxy: npInGraph, } tests := []struct { @@ -781,21 +774,15 @@ func TestIsReferenced(t *testing.T) { // NginxProxy tests { - name: "NginxProxy in the Graph is referenced", - resource: npInGraph, - graph: graph, - expected: true, - }, - { - name: "NginxProxy is not yet in Graph but is referenced in GatewayClass", - resource: npNotInGraphButInGatewayClass, + name: "NginxProxy is referenced in GatewayClass", + resource: npInGatewayClass, gc: gcWithNginxProxy, graph: graph, expected: true, }, { - name: "NginxProxy not in Graph or referenced in GatewayClass", - resource: npNotInGraph, + name: "NginxProxy is not referenced in GatewayClass", + resource: npNotInGatewayClass, gc: gcWithNginxProxy, graph: graph, expected: false, diff --git a/internal/mode/static/state/graph/nginxproxy.go b/internal/mode/static/state/graph/nginxproxy.go index a367fffad0..73427594ee 100644 --- a/internal/mode/static/state/graph/nginxproxy.go +++ b/internal/mode/static/state/graph/nginxproxy.go @@ -3,7 +3,6 @@ package graph import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/validation/field" - "sigs.k8s.io/controller-runtime/pkg/client" v1 "sigs.k8s.io/gateway-api/apis/v1" ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1" @@ -23,11 +22,8 @@ func getNginxProxy( } // isNginxProxyReferenced returns whether or not a specific NginxProxy is referenced in the GatewayClass. -func isNginxProxyReferenced(npNSName types.NamespacedName, g *Graph) bool { - existed := g.NginxProxy != nil && npNSName == client.ObjectKeyFromObject(g.NginxProxy) - gc := g.GatewayClass - exists := gc != nil && gcReferencesAnyNginxProxy(gc.Source) && gc.Source.Spec.ParametersRef.Name == npNSName.Name - return existed || exists +func isNginxProxyReferenced(npNSName types.NamespacedName, gc *GatewayClass) bool { + return gc != nil && gcReferencesAnyNginxProxy(gc.Source) && gc.Source.Spec.ParametersRef.Name == npNSName.Name } // gcReferencesNginxProxy returns whether a GatewayClass references any NginxProxy resource. diff --git a/internal/mode/static/state/graph/nginxproxy_test.go b/internal/mode/static/state/graph/nginxproxy_test.go index fd99b246a9..add1c55204 100644 --- a/internal/mode/static/state/graph/nginxproxy_test.go +++ b/internal/mode/static/state/graph/nginxproxy_test.go @@ -85,107 +85,56 @@ func TestGetNginxProxy(t *testing.T) { func TestIsNginxProxyReferenced(t *testing.T) { tests := []struct { - g *Graph + gc *GatewayClass npName types.NamespacedName name string expRes bool }{ { - g: &Graph{ - GatewayClass: &GatewayClass{ - Source: &v1.GatewayClass{ - Spec: v1.GatewayClassSpec{ - ParametersRef: &v1.ParametersReference{ - Group: ngfAPI.GroupName, - Kind: v1.Kind("NginxProxy"), - Name: "nginx-proxy", - }, + gc: &GatewayClass{ + Source: &v1.GatewayClass{ + Spec: v1.GatewayClassSpec{ + ParametersRef: &v1.ParametersReference{ + Group: ngfAPI.GroupName, + Kind: v1.Kind("NginxProxy"), + Name: "nginx-proxy", }, }, }, }, - npName: types.NamespacedName{Name: "not referenced"}, + npName: types.NamespacedName{}, expRes: false, - name: "nginxproxy not in graph and not referenced in gatewayclass", + name: "nil nginxproxy", }, { - g: &Graph{ - GatewayClass: nil, - NginxProxy: nil, - }, + gc: nil, npName: types.NamespacedName{Name: "nginx-proxy"}, expRes: false, - name: "nil gatewayclass and nginxproxy not in graph", + name: "nil gatewayclass", }, { - g: &Graph{ - GatewayClass: &GatewayClass{ - Source: nil, - }, - NginxProxy: nil, + gc: &GatewayClass{ + Source: nil, }, npName: types.NamespacedName{Name: "nginx-proxy"}, expRes: false, - name: "nil gatewayclass source and nginxproxy not in graph", + name: "nil gatewayclass source", }, { - g: &Graph{ - GatewayClass: &GatewayClass{ - Source: &v1.GatewayClass{ - Spec: v1.GatewayClassSpec{ - ParametersRef: &v1.ParametersReference{ - Group: ngfAPI.GroupName, - Kind: v1.Kind("NginxProxy"), - Name: "nginx-proxy", - }, + gc: &GatewayClass{ + Source: &v1.GatewayClass{ + Spec: v1.GatewayClassSpec{ + ParametersRef: &v1.ParametersReference{ + Group: ngfAPI.GroupName, + Kind: v1.Kind("NginxProxy"), + Name: "nginx-proxy", }, }, }, - NginxProxy: nil, - }, - npName: types.NamespacedName{Name: "nginx-proxy"}, - expRes: true, - name: "nginxproxy not in graph but referenced in gatewayclass", - }, - { - g: &Graph{ - GatewayClass: &GatewayClass{ - Source: &v1.GatewayClass{ - Spec: v1.GatewayClassSpec{}, - }, - }, - NginxProxy: &ngfAPI.NginxProxy{ - ObjectMeta: metav1.ObjectMeta{ - Name: "nginx-proxy", - }, - }, - }, - npName: types.NamespacedName{Name: "nginx-proxy"}, - expRes: true, - name: "nginxproxy in graph but not referenced in gatewayclass", - }, - { - g: &Graph{ - GatewayClass: &GatewayClass{ - Source: &v1.GatewayClass{ - Spec: v1.GatewayClassSpec{ - ParametersRef: &v1.ParametersReference{ - Group: ngfAPI.GroupName, - Kind: v1.Kind("NginxProxy"), - Name: "nginx-proxy", - }, - }, - }, - }, - NginxProxy: &ngfAPI.NginxProxy{ - ObjectMeta: metav1.ObjectMeta{ - Name: "nginx-proxy", - }, - }, }, npName: types.NamespacedName{Name: "nginx-proxy"}, expRes: true, - name: "references the nginxproxy and exists in graph", + name: "references the NginxProxy", }, } @@ -193,7 +142,7 @@ func TestIsNginxProxyReferenced(t *testing.T) { t.Run(test.name, func(t *testing.T) { g := NewWithT(t) - g.Expect(isNginxProxyReferenced(test.npName, test.g)).To(Equal(test.expRes)) + g.Expect(isNginxProxyReferenced(test.npName, test.gc)).To(Equal(test.expRes)) }) } }