Skip to content

Commit 9d28194

Browse files
committed
Removing NginxLogFormat for usability purpose
1 parent 3070740 commit 9d28194

File tree

10 files changed

+92
-266
lines changed

10 files changed

+92
-266
lines changed

apis/v1alpha2/nginxproxy_types.go

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -298,17 +298,8 @@ type NginxLogging struct {
298298
// +kubebuilder:default=info
299299
AgentLevel *AgentLogLevel `json:"agentLevel,omitempty"`
300300

301-
// LogFormat defines custom log format that can be used in access logs.
302-
// Each log format must have a unique name.
303-
// https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
304-
//
305-
// +optional
306-
LogFormat *NginxLogFormat `json:"logFormat,omitempty"`
307-
308-
// AccessLog defines the access log settings, including the log file path and format name.
301+
// AccessLog defines the access log settings, including format itself and disabling option.
309302
// For now only path /dev/stdout can be used.
310-
// Path can be set to "OFF" to disable logging.
311-
// https://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
312303
//
313304
// +optional
314305
AccessLog *NginxAccessLog `json:"accessLog,omitempty"`
@@ -367,36 +358,29 @@ const (
367358
AgentLogLevelFatal AgentLogLevel = "fatal"
368359
)
369360

370-
// NginxLogFormat defines a custom log format for NGINX.
371-
// https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
372-
type NginxLogFormat struct {
373-
// Name specifies the name of the log format.
374-
//
375-
// +optional
376-
Name *string `json:"name,omitempty"`
377-
378-
// Format specifies the log format string.
379-
//
380-
// +optional
381-
Format *string `json:"format,omitempty"`
382-
}
383-
384361
// NginxAccessLog defines the configuration for an NGINX access log.
385-
// For now only path /dev/stdout can be used.
386-
// Path can be set to "OFF" to disable logging.
387-
// https://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
388362
type NginxAccessLog struct {
389-
// Path specifies the log file path. Or "OFF" to disable logging.
363+
// Disabled turns off access logging when set to true.
390364
//
391365
// +optional
392-
Path *string `json:"path,omitempty"`
366+
Disabled *bool `json:"disabled,omitempty"`
393367

394-
// Format specifies the log format name to be used.
368+
// Format specifies the custom log format string.
369+
// If not specified, NGINX default 'combined' format is used.
370+
// For now only path /dev/stdout can be used.
371+
// See https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
395372
//
396373
// +optional
397374
Format *string `json:"format,omitempty"`
398375
}
399376

377+
const (
378+
// DefaultLogFormatName is used when user provides custom access_log format.
379+
DefaultLogFormatName = "ngf_user_defined_log_format"
380+
// DefaultAccessLogPath is the default path for the access log.
381+
DefaultAccessLogPath = "/dev/stdout"
382+
)
383+
400384
// NginxPlus specifies NGINX Plus additional settings. These will only be applied if NGINX Plus is being used.
401385
type NginxPlus struct {
402386
// AllowedAddresses specifies IPAddresses or CIDR blocks to the allow list for accessing the NGINX Plus API.

apis/v1alpha2/zz_generated.deepcopy.go

Lines changed: 3 additions & 33 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: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8018,17 +8018,19 @@ spec:
80188018
properties:
80198019
accessLog:
80208020
description: |-
8021-
AccessLog defines the access log settings, including the log file path and format name.
8021+
AccessLog defines the access log settings, including format itself and disabling option.
80228022
For now only path /dev/stdout can be used.
8023-
Path can be set to "OFF" to disable logging.
8024-
https://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
80258023
properties:
8024+
disabled:
8025+
description: Disabled turns off access logging when set to
8026+
true.
8027+
type: boolean
80268028
format:
8027-
description: Format specifies the log format name to be used.
8028-
type: string
8029-
path:
8030-
description: Path specifies the log file path. Or "OFF" to
8031-
disable logging.
8029+
description: |-
8030+
Format specifies the custom log format string.
8031+
If not specified, NGINX default 'combined' format is used.
8032+
For now only path /dev/stdout can be used.
8033+
See https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
80328034
type: string
80338035
type: object
80348036
agentLevel:
@@ -8060,19 +8062,6 @@ spec:
80608062
- alert
80618063
- emerg
80628064
type: string
8063-
logFormat:
8064-
description: |-
8065-
LogFormat defines custom log format that can be used in access logs.
8066-
Each log format must have a unique name.
8067-
https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
8068-
properties:
8069-
format:
8070-
description: Format specifies the log format string.
8071-
type: string
8072-
name:
8073-
description: Name specifies the name of the log format.
8074-
type: string
8075-
type: object
80768065
type: object
80778066
metrics:
80788067
description: |-

deploy/crds.yaml

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8605,17 +8605,19 @@ spec:
86058605
properties:
86068606
accessLog:
86078607
description: |-
8608-
AccessLog defines the access log settings, including the log file path and format name.
8608+
AccessLog defines the access log settings, including format itself and disabling option.
86098609
For now only path /dev/stdout can be used.
8610-
Path can be set to "OFF" to disable logging.
8611-
https://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
86128610
properties:
8611+
disabled:
8612+
description: Disabled turns off access logging when set to
8613+
true.
8614+
type: boolean
86138615
format:
8614-
description: Format specifies the log format name to be used.
8615-
type: string
8616-
path:
8617-
description: Path specifies the log file path. Or "OFF" to
8618-
disable logging.
8616+
description: |-
8617+
Format specifies the custom log format string.
8618+
If not specified, NGINX default 'combined' format is used.
8619+
For now only path /dev/stdout can be used.
8620+
See https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
86198621
type: string
86208622
type: object
86218623
agentLevel:
@@ -8647,19 +8649,6 @@ spec:
86478649
- alert
86488650
- emerg
86498651
type: string
8650-
logFormat:
8651-
description: |-
8652-
LogFormat defines custom log format that can be used in access logs.
8653-
Each log format must have a unique name.
8654-
https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
8655-
properties:
8656-
format:
8657-
description: Format specifies the log format string.
8658-
type: string
8659-
name:
8660-
description: Name specifies the name of the log format.
8661-
type: string
8662-
type: object
86638652
type: object
86648653
metrics:
86658654
description: |-

internal/controller/nginx/config/base_http_config_template.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ server {
5050
5151
{{- /* Define custom log format */ -}}
5252
{{- if .LogFormat }}
53-
{{- if .LogFormat.Name }}
54-
log_format {{ .LogFormat.Name }} '{{ .LogFormat.Format }}';
55-
{{- end }}
53+
{{- /* We use a fixed name for user-defined log format to avoid complexity of passing the name around. */ -}}
54+
{{- if .LogFormat.Format }}
55+
log_format ngf_user_defined_log_format '{{ .LogFormat.Format }}';
56+
{{- end }}
5657
{{- end }}
5758
5859
{{- /* 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. */ -}}
@@ -61,7 +62,7 @@ log_format {{ .LogFormat.Name }} '{{ .LogFormat.Format }}';
6162
access_log off;
6263
{{- else }}
6364
{{- if .AccessLog.Format }}
64-
access_log /dev/stdout {{ .AccessLog.Format }};
65+
access_log /dev/stdout ngf_user_defined_log_format;
6566
{{- end }}
6667
{{- end }}
6768
{{- end }}

internal/controller/nginx/config/base_http_config_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ func TestLoggingSettingsTemplate(t *testing.T) {
2121
unexpectedOutputs []string
2222
}{
2323
{
24-
name: "Log format and access log with custom path",
24+
name: "Log format and access log with custom path and custom format name",
2525
logFormat: &dataplane.LogFormat{
2626
Name: "custom_format",
2727
Format: "$remote_addr - [$time_local] \"$request\" $status $body_bytes_sent",
2828
},
2929
accessLog: &dataplane.AccessLog{Path: "/path/to/log.gz", Format: "custom_format"},
3030
expectedOutputs: []string{
31-
`log_format custom_format '$remote_addr - [$time_local] "$request" $status $body_bytes_sent';`,
32-
`access_log /dev/stdout custom_format;`,
31+
`log_format ngf_user_defined_log_format '$remote_addr - [$time_local] "$request" $status $body_bytes_sent';`,
32+
`access_log /dev/stdout ngf_user_defined_log_format;`,
3333
},
3434
},
3535
{
@@ -40,23 +40,23 @@ func TestLoggingSettingsTemplate(t *testing.T) {
4040
},
4141
accessLog: &dataplane.AccessLog{Path: "", Format: ""},
4242
unexpectedOutputs: []string{
43-
`log_format custom_format`,
43+
`log_format ngf_user_defined_log_format`,
4444
`access_log /dev/stdout`,
4545
},
4646
},
4747
{
4848
name: "Access log off while format presented",
4949
logFormat: &dataplane.LogFormat{
50-
Name: "custom_format",
50+
Name: "ngf_user_defined_log_format",
5151
Format: "$remote_addr - [$time_local] \"$request\" $status $body_bytes_sent",
5252
},
53-
accessLog: &dataplane.AccessLog{Path: "off", Format: "custom_format"},
53+
accessLog: &dataplane.AccessLog{Path: "off", Format: "ngf_user_defined_log_format"},
5454
expectedOutputs: []string{
5555
`access_log off;`,
56-
`log_format custom_format`,
56+
`log_format ngf_user_defined_log_format`,
5757
},
5858
unexpectedOutputs: []string{
59-
`access_log off custom_format`,
59+
`access_log off ngf_user_defined_log_format`,
6060
},
6161
},
6262
{

internal/controller/provisioner/objects.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -407,17 +407,13 @@ func (p *NginxProvisioner) buildNginxConfigMaps(
407407
}
408408

409409
// Add LogFormats and AccessLogs to mainFields
410-
logFormat := addLogFormatToNginxConfig(logging)
411410
accessLog := addAccessLogsToNginxConfig(logging)
412411

413412
mainFields := map[string]interface{}{
414413
"ErrorLevel": logLevel,
415414
"WorkerConnections": workerConnections,
416415
}
417416

418-
if logFormat != nil {
419-
mainFields["LogFormat"] = logFormat
420-
}
421417
if accessLog != nil {
422418
mainFields["AccessLog"] = accessLog
423419
}
@@ -1448,22 +1444,6 @@ func DetermineNginxImageName(
14481444
return fmt.Sprintf("%s:%s", image, tag), pullPolicy
14491445
}
14501446

1451-
func addLogFormatToNginxConfig(logging *ngfAPIv1alpha2.NginxLogging) *ngfAPIv1alpha2.NginxLogFormat {
1452-
logFormat := &ngfAPIv1alpha2.NginxLogFormat{}
1453-
if logging == nil {
1454-
return logFormat
1455-
}
1456-
1457-
if logging.LogFormat != nil {
1458-
logFormat = &ngfAPIv1alpha2.NginxLogFormat{
1459-
Name: logging.LogFormat.Name,
1460-
Format: logging.LogFormat.Format,
1461-
}
1462-
}
1463-
1464-
return logFormat
1465-
}
1466-
14671447
func addAccessLogsToNginxConfig(logging *ngfAPIv1alpha2.NginxLogging) *ngfAPIv1alpha2.NginxAccessLog {
14681448
accessLog := &ngfAPIv1alpha2.NginxAccessLog{}
14691449
if logging == nil {
@@ -1472,8 +1452,8 @@ func addAccessLogsToNginxConfig(logging *ngfAPIv1alpha2.NginxLogging) *ngfAPIv1a
14721452

14731453
if logging.AccessLog != nil {
14741454
accessLog = &ngfAPIv1alpha2.NginxAccessLog{
1475-
Path: logging.AccessLog.Path,
1476-
Format: logging.AccessLog.Format,
1455+
Disabled: logging.AccessLog.Disabled,
1456+
Format: logging.AccessLog.Format,
14771457
}
14781458
}
14791459

0 commit comments

Comments
 (0)