From de7f2a1a74c1141b852cb54c05ed6573ee045c5f Mon Sep 17 00:00:00 2001 From: freddygv Date: Wed, 17 Mar 2021 21:37:55 -0600 Subject: [PATCH] Split up normalizing from defaulting values for upstream cfg --- agent/service_manager.go | 2 +- agent/structs/config_entry.go | 11 +++++++---- agent/structs/config_entry_test.go | 16 +++++++++++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/agent/service_manager.go b/agent/service_manager.go index 2c0fc2f2d..29808580e 100644 --- a/agent/service_manager.go +++ b/agent/service_manager.go @@ -384,7 +384,7 @@ func mergeServiceConfig(defaults *structs.ServiceConfigResponse, service *struct remoteUpstreams := make(map[structs.ServiceID]structs.Upstream) for _, us := range defaults.UpstreamIDConfigs { - parsed, err := structs.ParseUpstreamConfig(us.Config) + parsed, err := structs.ParseUpstreamConfigNoDefaults(us.Config) if err != nil { return nil, fmt.Errorf("failed to parse upstream config map for %s: %v", us.Upstream.String(), err) } diff --git a/agent/structs/config_entry.go b/agent/structs/config_entry.go index ef6c8c781..c2427dfa9 100644 --- a/agent/structs/config_entry.go +++ b/agent/structs/config_entry.go @@ -701,8 +701,8 @@ func (cfg UpstreamConfig) MergeInto(dst map[string]interface{}) { func (cfg *UpstreamConfig) Normalize() { cfg.Protocol = strings.ToLower(cfg.Protocol) - if cfg.ConnectTimeoutMs < 1 { - cfg.ConnectTimeoutMs = 5000 + if cfg.ConnectTimeoutMs < 0 { + cfg.ConnectTimeoutMs = 0 } } @@ -744,6 +744,8 @@ func ParseUpstreamConfigNoDefaults(m map[string]interface{}) (UpstreamConfig, er } err = decoder.Decode(m) + cfg.Normalize() + return cfg, err } @@ -753,12 +755,13 @@ func ParseUpstreamConfigNoDefaults(m map[string]interface{}) (UpstreamConfig, er func ParseUpstreamConfig(m map[string]interface{}) (UpstreamConfig, error) { cfg, err := ParseUpstreamConfigNoDefaults(m) - cfg.Normalize() - // Set default (even if error is returned) if cfg.Protocol == "" { cfg.Protocol = "tcp" } + if cfg.ConnectTimeoutMs == 0 { + cfg.ConnectTimeoutMs = 5000 + } return cfg, err } diff --git a/agent/structs/config_entry_test.go b/agent/structs/config_entry_test.go index 97bc1c972..523cab002 100644 --- a/agent/structs/config_entry_test.go +++ b/agent/structs/config_entry_test.go @@ -1572,14 +1572,14 @@ func TestServiceConfigEntry_Normalize(t *testing.T) { UpstreamConfigs: map[string]*UpstreamConfig{ "redis": { Protocol: "tcp", - ConnectTimeoutMs: 5000, + ConnectTimeoutMs: 0, }, "memcached": { - ConnectTimeoutMs: 5000, + ConnectTimeoutMs: 0, }, }, UpstreamDefaults: &UpstreamConfig{ - ConnectTimeoutMs: 5000, + ConnectTimeoutMs: 0, }, }, }, @@ -1751,6 +1751,7 @@ func TestParseUpstreamConfig(t *testing.T) { input: nil, want: UpstreamConfig{ ConnectTimeoutMs: 5000, + Protocol: "tcp", }, }, { @@ -1758,6 +1759,7 @@ func TestParseUpstreamConfig(t *testing.T) { input: map[string]interface{}{}, want: UpstreamConfig{ ConnectTimeoutMs: 5000, + Protocol: "tcp", }, }, { @@ -1768,6 +1770,7 @@ func TestParseUpstreamConfig(t *testing.T) { }, want: UpstreamConfig{ ConnectTimeoutMs: 5000, + Protocol: "tcp", }, }, { @@ -1787,6 +1790,7 @@ func TestParseUpstreamConfig(t *testing.T) { }, want: UpstreamConfig{ ConnectTimeoutMs: 1000, + Protocol: "tcp", }, }, { @@ -1796,6 +1800,7 @@ func TestParseUpstreamConfig(t *testing.T) { }, want: UpstreamConfig{ ConnectTimeoutMs: 1000, + Protocol: "tcp", }, }, { @@ -1805,6 +1810,7 @@ func TestParseUpstreamConfig(t *testing.T) { }, want: UpstreamConfig{ ConnectTimeoutMs: 1000, + Protocol: "tcp", }, }, { @@ -1823,6 +1829,7 @@ func TestParseUpstreamConfig(t *testing.T) { MaxPendingRequests: intPointer(60), MaxConcurrentRequests: intPointer(70), }, + Protocol: "tcp", }, }, { @@ -1841,6 +1848,7 @@ func TestParseUpstreamConfig(t *testing.T) { MaxPendingRequests: intPointer(0), MaxConcurrentRequests: intPointer(0), }, + Protocol: "tcp", }, }, { @@ -1857,6 +1865,7 @@ func TestParseUpstreamConfig(t *testing.T) { Interval: 22 * time.Second, MaxFailures: 7, }, + Protocol: "tcp", }, }, { @@ -1871,6 +1880,7 @@ func TestParseUpstreamConfig(t *testing.T) { MeshGateway: MeshGatewayConfig{ Mode: MeshGatewayModeRemote, }, + Protocol: "tcp", }, }, }