Skip to content

Commit

Permalink
Add envoy connection balancing to upstreams.
Browse files Browse the repository at this point in the history
  • Loading branch information
hashi-derek committed Sep 14, 2022
1 parent acd9d42 commit 82d5c1a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
9 changes: 9 additions & 0 deletions agent/structs/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,12 @@ type UpstreamConfig struct {
// EnterpriseMeta is only accepted within a service-defaults config entry.
acl.EnterpriseMeta `hcl:",squash" mapstructure:",squash"`

// EnvoyConnectionBalanceType specifies how envoy connections should
// be distributed across worker threads. Currently, only the "exact_balance"
// type is accepted.
// https://cloudnative.to/envoy/api-v3/config/listener/v3/listener.proto.html#config-listener-v3-listener-connectionbalanceconfig
EnvoyConnectionBalanceType string `json:",omitempty" alias:"envoy_connection_balance_type"`

// EnvoyListenerJSON is a complete override ("escape hatch") for the upstream's
// listener.
//
Expand Down Expand Up @@ -827,6 +833,9 @@ func (cfg *UpstreamConfig) ServiceName() ServiceName {

func (cfg UpstreamConfig) MergeInto(dst map[string]interface{}) {
// Avoid storing empty values in the map, since these can act as overrides
if cfg.EnvoyConnectionBalanceType != "" {
dst["envoy_connection_balance_type"] = cfg.EnvoyConnectionBalanceType
}
if cfg.EnvoyListenerJSON != "" {
dst["envoy_listener_json"] = cfg.EnvoyListenerJSON
}
Expand Down
11 changes: 7 additions & 4 deletions agent/structs/config_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ func TestDecodeConfigEntry(t *testing.T) {
defaults {
connect_timeout_ms = 5
protocol = "http"
envoy_connection_balance_type = "something"
envoy_listener_json = "foo"
envoy_cluster_json = "bar"
limits {
Expand Down Expand Up @@ -454,6 +455,7 @@ func TestDecodeConfigEntry(t *testing.T) {
},
]
Defaults {
EnvoyConnectionBalanceType = "something"
EnvoyListenerJSON = "foo"
EnvoyClusterJSON = "bar"
ConnectTimeoutMs = 5
Expand Down Expand Up @@ -493,10 +495,11 @@ func TestDecodeConfigEntry(t *testing.T) {
},
},
Defaults: &UpstreamConfig{
EnvoyListenerJSON: "foo",
EnvoyClusterJSON: "bar",
ConnectTimeoutMs: 5,
Protocol: "http",
EnvoyConnectionBalanceType: "something",
EnvoyListenerJSON: "foo",
EnvoyClusterJSON: "bar",
ConnectTimeoutMs: 5,
Protocol: "http",
Limits: &UpstreamLimits{
MaxConnections: intPointer(3),
MaxPendingRequests: intPointer(4),
Expand Down
11 changes: 11 additions & 0 deletions agent/xds/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (s *ResourceGenerator) listenersFromSnapshotConnectProxy(cfgSnap *proxycfg.
upstreamListener.FilterChains = []*envoy_listener_v3.FilterChain{
filterChain,
}
injectConnectionBalanceConfig(&cfg, upstreamListener)
resources = append(resources, upstreamListener)

// Avoid creating filter chains below for upstreams that have dedicated listeners
Expand Down Expand Up @@ -388,6 +389,7 @@ func (s *ResourceGenerator) listenersFromSnapshotConnectProxy(cfgSnap *proxycfg.
upstreamListener.FilterChains = []*envoy_listener_v3.FilterChain{
filterChain,
}
injectConnectionBalanceConfig(&cfg, upstreamListener)
resources = append(resources, upstreamListener)

// Avoid creating filter chains below for upstreams that have dedicated listeners
Expand Down Expand Up @@ -574,6 +576,7 @@ func (s *ResourceGenerator) listenersFromSnapshotConnectProxy(cfgSnap *proxycfg.
upstreamListener.FilterChains = []*envoy_listener_v3.FilterChain{
filterChain,
}
injectConnectionBalanceConfig(&cfg, upstreamListener)
resources = append(resources, upstreamListener)
}

Expand Down Expand Up @@ -905,6 +908,14 @@ func makeListenerFromUserConfig(configJSON string) (*envoy_listener_v3.Listener,
return &l, nil
}

func injectConnectionBalanceConfig(cfg *structs.UpstreamConfig, listener *envoy_listener_v3.Listener) {
if cfg.EnvoyConnectionBalanceType == "exact_balance" {
listener.ConnectionBalanceConfig = &envoy_listener_v3.Listener_ConnectionBalanceConfig{
BalanceType: &envoy_listener_v3.Listener_ConnectionBalanceConfig_ExactBalance_{},
}
}
}

// Ensure that the first filter in each filter chain of a public listener is
// the authz filter to prevent unauthorized access.
func (s *ResourceGenerator) injectConnectFilters(cfgSnap *proxycfg.ConfigSnapshot, listener *envoy_listener_v3.Listener) error {
Expand Down
6 changes: 6 additions & 0 deletions api/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ type UpstreamConfig struct {
// Namespace is only accepted within a service-defaults config entry.
Namespace string `json:",omitempty"`

// EnvoyConnectionBalanceType specifies how envoy connections should
// be distributed across worker threads. Currently, only the "exact_balance"
// type is accepted.
// https://cloudnative.to/envoy/api-v3/config/listener/v3/listener.proto.html#config-listener-v3-listener-connectionbalanceconfig
EnvoyConnectionBalanceType string `json:",omitempty" alias:"envoy_connection_balance_type"`

// EnvoyListenerJSON is a complete override ("escape hatch") for the upstream's
// listener.
//
Expand Down

0 comments on commit 82d5c1a

Please sign in to comment.