Skip to content

Commit

Permalink
add validate listener checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthyparty committed Aug 7, 2024
1 parent 02ebcfe commit 94e7237
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions internal/mode/static/state/graph/gateway_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,44 +254,49 @@ func getAndValidateListenerSupportedKinds(listener v1.Listener) (
var conds []conditions.Condition
var supportedKinds []v1.RouteGroupKind

ngfSupportedKinds := map[v1.Kind]struct{}{
kinds.TLSRoute: {},
kinds.HTTPRoute: {},
kinds.GRPCRoute: {},
}

validRouteKind := func(kind v1.RouteGroupKind) bool {
if _, ok := ngfSupportedKinds[kind.Kind]; !ok {
validProtocolRouteKind := func(kind v1.RouteGroupKind, validKinds []v1.RouteGroupKind) bool {
matchedKind := false
for _, k := range validKinds {
if k.Kind == kind.Kind {
matchedKind = true
break
}
}
if !matchedKind {
return false
}
if kind.Group == nil || *kind.Group != v1.GroupName {
if kind.Group != nil && *kind.Group != v1.GroupName {
return false
}
return true
}

var validKinds []v1.RouteGroupKind

switch listener.Protocol {
case v1.HTTPProtocolType, v1.HTTPSProtocolType:
validKinds = []v1.RouteGroupKind{
{Kind: v1.Kind(kinds.HTTPRoute), Group: helpers.GetPointer[v1.Group](v1.GroupName)},
{Kind: v1.Kind(kinds.GRPCRoute), Group: helpers.GetPointer[v1.Group](v1.GroupName)},
}
case v1.TLSProtocolType:
validKinds = []v1.RouteGroupKind{
{Kind: v1.Kind(kinds.TLSRoute), Group: helpers.GetPointer[v1.Group](v1.GroupName)},
}
}

if listener.AllowedRoutes != nil && listener.AllowedRoutes.Kinds != nil {
supportedKinds = make([]v1.RouteGroupKind, 0, len(listener.AllowedRoutes.Kinds))
for _, kind := range listener.AllowedRoutes.Kinds {
if !validRouteKind(kind) {
if !validProtocolRouteKind(kind, validKinds) {
msg := fmt.Sprintf("Unsupported route kind \"%s/%s\"", *kind.Group, kind.Kind)
conds = append(conds, staticConds.NewListenerInvalidRouteKinds(msg)...)
continue
}
supportedKinds = append(supportedKinds, kind)
}
} else {
switch listener.Protocol {
case v1.HTTPProtocolType, v1.HTTPSProtocolType:
supportedKinds = []v1.RouteGroupKind{
{Kind: v1.Kind(kinds.HTTPRoute), Group: helpers.GetPointer[v1.Group](v1.GroupName)},
{Kind: v1.Kind(kinds.GRPCRoute), Group: helpers.GetPointer[v1.Group](v1.GroupName)},
}
case v1.TLSProtocolType:
supportedKinds = []v1.RouteGroupKind{
{Kind: v1.Kind(kinds.TLSRoute), Group: helpers.GetPointer[v1.Group](v1.GroupName)},
}
}
supportedKinds = validKinds
}

return conds, supportedKinds
Expand Down

0 comments on commit 94e7237

Please sign in to comment.