@@ -13,12 +13,10 @@ import (
1313 gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
1414 "sigs.k8s.io/gateway-api/apis/v1alpha2"
1515 "sigs.k8s.io/gateway-api/apis/v1alpha3"
16- "sigs.k8s.io/gateway-api/apis/v1beta1"
1716
1817 ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
1918 "github.com/nginxinc/nginx-gateway-fabric/internal/framework/conditions"
2019 "github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
21- "github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
2220 staticConds "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/conditions"
2321)
2422
@@ -88,9 +86,9 @@ func TestValidateRouteBackendRef(t *testing.T) {
8886 for _ , test := range tests {
8987 t .Run (test .name , func (t * testing.T ) {
9088 g := NewWithT (t )
91- resolver := newReferenceGrantResolver ( nil )
89+ alwaysTrueRefGrantResolver := func ( _ toResource ) bool { return true }
9290
93- valid , cond := validateRouteBackendRef (test .ref , "test" , resolver , field .NewPath ("test" ))
91+ valid , cond := validateRouteBackendRef (test .ref , "test" , alwaysTrueRefGrantResolver , field .NewPath ("test" ))
9492
9593 g .Expect (valid ).To (Equal (test .expectedValid ))
9694 g .Expect (cond ).To (Equal (test .expectedCondition ))
@@ -99,84 +97,57 @@ func TestValidateRouteBackendRef(t *testing.T) {
9997}
10098
10199func TestValidateBackendRef (t * testing.T ) {
102- specificRefGrant := & v1beta1.ReferenceGrant {
103- Spec : v1beta1.ReferenceGrantSpec {
104- To : []v1beta1.ReferenceGrantTo {
105- {
106- Kind : "Service" ,
107- Name : helpers.GetPointer [gatewayv1.ObjectName ]("service1" ),
108- },
109- },
110- From : []v1beta1.ReferenceGrantFrom {
111- {
112- Group : gatewayv1 .GroupName ,
113- Kind : kinds .HTTPRoute ,
114- Namespace : "test" ,
115- },
116- },
117- },
118- }
119-
120- allInNamespaceRefGrant := specificRefGrant .DeepCopy ()
121- allInNamespaceRefGrant .Spec .To [0 ].Name = nil
100+ alwaysFalseRefGrantResolver := func (_ toResource ) bool { return false }
101+ alwaysTrueRefGrantResolver := func (_ toResource ) bool { return true }
122102
123103 tests := []struct {
124104 ref gatewayv1.BackendRef
125- refGrants map [types. NamespacedName ] * v1beta1. ReferenceGrant
105+ refGrantResolver func ( resource toResource ) bool
126106 expectedCondition conditions.Condition
127107 name string
128108 expectedValid bool
129109 }{
130110 {
131- name : "normal case" ,
132- ref : getNormalRef (),
133- expectedValid : true ,
111+ name : "normal case" ,
112+ ref : getNormalRef (),
113+ refGrantResolver : alwaysTrueRefGrantResolver ,
114+ expectedValid : true ,
134115 },
135116 {
136117 name : "normal case with implicit namespace" ,
137118 ref : getModifiedRef (func (backend gatewayv1.BackendRef ) gatewayv1.BackendRef {
138119 backend .Namespace = nil
139120 return backend
140121 }),
141- expectedValid : true ,
122+ refGrantResolver : alwaysTrueRefGrantResolver ,
123+ expectedValid : true ,
142124 },
143125 {
144126 name : "normal case with implicit kind Service" ,
145127 ref : getModifiedRef (func (backend gatewayv1.BackendRef ) gatewayv1.BackendRef {
146128 backend .Kind = nil
147129 return backend
148130 }),
149- expectedValid : true ,
131+ refGrantResolver : alwaysTrueRefGrantResolver ,
132+ expectedValid : true ,
150133 },
151134 {
152- name : "normal case with backend ref allowed by specific reference grant" ,
135+ name : "normal case with backend ref allowed by reference grant" ,
153136 ref : getModifiedRef (func (backend gatewayv1.BackendRef ) gatewayv1.BackendRef {
154137 backend .Namespace = helpers.GetPointer [gatewayv1.Namespace ]("cross-ns" )
155138 return backend
156139 }),
157- refGrants : map [types.NamespacedName ]* v1beta1.ReferenceGrant {
158- {Namespace : "cross-ns" , Name : "rg" }: specificRefGrant ,
159- },
160- expectedValid : true ,
161- },
162- {
163- name : "normal case with backend ref allowed by all-in-namespace reference grant" ,
164- ref : getModifiedRef (func (backend gatewayv1.BackendRef ) gatewayv1.BackendRef {
165- backend .Namespace = helpers.GetPointer [gatewayv1.Namespace ]("cross-ns" )
166- return backend
167- }),
168- refGrants : map [types.NamespacedName ]* v1beta1.ReferenceGrant {
169- {Namespace : "cross-ns" , Name : "rg" }: allInNamespaceRefGrant ,
170- },
171- expectedValid : true ,
140+ refGrantResolver : alwaysTrueRefGrantResolver ,
141+ expectedValid : true ,
172142 },
173143 {
174144 name : "invalid group" ,
175145 ref : getModifiedRef (func (backend gatewayv1.BackendRef ) gatewayv1.BackendRef {
176146 backend .Group = helpers.GetPointer [gatewayv1.Group ]("invalid" )
177147 return backend
178148 }),
179- expectedValid : false ,
149+ refGrantResolver : alwaysTrueRefGrantResolver ,
150+ expectedValid : false ,
180151 expectedCondition : staticConds .NewRouteBackendRefInvalidKind (
181152 `test.group: Unsupported value: "invalid": supported values: "core", ""` ,
182153 ),
@@ -187,7 +158,8 @@ func TestValidateBackendRef(t *testing.T) {
187158 backend .Kind = helpers.GetPointer [gatewayv1.Kind ]("NotService" )
188159 return backend
189160 }),
190- expectedValid : false ,
161+ refGrantResolver : alwaysTrueRefGrantResolver ,
162+ expectedValid : false ,
191163 expectedCondition : staticConds .NewRouteBackendRefInvalidKind (
192164 `test.kind: Unsupported value: "NotService": supported values: "Service"` ,
193165 ),
@@ -198,7 +170,8 @@ func TestValidateBackendRef(t *testing.T) {
198170 backend .Namespace = helpers.GetPointer [gatewayv1.Namespace ]("invalid" )
199171 return backend
200172 }),
201- expectedValid : false ,
173+ refGrantResolver : alwaysFalseRefGrantResolver ,
174+ expectedValid : false ,
202175 expectedCondition : staticConds .NewRouteBackendRefRefNotPermitted (
203176 "Backend ref to Service invalid/service1 not permitted by any ReferenceGrant" ,
204177 ),
@@ -209,7 +182,8 @@ func TestValidateBackendRef(t *testing.T) {
209182 backend .Weight = helpers.GetPointer [int32 ](- 1 )
210183 return backend
211184 }),
212- expectedValid : false ,
185+ refGrantResolver : alwaysTrueRefGrantResolver ,
186+ expectedValid : false ,
213187 expectedCondition : staticConds .NewRouteBackendRefUnsupportedValue (
214188 "test.weight: Invalid value: -1: must be in the range [0, 1000000]" ,
215189 ),
@@ -220,7 +194,8 @@ func TestValidateBackendRef(t *testing.T) {
220194 backend .Port = nil
221195 return backend
222196 }),
223- expectedValid : false ,
197+ refGrantResolver : alwaysTrueRefGrantResolver ,
198+ expectedValid : false ,
224199 expectedCondition : staticConds .NewRouteBackendRefUnsupportedValue (
225200 "test.port: Required value: port cannot be nil" ,
226201 ),
@@ -231,8 +206,7 @@ func TestValidateBackendRef(t *testing.T) {
231206 t .Run (test .name , func (t * testing.T ) {
232207 g := NewWithT (t )
233208
234- resolver := newReferenceGrantResolver (test .refGrants )
235- valid , cond := validateBackendRef (test .ref , "test" , resolver , field .NewPath ("test" ))
209+ valid , cond := validateBackendRef (test .ref , "test" , test .refGrantResolver , field .NewPath ("test" ))
236210
237211 g .Expect (valid ).To (Equal (test .expectedValid ))
238212 g .Expect (cond ).To (Equal (test .expectedCondition ))
@@ -437,6 +411,7 @@ func TestAddBackendRefsToRulesTest(t *testing.T) {
437411 Name : name ,
438412 },
439413 },
414+ RouteType : RouteTypeHTTP ,
440415 ParentRefs : sectionNameRefs ,
441416 Valid : true ,
442417 }
@@ -1034,7 +1009,7 @@ func TestCreateBackend(t *testing.T) {
10341009 t .Run (test .name , func (t * testing.T ) {
10351010 g := NewWithT (t )
10361011
1037- resolver := newReferenceGrantResolver ( nil )
1012+ alwaysTrueRefGrantResolver := func ( _ toResource ) bool { return true }
10381013
10391014 rbr := RouteBackendRef {
10401015 test .ref .BackendRef ,
@@ -1043,7 +1018,7 @@ func TestCreateBackend(t *testing.T) {
10431018 backend , cond := createBackendRef (
10441019 rbr ,
10451020 sourceNamespace ,
1046- resolver ,
1021+ alwaysTrueRefGrantResolver ,
10471022 services ,
10481023 refPath ,
10491024 policies ,
@@ -1265,3 +1240,42 @@ func TestFindBackendTLSPolicyForService(t *testing.T) {
12651240 })
12661241 }
12671242}
1243+
1244+ func TestGetRefGrantFromResourceForRoute (t * testing.T ) {
1245+ tests := []struct {
1246+ name string
1247+ routeType RouteType
1248+ ns string
1249+ expFromResource fromResource
1250+ }{
1251+ {
1252+ name : "HTTPRoute" ,
1253+ routeType : RouteTypeHTTP ,
1254+ ns : "hr" ,
1255+ expFromResource : fromHTTPRoute ("hr" ),
1256+ },
1257+ {
1258+ name : "GRPCRoute" ,
1259+ routeType : RouteTypeGRPC ,
1260+ ns : "gr" ,
1261+ expFromResource : fromGRPCRoute ("gr" ),
1262+ },
1263+ }
1264+
1265+ for _ , test := range tests {
1266+ t .Run (test .name , func (t * testing.T ) {
1267+ g := NewWithT (t )
1268+ g .Expect (getRefGrantFromResourceForRoute (test .routeType , test .ns )).To (Equal (test .expFromResource ))
1269+ })
1270+ }
1271+ }
1272+
1273+ func TestGetRefGrantFromResourceForRoute_Panics (t * testing.T ) {
1274+ g := NewWithT (t )
1275+
1276+ get := func () {
1277+ getRefGrantFromResourceForRoute ("unknown" , "ns" )
1278+ }
1279+
1280+ g .Expect (get ).To (Panic ())
1281+ }
0 commit comments