Cleanup custom log format configuration

This commit is contained in:
Manuel de Brito Fontes 2017-03-04 16:46:45 -03:00
parent 75124bc9f1
commit 2399be867e
4 changed files with 22 additions and 31 deletions

View file

@ -17,11 +17,11 @@ limitations under the License.
package config package config
import ( import (
"fmt"
"runtime" "runtime"
"github.com/golang/glog" "github.com/golang/glog"
"fmt"
"k8s.io/ingress/core/pkg/ingress" "k8s.io/ingress/core/pkg/ingress"
"k8s.io/ingress/core/pkg/ingress/defaults" "k8s.io/ingress/core/pkg/ingress/defaults"
) )
@ -47,9 +47,9 @@ const (
gzipTypes = "application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component" gzipTypes = "application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component"
logFormatUpstream = "'%v - [$proxy_add_x_forwarded_for] - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" $request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status'" logFormatUpstream = `[$proxy_add_x_forwarded_for] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status"`
logFormatStream = "'$remote_addr [$time_local] $protocol [$ssl_preread_server_name] [$stream_upstream] $status $bytes_sent $bytes_received $session_time'" logFormatStream = `[$time_local] $protocol [$ssl_preread_server_name] [$stream_upstream] $status $bytes_sent $bytes_received $session_time`
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_buffer_size // http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_buffer_size
// Sets the size of the buffer used for sending data. // Sets the size of the buffer used for sending data.
@ -264,7 +264,7 @@ func NewDefault() Configuration {
KeepAlive: 75, KeepAlive: 75,
LargeClientHeaderBuffers: "4 8k", LargeClientHeaderBuffers: "4 8k",
LogFormatStream: logFormatStream, LogFormatStream: logFormatStream,
LogFormatUpstream: BuildLogFormatUpstream(false, ""), LogFormatUpstream: logFormatUpstream,
MaxWorkerConnections: 16384, MaxWorkerConnections: 16384,
MapHashBucketSize: 64, MapHashBucketSize: 64,
ProxyRealIPCIDR: defIPCIDR, ProxyRealIPCIDR: defIPCIDR,
@ -307,20 +307,14 @@ func NewDefault() Configuration {
return cfg return cfg
} }
// BuildLogFormatUpstream format the log_format upstream based on proxy_protocol // BuildLogFormatUpstream format the log_format upstream using
func BuildLogFormatUpstream(useProxyProtocol bool, curLogFormatUpstream string) string { // proxy_protocol_addr as remote client address if UseProxyProtocol
// is enabled.
// test if log_format comes from configmap func (cfg Configuration) BuildLogFormatUpstream() string {
if curLogFormatUpstream != "" && if cfg.UseProxyProtocol {
curLogFormatUpstream != fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr") && return fmt.Sprintf("$proxy_protocol_addr - %s", cfg.LogFormatUpstream)
curLogFormatUpstream != fmt.Sprintf(logFormatUpstream, "$remote_addr") {
return curLogFormatUpstream
} }
return fmt.Sprintf("$remote_addr - %s", cfg.LogFormatUpstream)
if useProxyProtocol {
return fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr")
}
return fmt.Sprintf(logFormatUpstream, "$remote_addr")
} }
// TemplateConfig contains the nginx configuration to render the file nginx.conf // TemplateConfig contains the nginx configuration to render the file nginx.conf

View file

@ -12,19 +12,19 @@ func TestBuildLogFormatUpstream(t *testing.T) {
curLogFormat string curLogFormat string
expected string expected string
}{ }{
{true, "", fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr")}, {true, logFormatUpstream, fmt.Sprintf("$proxy_protocol_addr - %s", logFormatUpstream)},
{false, "", fmt.Sprintf(logFormatUpstream, "$remote_addr")}, {false, logFormatUpstream, fmt.Sprintf("$remote_addr - %s", logFormatUpstream)},
{true, "my-log-format", "my-log-format"}, {true, "my-log-format", "$proxy_protocol_addr - my-log-format"},
{false, "john-log-format", "john-log-format"}, {false, "john-log-format", "$remote_addr - john-log-format"},
} }
for _, testCase := range testCases { for _, testCase := range testCases {
cfg := NewDefault()
result := BuildLogFormatUpstream(testCase.useProxyProtocol, testCase.curLogFormat) cfg.UseProxyProtocol = testCase.useProxyProtocol
cfg.LogFormatUpstream = testCase.curLogFormat
result := cfg.BuildLogFormatUpstream()
if result != testCase.expected { if result != testCase.expected {
t.Errorf(" expected %v but return %v", testCase.expected, result) t.Errorf(" expected %v but return %v", testCase.expected, result)
} }
} }
} }

View file

@ -31,7 +31,6 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"k8s.io/ingress/controllers/nginx/pkg/config" "k8s.io/ingress/controllers/nginx/pkg/config"
nginxconfig "k8s.io/ingress/controllers/nginx/pkg/config"
"k8s.io/ingress/core/pkg/ingress" "k8s.io/ingress/core/pkg/ingress"
ing_net "k8s.io/ingress/core/pkg/net" ing_net "k8s.io/ingress/core/pkg/net"
"k8s.io/ingress/core/pkg/watch" "k8s.io/ingress/core/pkg/watch"
@ -229,14 +228,12 @@ func buildAuthLocation(input interface{}) string {
} }
func buildLogFormatUpstream(input interface{}) string { func buildLogFormatUpstream(input interface{}) string {
config, ok := input.(config.Configuration) cfg, ok := input.(config.Configuration)
if !ok { if !ok {
glog.Errorf("error an ingress.buildLogFormatUpstream type but %T was returned", input) glog.Errorf("error an ingress.buildLogFormatUpstream type but %T was returned", input)
} }
return nginxconfig.BuildLogFormatUpstream(config.UseProxyProtocol, config.LogFormatUpstream) return cfg.BuildLogFormatUpstream()
} }
// buildProxyPass produces the proxy pass string, if the ingress has redirects // buildProxyPass produces the proxy pass string, if the ingress has redirects

View file

@ -79,7 +79,7 @@ http {
server_tokens {{ if $cfg.ShowServerTokens }}on{{ else }}off{{ end }}; server_tokens {{ if $cfg.ShowServerTokens }}on{{ else }}off{{ end }};
log_format upstreaminfo {{ buildLogFormatUpstream $cfg }}; log_format upstreaminfo '{{ buildLogFormatUpstream $cfg }}';
{{/* map urls that should not appear in access.log */}} {{/* map urls that should not appear in access.log */}}
{{/* http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log */}} {{/* http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log */}}