Skip to content

Commit 73015f8

Browse files
authored
Support SupportedKinds in ListenerStatus (#809)
* Support SupportedKinds in ListenerStatus
1 parent 36875ca commit 73015f8

10 files changed

+215
-59
lines changed

docs/gateway-api-compatibility.md

+1-1
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

+3
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

+40-11
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
@@ -158,11 +160,14 @@ func (c *listenerConfigurator) configure(listener v1beta1.Listener) *Listener {
158160
}
159161
}
160162

163+
supportedKinds := getListenerSupportedKinds(listener)
164+
161165
if len(conds) > 0 {
162166
return &Listener{
163-
Source: listener,
164-
Conditions: conds,
165-
Valid: false,
167+
Source: listener,
168+
Conditions: conds,
169+
Valid: false,
170+
SupportedKinds: supportedKinds,
166171
}
167172
}
168173

@@ -171,6 +176,7 @@ func (c *listenerConfigurator) configure(listener v1beta1.Listener) *Listener {
171176
AllowedRouteLabelSelector: allowedRouteSelector,
172177
Routes: make(map[types.NamespacedName]*Route),
173178
Valid: true,
179+
SupportedKinds: supportedKinds,
174180
}
175181

176182
// resolvers might add different conditions to the listener, so we run them all.
@@ -206,7 +212,21 @@ func validateListenerHostname(listener v1beta1.Listener) []conditions.Condition
206212
return nil
207213
}
208214

