Skip to content

Commit abba57e

Browse files
committed
add conversion code from state to graph
1 parent 6692f13 commit abba57e

File tree

10 files changed

+444
-106
lines changed

10 files changed

+444
-106
lines changed

internal/framework/kinds/kinds.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const (
1919
HTTPRoute = "HTTPRoute"
2020
// GRPCRoute is the GRPCRoute kind.
2121
GRPCRoute = "GRPCRoute"
22+
// TLSRoute is the TLSRoute kind.
23+
TLSRoute = "TLSRoute"
2224
)
2325

2426
// NGINX Gateway Fabric kinds.

internal/mode/static/state/graph/backend_refs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
staticConds "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/conditions"
1717
)
1818

19-
// BackendRef is an internal representation of a backendRef in an HTTP/GRPCRoute.
19+
// BackendRef is an internal representation of a backendRef in an HTTP/GRPC/TLSRoute.
2020
type BackendRef struct {
2121
// BackendTLSPolicy is the BackendTLSPolicy of the Service which is referenced by the backendRef.
2222
BackendTLSPolicy *BackendTLSPolicy

internal/mode/static/state/graph/gateway_listener.go

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func buildListeners(
6262
}
6363

6464
type listenerConfiguratorFactory struct {
65-
http, https, unsupportedProtocol *listenerConfigurator
65+
http, https, tls, unsupportedProtocol *listenerConfigurator
6666
}
6767

6868
func (f *listenerConfiguratorFactory) getConfiguratorForListener(l v1.Listener) *listenerConfigurator {
@@ -71,6 +71,8 @@ func (f *listenerConfiguratorFactory) getConfiguratorForListener(l v1.Listener)
7171
return f.http
7272
case v1.HTTPSProtocolType:
7373
return f.https
74+
case v1.TLSProtocolType:
75+
return f.tls
7476
default:
7577
return f.unsupportedProtocol
7678
}
@@ -122,6 +124,15 @@ func newListenerConfiguratorFactory(
122124
createExternalReferencesForTLSSecretsResolver(gw.Namespace, secretResolver, refGrantResolver),
123125
},
124126
},
127+
tls: &listenerConfigurator{
128+
validators: []listenerValidator{
129+
validateListenerAllowedRouteKind,
130+
validateListenerLabelSelector,
131+
validateListenerHostname,
132+
},
133+
conflictResolvers: []listenerConflictResolver{},
134+
externalReferenceResolvers: []listenerExternalReferenceResolver{},
135+
},
125136
}
126137
}
127138

@@ -185,6 +196,7 @@ func (c *listenerConfigurator) configure(listener v1.Listener) *Listener {
185196
Conditions: conds,
186197
AllowedRouteLabelSelector: allowedRouteSelector,
187198
Routes: make(map[RouteKey]*L7Route),
199+
L4Routes: make(map[L4RouteKey]*L4Route),
188200
Valid: valid,
189201
Attachable: attachable,
190202
SupportedKinds: supportedKinds,
@@ -196,7 +208,8 @@ func (c *listenerConfigurator) configure(listener v1.Listener) *Listener {
196208

197209
// resolvers might add different conditions to the listener, so we run them all.
198210

199-
for _, resolver := range c.conflictResolvers {
211+
for _, resolver := range c.
212+
conflictResolvers {
200213
resolver(l)
201214
}
202215

@@ -231,37 +244,74 @@ func getAndValidateListenerSupportedKinds(listener v1.Listener) (
231244
[]v1.RouteGroupKind,
232245
) {
233246
if listener.AllowedRoutes == nil || listener.AllowedRoutes.Kinds == nil {
234-
return nil, []v1.RouteGroupKind{
235-
{
236-
Kind: kinds.HTTPRoute,
237-
},
247+
switch listener.Protocol {
248+
case v1.HTTPProtocolType, v1.HTTPSProtocolType:
249+
return nil, []v1.RouteGroupKind{
250+
{
251+
Kind: kinds.HTTPRoute,
252+
},
253+
{
254+
Kind: kinds.GRPCRoute,
255+
},
256+
}
257+
case v1.TLSProtocolType:
258+
return nil, []v1.RouteGroupKind{
259+
{
260+
Kind: kinds.TLSRoute,
261+
},
262+
}
263+
default:
264+
return nil, []v1.RouteGroupKind{
265+
{
266+
Kind: kinds.HTTPRoute,
267+
},
268+
{
269+
Kind: kinds.GRPCRoute,
270+
},
271+
}
238272
}
239273
}
240274
var conds []conditions.Condition
241275

242276
supportedKinds := make([]v1.RouteGroupKind, 0, len(listener.AllowedRoutes.Kinds))
243277

244-
validHTTPProtocolRouteKind := func(kind v1.RouteGroupKind) bool {
245-
if kind.Kind != v1.Kind(kinds.HTTPRoute) && kind.Kind != v1.Kind(kinds.GRPCRoute) {
278+
validProtocolRouteKind := func(kind v1.RouteGroupKind, validKinds []v1.Kind) bool {
279+
matchedKind := false
280+
for _, k := range validKinds {
281+
if k == kind.Kind {
282+
matchedKind = true
283+
break
284+
}
285+
}
286+
if !matchedKind {
246287
return false
247288
}
248-
if kind.Group == nil || *kind.Group != v1.GroupName {
289+
if kind.Group != nil && *kind.Group != v1.GroupName {
249290
return false
250291
}
251292
return true
252293
}
253294

254-
switch listener.Protocol {
255-
case v1.HTTPProtocolType, v1.HTTPSProtocolType:
295+
validateProtocolRouteKinds := func(validKinds []v1.Kind) {
256296
for _, kind := range listener.AllowedRoutes.Kinds {
257-
if !validHTTPProtocolRouteKind(kind) {
297+
if !validProtocolRouteKind(kind, validKinds) {
258298
msg := fmt.Sprintf("Unsupported route kind \"%s/%s\"", *kind.Group, kind.Kind)
259299
conds = append(conds, staticConds.NewListenerInvalidRouteKinds(msg)...)
260300
continue
261301
}
262302
supportedKinds = append(supportedKinds, kind)
263303
}
264304
}
305+
306+
switch listener.Protocol {
307+
case v1.HTTPProtocolType, v1.HTTPSProtocolType:
308+
validKinds := []v1.Kind{kinds.GRPCRoute, kinds.HTTPRoute}
309+
validateProtocolRouteKinds(validKinds)
310+
311+
case v1.TLSProtocolType:
312+
validKinds := []v1.Kind{kinds.TLSRoute}
313+
validateProtocolRouteKinds(validKinds)
314+
}
265315
return conds, supportedKinds
266316
}
267317

internal/mode/static/state/graph/gateway_listener_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ func TestGetAndValidateListenerSupportedKinds(t *testing.T) {
356356
{
357357
Kind: kinds.HTTPRoute,
358358
},
359+
{
360+
Kind: kinds.GRPCRoute,
361+
},
359362
},
360363
},
361364
{

0 commit comments

Comments
 (0)