@@ -13,12 +13,10 @@ import (
13
13
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
14
14
"sigs.k8s.io/gateway-api/apis/v1alpha2"
15
15
"sigs.k8s.io/gateway-api/apis/v1alpha3"
16
- "sigs.k8s.io/gateway-api/apis/v1beta1"
17
16
18
17
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
19
18
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/conditions"
20
19
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
21
- "github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
22
20
staticConds "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/conditions"
23
21
)
24
22
@@ -88,9 +86,9 @@ func TestValidateRouteBackendRef(t *testing.T) {
88
86
for _ , test := range tests {
89
87
t .Run (test .name , func (t * testing.T ) {
90
88
g := NewWithT (t )
91
- resolver := newReferenceGrantResolver ( nil )
89
+ alwaysTrueRefGrantResolver := func ( _ toResource ) bool { return true }
92
90
93
- valid , cond := validateRouteBackendRef (test .ref , "test" , resolver , field .NewPath ("test" ))
91
+ valid , cond := validateRouteBackendRef (test .ref , "test" , alwaysTrueRefGrantResolver , field .NewPath ("test" ))
94
92
95
93
g .Expect (valid ).To (Equal (test .expectedValid ))
96
94
g .Expect (cond ).To (Equal (test .expectedCondition ))
@@ -99,84 +97,57 @@ func TestValidateRouteBackendRef(t *testing.T) {
99
97
}
100
98
101
99
func 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 }
122
102
123
103
tests := []struct {
124
104
ref gatewayv1.BackendRef
125
- refGrants map [types. NamespacedName ] * v1beta1. ReferenceGrant
105
+ refGrantResolver func ( resource toResource ) bool
126
106
expectedCondition conditions.Condition
127
107
name string
128
108
expectedValid bool
129
109
}{
130
110
{
131
- name : "normal case" ,
132
- ref : getNormalRef (),
133
- expectedValid : true ,
111
+ name : "normal case" ,
112
+ ref : getNormalRef (),
113
+ refGrantResolver : alwaysTrueRefGrantResolver ,
114
+ expectedValid : true ,
134
115
},
135
116
{
136
117
name : "normal case with implicit namespace" ,
137
118
ref : getModifiedRef (func (backend gatewayv1.BackendRef ) gatewayv1.BackendRef {
138
119
backend .Namespace = nil
139
120
return backend
140
121
}),
141
- expectedValid : true ,
122
+ refGrantResolver : alwaysTrueRefGrantResolver ,
123
+ expectedValid : true ,
142
124
},
143
125
{
144
126
name : "normal case with implicit kind Service" ,
145
127
ref : getModifiedRef (func (backend gatewayv1.BackendRef ) gatewayv1.BackendRef {
146
128
backend .Kind = nil
147
129
return backend
148
130
}),
149
- expectedValid : true ,
131
+ refGrantResolver : alwaysTrueRefGrantResolver ,
132
+ expectedValid : true ,
150
133
},
151
134
{
152
- name : "normal case with backend ref allowed by specific reference grant" ,
135
+ name : "normal case with backend ref allowed by reference grant" ,
153
136
ref : getModifiedRef (func (backend gatewayv1.BackendRef ) gatewayv1.BackendRef {
154
137
backend .Namespace = helpers.GetPointer [gatewayv1.Namespace ]("cross-ns" )
155
138
return backend
156
139
}),
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 ,
172
142
},
173
143
{
174
144
name : "invalid group" ,
175
145
ref : getModifiedRef (func (backend gatewayv1.BackendRef ) gatewayv1.BackendRef {
176
146
backend .Group = helpers.GetPointer [gatewayv1.Group ]("invalid" )
177
147
return backend
178
148
}),
179
- expectedValid : false ,
149
+ refGrantResolver : alwaysTrueRefGrantResolver ,
150
+ expectedValid : false ,
180
151
expectedCondition : staticConds .NewRouteBackendRefInvalidKind (
181
152
`test.group: Unsupported value: "invalid": supported values: "core", ""` ,
182
153
),
@@ -187,7 +158,8 @@ func TestValidateBackendRef(t *testing.T) {
187
158
backend .Kind = helpers.GetPointer [gatewayv1.Kind ]("NotService" )
188
159
return backend
189
160
}),
190
- expectedValid : false ,
161
+ refGrantResolver : alwaysTrueRefGrantResolver ,
162
+ expectedValid : false ,
191
163
expectedCondition : staticConds .NewRouteBackendRefInvalidKind (
192
164
`test.kind: Unsupported value: "NotService": supported values: "Service"` ,
193
165
),
@@ -198,7 +170,8 @@ func TestValidateBackendRef(t *testing.T) {
198
170
backend .Namespace = helpers.GetPointer [gatewayv1.Namespace ]("invalid" )
199
171
return backend
200
172
}),
201
- expectedValid : false ,
173
+ refGrantResolver : alwaysFalseRefGrantResolver ,
174
+ expectedValid : false ,
202
175
expectedCondition : staticConds .NewRouteBackendRefRefNotPermitted (
203
176
"Backend ref to Service invalid/service1 not permitted by any ReferenceGrant" ,
204
177
),
@@ -209,7 +182,8 @@ func TestValidateBackendRef(t *testing.T) {
209
182
backend .Weight = helpers.GetPointer [int32 ](- 1 )
210
183
return backend
211
184
}),
212
- expectedValid : false ,
185
+ refGrantResolver : alwaysTrueRefGrantResolver ,
186
+ expectedValid : false ,
213
187
expectedCondition : staticConds .NewRouteBackendRefUnsupportedValue (
214
188
"test.weight: Invalid value: -1: must be in the range [0, 1000000]" ,
215
189
),
@@ -220,7 +194,8 @@ func TestValidateBackendRef(t *testing.T) {
220
194
backend .Port = nil
221
195
return backend
222
196
}),
223
- expectedValid : false ,
197
+ refGrantResolver : alwaysTrueRefGrantResolver ,
198
+ expectedValid : false ,
224
199
expectedCondition : staticConds .NewRouteBackendRefUnsupportedValue (
225
200
"test.port: Required value: port cannot be nil" ,
226
201
),
@@ -231,8 +206,7 @@ func TestValidateBackendRef(t *testing.T) {
231
206
t .Run (test .name , func (t * testing.T ) {
232
207
g := NewWithT (t )
233
208
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" ))
236
210
237
211
g .Expect (valid ).To (Equal (test .expectedValid ))
238
212
g .Expect (cond ).To (Equal (test .expectedCondition ))
@@ -437,6 +411,7 @@ func TestAddBackendRefsToRulesTest(t *testing.T) {
437
411
Name : name ,
438
412
},
439
413
},
414
+ RouteType : RouteTypeHTTP ,
440
415
ParentRefs : sectionNameRefs ,
441
416
Valid : true ,
442
417
}
@@ -1034,7 +1009,7 @@ func TestCreateBackend(t *testing.T) {
1034
1009
t .Run (test .name , func (t * testing.T ) {
1035
1010
g := NewWithT (t )
1036
1011
1037
- resolver := newReferenceGrantResolver ( nil )
1012
+ alwaysTrueRefGrantResolver := func ( _ toResource ) bool { return true }
1038
1013
1039
1014
rbr := RouteBackendRef {
1040
1015
test .ref .BackendRef ,
@@ -1043,7 +1018,7 @@ func TestCreateBackend(t *testing.T) {
1043
1018
backend , cond := createBackendRef (
1044
1019
rbr ,
1045
1020
sourceNamespace ,
1046
- resolver ,
1021
+ alwaysTrueRefGrantResolver ,
1047
1022
services ,
1048
1023
refPath ,
1049
1024
policies ,
@@ -1265,3 +1240,42 @@ func TestFindBackendTLSPolicyForService(t *testing.T) {
1265
1240
})
1266
1241
}
1267
1242
}
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