From 1e5081baf2e7c2ac28a324c2df5415f09ddc2b9c Mon Sep 17 00:00:00 2001 From: Giancarlo Rubio Date: Fri, 3 Mar 2017 13:02:36 +0100 Subject: [PATCH] BuildLogFormatUpstream function was always using the default log-format-upstream, --- controllers/nginx/pkg/config/config.go | 11 +++++++++-- controllers/nginx/pkg/config/config_test.go | 9 ++++++--- controllers/nginx/pkg/template/template.go | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/controllers/nginx/pkg/config/config.go b/controllers/nginx/pkg/config/config.go index c23c158677..b8dbe2185d 100644 --- a/controllers/nginx/pkg/config/config.go +++ b/controllers/nginx/pkg/config/config.go @@ -264,7 +264,7 @@ func NewDefault() Configuration { KeepAlive: 75, LargeClientHeaderBuffers: "4 8k", LogFormatStream: logFormatStream, - LogFormatUpstream: BuildLogFormatUpstream(false), + LogFormatUpstream: BuildLogFormatUpstream(false, ""), MaxWorkerConnections: 16384, MapHashBucketSize: 64, ProxyRealIPCIDR: defIPCIDR, @@ -307,7 +307,14 @@ func NewDefault() Configuration { } // BuildLogFormatUpstream format the log_format upstream based on proxy_protocol -func BuildLogFormatUpstream(useProxyProtocol bool) string { +func BuildLogFormatUpstream(useProxyProtocol bool, curLogFormatUpstream string) string { + + // test if log_format comes from configmap + if curLogFormatUpstream != "" && + curLogFormatUpstream != fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr") && + curLogFormatUpstream != fmt.Sprintf(logFormatUpstream, "$remote_addr") { + return curLogFormatUpstream + } if useProxyProtocol { return fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr") diff --git a/controllers/nginx/pkg/config/config_test.go b/controllers/nginx/pkg/config/config_test.go index d0fccc69b0..b198b8adba 100644 --- a/controllers/nginx/pkg/config/config_test.go +++ b/controllers/nginx/pkg/config/config_test.go @@ -9,15 +9,18 @@ func TestBuildLogFormatUpstream(t *testing.T) { testCases := []struct { useProxyProtocol bool // use proxy protocol + curLogFormat string expected string }{ - {true, fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr")}, - {false, fmt.Sprintf(logFormatUpstream, "$remote_addr")}, + {true, "", fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr")}, + {false, "", fmt.Sprintf(logFormatUpstream, "$remote_addr")}, + {true, "my-log-format", "my-log-format"}, + {false, "john-log-format", "john-log-format"}, } for _, testCase := range testCases { - result := BuildLogFormatUpstream(testCase.useProxyProtocol) + result := BuildLogFormatUpstream(testCase.useProxyProtocol, testCase.curLogFormat) if result != testCase.expected { t.Errorf(" expected %v but return %v", testCase.expected, result) diff --git a/controllers/nginx/pkg/template/template.go b/controllers/nginx/pkg/template/template.go index d23a78be2e..c366943633 100644 --- a/controllers/nginx/pkg/template/template.go +++ b/controllers/nginx/pkg/template/template.go @@ -235,7 +235,7 @@ func buildLogFormatUpstream(input interface{}) string { glog.Errorf("error an ingress.buildLogFormatUpstream type but %T was returned", input) } - return nginxconfig.BuildLogFormatUpstream(config.UseProxyProtocol) + return nginxconfig.BuildLogFormatUpstream(config.UseProxyProtocol, config.LogFormatUpstream) }