2018-10-03 18:18:55 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-06-24 19:05:36 +00:00
|
|
|
"fmt"
|
2018-10-03 18:18:55 +00:00
|
|
|
|
|
|
|
envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2"
|
2019-03-22 19:37:14 +00:00
|
|
|
envoycore "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
|
2018-10-03 18:18:55 +00:00
|
|
|
envoyendpoint "github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-03-22 19:37:14 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2019-07-02 13:43:35 +00:00
|
|
|
|
|
|
|
bexpr "github.com/hashicorp/go-bexpr"
|
2018-10-03 18:18:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// endpointsFromSnapshot returns the xDS API representation of the "endpoints"
|
2019-06-18 00:52:01 +00:00
|
|
|
func (s *Server) endpointsFromSnapshot(cfgSnap *proxycfg.ConfigSnapshot, token string) ([]proto.Message, error) {
|
2018-10-03 18:18:55 +00:00
|
|
|
if cfgSnap == nil {
|
|
|
|
return nil, errors.New("nil config given")
|
|
|
|
}
|
2019-06-24 19:05:36 +00:00
|
|
|
|
|
|
|
switch cfgSnap.Kind {
|
|
|
|
case structs.ServiceKindConnectProxy:
|
2019-06-18 00:52:01 +00:00
|
|
|
return s.endpointsFromSnapshotConnectProxy(cfgSnap, token)
|
|
|
|
case structs.ServiceKindMeshGateway:
|
|
|
|
return s.endpointsFromSnapshotMeshGateway(cfgSnap, token)
|
2019-06-24 19:05:36 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid service kind: %v", cfgSnap.Kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// endpointsFromSnapshotConnectProxy returns the xDS API representation of the "endpoints"
|
|
|
|
// (upstream instances) in the snapshot.
|
2019-06-18 00:52:01 +00:00
|
|
|
func (s *Server) endpointsFromSnapshotConnectProxy(cfgSnap *proxycfg.ConfigSnapshot, token string) ([]proto.Message, error) {
|
2019-07-02 03:10:51 +00:00
|
|
|
// TODO(rb): this sizing is a low bound.
|
2019-06-18 00:52:01 +00:00
|
|
|
resources := make([]proto.Message, 0, len(cfgSnap.ConnectProxy.UpstreamEndpoints))
|
2019-07-02 03:10:51 +00:00
|
|
|
|
|
|
|
// TODO(rb): should naming from 1.5 -> 1.6 for clusters remain unchanged?
|
|
|
|
|
|
|
|
for _, u := range cfgSnap.Proxy.Upstreams {
|
|
|
|
id := u.Identifier()
|
|
|
|
|
|
|
|
var chain *structs.CompiledDiscoveryChain
|
|
|
|
if u.DestinationType != structs.UpstreamDestTypePreparedQuery {
|
|
|
|
chain = cfgSnap.ConnectProxy.DiscoveryChain[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
if chain == nil {
|
|
|
|
// We ONLY want this branch for prepared queries.
|
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
clusterName := UpstreamSNI(&u, "", cfgSnap)
|
2019-07-02 03:10:51 +00:00
|
|
|
endpoints, ok := cfgSnap.ConnectProxy.UpstreamEndpoints[id]
|
|
|
|
if ok {
|
|
|
|
la := makeLoadAssignment(
|
2019-08-02 03:03:34 +00:00
|
|
|
clusterName,
|
2019-07-24 01:20:24 +00:00
|
|
|
[]loadAssignmentEndpointGroup{
|
|
|
|
{Endpoints: endpoints},
|
|
|
|
},
|
2019-07-02 03:10:51 +00:00
|
|
|
cfgSnap.Datacenter,
|
|
|
|
)
|
|
|
|
resources = append(resources, la)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Newfangled discovery chain plumbing.
|
|
|
|
|
|
|
|
chainEndpointMap, ok := cfgSnap.ConnectProxy.WatchedUpstreamEndpoints[id]
|
|
|
|
if !ok {
|
2019-07-24 01:20:24 +00:00
|
|
|
continue // skip the upstream (should not happen)
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// Find all resolver nodes.
|
|
|
|
for _, node := range chain.Nodes {
|
|
|
|
if node.Type != structs.DiscoveryGraphNodeTypeResolver {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
failover := node.Resolver.Failover
|
2019-08-02 20:34:54 +00:00
|
|
|
targetID := node.Resolver.Target
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
target := chain.Targets[targetID]
|
|
|
|
|
|
|
|
endpoints, ok := chainEndpointMap[targetID]
|
2019-07-02 03:10:51 +00:00
|
|
|
if !ok {
|
2019-07-24 01:20:24 +00:00
|
|
|
continue // skip the cluster (should not happen)
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
var endpointGroups []loadAssignmentEndpointGroup
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2019-07-24 01:20:24 +00:00
|
|
|
primaryGroup := loadAssignmentEndpointGroup{
|
|
|
|
Endpoints: endpoints,
|
2019-08-02 20:34:54 +00:00
|
|
|
OnlyPassing: target.Subset.OnlyPassing,
|
2019-07-24 01:20:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 03:10:51 +00:00
|
|
|
if failover != nil && len(failover.Targets) > 0 {
|
2019-07-24 01:20:24 +00:00
|
|
|
endpointGroups = make([]loadAssignmentEndpointGroup, 0, len(failover.Targets)+1)
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2019-07-24 01:20:24 +00:00
|
|
|
endpointGroups = append(endpointGroups, primaryGroup)
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
for _, failTargetID := range failover.Targets {
|
|
|
|
failEndpoints, ok := chainEndpointMap[failTargetID]
|
2019-07-24 01:20:24 +00:00
|
|
|
if !ok {
|
|
|
|
continue // skip the failover target (should not happen)
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
2019-07-24 01:20:24 +00:00
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
failTarget := chain.Targets[failTargetID]
|
2019-08-02 03:44:05 +00:00
|
|
|
|
2019-07-24 01:20:24 +00:00
|
|
|
endpointGroups = append(endpointGroups, loadAssignmentEndpointGroup{
|
|
|
|
Endpoints: failEndpoints,
|
2019-08-02 20:34:54 +00:00
|
|
|
OnlyPassing: failTarget.Subset.OnlyPassing,
|
2019-07-24 01:20:24 +00:00
|
|
|
})
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-07-24 01:20:24 +00:00
|
|
|
endpointGroups = append(endpointGroups, primaryGroup)
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 11:48:48 +00:00
|
|
|
sni := TargetSNI(target, cfgSnap)
|
2019-08-02 03:03:34 +00:00
|
|
|
clusterName := CustomizeClusterName(sni, chain)
|
2019-07-02 03:10:51 +00:00
|
|
|
|
|
|
|
la := makeLoadAssignment(
|
2019-08-02 03:03:34 +00:00
|
|
|
clusterName,
|
2019-07-24 01:20:24 +00:00
|
|
|
endpointGroups,
|
2019-07-02 03:10:51 +00:00
|
|
|
cfgSnap.Datacenter,
|
|
|
|
)
|
|
|
|
resources = append(resources, la)
|
|
|
|
}
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) endpointsFromSnapshotMeshGateway(cfgSnap *proxycfg.ConfigSnapshot, token string) ([]proto.Message, error) {
|
|
|
|
resources := make([]proto.Message, 0, len(cfgSnap.MeshGateway.GatewayGroups)+len(cfgSnap.MeshGateway.ServiceGroups))
|
|
|
|
|
|
|
|
// generate the endpoints for the gateways in the remote datacenters
|
|
|
|
for dc, endpoints := range cfgSnap.MeshGateway.GatewayGroups {
|
|
|
|
clusterName := DatacenterSNI(dc, cfgSnap)
|
2019-07-02 03:10:51 +00:00
|
|
|
la := makeLoadAssignment(
|
|
|
|
clusterName,
|
2019-07-24 01:20:24 +00:00
|
|
|
[]loadAssignmentEndpointGroup{
|
|
|
|
{Endpoints: endpoints},
|
2019-07-02 03:10:51 +00:00
|
|
|
},
|
|
|
|
cfgSnap.Datacenter,
|
|
|
|
)
|
2019-06-18 00:52:01 +00:00
|
|
|
resources = append(resources, la)
|
2019-06-24 19:05:36 +00:00
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
// generate the endpoints for the local service groups
|
|
|
|
for svc, endpoints := range cfgSnap.MeshGateway.ServiceGroups {
|
2019-07-02 13:43:35 +00:00
|
|
|
clusterName := ServiceSNI(svc, "", "default", cfgSnap.Datacenter, cfgSnap)
|
2019-07-02 03:10:51 +00:00
|
|
|
la := makeLoadAssignment(
|
|
|
|
clusterName,
|
2019-07-24 01:20:24 +00:00
|
|
|
[]loadAssignmentEndpointGroup{
|
|
|
|
{Endpoints: endpoints},
|
2019-07-02 03:10:51 +00:00
|
|
|
},
|
|
|
|
cfgSnap.Datacenter,
|
|
|
|
)
|
2018-10-03 18:18:55 +00:00
|
|
|
resources = append(resources, la)
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2019-07-02 13:43:35 +00:00
|
|
|
// generate the endpoints for the service subsets
|
|
|
|
for svc, resolver := range cfgSnap.MeshGateway.ServiceResolvers {
|
|
|
|
for subsetName, subset := range resolver.Subsets {
|
|
|
|
clusterName := ServiceSNI(svc, subsetName, "default", cfgSnap.Datacenter, cfgSnap)
|
|
|
|
|
|
|
|
endpoints := cfgSnap.MeshGateway.ServiceGroups[svc]
|
|
|
|
|
|
|
|
// locally execute the subsets filter
|
2019-07-24 01:20:24 +00:00
|
|
|
if subset.Filter != "" {
|
|
|
|
filter, err := bexpr.CreateFilter(subset.Filter, nil, endpoints)
|
2019-07-02 13:43:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := filter.Execute(endpoints)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
endpoints = raw.(structs.CheckServiceNodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
la := makeLoadAssignment(
|
|
|
|
clusterName,
|
2019-07-24 01:20:24 +00:00
|
|
|
[]loadAssignmentEndpointGroup{
|
|
|
|
{
|
|
|
|
Endpoints: endpoints,
|
|
|
|
OnlyPassing: subset.OnlyPassing,
|
|
|
|
},
|
2019-07-02 13:43:35 +00:00
|
|
|
},
|
|
|
|
cfgSnap.Datacenter,
|
|
|
|
)
|
|
|
|
resources = append(resources, la)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 18:18:55 +00:00
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeEndpoint(clusterName, host string, port int) envoyendpoint.LbEndpoint {
|
|
|
|
return envoyendpoint.LbEndpoint{
|
2019-07-02 03:10:51 +00:00
|
|
|
HostIdentifier: &envoyendpoint.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoyendpoint.Endpoint{
|
|
|
|
Address: makeAddressPtr(host, port),
|
|
|
|
},
|
2018-10-03 18:18:55 +00:00
|
|
|
},
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 01:20:24 +00:00
|
|
|
type loadAssignmentEndpointGroup struct {
|
|
|
|
Endpoints structs.CheckServiceNodes
|
|
|
|
OnlyPassing bool
|
|
|
|
}
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
func makeLoadAssignment(clusterName string, endpointGroups []loadAssignmentEndpointGroup, localDatacenter string) *envoy.ClusterLoadAssignment {
|
2019-07-02 03:10:51 +00:00
|
|
|
cla := &envoy.ClusterLoadAssignment{
|
|
|
|
ClusterName: clusterName,
|
2019-07-24 01:20:24 +00:00
|
|
|
Endpoints: make([]envoyendpoint.LocalityLbEndpoints, 0, len(endpointGroups)),
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
2019-08-02 20:34:54 +00:00
|
|
|
|
|
|
|
if len(endpointGroups) > 1 {
|
2019-07-02 03:10:51 +00:00
|
|
|
cla.Policy = &envoy.ClusterLoadAssignment_Policy{
|
2019-08-02 20:34:54 +00:00
|
|
|
// We choose such a large value here that the failover math should
|
|
|
|
// in effect not happen until zero instances are healthy.
|
|
|
|
OverprovisioningFactor: makeUint32Value(100000),
|
2019-03-22 19:37:14 +00:00
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 01:20:24 +00:00
|
|
|
for priority, endpointGroup := range endpointGroups {
|
|
|
|
endpoints := endpointGroup.Endpoints
|
2019-07-02 03:10:51 +00:00
|
|
|
es := make([]envoyendpoint.LbEndpoint, 0, len(endpoints))
|
|
|
|
|
|
|
|
for _, ep := range endpoints {
|
|
|
|
// TODO (mesh-gateway) - should we respect the translate_wan_addrs configuration here or just always use the wan for cross-dc?
|
|
|
|
addr, port := ep.BestAddress(localDatacenter != ep.Node.Datacenter)
|
|
|
|
healthStatus := envoycore.HealthStatus_HEALTHY
|
|
|
|
weight := 1
|
|
|
|
if ep.Service.Weights != nil {
|
|
|
|
weight = ep.Service.Weights.Passing
|
|
|
|
}
|
2019-03-22 19:37:14 +00:00
|
|
|
|
2019-07-02 03:10:51 +00:00
|
|
|
for _, chk := range ep.Checks {
|
|
|
|
if chk.Status == api.HealthCritical {
|
2019-07-24 01:20:24 +00:00
|
|
|
healthStatus = envoycore.HealthStatus_UNHEALTHY
|
|
|
|
}
|
|
|
|
if endpointGroup.OnlyPassing && chk.Status != api.HealthPassing {
|
2019-07-02 03:10:51 +00:00
|
|
|
healthStatus = envoycore.HealthStatus_UNHEALTHY
|
|
|
|
}
|
|
|
|
if chk.Status == api.HealthWarning && ep.Service.Weights != nil {
|
|
|
|
weight = ep.Service.Weights.Warning
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make weights fit Envoy's limits. A zero weight means that either Warning
|
|
|
|
// (likely) or Passing (weirdly) weight has been set to 0 effectively making
|
|
|
|
// this instance unhealthy and should not be sent traffic.
|
|
|
|
if weight < 1 {
|
2019-03-22 19:37:14 +00:00
|
|
|
healthStatus = envoycore.HealthStatus_UNHEALTHY
|
2019-07-02 03:10:51 +00:00
|
|
|
weight = 1
|
2019-03-22 19:37:14 +00:00
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
if weight > 128 {
|
|
|
|
weight = 128
|
2019-03-22 19:37:14 +00:00
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
es = append(es, envoyendpoint.LbEndpoint{
|
|
|
|
HostIdentifier: &envoyendpoint.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoyendpoint.Endpoint{
|
|
|
|
Address: makeAddressPtr(addr, port),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthStatus: healthStatus,
|
|
|
|
LoadBalancingWeight: makeUint32Value(weight),
|
|
|
|
})
|
2019-03-22 19:37:14 +00:00
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
|
|
|
|
cla.Endpoints = append(cla.Endpoints, envoyendpoint.LocalityLbEndpoints{
|
|
|
|
Priority: uint32(priority),
|
2018-10-03 18:18:55 +00:00
|
|
|
LbEndpoints: es,
|
2019-07-02 03:10:51 +00:00
|
|
|
})
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
|
|
|
|
return cla
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|