Skip to content

Commit

Permalink
Revert reference check
Browse files Browse the repository at this point in the history
  • Loading branch information
sjberman committed Apr 26, 2024
1 parent 020ea6b commit 2e10400
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 102 deletions.
4 changes: 2 additions & 2 deletions internal/mode/static/state/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
25 changes: 6 additions & 19 deletions internal/mode/static/state/graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -662,7 +656,6 @@ func TestIsReferenced(t *testing.T) {
CACert: []byte(caBlock),
},
},
NginxProxy: npInGraph,
}

tests := []struct {
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions internal/mode/static/state/graph/nginxproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down
99 changes: 24 additions & 75 deletions internal/mode/static/state/graph/nginxproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,115 +85,64 @@ 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",
},
}

for _, test := range tests {
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))
})
}
}
Expand Down

0 comments on commit 2e10400

Please sign in to comment.