Skip to content

Commit

Permalink
add more coverage to just the generate.go file
Browse files Browse the repository at this point in the history
  • Loading branch information
rboyer committed Aug 22, 2023
1 parent 9030870 commit f1f3c29
Show file tree
Hide file tree
Showing 2 changed files with 1,016 additions and 65 deletions.
76 changes: 21 additions & 55 deletions internal/mesh/internal/controllers/routes/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package routes

import (
"fmt"

"github.com/hashicorp/consul/internal/catalog"
"github.com/hashicorp/consul/internal/mesh/internal/controllers/routes/loader"
"github.com/hashicorp/consul/internal/mesh/internal/types"
Expand Down Expand Up @@ -102,11 +104,13 @@ func compile(
)
for _, ref := range xroute.GetParentRefs() {
if resource.ReferenceOrIDMatch(ref.Ref, parentServiceRef) {
ports = append(ports, ref.Port)
if ref.Port == "" {
wildcardedPort = true
break
}
if _, ok := allowedPortProtocols[ref.Port]; ok {
ports = append(ports, ref.Port)
}
}
}

Expand All @@ -127,37 +131,16 @@ func compile(
panic("impossible to have an empty port here")
}

// Check if the user provided port is actually valid.
nullRouteTraffic := (parentServiceDec == nil)
if _, ok := allowedPortProtocols[port]; !ok {
nullRouteTraffic = true
}

var node *inputRouteNode
switch route := xroute.(type) {
case *pbmesh.HTTPRoute:
if nullRouteTraffic {
node = newInputRouteNode(port)
setupDefaultHTTPRouteNode(node, types.NullRouteBackend)
} else {
node = compileHTTPRouteNode(port, res, route, related)
}
node = compileHTTPRouteNode(port, res, route, related)
case *pbmesh.GRPCRoute:
if nullRouteTraffic {
node = newInputRouteNode(port)
setupDefaultGRPCRouteNode(node, types.NullRouteBackend)
} else {
node = compileGRPCRouteNode(port, res, route, related)
}
node = compileGRPCRouteNode(port, res, route, related)
case *pbmesh.TCPRoute:
if nullRouteTraffic {
node = newInputRouteNode(port)
setupDefaultTCPRouteNode(node, types.NullRouteBackend)
} else {
node = compileTCPRouteNode(port, res, route, related)
}
node = compileTCPRouteNode(port, res, route, related)
default:
return // unknown xroute type (impossible)
panic(fmt.Sprintf("unexpected xroute type: %T", xroute))
}

routeNodesByPort[node.ParentPort] = append(routeNodesByPort[node.ParentPort], node)
Expand All @@ -168,6 +151,8 @@ func compile(
for port, protocol := range allowedPortProtocols {
if _, ok := routeNodesByPort[port]; !ok {
var typ *pbresource.Type

// enumcover:pbcatalog.Protocol
switch protocol {
case pbcatalog.Protocol_PROTOCOL_HTTP2:
typ = types.HTTPRouteType
Expand All @@ -177,8 +162,10 @@ func compile(
typ = types.GRPCRouteType
case pbcatalog.Protocol_PROTOCOL_TCP:
typ = types.TCPRouteType
case pbcatalog.Protocol_PROTOCOL_MESH:
fallthrough // to default
default:
continue // unknown protocol (impossible through validation)
continue // not possible
}

routeNode := createDefaultRouteNode(parentServiceRef, port, typ)
Expand Down Expand Up @@ -271,7 +258,7 @@ func compile(

svc := related.GetService(svcRef)
failoverPolicy := related.GetFailoverPolicyForService(svcRef)
destConfig := related.GetDestinationPolicy(svcRef)
destConfig := related.GetDestinationPolicyForService(svcRef)

if svc == nil {
panic("impossible at this point; should already have been handled before getting here")
Expand Down Expand Up @@ -535,13 +522,16 @@ func createDefaultRouteNode(
})
switch {
case resource.EqualType(types.HTTPRouteType, typ):
setupDefaultHTTPRouteNode(routeNode, defaultBackendTarget)
routeNode.RouteType = types.HTTPRouteType
appendDefaultHTTPRouteRule(routeNode, defaultBackendTarget)
case resource.EqualType(types.GRPCRouteType, typ):
setupDefaultGRPCRouteNode(routeNode, defaultBackendTarget)
routeNode.RouteType = types.GRPCRouteType
appendDefaultGRPCRouteRule(routeNode, defaultBackendTarget)
case resource.EqualType(types.TCPRouteType, typ):
fallthrough
default:
setupDefaultTCPRouteNode(routeNode, defaultBackendTarget)
routeNode.RouteType = types.TCPRouteType
appendDefaultTCPRouteRule(routeNode, defaultBackendTarget)
}

routeNode.Default = true
Expand All @@ -564,14 +554,6 @@ func appendDefaultRouteNode(
}
}

func setupDefaultHTTPRouteNode(
routeNode *inputRouteNode,
defaultBackendTarget string,
) {
routeNode.RouteType = types.HTTPRouteType
appendDefaultHTTPRouteRule(routeNode, defaultBackendTarget)
}

func appendDefaultHTTPRouteRule(
routeNode *inputRouteNode,
backendTarget string,
Expand All @@ -584,14 +566,6 @@ func appendDefaultHTTPRouteRule(
})
}

func setupDefaultGRPCRouteNode(
routeNode *inputRouteNode,
defaultBackendTarget string,
) {
routeNode.RouteType = types.GRPCRouteType
appendDefaultGRPCRouteRule(routeNode, defaultBackendTarget)
}

func appendDefaultGRPCRouteRule(
routeNode *inputRouteNode,
backendTarget string,
Expand All @@ -604,14 +578,6 @@ func appendDefaultGRPCRouteRule(
})
}

func setupDefaultTCPRouteNode(
routeNode *inputRouteNode,
defaultBackendTarget string,
) {
routeNode.RouteType = types.TCPRouteType
appendDefaultTCPRouteRule(routeNode, defaultBackendTarget)
}

func appendDefaultTCPRouteRule(
routeNode *inputRouteNode,
backendTarget string,
Expand Down
Loading

0 comments on commit f1f3c29

Please sign in to comment.