209-
func validateListenerAllowedRouteKind(listener v1beta1.Listener) []conditions.Condition {
215+
func getAndValidateListenerSupportedKinds(listener v1beta1.Listener) (
216+
[]conditions.Condition,
217+
[]v1beta1.RouteGroupKind,
218+
) {
219+
if listener.AllowedRoutes == nil || listener.AllowedRoutes.Kinds == nil {
220+
return nil, []v1beta1.RouteGroupKind{
221+
{
222+
Kind: "HTTPRoute",
223+
},
224+
}
225+
}
226+
var conds []conditions.Condition
227+
228+
supportedKinds := make([]v1beta1.RouteGroupKind, 0, len(listener.AllowedRoutes.Kinds))
229+
210230
validHTTPRouteKind := func(kind v1beta1.RouteGroupKind) bool {
211231
if kind.Kind != v1beta1.Kind("HTTPRoute") {
212232
return false
@@ -219,17 +239,26 @@ func validateListenerAllowedRouteKind(listener v1beta1.Listener) []conditions.Co
219239

220240
switch listener.Protocol {
221241
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-
}
242+
for _, kind := range listener.AllowedRoutes.Kinds {
243+
if !validHTTPRouteKind(kind) {
244+
msg := fmt.Sprintf("Unsupported route kind \"%s/%s\"", *kind.Group, kind.Kind)
245+
conds = append(conds, conditions.NewListenerInvalidRouteKinds(msg)...)
246+
continue
228247
}
248+
supportedKinds = append(supportedKinds, kind)
229249
}
230250
}
251+
return conds, supportedKinds
252+
}
231253

232-
return nil
254+
func validateListenerAllowedRouteKind(listener v1beta1.Listener) []conditions.Condition {
255+
conds, _ := getAndValidateListenerSupportedKinds(listener)
256+
return conds
257+
}
258+
259+
func getListenerSupportedKinds(listener v1beta1.Listener) []v1beta1.RouteGroupKind {
260+
_, kinds := getAndValidateListenerSupportedKinds(listener)
261+
return kinds
233262
}
234263

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

internal/state/graph/gateway_listener_test.go

+65-21
Original file line numberDiff line numberDiff line change
@@ -219,46 +219,91 @@ func TestValidateListenerHostname(t *testing.T) {
219219
}
220220
}
221221

222-
func TestValidateListenerAllowedRouteKind(t *testing.T) {
222+
func TestGetAndValidateListenerSupportedKinds(t *testing.T) {
223+
HTTPRouteGroupKind := []v1beta1.RouteGroupKind{
224+
{
225+
Kind: "HTTPRoute",
226+
Group: helpers.GetPointer[v1beta1.Group](v1beta1.GroupName),
227+
},
228+
}
229+
TCPRouteGroupKind := []v1beta1.RouteGroupKind{
230+
{
231+
Kind: "TCPRoute",
232+
Group: helpers.GetPointer[v1beta1.Group](v1beta1.GroupName),
233+
},
234+
}
223235
tests := []struct {
224236
protocol v1beta1.ProtocolType
225-
kind v1beta1.Kind
226-
group v1beta1.Group
227237
name string
238+
kind []v1beta1.RouteGroupKind
239+
expected []v1beta1.RouteGroupKind
228240
expectErr bool
229241
}{
230242
{
231243
protocol: v1beta1.TCPProtocolType,
232244
expectErr: false,
233245
name: "unsupported protocol is ignored",
246+
kind: TCPRouteGroupKind,
247+
expected: []v1beta1.RouteGroupKind{},
234248
},
235249
{
236-
protocol: v1beta1.HTTPProtocolType,
237-
group: "bad-group",
238-
kind: "HTTPRoute",
250+
protocol: v1beta1.HTTPProtocolType,
251+
kind: []v1beta1.RouteGroupKind{
252+
{
253+
Kind: "HTTPRoute",
254+
Group: helpers.GetPointer[v1beta1.Group]("bad-group"),
255+
},
256+
},
239257
expectErr: true,
240258
name: "invalid group",
259+
expected: []v1beta1.RouteGroupKind{},
241260
},
242261
{
243262
protocol: v1beta1.HTTPProtocolType,
244-
group: v1beta1.GroupName,
245-
kind: "TCPRoute",
263+
kind: TCPRouteGroupKind,
246264
expectErr: true,
247265
name: "invalid kind",
266+
expected: []v1beta1.RouteGroupKind{},
248267
},
249268
{
250269
protocol: v1beta1.HTTPProtocolType,
251-
group: v1beta1.GroupName,
252-
kind: "HTTPRoute",
270+
kind: HTTPRouteGroupKind,
253271
expectErr: false,
254272
name: "valid HTTP",
273+
expected: HTTPRouteGroupKind,
255274
},
256275
{
257276
protocol: v1beta1.HTTPSProtocolType,
258-
group: v1beta1.GroupName,
259-
kind: "HTTPRoute",
277+
kind: HTTPRouteGroupKind,
260278
expectErr: false,
261279
name: "valid HTTPS",
280+
expected: HTTPRouteGroupKind,
281+
},
282+
{
283+
protocol: v1beta1.HTTPSProtocolType,
284+
expectErr: false,
285+
name: "valid HTTPS no kind specified",
286+
expected: []v1beta1.RouteGroupKind{
287+
{
288+
Kind: "HTTPRoute",
289+
},
290+
},
291+
},
292+
{
293+
protocol: v1beta1.HTTPProtocolType,
294+
kind: []v1beta1.RouteGroupKind{
295+
{
296+
Kind: "HTTPRoute",
297+
Group: helpers.GetPointer[v1beta1.Group](v1beta1.GroupName),
298+
},
299+
{
300+
Kind: "bad-kind",
301+
Group: helpers.GetPointer[v1beta1.Group](v1beta1.GroupName),
302+
},
303+
},
304+
expectErr: true,
305+
name: "valid and invalid kinds",
306+
expected: HTTPRouteGroupKind,
262307
},
263308
}
264309

@@ -268,17 +313,16 @@ func TestValidateListenerAllowedRouteKind(t *testing.T) {
268313

269314
listener := v1beta1.Listener{
270315
Protocol: test.protocol,
271-
AllowedRoutes: &v1beta1.AllowedRoutes{
272-
Kinds: []v1beta1.RouteGroupKind{
273-
{
274-
Kind: test.kind,
275-
Group: &test.group,
276-
},
277-
},
278-
},
279316
}
280317

281-
conds := validateListenerAllowedRouteKind(listener)
318+
if test.kind != nil {
319+
listener.AllowedRoutes = &v1beta1.AllowedRoutes{
320+
Kinds: test.kind,
321+
}
322+
}
323+
324+
conds, kinds := getAndValidateListenerSupportedKinds(listener)
325+
g.Expect(helpers.Diff(test.expected, kinds)).To(BeEmpty())
282326
if test.expectErr {
283327
g.Expect(conds).ToNot(BeEmpty())
284328
} else {

0 commit comments

Comments
 (0)