Skip to content

Commit 84acaec

Browse files
authored
Merge pull request #373 from aledbf/log-format
Cleanup
2 parents 75124bc + cd924f5 commit 84acaec

File tree

5 files changed

+27
-58
lines changed

5 files changed

+27
-58
lines changed

controllers/nginx/pkg/cmd/controller/nginx.go

+5-21
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"time"
3030

3131
"github.com/golang/glog"
32-
"github.com/mitchellh/mapstructure"
3332
"github.com/spf13/pflag"
3433

3534
"k8s.io/kubernetes/pkg/api"
@@ -186,26 +185,7 @@ func (n NGINXController) BackendDefaults() defaults.Backend {
186185
return d.Backend
187186
}
188187

189-
return n.backendDefaults()
190-
}
191-
192-
func (n *NGINXController) backendDefaults() defaults.Backend {
193-
d := config.NewDefault()
194-
config := &mapstructure.DecoderConfig{
195-
Metadata: nil,
196-
WeaklyTypedInput: true,
197-
Result: &d,
198-
TagName: "json",
199-
}
200-
decoder, err := mapstructure.NewDecoder(config)
201-
if err != nil {
202-
glog.Warningf("unexpected error merging defaults: %v", err)
203-
}
204-
err = decoder.Decode(n.configmap.Data)
205-
if err != nil {
206-
glog.Warningf("unexpected error decoding: %v", err)
207-
}
208-
return d.Backend
188+
return ngx_template.ReadConfig(n.configmap.Data).Backend
209189
}
210190

