From 7cb3f326720eea64f229898e2aec5d14b07dec3b Mon Sep 17 00:00:00 2001 From: freddygv Date: Tue, 13 Apr 2021 11:54:56 -0600 Subject: [PATCH 1/2] Convert new tproxy structs in api module into ptrs This way we avoid serializing these when empty. Otherwise users of the latest version of the api submodule cannot interact with older versions of Consul, because a new api client would send keys that the older Consul doesn't recognize yet. --- agent/structs/connect_proxy_config.go | 4 +-- api/agent.go | 20 +++++++------- api/catalog_test.go | 4 +++ api/config_entry.go | 32 +++++++++++------------ api/config_entry_test.go | 6 ++--- command/config/write/config_write_test.go | 8 +++--- 6 files changed, 39 insertions(+), 35 deletions(-) diff --git a/agent/structs/connect_proxy_config.go b/agent/structs/connect_proxy_config.go index 225c663d75..2d534ffd3e 100644 --- a/agent/structs/connect_proxy_config.go +++ b/agent/structs/connect_proxy_config.go @@ -110,8 +110,8 @@ type TransparentProxyConfig struct { OutboundListenerPort int `json:",omitempty" alias:"outbound_listener_port"` } -func (c TransparentProxyConfig) ToAPI() api.TransparentProxyConfig { - return api.TransparentProxyConfig{OutboundListenerPort: c.OutboundListenerPort} +func (c TransparentProxyConfig) ToAPI() *api.TransparentProxyConfig { + return &api.TransparentProxyConfig{OutboundListenerPort: c.OutboundListenerPort} } // ConnectProxyConfig describes the configuration needed for any proxy managed diff --git a/api/agent.go b/api/agent.go index acafa9cc91..c21ef20169 100644 --- a/api/agent.go +++ b/api/agent.go @@ -114,16 +114,16 @@ type AgentServiceConnect struct { // AgentServiceConnectProxyConfig is the proxy configuration in a connect-proxy // ServiceDefinition or response. type AgentServiceConnectProxyConfig struct { - DestinationServiceName string `json:",omitempty"` - DestinationServiceID string `json:",omitempty"` - LocalServiceAddress string `json:",omitempty"` - LocalServicePort int `json:",omitempty"` - Mode ProxyMode `json:",omitempty"` - TransparentProxy TransparentProxyConfig `json:",omitempty"` - Config map[string]interface{} `json:",omitempty" bexpr:"-"` - Upstreams []Upstream `json:",omitempty"` - MeshGateway MeshGatewayConfig `json:",omitempty"` - Expose ExposeConfig `json:",omitempty"` + DestinationServiceName string `json:",omitempty"` + DestinationServiceID string `json:",omitempty"` + LocalServiceAddress string `json:",omitempty"` + LocalServicePort int `json:",omitempty"` + Mode ProxyMode `json:",omitempty"` + TransparentProxy *TransparentProxyConfig `json:",omitempty"` + Config map[string]interface{} `json:",omitempty" bexpr:"-"` + Upstreams []Upstream `json:",omitempty"` + MeshGateway MeshGatewayConfig `json:",omitempty"` + Expose ExposeConfig `json:",omitempty"` } const ( diff --git a/api/catalog_test.go b/api/catalog_test.go index 0b8af734f0..faa7c73b62 100644 --- a/api/catalog_test.go +++ b/api/catalog_test.go @@ -505,6 +505,10 @@ func testUnmanagedProxy(t *testing.T) *AgentService { LocalServiceAddress: "127.0.0.2", LocalServicePort: 8080, Upstreams: testUpstreams(t), + Mode: ProxyModeTransparent, + TransparentProxy: &TransparentProxyConfig{ + OutboundListenerPort: 808, + }, }, ID: "web-proxy1", Service: "web-proxy", diff --git a/api/config_entry.go b/api/config_entry.go index 7ca8e3d78f..8e516b80ea 100644 --- a/api/config_entry.go +++ b/api/config_entry.go @@ -193,15 +193,15 @@ type UpstreamLimits struct { type ServiceConfigEntry struct { Kind string Name string - Namespace string `json:",omitempty"` - Protocol string `json:",omitempty"` - Mode ProxyMode `json:",omitempty"` - TransparentProxy TransparentProxyConfig `json:",omitempty" alias:"transparent_proxy"` - MeshGateway MeshGatewayConfig `json:",omitempty" alias:"mesh_gateway"` - Connect ConnectConfiguration `json:",omitempty"` - Expose ExposeConfig `json:",omitempty"` - ExternalSNI string `json:",omitempty" alias:"external_sni"` - Meta map[string]string `json:",omitempty"` + Namespace string `json:",omitempty"` + Protocol string `json:",omitempty"` + Mode ProxyMode `json:",omitempty"` + TransparentProxy *TransparentProxyConfig `json:",omitempty" alias:"transparent_proxy"` + MeshGateway MeshGatewayConfig `json:",omitempty" alias:"mesh_gateway"` + Connect *ConnectConfiguration `json:",omitempty"` + Expose ExposeConfig `json:",omitempty"` + ExternalSNI string `json:",omitempty" alias:"external_sni"` + Meta map[string]string `json:",omitempty"` CreateIndex uint64 ModifyIndex uint64 } @@ -233,13 +233,13 @@ func (s *ServiceConfigEntry) GetModifyIndex() uint64 { type ProxyConfigEntry struct { Kind string Name string - Namespace string `json:",omitempty"` - Mode ProxyMode `json:",omitempty"` - TransparentProxy TransparentProxyConfig `json:",omitempty" alias:"transparent_proxy"` - Config map[string]interface{} `json:",omitempty"` - MeshGateway MeshGatewayConfig `json:",omitempty" alias:"mesh_gateway"` - Expose ExposeConfig `json:",omitempty"` - Meta map[string]string `json:",omitempty"` + Namespace string `json:",omitempty"` + Mode ProxyMode `json:",omitempty"` + TransparentProxy *TransparentProxyConfig `json:",omitempty" alias:"transparent_proxy"` + Config map[string]interface{} `json:",omitempty"` + MeshGateway MeshGatewayConfig `json:",omitempty" alias:"mesh_gateway"` + Expose ExposeConfig `json:",omitempty"` + Meta map[string]string `json:",omitempty"` CreateIndex uint64 ModifyIndex uint64 } diff --git a/api/config_entry_test.go b/api/config_entry_test.go index 7492bc8722..d0425523e0 100644 --- a/api/config_entry_test.go +++ b/api/config_entry_test.go @@ -321,7 +321,7 @@ func TestDecodeConfigEntry(t *testing.T) { Mode: MeshGatewayModeRemote, }, Mode: ProxyModeTransparent, - TransparentProxy: TransparentProxyConfig{OutboundListenerPort: 808}, + TransparentProxy: &TransparentProxyConfig{OutboundListenerPort: 808}, }, }, { @@ -388,8 +388,8 @@ func TestDecodeConfigEntry(t *testing.T) { Mode: MeshGatewayModeRemote, }, Mode: ProxyModeTransparent, - TransparentProxy: TransparentProxyConfig{OutboundListenerPort: 808}, - Connect: ConnectConfiguration{ + TransparentProxy: &TransparentProxyConfig{OutboundListenerPort: 808}, + Connect: &ConnectConfiguration{ UpstreamConfigs: map[string]UpstreamConfig{ "redis": { PassiveHealthCheck: &PassiveHealthCheck{ diff --git a/command/config/write/config_write_test.go b/command/config/write/config_write_test.go index 707b403750..a9eb84d0da 100644 --- a/command/config/write/config_write_test.go +++ b/command/config/write/config_write_test.go @@ -275,7 +275,7 @@ func TestParseConfigEntry(t *testing.T) { Mode: api.MeshGatewayModeRemote, }, Mode: api.ProxyModeDirect, - TransparentProxy: api.TransparentProxyConfig{ + TransparentProxy: &api.TransparentProxyConfig{ OutboundListenerPort: 10101, }, }, @@ -297,7 +297,7 @@ func TestParseConfigEntry(t *testing.T) { Mode: api.MeshGatewayModeRemote, }, Mode: api.ProxyModeDirect, - TransparentProxy: api.TransparentProxyConfig{ + TransparentProxy: &api.TransparentProxyConfig{ OutboundListenerPort: 10101, }, }, @@ -654,10 +654,10 @@ func TestParseConfigEntry(t *testing.T) { Mode: api.MeshGatewayModeRemote, }, Mode: api.ProxyModeDirect, - TransparentProxy: api.TransparentProxyConfig{ + TransparentProxy: &api.TransparentProxyConfig{ OutboundListenerPort: 10101, }, - Connect: api.ConnectConfiguration{ + Connect: &api.ConnectConfiguration{ UpstreamConfigs: map[string]api.UpstreamConfig{ "redis": { PassiveHealthCheck: &api.PassiveHealthCheck{ From e1808af729124444cc0e633edf3ddbb1d8401c17 Mon Sep 17 00:00:00 2001 From: freddygv Date: Tue, 13 Apr 2021 16:08:41 -0600 Subject: [PATCH 2/2] Fixup tests --- agent/agent_endpoint_test.go | 4 ++++ agent/structs/connect_proxy_config_test.go | 8 ++++++++ command/services/config_test.go | 8 ++++++++ 3 files changed, 20 insertions(+) diff --git a/agent/agent_endpoint_test.go b/agent/agent_endpoint_test.go index 1347055fd2..5aba74bf16 100644 --- a/agent/agent_endpoint_test.go +++ b/agent/agent_endpoint_test.go @@ -3519,6 +3519,10 @@ func testAgent_RegisterService_UnmanagedConnectProxy(t *testing.T, extraHCL stri LocalBindPort: 1235, }, }, + Mode: api.ProxyModeTransparent, + TransparentProxy: &api.TransparentProxyConfig{ + OutboundListenerPort: 808, + }, }, } diff --git a/agent/structs/connect_proxy_config_test.go b/agent/structs/connect_proxy_config_test.go index df680a0690..e57054a8fe 100644 --- a/agent/structs/connect_proxy_config_test.go +++ b/agent/structs/connect_proxy_config_test.go @@ -46,6 +46,10 @@ func TestConnectProxyConfig_ToAPI(t *testing.T) { LocalBindAddress: "127.10.10.10", }, }, + Mode: ProxyModeTransparent, + TransparentProxy: TransparentProxyConfig{ + OutboundListenerPort: 808, + }, }, want: &api.AgentServiceConnectProxyConfig{ DestinationServiceName: "web", @@ -76,6 +80,10 @@ func TestConnectProxyConfig_ToAPI(t *testing.T) { LocalBindAddress: "127.10.10.10", }, }, + Mode: api.ProxyModeTransparent, + TransparentProxy: &api.TransparentProxyConfig{ + OutboundListenerPort: 808, + }, }, }, } diff --git a/command/services/config_test.go b/command/services/config_test.go index ccf0d36228..d0bd35bfa4 100644 --- a/command/services/config_test.go +++ b/command/services/config_test.go @@ -122,6 +122,10 @@ func TestStructsToAgentService(t *testing.T) { LocalServiceAddress: "127.0.0.1", LocalServicePort: 8181, Upstreams: structs.TestUpstreams(t), + Mode: structs.ProxyModeTransparent, + TransparentProxy: structs.TransparentProxyConfig{ + OutboundListenerPort: 808, + }, Config: map[string]interface{}{ "foo": "bar", }, @@ -138,6 +142,10 @@ func TestStructsToAgentService(t *testing.T) { LocalServiceAddress: "127.0.0.1", LocalServicePort: 8181, Upstreams: structs.TestUpstreams(t).ToAPI(), + Mode: api.ProxyModeTransparent, + TransparentProxy: &api.TransparentProxyConfig{ + OutboundListenerPort: 808, + }, Config: map[string]interface{}{ "foo": "bar", },