Skip to content

Commit

Permalink
fix integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
DanStough committed Dec 22, 2022
1 parent d016288 commit 1869cbb
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
36 changes: 31 additions & 5 deletions agent/structs/connect_proxy_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,16 @@ type AccessLogsConfig struct {
TextFormat string `json:",omitempty" alias:"text_format"`
}

func (c AccessLogsConfig) ToAPI() api.AccessLogsConfig {
return api.AccessLogsConfig{
func (c *AccessLogsConfig) IsZero() bool {
zeroVal := AccessLogsConfig{}
return *c == zeroVal
}

func (c *AccessLogsConfig) ToAPI() *api.AccessLogsConfig {
if c.IsZero() {
return nil
}
return &api.AccessLogsConfig{
Enabled: c.Enabled,
DisableListenerLogs: c.DisableListenerLogs,
Type: api.LogSinkType(c.Type),
Expand All @@ -185,7 +193,7 @@ func (c AccessLogsConfig) ToAPI() api.AccessLogsConfig {
}
}

func (c AccessLogsConfig) Validate() error {
func (c *AccessLogsConfig) Validate() error {
switch c.Type {
case DefaultLogSinkType, StdErrLogSinkType, StdOutLogSinkType:
// OK
Expand Down Expand Up @@ -283,6 +291,7 @@ func (t *ConnectProxyConfig) UnmarshalJSON(data []byte) (err error) {
LocalServiceSocketPathSnake string `json:"local_service_socket_path"`
MeshGatewaySnake MeshGatewayConfig `json:"mesh_gateway"`
TransparentProxySnake TransparentProxyConfig `json:"transparent_proxy"`
AccessLogsSnake AccessLogsConfig `json:"access_logs"`
*Alias
}{
Alias: (*Alias)(t),
Expand Down Expand Up @@ -314,7 +323,24 @@ func (t *ConnectProxyConfig) UnmarshalJSON(data []byte) (err error) {
if !t.TransparentProxy.DialedDirectly {
t.TransparentProxy.DialedDirectly = aux.TransparentProxySnake.DialedDirectly
}

if !t.AccessLogs.Enabled {
t.AccessLogs.Enabled = aux.AccessLogsSnake.Enabled
}
if !t.AccessLogs.DisableListenerLogs {
t.AccessLogs.DisableListenerLogs = aux.AccessLogsSnake.DisableListenerLogs
}
if t.AccessLogs.Type == "" {
t.AccessLogs.Type = aux.AccessLogsSnake.Type
}
if t.AccessLogs.Path == "" {
t.AccessLogs.Path = aux.AccessLogsSnake.Path
}
if t.AccessLogs.JSONFormat == "" {
t.AccessLogs.JSONFormat = aux.AccessLogsSnake.JSONFormat
}
if t.AccessLogs.TextFormat == "" {
t.AccessLogs.TextFormat = aux.AccessLogsSnake.TextFormat
}
return nil
}

Expand All @@ -338,7 +364,7 @@ func (c *ConnectProxyConfig) MarshalJSON() ([]byte, error) {
out.TransparentProxy = &out.Alias.TransparentProxy
}

if c.AccessLogs.Enabled {
if !c.AccessLogs.IsZero() {
out.AccessLogs = &out.Alias.AccessLogs
}

Expand Down
2 changes: 1 addition & 1 deletion api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ type AgentServiceConnectProxyConfig struct {
Upstreams []Upstream `json:",omitempty"`
MeshGateway MeshGatewayConfig `json:",omitempty"`
Expose ExposeConfig `json:",omitempty"`
AccessLogs AccessLogsConfig `json:",omitempty"`
AccessLogs *AccessLogsConfig `json:",omitempty"`
}

const (
Expand Down
10 changes: 10 additions & 0 deletions command/services/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ func serviceToAgentService(svc *structs.ServiceDefinition) (*api.AgentServiceReg
result.Check = nil
}

// The structs version has non-pointer Proxy.TransparentProxy and Proxy.AccessLogs
// The destination has pointers, so we need to set the destination to nil if there
// is a zero-value field.
if result.Proxy != nil && result.Proxy.TransparentProxy != nil && reflect.DeepEqual(*result.Proxy.TransparentProxy, api.TransparentProxyConfig{}) {
result.Proxy.TransparentProxy = nil
}
if result.Proxy != nil && result.Proxy.AccessLogs != nil && reflect.DeepEqual(*result.Proxy.AccessLogs, api.AccessLogsConfig{}) {
result.Proxy.AccessLogs = nil
}

return &result, nil
}

Expand Down
51 changes: 51 additions & 0 deletions command/services/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,43 @@ func TestStructsToAgentService(t *testing.T) {
},
{
"Proxy service",
&structs.ServiceDefinition{
Name: "web-proxy",
Kind: structs.ServiceKindConnectProxy,
Tags: []string{"leader"},
Port: 1234,
Proxy: &structs.ConnectProxyConfig{
DestinationServiceID: "web1",
DestinationServiceName: "web",
LocalServiceAddress: "127.0.0.1",
LocalServicePort: 8181,
Upstreams: structs.TestUpstreams(t),
Mode: structs.ProxyModeTransparent,
Config: map[string]interface{}{
"foo": "bar",
},
},
},
&api.AgentServiceRegistration{
Name: "web-proxy",
Tags: []string{"leader"},
Port: 1234,
Kind: api.ServiceKindConnectProxy,
Proxy: &api.AgentServiceConnectProxyConfig{
DestinationServiceID: "web1",
DestinationServiceName: "web",
LocalServiceAddress: "127.0.0.1",
LocalServicePort: 8181,
Upstreams: structs.TestUpstreams(t).ToAPI(),
Mode: api.ProxyModeTransparent,
Config: map[string]interface{}{
"foo": "bar",
},
},
},
},
{
"TProxy proxy service w/ access logs",
&structs.ServiceDefinition{
Name: "web-proxy",
Kind: structs.ServiceKindConnectProxy,
Expand All @@ -142,6 +179,13 @@ func TestStructsToAgentService(t *testing.T) {
TransparentProxy: structs.TransparentProxyConfig{
OutboundListenerPort: 808,
},
AccessLogs: structs.AccessLogsConfig{
Enabled: true,
DisableListenerLogs: true,
Type: structs.FileLogSinkType,
Path: "/var/logs/envoy.logs",
TextFormat: "MY START TIME %START_TIME%",
},
Config: map[string]interface{}{
"foo": "bar",
},
Expand All @@ -162,6 +206,13 @@ func TestStructsToAgentService(t *testing.T) {
TransparentProxy: &api.TransparentProxyConfig{
OutboundListenerPort: 808,
},
AccessLogs: &api.AccessLogsConfig{
Enabled: true,
DisableListenerLogs: true,
Type: api.FileLogSinkType,
Path: "/var/logs/envoy.logs",
TextFormat: "MY START TIME %START_TIME%",
},
Config: map[string]interface{}{
"foo": "bar",
},
Expand Down

0 comments on commit 1869cbb

Please sign in to comment.