Add server receiver to routes and log tgw err

This commit is contained in:
freddygv 2020-09-03 16:19:58 -06:00
parent cd4cf5161f
commit 959d9913b8
3 changed files with 14 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package xds
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/hashicorp/consul/logging"
"net" "net"
"strings" "strings"
@ -18,7 +19,7 @@ import (
// routesFromSnapshot returns the xDS API representation of the "routes" in the // routesFromSnapshot returns the xDS API representation of the "routes" in the
// snapshot. // snapshot.
func routesFromSnapshot(cInfo connectionInfo, cfgSnap *proxycfg.ConfigSnapshot) ([]proto.Message, error) { func (s *Server) routesFromSnapshot(cInfo connectionInfo, cfgSnap *proxycfg.ConfigSnapshot) ([]proto.Message, error) {
if cfgSnap == nil { if cfgSnap == nil {
return nil, errors.New("nil config given") return nil, errors.New("nil config given")
} }
@ -29,7 +30,7 @@ func routesFromSnapshot(cInfo connectionInfo, cfgSnap *proxycfg.ConfigSnapshot)
case structs.ServiceKindIngressGateway: case structs.ServiceKindIngressGateway:
return routesForIngressGateway(cInfo, cfgSnap.IngressGateway.Upstreams, cfgSnap.IngressGateway.DiscoveryChain) return routesForIngressGateway(cInfo, cfgSnap.IngressGateway.Upstreams, cfgSnap.IngressGateway.DiscoveryChain)
case structs.ServiceKindTerminatingGateway: case structs.ServiceKindTerminatingGateway:
return routesFromSnapshotTerminatingGateway(cInfo, cfgSnap) return s.routesFromSnapshotTerminatingGateway(cInfo, cfgSnap)
default: default:
return nil, fmt.Errorf("Invalid service kind: %v", cfgSnap.Kind) return nil, fmt.Errorf("Invalid service kind: %v", cfgSnap.Kind)
} }
@ -37,10 +38,11 @@ func routesFromSnapshot(cInfo connectionInfo, cfgSnap *proxycfg.ConfigSnapshot)
// routesFromSnapshotTerminatingGateway returns the xDS API representation of the "routes" in the snapshot. // routesFromSnapshotTerminatingGateway returns the xDS API representation of the "routes" in the snapshot.
// For any HTTP service we will return a default route. // For any HTTP service we will return a default route.
func routesFromSnapshotTerminatingGateway(_ connectionInfo, cfgSnap *proxycfg.ConfigSnapshot) ([]proto.Message, error) { func (s *Server) routesFromSnapshotTerminatingGateway(_ connectionInfo, cfgSnap *proxycfg.ConfigSnapshot) ([]proto.Message, error) {
if cfgSnap == nil { if cfgSnap == nil {
return nil, errors.New("nil config given") return nil, errors.New("nil config given")
} }
logger := s.Logger.Named(logging.TerminatingGateway)
var resources []proto.Message var resources []proto.Message
for _, svc := range cfgSnap.TerminatingGateway.ValidServices() { for _, svc := range cfgSnap.TerminatingGateway.ValidServices() {
@ -69,6 +71,7 @@ func routesFromSnapshotTerminatingGateway(_ connectionInfo, cfgSnap *proxycfg.Co
} }
route, err := makeNamedDefaultRouteWithLB(clusterName, lb) route, err := makeNamedDefaultRouteWithLB(clusterName, lb)
if err != nil { if err != nil {
logger.Error("failed to make route", "cluster", clusterName, "error", err)
continue continue
} }
resources = append(resources, route) resources = append(resources, route)
@ -78,6 +81,7 @@ func routesFromSnapshotTerminatingGateway(_ connectionInfo, cfgSnap *proxycfg.Co
clusterName = connect.ServiceSNI(svc.Name, name, svc.NamespaceOrDefault(), cfgSnap.Datacenter, cfgSnap.Roots.TrustDomain) clusterName = connect.ServiceSNI(svc.Name, name, svc.NamespaceOrDefault(), cfgSnap.Datacenter, cfgSnap.Roots.TrustDomain)
route, err := makeNamedDefaultRouteWithLB(clusterName, lb) route, err := makeNamedDefaultRouteWithLB(clusterName, lb)
if err != nil { if err != nil {
logger.Error("failed to make route", "cluster", clusterName, "error", err)
continue continue
} }
resources = append(resources, route) resources = append(resources, route)

View File

@ -14,6 +14,7 @@ import (
"github.com/hashicorp/consul/agent/proxycfg" "github.com/hashicorp/consul/agent/proxycfg"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/agent/xds/proxysupport" "github.com/hashicorp/consul/agent/xds/proxysupport"
"github.com/hashicorp/consul/sdk/testutil"
testinf "github.com/mitchellh/go-testing-interface" testinf "github.com/mitchellh/go-testing-interface"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -257,11 +258,15 @@ func TestRoutesFromSnapshot(t *testing.T) {
tt.setup(snap) tt.setup(snap)
} }
logger := testutil.Logger(t)
s := Server{
Logger: logger,
}
cInfo := connectionInfo{ cInfo := connectionInfo{
Token: "my-token", Token: "my-token",
ProxyFeatures: sf, ProxyFeatures: sf,
} }
routes, err := routesFromSnapshot(cInfo, snap) routes, err := s.routesFromSnapshot(cInfo, snap)
require.NoError(err) require.NoError(err)
sort.Slice(routes, func(i, j int) bool { sort.Slice(routes, func(i, j int) bool {
return routes[i].(*envoy.RouteConfiguration).Name < routes[j].(*envoy.RouteConfiguration).Name return routes[i].(*envoy.RouteConfiguration).Name < routes[j].(*envoy.RouteConfiguration).Name

View File

@ -218,7 +218,7 @@ func (s *Server) process(stream ADSStream, reqCh <-chan *envoy.DiscoveryRequest)
}, },
RouteType: { RouteType: {
typeURL: RouteType, typeURL: RouteType,
resources: routesFromSnapshot, resources: s.routesFromSnapshot,
stream: stream, stream: stream,
allowEmptyFn: func(cfgSnap *proxycfg.ConfigSnapshot) bool { allowEmptyFn: func(cfgSnap *proxycfg.ConfigSnapshot) bool {
return cfgSnap.Kind == structs.ServiceKindIngressGateway return cfgSnap.Kind == structs.ServiceKindIngressGateway