2018-10-03 18:18:55 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
2019-02-19 13:45:33 +00:00
|
|
|
"encoding/json"
|
2018-10-03 18:18:55 +00:00
|
|
|
"errors"
|
2019-02-19 13:45:33 +00:00
|
|
|
"fmt"
|
2018-10-03 18:18:55 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2"
|
|
|
|
envoyauth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
|
2019-03-22 19:37:14 +00:00
|
|
|
envoycluster "github.com/envoyproxy/go-control-plane/envoy/api/v2/cluster"
|
2018-10-03 18:18:55 +00:00
|
|
|
envoycore "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
|
2019-04-29 16:27:57 +00:00
|
|
|
envoyendpoint "github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint"
|
2019-02-19 13:45:33 +00:00
|
|
|
"github.com/gogo/protobuf/jsonpb"
|
2018-10-03 18:18:55 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-02-19 13:45:33 +00:00
|
|
|
"github.com/gogo/protobuf/types"
|
2018-10-03 18:18:55 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
2019-02-19 13:45:33 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-10-03 18:18:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// clustersFromSnapshot returns the xDS API representation of the "clusters"
|
|
|
|
// (upstreams) in the snapshot.
|
2019-04-29 16:27:57 +00:00
|
|
|
func (s *Server) clustersFromSnapshot(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")
|
|
|
|
}
|
|
|
|
// Include the "app" cluster for the public listener
|
|
|
|
clusters := make([]proto.Message, len(cfgSnap.Proxy.Upstreams)+1)
|
|
|
|
|
2019-02-19 13:45:33 +00:00
|
|
|
var err error
|
2019-04-29 16:27:57 +00:00
|
|
|
clusters[0], err = s.makeAppCluster(cfgSnap)
|
2019-02-19 13:45:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-03 18:18:55 +00:00
|
|
|
|
|
|
|
for idx, upstream := range cfgSnap.Proxy.Upstreams {
|
2019-04-29 16:27:57 +00:00
|
|
|
clusters[idx+1], err = s.makeUpstreamCluster(upstream, cfgSnap)
|
2019-02-19 13:45:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return clusters, nil
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
func (s *Server) makeAppCluster(cfgSnap *proxycfg.ConfigSnapshot) (*envoy.Cluster, error) {
|
2019-02-19 13:45:33 +00:00
|
|
|
var c *envoy.Cluster
|
|
|
|
var err error
|
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
cfg, err := ParseProxyConfig(cfgSnap.Proxy.Config)
|
|
|
|
if err != nil {
|
|
|
|
// Don't hard fail on a config typo, just warn. The parse func returns
|
|
|
|
// default config if there is an error so it's safe to continue.
|
|
|
|
s.Logger.Printf("[WARN] envoy: failed to parse Connect.Proxy.Config: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-03-06 17:13:28 +00:00
|
|
|
// If we have overridden local cluster config try to parse it into an Envoy cluster
|
2019-04-29 16:27:57 +00:00
|
|
|
if cfg.LocalClusterJSON != "" {
|
|
|
|
return makeClusterFromUserConfig(cfg.LocalClusterJSON)
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2019-02-19 13:45:33 +00:00
|
|
|
|
2019-07-19 11:53:42 +00:00
|
|
|
addr := cfgSnap.Proxy.LocalServiceAddress
|
|
|
|
if addr == "" {
|
|
|
|
addr = "127.0.0.1"
|
|
|
|
}
|
|
|
|
c = &envoy.Cluster{
|
|
|
|
Name: LocalAppClusterName,
|
|
|
|
ConnectTimeout: time.Duration(cfg.LocalConnectTimeoutMs) * time.Millisecond,
|
|
|
|
ClusterDiscoveryType: &envoy.Cluster_Type{Type: envoy.Cluster_STATIC},
|
|
|
|
LoadAssignment: &envoy.ClusterLoadAssignment{
|
|
|
|
ClusterName: LocalAppClusterName,
|
|
|
|
Endpoints: []envoyendpoint.LocalityLbEndpoints{
|
|
|
|
{
|
|
|
|
LbEndpoints: []envoyendpoint.LbEndpoint{
|
|
|
|
makeEndpoint(LocalAppClusterName,
|
|
|
|
addr,
|
|
|
|
cfgSnap.Proxy.LocalServicePort),
|
2019-04-29 16:27:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-19 11:53:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if cfg.Protocol == "http2" || cfg.Protocol == "grpc" {
|
|
|
|
c.Http2ProtocolOptions = &envoycore.Http2ProtocolOptions{}
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2019-02-19 13:45:33 +00:00
|
|
|
|
|
|
|
return c, err
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
func (s *Server) makeUpstreamCluster(upstream structs.Upstream, cfgSnap *proxycfg.ConfigSnapshot) (*envoy.Cluster, error) {
|
2019-02-19 13:45:33 +00:00
|
|
|
var c *envoy.Cluster
|
|
|
|
var err error
|
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
cfg, err := ParseUpstreamConfig(upstream.Config)
|
|
|
|
if err != nil {
|
|
|
|
// Don't hard fail on a config typo, just warn. The parse func returns
|
|
|
|
// default config if there is an error so it's safe to continue.
|
|
|
|
s.Logger.Printf("[WARN] envoy: failed to parse Upstream[%s].Config: %s",
|
|
|
|
upstream.Identifier(), err)
|
|
|
|
}
|
|
|
|
if cfg.ClusterJSON != "" {
|
|
|
|
c, err = makeClusterFromUserConfig(cfg.ClusterJSON)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
2019-02-19 13:45:33 +00:00
|
|
|
}
|
2019-04-29 16:27:57 +00:00
|
|
|
// In the happy path don't return yet as we need to inject TLS config still.
|
2019-02-19 13:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
c = &envoy.Cluster{
|
2019-06-07 12:10:43 +00:00
|
|
|
Name: upstream.Identifier(),
|
|
|
|
ConnectTimeout: time.Duration(cfg.ConnectTimeoutMs) * time.Millisecond,
|
|
|
|
ClusterDiscoveryType: &envoy.Cluster_Type{Type: envoy.Cluster_EDS},
|
2019-02-19 13:45:33 +00:00
|
|
|
EdsClusterConfig: &envoy.Cluster_EdsClusterConfig{
|
|
|
|
EdsConfig: &envoycore.ConfigSource{
|
|
|
|
ConfigSourceSpecifier: &envoycore.ConfigSource_Ads{
|
|
|
|
Ads: &envoycore.AggregatedConfigSource{},
|
|
|
|
},
|
2018-10-03 18:18:55 +00:00
|
|
|
},
|
|
|
|
},
|
2019-03-22 19:37:14 +00:00
|
|
|
// Having an empty config enables outlier detection with default config.
|
|
|
|
OutlierDetection: &envoycluster.OutlierDetection{},
|
2019-02-19 13:45:33 +00:00
|
|
|
}
|
2019-04-29 16:27:57 +00:00
|
|
|
if cfg.Protocol == "http2" || cfg.Protocol == "grpc" {
|
|
|
|
c.Http2ProtocolOptions = &envoycore.Http2ProtocolOptions{}
|
|
|
|
}
|
2019-02-19 13:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable TLS upstream with the configured client certificate.
|
|
|
|
c.TlsContext = &envoyauth.UpstreamTlsContext{
|
|
|
|
CommonTlsContext: makeCommonTLSContext(cfgSnap),
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2019-02-19 13:45:33 +00:00
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeClusterFromUserConfig returns the listener config decoded from an
|
|
|
|
// arbitrary proto3 json format string or an error if it's invalid.
|
|
|
|
//
|
|
|
|
// For now we only support embedding in JSON strings because of the hcl parsing
|
|
|
|
// pain (see config.go comment above call to patchSliceOfMaps). Until we
|
|
|
|
// refactor config parser a _lot_ user's opaque config that contains arrays will
|
|
|
|
// be mangled. We could actually fix that up in mapstructure which knows the
|
|
|
|
// type of the target so could resolve the slices to singletons unambiguously
|
|
|
|
// and it would work for us here... but we still have the problem that the
|
|
|
|
// config would render incorrectly in general in our HTTP API responses so we
|
|
|
|
// really need to fix it "properly".
|
|
|
|
//
|
|
|
|
// When we do that we can support just nesting the config directly into the
|
|
|
|
// JSON/hcl naturally but this is a stop-gap that gets us an escape hatch
|
|
|
|
// immediately. It's also probably not a bad thing to support long-term since
|
|
|
|
// any config generated by other systems will likely be in canonical protobuf
|
|
|
|
// from rather than our slight variant in JSON/hcl.
|
|
|
|
func makeClusterFromUserConfig(configJSON string) (*envoy.Cluster, error) {
|
|
|
|
var jsonFields map[string]*json.RawMessage
|
|
|
|
if err := json.Unmarshal([]byte(configJSON), &jsonFields); err != nil {
|
|
|
|
fmt.Println("Custom error", err, configJSON)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var c envoy.Cluster
|
|
|
|
|
|
|
|
if _, ok := jsonFields["@type"]; ok {
|
|
|
|
// Type field is present so decode it as a types.Any
|
|
|
|
var any types.Any
|
|
|
|
err := jsonpb.UnmarshalString(configJSON, &any)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// And then unmarshal the listener again...
|
|
|
|
err = proto.Unmarshal(any.Value, &c)
|
|
|
|
if err != nil {
|
2019-04-29 16:27:57 +00:00
|
|
|
return nil, err
|
2019-02-19 13:45:33 +00:00
|
|
|
}
|
|
|
|
return &c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// No @type so try decoding as a straight listener.
|
|
|
|
err := jsonpb.UnmarshalString(configJSON, &c)
|
|
|
|
return &c, err
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|