Skip to content

Commit dcd13ea

Browse files
committed
Support SupportedKinds in ListenerStatus
1 parent 36875ca commit dcd13ea

File tree

10 files changed

+179
-50
lines changed

10 files changed

+179
-50
lines changed

docs/gateway-api-compatibility.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Fields:
9797
Gateway. NKG only supports a single Gateway.
9898
* `listeners`
9999
* `name` - supported.
100-
* `supportedKinds` - not supported.
100+
* `supportedKinds` - supported.
101101
* `attachedRoutes` - supported.
102102
* `conditions` - Supported (Condition/Status/Reason):
103103
* `Accepted/True/Accepted`

internal/state/change_processor_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ var _ = Describe("ChangeProcessor", func() {
451451
Routes: map[types.NamespacedName]*graph.Route{
452452
{Namespace: "test", Name: "hr-1"}: expRouteHR1,
453453
},
454+
SupportedKinds: []v1beta1.RouteGroupKind{{Kind: "HTTPRoute"}},
454455
},
455456
"listener-443-1": {
456457
Source: gw1.Spec.Listeners[1],
@@ -459,6 +460,7 @@ var _ = Describe("ChangeProcessor", func() {
459460
{Namespace: "test", Name: "hr-1"}: expRouteHR1,
460461
},
461462
ResolvedSecret: helpers.GetPointer(client.ObjectKeyFromObject(diffNsTLSSecret)),
463+
SupportedKinds: []v1beta1.RouteGroupKind{{Kind: "HTTPRoute"}},
462464
},
463465
},
464466
Valid: true,
@@ -544,6 +546,7 @@ var _ = Describe("ChangeProcessor", func() {
544546
Conditions: conditions.NewListenerRefNotPermitted(
545547
"Certificate ref to secret cert-ns/different-ns-tls-secret not permitted by any ReferenceGrant",
546548
),
549+
SupportedKinds: []v1beta1.RouteGroupKind{{Kind: "HTTPRoute"}},
547550
}
548551

549552
expAttachment := &graph.ParentRefAttachmentStatus{

internal/state/graph/gateway_listener.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type Listener struct {
2828
ResolvedSecret *types.NamespacedName
2929
// Conditions holds the conditions of the Listener.
3030
Conditions []conditions.Condition
31+
// SupportedKinds is the list of RouteGroupKinds allowed by the listener.
32+
SupportedKinds []v1beta1.RouteGroupKind
3133
// Valid shows whether the Listener is valid.
3234
// A Listener is considered valid if NKG can generate valid NGINX configuration for it.
3335
Valid bool
@@ -87,7 +89,6 @@ func newListenerConfiguratorFactory(
8789
},
8890
http: &listenerConfigurator{
8991
validators: []listenerValidator{
90-
validateListenerAllowedRouteKind,
9192
validateListenerLabelSelector,
9293
validateListenerHostname,
9394
validateHTTPListener,
@@ -98,7 +99,6 @@ func newListenerConfiguratorFactory(
9899
},
99100
https: &listenerConfigurator{
100101
validators: []listenerValidator{
101-
validateListenerAllowedRouteKind,
102102
validateListenerLabelSelector,
103103
validateListenerHostname,
104104
createHTTPSListenerValidator(),
@@ -158,11 +158,15 @@ func (c *listenerConfigurator) configure(listener v1beta1.Listener) *Listener {
158158
}
159159
}
160160

161+
cnds, supportedKinds := getAndValidateSupportedKinds(listener)
162+
conds = append(conds, cnds...)
163+
161164
if len(conds) > 0 {
162165
return &Listener{
163-
Source: listener,
164-
Conditions: conds,
165-
Valid: false,
166+
Source: listener,
167+
Conditions: conds,
168+
Valid: false,
169+
SupportedKinds: supportedKinds,
166170
}
167171
}
168172

@@ -171,6 +175,7 @@ func (c *listenerConfigurator) configure(listener v1beta1.Listener) *Listener {
171175
AllowedRouteLabelSelector: allowedRouteSelector,
172176
Routes: make(map[types.NamespacedName]*Route),
173177
Valid: true,
178+
SupportedKinds: supportedKinds,
174179
}
175180

176181
// resolvers might add different conditions to the listener, so we run them all.
@@ -206,7 +211,18 @@ func validateListenerHostname(listener v1beta1.Listener) []conditions.Condition
206211
return nil
207212
}
208213

209-
func validateListenerAllowedRouteKind(listener v1beta1.Listener) []conditions.Condition {
214+
func getAndValidateSupportedKinds(listener v1beta1.Listener) ([]conditions.Condition, []v1beta1.RouteGroupKind) {
215+
if listener.AllowedRoutes == nil || listener.AllowedRoutes.Kinds == nil {
216+
return nil, []v1beta1.RouteGroupKind{
217+
{
218+
Kind: "HTTPRoute",
219+
},
220+
}
221+
}
222+
var conds []conditions.Condition
223+
224+
supportedKinds := make([]v1beta1.RouteGroupKind, 0, len(listener.AllowedRoutes.Kinds))
225+
210226
validHTTPRouteKind := func(kind v1beta1.RouteGroupKind) bool {
211227
if kind.Kind != v1beta1.Kind("HTTPRoute") {
212228
return false
@@ -219,17 +235,16 @@ func validateListenerAllowedRouteKind(listener v1beta1.Listener) []conditions.Co
219235

220236
switch listener.Protocol {
221237
case v1beta1.HTTPProtocolType, v1beta1.HTTPSProtocolType:
222-
if listener.AllowedRoutes != nil {
223-
for _, kind := range listener.AllowedRoutes.Kinds {
224-
if !validHTTPRouteKind(kind) {
225-
msg := fmt.Sprintf("Unsupported route kind \"%s/%s\"", *kind.Group, kind.Kind)
226-
return conditions.NewListenerInvalidRouteKinds(msg)
227-
}
238+
for _, kind := range listener.AllowedRoutes.Kinds {
239+
if !validHTTPRouteKind(kind) {
240+
msg := fmt.Sprintf("Unsupported route kind \"%s/%s\"", *kind.Group, kind.Kind)
241+
conds = append(conds, conditions.NewListenerInvalidRouteKinds(msg)...)
242+
continue
228243
}
244+
supportedKinds = append(supportedKinds, kind)
229245
}
230246
}
231-
232-
return nil
247+
return conds, supportedKinds
233248
}
234249

235250
func validateListenerLabelSelector(listener v1beta1.Listener) []conditions.Condition {

internal/state/graph/gateway_listener_test.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,27 @@ func TestValidateListenerHostname(t *testing.T) {
220220
}
221221

222222
func TestValidateListenerAllowedRouteKind(t *testing.T) {
223+
expectedKinds := []v1beta1.RouteGroupKind{
224+
{
225+
Kind: "HTTPRoute",
226+
Group: helpers.GetPointer[v1beta1.Group](v1beta1.GroupName),
227+
},
228+
}
223229
tests := []struct {
224230
protocol v1beta1.ProtocolType
225231
kind v1beta1.Kind
226232
group v1beta1.Group
227233
name string
234+
expected []v1beta1.RouteGroupKind
228235
expectErr bool
229236
}{
230237
{
231238
protocol: v1beta1.TCPProtocolType,
232239
expectErr: false,
233240
name: "unsupported protocol is ignored",
241+
kind: "TCPRoute",
242+
group: v1beta1.GroupName,
243+
expected: []v1beta1.RouteGroupKind{},
234244
},
235245
{
236246
protocol: v1beta1.HTTPProtocolType,
@@ -252,37 +262,58 @@ func TestValidateListenerAllowedRouteKind(t *testing.T) {
252262
kind: "HTTPRoute",
253263
expectErr: false,
254264
name: "valid HTTP",
265+
expected: expectedKinds,
255266
},
256267
{
257268
protocol: v1beta1.HTTPSProtocolType,
258269
group: v1beta1.GroupName,
259270
kind: "HTTPRoute",
260271
expectErr: false,
261272
name: "valid HTTPS",
273+
expected: expectedKinds,
274+
},
275+
{
276+
protocol: v1beta1.HTTPSProtocolType,
277+
expectErr: false,
278+
name: "valid HTTPS no kind specified",
279+
expected: []v1beta1.RouteGroupKind{
280+
{
281+
Kind: "HTTPRoute",
282+
},
283+
},
262284
},
263285
}
264286

265287
for _, test := range tests {
266288
t.Run(test.name, func(t *testing.T) {
267289
g := NewGomegaWithT(t)
290+
var listener v1beta1.Listener
268291

269-
listener := v1beta1.Listener{
270-
Protocol: test.protocol,
271-
AllowedRoutes: &v1beta1.AllowedRoutes{
272-
Kinds: []v1beta1.RouteGroupKind{
273-
{
274-
Kind: test.kind,
275-
Group: &test.group,
292+
if test.kind != "" {
293+
listener = v1beta1.Listener{
294+
Protocol: test.protocol,
295+
AllowedRoutes: &v1beta1.AllowedRoutes{
296+
Kinds: []v1beta1.RouteGroupKind{
297+
{
298+
Kind: test.kind,
299+
Group: &test.group,
300+
},
276301
},
277302
},
278-
},
303+
}
304+
} else {
305+
listener = v1beta1.Listener{
306+
Protocol: test.protocol,
307+
}
279308
}
280309

281-
conds := validateListenerAllowedRouteKind(listener)
310+
conds, kinds := getAndValidateSupportedKinds(listener)
282311
if test.expectErr {
283312
g.Expect(conds).ToNot(BeEmpty())
313+
g.Expect(kinds).To(BeEmpty())
284314
} else {
285315
g.Expect(conds).To(BeEmpty())
316+
g.Expect(helpers.Diff(test.expected, kinds)).To(BeEmpty())
286317
}
287318
})
288319
}

0 commit comments

Comments
 (0)