From 98db449208ae4ed1068abada33dba3c4fcce1e4e Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Wed, 1 Apr 2020 10:22:31 -0600 Subject: [PATCH] connect: fix bug where absent connect.proxy stanza needs default config In some refactoring, a bug was introduced where if the connect.proxy stanza in a submitted job was nil, the default proxy configuration would not be initialized with default values, effectively breaking Connect. connect { sidecar_service {} # should work } In contrast, by setting an empty proxy stanza, the config values would be inserted correctly. connect { sidecar_service { proxy {} # workaround } } This commit restores the original behavior, where having a proxy stanza present is not required. The unit test for this case has also been corrected. --- command/agent/consul/connect.go | 2 +- command/agent/consul/connect_test.go | 29 +++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/command/agent/consul/connect.go b/command/agent/consul/connect.go index 5adba80fa..888824766 100644 --- a/command/agent/consul/connect.go +++ b/command/agent/consul/connect.go @@ -58,7 +58,7 @@ func connectSidecarRegistration(serviceName string, css *structs.ConsulSidecarSe func connectProxy(proxy *structs.ConsulProxy, cPort int, networks structs.Networks) (*api.AgentServiceConnectProxyConfig, error) { if proxy == nil { - return nil, nil + proxy = new(structs.ConsulProxy) } expose, err := connectProxyExpose(proxy.Expose, networks) diff --git a/command/agent/consul/connect_test.go b/command/agent/consul/connect_test.go index 5f14988c6..8ce74c1df 100644 --- a/command/agent/consul/connect_test.go +++ b/command/agent/consul/connect_test.go @@ -52,6 +52,12 @@ func TestConnect_newConnect(t *testing.T) { Tags: []string{"foo", "bar"}, Port: 3000, Address: "192.168.30.1", + Proxy: &api.AgentServiceConnectProxyConfig{ + Config: map[string]interface{}{ + "bind_address": "0.0.0.0", + "bind_port": 3000, + }, + }, }, asr.SidecarService) }) } @@ -95,6 +101,12 @@ func TestConnect_connectSidecarRegistration(t *testing.T) { Tags: []string{"foo", "bar"}, Port: 3000, Address: "192.168.30.1", + Proxy: &api.AgentServiceConnectProxyConfig{ + Config: map[string]interface{}{ + "bind_address": "0.0.0.0", + "bind_port": 3000, + }, + }, }, proxy) }) } @@ -102,10 +114,21 @@ func TestConnect_connectSidecarRegistration(t *testing.T) { func TestConnect_connectProxy(t *testing.T) { t.Parallel() - t.Run("nil", func(t *testing.T) { - proxy, err := connectProxy(nil, 2000, nil) + // If the input proxy is nil, we expect the output to be a proxy with its + // config set to default values. + t.Run("nil proxy", func(t *testing.T) { + proxy, err := connectProxy(nil, 2000, testConnectNetwork) require.NoError(t, err) - require.Nil(t, proxy) + require.Equal(t, &api.AgentServiceConnectProxyConfig{ + LocalServiceAddress: "", + LocalServicePort: 0, + Upstreams: nil, + Expose: api.ExposeConfig{}, + Config: map[string]interface{}{ + "bind_address": "0.0.0.0", + "bind_port": 2000, + }, + }, proxy) }) t.Run("bad proxy", func(t *testing.T) {