211191
// isReloadRequired check if the new configuration file is different
@@ -260,6 +240,10 @@ func (n NGINXController) Info() *ingress.BackendInfo {
260240

261241
// OverrideFlags customize NGINX controller flags
262242
func (n NGINXController) OverrideFlags(flags *pflag.FlagSet) {
243+
ig, err := flags.GetString("ingress-class")
244+
if err == nil && ig != "" && ig != defIngressClass {
245+
glog.Warningf("only Ingress with class %v will be processed by this ingress controller", ig)
246+
}
263247
flags.Set("ingress-class", defIngressClass)
264248
}
265249

controllers/nginx/pkg/config/config.go

+11-23
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ limitations under the License.
1717
package config
1818

1919
import (
20+
"fmt"
2021
"runtime"
2122

2223
"github.com/golang/glog"
2324

24-
"fmt"
2525
"k8s.io/ingress/core/pkg/ingress"
2626
"k8s.io/ingress/core/pkg/ingress/defaults"
2727
)
@@ -47,9 +47,9 @@ const (
4747

4848
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"
4949

50-
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'"
50+
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"`
5151

52-
logFormatStream = "'$remote_addr [$time_local] $protocol [$ssl_preread_server_name] [$stream_upstream] $status $bytes_sent $bytes_received $session_time'"
52+
logFormatStream = `[$time_local] $protocol [$ssl_preread_server_name] [$stream_upstream] $status $bytes_sent $bytes_received $session_time`
5353

5454
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_buffer_size
5555
// Sets the size of the buffer used for sending data.
@@ -97,11 +97,6 @@ type Configuration struct {
9797
//http://nginx.org/en/docs/http/ngx_http_log_module.html
9898
DisableAccessLog bool `json:"disable-access-log,omitempty"`
9999

100-
// EnableSPDY enables spdy and use ALPN and NPN to advertise the availability of the two protocols
101-
// https://blog.cloudflare.com/open-sourcing-our-nginx-http-2-spdy-code
102-
// By default this is enabled
103-
EnableSPDY bool `json:"enable-spdy"`
104-
105100
// EnableStickySessions enabled sticky sessions using cookies
106101
// https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng
107102
// By default this is disabled
@@ -255,7 +250,6 @@ func NewDefault() Configuration {
255250
ClientHeaderBufferSize: "1k",
256251
DisableAccessLog: false,
257252
EnableDynamicTLSRecords: true,
258-
EnableSPDY: false,
259253
ErrorLogLevel: errorLevel,
260254
HSTS: true,
261255
HSTSIncludeSubdomains: true,
@@ -264,7 +258,7 @@ func NewDefault() Configuration {
264258
KeepAlive: 75,
265259
LargeClientHeaderBuffers: "4 8k",
266260
LogFormatStream: logFormatStream,
267-
LogFormatUpstream: BuildLogFormatUpstream(false, ""),
261+
LogFormatUpstream: logFormatUpstream,
268262
MaxWorkerConnections: 16384,
269263
MapHashBucketSize: 64,
270264
ProxyRealIPCIDR: defIPCIDR,
@@ -307,20 +301,14 @@ func NewDefault() Configuration {
307301
return cfg
308302
}
309303

310-
// BuildLogFormatUpstream format the log_format upstream based on proxy_protocol
311-
func BuildLogFormatUpstream(useProxyProtocol bool, curLogFormatUpstream string) string {
312-
313-
// test if log_format comes from configmap
314-
if curLogFormatUpstream != "" &&
315-
curLogFormatUpstream != fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr") &&
316-
curLogFormatUpstream != fmt.Sprintf(logFormatUpstream, "$remote_addr") {
317-
return curLogFormatUpstream
318-
}
319-
320-
if useProxyProtocol {
321-
return fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr")
304+
// BuildLogFormatUpstream format the log_format upstream using
305+
// proxy_protocol_addr as remote client address if UseProxyProtocol
306+
// is enabled.
307+
func (cfg Configuration) BuildLogFormatUpstream() string {
308+
if cfg.UseProxyProtocol {
309+
return fmt.Sprintf("$proxy_protocol_addr - %s", cfg.LogFormatUpstream)
322310
}
323-
return fmt.Sprintf(logFormatUpstream, "$remote_addr")
311+
return fmt.Sprintf("$remote_addr - %s", cfg.LogFormatUpstream)
324312
}
325313

326314
// TemplateConfig contains the nginx configuration to render the file nginx.conf

controllers/nginx/pkg/config/config_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ func TestBuildLogFormatUpstream(t *testing.T) {
1212
curLogFormat string
1313
expected string
1414
}{
15-
{true, "", fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr")},
16-
{false, "", fmt.Sprintf(logFormatUpstream, "$remote_addr")},
17-
{true, "my-log-format", "my-log-format"},
18-
{false, "john-log-format", "john-log-format"},
15+
{true, logFormatUpstream, fmt.Sprintf("$proxy_protocol_addr - %s", logFormatUpstream)},
16+
{false, logFormatUpstream, fmt.Sprintf("$remote_addr - %s", logFormatUpstream)},
17+
{true, "my-log-format", "$proxy_protocol_addr - my-log-format"},
18+
{false, "john-log-format", "$remote_addr - john-log-format"},
1919
}
2020

2121
for _, testCase := range testCases {
22-
23-
result := BuildLogFormatUpstream(testCase.useProxyProtocol, testCase.curLogFormat)
24-
22+
cfg := NewDefault()
23+
cfg.UseProxyProtocol = testCase.useProxyProtocol
24+
cfg.LogFormatUpstream = testCase.curLogFormat
25+
result := cfg.BuildLogFormatUpstream()
2526
if result != testCase.expected {
2627
t.Errorf(" expected %v but return %v", testCase.expected, result)
2728
}
28-
2929
}
3030
}

controllers/nginx/pkg/template/template.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/golang/glog"
3232

3333
"k8s.io/ingress/controllers/nginx/pkg/config"
34-
nginxconfig "k8s.io/ingress/controllers/nginx/pkg/config"
3534
"k8s.io/ingress/core/pkg/ingress"
3635
ing_net "k8s.io/ingress/core/pkg/net"
3736
"k8s.io/ingress/core/pkg/watch"
@@ -229,14 +228,12 @@ func buildAuthLocation(input interface{}) string {
229228
}
230229

231230
func buildLogFormatUpstream(input interface{}) string {
232-
config, ok := input.(config.Configuration)
233-
231+
cfg, ok := input.(config.Configuration)
234232
if !ok {
235233
glog.Errorf("error an ingress.buildLogFormatUpstream type but %T was returned", input)
236234
}
237235

238-
return nginxconfig.BuildLogFormatUpstream(config.UseProxyProtocol, config.LogFormatUpstream)
239-
236+
return cfg.BuildLogFormatUpstream()
240237
}
241238

242239
// buildProxyPass produces the proxy pass string, if the ingress has redirects

controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ http {
7979

8080
server_tokens {{ if $cfg.ShowServerTokens }}on{{ else }}off{{ end }};
8181

82-
log_format upstreaminfo {{ buildLogFormatUpstream $cfg }};
82+
log_format upstreaminfo '{{ buildLogFormatUpstream $cfg }}';
8383

8484
{{/* map urls that should not appear in access.log */}}
8585
{{/* http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log */}}

0 commit comments

Comments
 (0)