Skip to content

Commit 3d18022

Browse files
committed
Make logFormat singular and add ability to turn logs off
1 parent 3131263 commit 3d18022

File tree

14 files changed

+427
-176
lines changed

14 files changed

+427
-176
lines changed

apis/v1alpha2/nginxproxy_types.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,12 @@ type NginxLogging struct {
302302
// Each log format must have a unique name.
303303
//
304304
// +optional
305-
LogFormats []LogFormat `json:"logFormats,omitempty"`
305+
LogFormat *LogFormat `json:"logFormat,omitempty"`
306306

307307
// AccessLogs defines the access log settings, including the log file path, format, and optional parameters.
308308
//
309309
// +optional
310-
AccessLogs []AccessLog `json:"accessLogs,omitempty"`
310+
AccessLog *AccessLog `json:"accessLog,omitempty"`
311311
}
312312

313313
// NginxErrorLogLevel type defines the log level of error logs for NGINX.
@@ -365,17 +365,14 @@ const (
365365

366366
// LogFormat defines a custom log format for NGINX.
367367
type LogFormat struct {
368-
Name string `json:"name"`
369-
Format string `json:"format"`
368+
Name *string `json:"name"`
369+
Format *string `json:"format"`
370370
}
371371

372-
// AccessLog defines the configuration for an NGINX access log.
372+
// AccessLog defines the configuration for an NGINX access log. For now only path dev/stdout is used.
373373
type AccessLog struct {
374-
Path string `json:"path"`
375-
Format string `json:"format"`
376-
Buffer string `json:"buffer,omitempty"`
377-
Condition string `json:"condition,omitempty"`
378-
Gzip bool `json:"gzip,omitempty"`
374+
Path *string `json:"path"`
375+
Format *string `json:"format"`
379376
}
380377

381378
// NginxPlus specifies NGINX Plus additional settings. These will only be applied if NGINX Plus is being used.

apis/v1alpha2/zz_generated.deepcopy.go

Lines changed: 28 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/gateway.nginx.org_nginxproxies.yaml

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8016,28 +8016,18 @@ spec:
80168016
logging:
80178017
description: Logging defines logging related settings for NGINX.
80188018
properties:
8019-
accessLogs:
8019+
accessLog:
80208020
description: AccessLogs defines the access log settings, including
80218021
the log file path, format, and optional parameters.
8022-
items:
8023-
description: AccessLog defines the configuration for an NGINX
8024-
access log.
8025-
properties:
8026-
buffer:
8027-
type: string
8028-
condition:
8029-
type: string
8030-
format:
8031-
type: string
8032-
gzip:
8033-
type: boolean
8034-
path:
8035-
type: string
8036-
required:
8037-
- format
8038-
- path
8039-
type: object
8040-
type: array
8022+
properties:
8023+
format:
8024+
type: string
8025+
path:
8026+
type: string
8027+
required:
8028+
- format
8029+
- path
8030+
type: object
80418031
agentLevel:
80428032
default: info
80438033
description: |-
@@ -8067,22 +8057,19 @@ spec:
80678057
- alert
80688058
- emerg
80698059
type: string
8070-
logFormats:
8060+
logFormat:
80718061
description: |-
80728062
LogFormats defines custom log formats that can be used in access logs.
80738063
Each log format must have a unique name.
8074-
items:
8075-
description: LogFormat defines a custom log format for NGINX.
8076-
properties:
8077-
format:
8078-
type: string
8079-
name:
8080-
type: string
8081-
required:
8082-
- format
8083-
- name
8084-
type: object
8085-
type: array
8064+
properties:
8065+
format:
8066+
type: string
8067+
name:
8068+
type: string
8069+
required:
8070+
- format
8071+
- name
8072+
type: object
80868073
type: object
80878074
metrics:
80888075
description: |-

deploy/crds.yaml

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8601,28 +8601,18 @@ spec:
86018601
logging:
86028602
description: Logging defines logging related settings for NGINX.
86038603
properties:
8604-
accessLogs:
8604+
accessLog:
86058605
description: AccessLogs defines the access log settings, including
86068606
the log file path, format, and optional parameters.
8607-
items:
8608-
description: AccessLog defines the configuration for an NGINX
8609-
access log.
8610-
properties:
8611-
buffer:
8612-
type: string
8613-
condition:
8614-
type: string
8615-
format:
8616-
type: string
8617-
gzip:
8618-
type: boolean
8619-
path:
8620-
type: string
8621-
required:
8622-
- format
8623-
- path
8624-
type: object
8625-
type: array
8607+
properties:
8608+
format:
8609+
type: string
8610+
path:
8611+
type: string
8612+
required:
8613+
- format
8614+
- path
8615+
type: object
86268616
agentLevel:
86278617
default: info
86288618
description: |-
@@ -8652,22 +8642,19 @@ spec:
86528642
- alert
86538643
- emerg
86548644
type: string
8655-
logFormats:
8645+
logFormat:
86568646
description: |-
86578647
LogFormats defines custom log formats that can be used in access logs.
86588648
Each log format must have a unique name.
8659-
items:
8660-
description: LogFormat defines a custom log format for NGINX.
8661-
properties:
8662-
format:
8663-
type: string
8664-
name:
8665-
type: string
8666-
required:
8667-
- format
8668-
- name
8669-
type: object
8670-
type: array
8649+
properties:
8650+
format:
8651+
type: string
8652+
name:
8653+
type: string
8654+
required:
8655+
- format
8656+
- name
8657+
type: object
86718658
type: object
86728659
metrics:
86738660
description: |-

internal/controller/nginx/config/base_http_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var baseHTTPTemplate = gotemplate.Must(gotemplate.New("baseHttp").Parse(baseHTTP
1212

1313
type httpConfig struct {
1414
DNSResolver *dataplane.DNSResolverConfig
15+
LoggingSettings *dataplane.LoggingSettings
1516
Includes []shared.Include
1617
NginxReadinessProbePort int32
1718
IPFamily shared.IPFamily
@@ -27,6 +28,7 @@ func executeBaseHTTPConfig(conf dataplane.Configuration) []executeResult {
2728
NginxReadinessProbePort: conf.BaseHTTPConfig.NginxReadinessProbePort,
2829
IPFamily: getIPFamily(conf.BaseHTTPConfig),
2930
DNSResolver: conf.BaseHTTPConfig.DNSResolver,
31+
LoggingSettings: conf.Logging.LoggingSettings,
3032
}
3133

3234
results := make([]executeResult, 0, len(includes)+1)

internal/controller/nginx/config/base_http_config_template.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ server {
4848
}
4949
}
5050
51+
{{- if .LoggingSettings }}
52+
{{- /* Define custom log format */ -}}
53+
{{- if .LoggingSettings.LogFormat.Name }}
54+
log_format {{ .LoggingSettings.LogFormat.Name }} '{{ .LoggingSettings.LogFormat.Format }}';
55+
{{- end }}
56+
57+
{{- /* Access log directives for AccessLog. If path is "off" we disable logging. If Format set, use dev/stdout with that format. Otherwise use the given path. */ -}}
58+
{{- $disable := false -}}
59+
{{- if .LoggingSettings.AccessLog.Path }}
60+
{{- if eq .LoggingSettings.AccessLog.Path "off" -}}
61+
{{- $disable = true -}}
62+
{{- end -}}
63+
{{- end }}
64+
{{- if $disable }}
65+
access_log off;
66+
{{- else }}
67+
{{- if and .LoggingSettings.AccessLog.Path .LoggingSettings.AccessLog.Format }}
68+
access_log dev/stdout {{ .LoggingSettings.AccessLog.Format }};
69+
{{- end }}
70+
{{- end }}
71+
{{- end }}
72+
5173
{{ range $i := .Includes -}}
5274
include {{ $i.Name }};
5375
{{ end -}}

internal/controller/nginx/config/base_http_config_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,89 @@ import (
1010
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/state/dataplane"
1111
)
1212

13+
func TestLoggingSettingsTemplate(t *testing.T) {
14+
t.Parallel()
15+
16+
tests := []struct {
17+
name string
18+
loggingSettings *dataplane.LoggingSettings
19+
expectedOutputs []string
20+
unexpectedOutputs []string
21+
}{
22+
{
23+
name: "Log format and access log with custom path",
24+
loggingSettings: &dataplane.LoggingSettings{
25+
LogFormat: dataplane.LogFormat{
26+
Name: "custom_format",
27+
Format: "$remote_addr - [$time_local] \"$request\" $status $body_bytes_sent",
28+
},
29+
AccessLog: dataplane.AccessLog{Path: "/path/to/log.gz", Format: "custom_format"},
30+
},
31+
expectedOutputs: []string{
32+
`log_format custom_format '$remote_addr - [$time_local] "$request" $status $body_bytes_sent';`,
33+
`access_log dev/stdout custom_format;`,
34+
},
35+
},
36+
{
37+
name: "Empty log format name and format",
38+
loggingSettings: &dataplane.LoggingSettings{
39+
LogFormat: dataplane.LogFormat{
40+
Name: "",
41+
Format: "",
42+
},
43+
AccessLog: dataplane.AccessLog{Path: "", Format: ""},
44+
},
45+
unexpectedOutputs: []string{
46+
`log_format custom_format`,
47+
`access_log dev/stdout`,
48+
},
49+
},
50+
{
51+
name: "Access log off while format presented",
52+
loggingSettings: &dataplane.LoggingSettings{
53+
LogFormat: dataplane.LogFormat{
54+
Name: "custom_format",
55+
Format: "$remote_addr - [$time_local] \"$request\" $status $body_bytes_sent",
56+
},
57+
AccessLog: dataplane.AccessLog{Path: "off"},
58+
},
59+
expectedOutputs: []string{
60+
`access_log off;`,
61+
},
62+
},
63+
{
64+
name: "Access log off",
65+
loggingSettings: &dataplane.LoggingSettings{
66+
AccessLog: dataplane.AccessLog{Path: "off"},
67+
},
68+
expectedOutputs: []string{
69+
`access_log off;`,
70+
},
71+
},
72+
}
73+
74+
for _, tt := range tests {
75+
t.Run(tt.name, func(t *testing.T) {
76+
t.Parallel()
77+
g := NewWithT(t)
78+
79+
conf := dataplane.Configuration{
80+
Logging: dataplane.Logging{LoggingSettings: tt.loggingSettings},
81+
}
82+
83+
res := executeBaseHTTPConfig(conf)
84+
g.Expect(res).To(HaveLen(1))
85+
httpConfig := string(res[0].data)
86+
for _, expectedOutput := range tt.expectedOutputs {
87+
g.Expect(httpConfig).To(ContainSubstring(expectedOutput))
88+
}
89+
for _, unexpectedOutput := range tt.unexpectedOutputs {
90+
g.Expect(httpConfig).ToNot(ContainSubstring(unexpectedOutput))
91+
}
92+
})
93+
}
94+
}
95+
1396
func TestExecuteBaseHttp_HTTP2(t *testing.T) {
1497
t.Parallel()
1598
confOn := dataplane.Configuration{

0 commit comments

Comments
 (0)