Split up normalizing from defaulting values for upstream cfg

This commit is contained in:
freddygv 2021-03-17 21:37:55 -06:00
parent c4ff0e6eca
commit de7f2a1a74
3 changed files with 21 additions and 8 deletions

View File

@ -384,7 +384,7 @@ func mergeServiceConfig(defaults *structs.ServiceConfigResponse, service *struct
remoteUpstreams := make(map[structs.ServiceID]structs.Upstream) remoteUpstreams := make(map[structs.ServiceID]structs.Upstream)
for _, us := range defaults.UpstreamIDConfigs { for _, us := range defaults.UpstreamIDConfigs {
parsed, err := structs.ParseUpstreamConfig(us.Config) parsed, err := structs.ParseUpstreamConfigNoDefaults(us.Config)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse upstream config map for %s: %v", us.Upstream.String(), err) return nil, fmt.Errorf("failed to parse upstream config map for %s: %v", us.Upstream.String(), err)
} }

View File

@ -701,8 +701,8 @@ func (cfg UpstreamConfig) MergeInto(dst map[string]interface{}) {
func (cfg *UpstreamConfig) Normalize() { func (cfg *UpstreamConfig) Normalize() {
cfg.Protocol = strings.ToLower(cfg.Protocol) cfg.Protocol = strings.ToLower(cfg.Protocol)
if cfg.ConnectTimeoutMs < 1 { if cfg.ConnectTimeoutMs < 0 {
cfg.ConnectTimeoutMs = 5000 cfg.ConnectTimeoutMs = 0
} }
} }
@ -744,6 +744,8 @@ func ParseUpstreamConfigNoDefaults(m map[string]interface{}) (UpstreamConfig, er
} }
err = decoder.Decode(m) err = decoder.Decode(m)
cfg.Normalize()
return cfg, err return cfg, err
} }
@ -753,12 +755,13 @@ func ParseUpstreamConfigNoDefaults(m map[string]interface{}) (UpstreamConfig, er
func ParseUpstreamConfig(m map[string]interface{}) (UpstreamConfig, error) { func ParseUpstreamConfig(m map[string]interface{}) (UpstreamConfig, error) {
cfg, err := ParseUpstreamConfigNoDefaults(m) cfg, err := ParseUpstreamConfigNoDefaults(m)
cfg.Normalize()
// Set default (even if error is returned) // Set default (even if error is returned)
if cfg.Protocol == "" { if cfg.Protocol == "" {
cfg.Protocol = "tcp" cfg.Protocol = "tcp"
} }
if cfg.ConnectTimeoutMs == 0 {
cfg.ConnectTimeoutMs = 5000
}
return cfg, err return cfg, err
} }

View File

@ -1572,14 +1572,14 @@ func TestServiceConfigEntry_Normalize(t *testing.T) {
UpstreamConfigs: map[string]*UpstreamConfig{ UpstreamConfigs: map[string]*UpstreamConfig{
"redis": { "redis": {
Protocol: "tcp", Protocol: "tcp",
ConnectTimeoutMs: 5000, ConnectTimeoutMs: 0,
}, },
"memcached": { "memcached": {
ConnectTimeoutMs: 5000, ConnectTimeoutMs: 0,
}, },
}, },
UpstreamDefaults: &UpstreamConfig{ UpstreamDefaults: &UpstreamConfig{
ConnectTimeoutMs: 5000, ConnectTimeoutMs: 0,
}, },
}, },
}, },
@ -1751,6 +1751,7 @@ func TestParseUpstreamConfig(t *testing.T) {
input: nil, input: nil,
want: UpstreamConfig{ want: UpstreamConfig{
ConnectTimeoutMs: 5000, ConnectTimeoutMs: 5000,
Protocol: "tcp",
}, },
}, },
{ {
@ -1758,6 +1759,7 @@ func TestParseUpstreamConfig(t *testing.T) {
input: map[string]interface{}{}, input: map[string]interface{}{},
want: UpstreamConfig{ want: UpstreamConfig{
ConnectTimeoutMs: 5000, ConnectTimeoutMs: 5000,
Protocol: "tcp",
}, },
}, },
{ {
@ -1768,6 +1770,7 @@ func TestParseUpstreamConfig(t *testing.T) {
}, },
want: UpstreamConfig{ want: UpstreamConfig{
ConnectTimeoutMs: 5000, ConnectTimeoutMs: 5000,
Protocol: "tcp",
}, },
}, },
{ {
@ -1787,6 +1790,7 @@ func TestParseUpstreamConfig(t *testing.T) {
}, },
want: UpstreamConfig{ want: UpstreamConfig{
ConnectTimeoutMs: 1000, ConnectTimeoutMs: 1000,
Protocol: "tcp",
}, },
}, },
{ {
@ -1796,6 +1800,7 @@ func TestParseUpstreamConfig(t *testing.T) {
}, },
want: UpstreamConfig{ want: UpstreamConfig{
ConnectTimeoutMs: 1000, ConnectTimeoutMs: 1000,
Protocol: "tcp",
}, },
}, },
{ {
@ -1805,6 +1810,7 @@ func TestParseUpstreamConfig(t *testing.T) {
}, },
want: UpstreamConfig{ want: UpstreamConfig{
ConnectTimeoutMs: 1000, ConnectTimeoutMs: 1000,
Protocol: "tcp",
}, },
}, },
{ {
@ -1823,6 +1829,7 @@ func TestParseUpstreamConfig(t *testing.T) {
MaxPendingRequests: intPointer(60), MaxPendingRequests: intPointer(60),
MaxConcurrentRequests: intPointer(70), MaxConcurrentRequests: intPointer(70),
}, },
Protocol: "tcp",
}, },
}, },
{ {
@ -1841,6 +1848,7 @@ func TestParseUpstreamConfig(t *testing.T) {
MaxPendingRequests: intPointer(0), MaxPendingRequests: intPointer(0),
MaxConcurrentRequests: intPointer(0), MaxConcurrentRequests: intPointer(0),
}, },
Protocol: "tcp",
}, },
}, },
{ {
@ -1857,6 +1865,7 @@ func TestParseUpstreamConfig(t *testing.T) {
Interval: 22 * time.Second, Interval: 22 * time.Second,
MaxFailures: 7, MaxFailures: 7,
}, },
Protocol: "tcp",
}, },
}, },
{ {
@ -1871,6 +1880,7 @@ func TestParseUpstreamConfig(t *testing.T) {
MeshGateway: MeshGatewayConfig{ MeshGateway: MeshGatewayConfig{
Mode: MeshGatewayModeRemote, Mode: MeshGatewayModeRemote,
}, },
Protocol: "tcp",
}, },
}, },
} }