Skip to content

Commit 0499b02

Browse files
committed
Remove LoggingSettings structure
1 parent 63b15ab commit 0499b02

File tree

9 files changed

+80
-104
lines changed

9 files changed

+80
-104
lines changed

apis/v1alpha2/nginxproxy_types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,14 +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,omitempty"`
369+
Format *string `json:"format,omitempty"`
370370
}
371371

372372
// 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"`
374+
Path *string `json:"path,omitempty"`
375+
Format *string `json:"format,omitempty"`
376376
}
377377

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

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8024,9 +8024,6 @@ spec:
80248024
type: string
80258025
path:
80268026
type: string
8027-
required:
8028-
- format
8029-
- path
80308027
type: object
80318028
agentLevel:
80328029
default: info
@@ -8066,9 +8063,6 @@ spec:
80668063
type: string
80678064
name:
80688065
type: string
8069-
required:
8070-
- format
8071-
- name
80728066
type: object
80738067
type: object
80748068
metrics:

deploy/crds.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8611,9 +8611,6 @@ spec:
86118611
type: string
86128612
path:
86138613
type: string
8614-
required:
8615-
- format
8616-
- path
86178614
type: object
86188615
agentLevel:
86198616
default: info
@@ -8653,9 +8650,6 @@ spec:
86538650
type: string
86548651
name:
86558652
type: string
8656-
required:
8657-
- format
8658-
- name
86598653
type: object
86608654
type: object
86618655
metrics:

internal/controller/nginx/config/base_http_config.go

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

1313
type httpConfig struct {
1414
DNSResolver *dataplane.DNSResolverConfig
15-
LoggingSettings *dataplane.LoggingSettings
15+
LogFormat *dataplane.LogFormat
16+
AccessLog *dataplane.AccessLog
1617
Includes []shared.Include
1718
NginxReadinessProbePort int32
1819
IPFamily shared.IPFamily
@@ -28,7 +29,8 @@ func executeBaseHTTPConfig(conf dataplane.Configuration) []executeResult {
2829
NginxReadinessProbePort: conf.BaseHTTPConfig.NginxReadinessProbePort,
2930
IPFamily: getIPFamily(conf.BaseHTTPConfig),
3031
DNSResolver: conf.BaseHTTPConfig.DNSResolver,
31-
LoggingSettings: conf.Logging.LoggingSettings,
32+
LogFormat: conf.Logging.LogFormat,
33+
AccessLog: conf.Logging.AccessLog,
3234
}
3335

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

internal/controller/nginx/config/base_http_config_template.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,23 @@ server {
4848
}
4949
}
5050
51-
{{- if .LoggingSettings }}
5251
{{- /* Define custom log format */ -}}
53-
{{- if .LoggingSettings.LogFormat.Name }}
54-
log_format {{ .LoggingSettings.LogFormat.Name }} '{{ .LoggingSettings.LogFormat.Format }}';
52+
{{- if .LogFormat }}
53+
{{- if .LogFormat.Name }}
54+
log_format {{ .LogFormat.Name }} '{{ .LogFormat.Format }}';
55+
{{- end }}
5556
{{- end }}
5657
5758
{{- /* 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 }}
59+
{{- if .AccessLog }}
60+
{{- if eq .AccessLog.Path "off" }}
6561
access_log off;
66-
{{- else }}
67-
{{- if and .LoggingSettings.AccessLog.Path .LoggingSettings.AccessLog.Format }}
68-
access_log dev/stdout {{ .LoggingSettings.AccessLog.Format }};
62+
{{- else }}
63+
{{- if .AccessLog.Format }}
64+
access_log dev/stdout {{ .AccessLog.Format }};
65+
{{- end }}
6966
{{- end }}
7067
{{- end }}
71-
{{- end }}
7268
7369
{{ range $i := .Includes -}}
7470
include {{ $i.Name }};

internal/controller/nginx/config/base_http_config_test.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,56 +15,53 @@ func TestLoggingSettingsTemplate(t *testing.T) {
1515

1616
tests := []struct {
1717
name string
18-
loggingSettings *dataplane.LoggingSettings
18+
accessLog *dataplane.AccessLog
19+
logFormat *dataplane.LogFormat
1920
expectedOutputs []string
2021
unexpectedOutputs []string
2122
}{
2223
{
2324
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"},
25+
logFormat: &dataplane.LogFormat{
26+
Name: "custom_format",
27+
Format: "$remote_addr - [$time_local] \"$request\" $status $body_bytes_sent",
3028
},
29+
accessLog: &dataplane.AccessLog{Path: "/path/to/log.gz", Format: "custom_format"},
3130
expectedOutputs: []string{
3231
`log_format custom_format '$remote_addr - [$time_local] "$request" $status $body_bytes_sent';`,
3332
`access_log dev/stdout custom_format;`,
3433
},
3534
},
3635
{
3736
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: ""},
37+
logFormat: &dataplane.LogFormat{
38+
Name: "",
39+
Format: "",
4440
},
41+
accessLog: &dataplane.AccessLog{Path: "", Format: ""},
4542
unexpectedOutputs: []string{
4643
`log_format custom_format`,
4744
`access_log dev/stdout`,
4845
},
4946
},
5047
{
5148
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"},
49+
logFormat: &dataplane.LogFormat{
50+
Name: "custom_format",
51+
Format: "$remote_addr - [$time_local] \"$request\" $status $body_bytes_sent",
5852
},
53+
accessLog: &dataplane.AccessLog{Path: "off", Format: "custom_format"},
5954
expectedOutputs: []string{
6055
`access_log off;`,
56+
`log_format custom_format`,
57+
},
58+
unexpectedOutputs: []string{
59+
`access_log off custom_format`,
6160
},
6261
},
6362
{
64-
name: "Access log off",
65-
loggingSettings: &dataplane.LoggingSettings{
66-
AccessLog: dataplane.AccessLog{Path: "off"},
67-
},
63+
name: "Access log off",
64+
accessLog: &dataplane.AccessLog{Path: "off"},
6865
expectedOutputs: []string{
6966
`access_log off;`,
7067
},
@@ -77,7 +74,7 @@ func TestLoggingSettingsTemplate(t *testing.T) {
7774
g := NewWithT(t)
7875

7976
conf := dataplane.Configuration{
80-
Logging: dataplane.Logging{LoggingSettings: tt.loggingSettings},
77+
Logging: dataplane.Logging{AccessLog: tt.accessLog, LogFormat: tt.logFormat},
8178
}
8279

8380
res := executeBaseHTTPConfig(conf)

internal/controller/state/dataplane/configuration.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,26 +1223,26 @@ func buildLogging(gateway *graph.Gateway) Logging {
12231223
}
12241224

12251225
srcLogSettings := ngfProxy.Logging
1226-
ls := LoggingSettings{}
1227-
ls.LogFormat = buildLogFormat(srcLogSettings)
1228-
ls.AccessLog = buildAccessLog(srcLogSettings)
1226+
logFormat := buildLogFormat(srcLogSettings)
1227+
accessLog := buildAccessLog(srcLogSettings)
12291228

1230-
if ls.AccessLog.Path == "off" {
1231-
logSettings.LoggingSettings = &LoggingSettings{AccessLog: AccessLog{Path: "off"}}
1229+
if accessLog.Path == "off" {
1230+
logSettings.AccessLog = &AccessLog{Path: "off"}
12321231

12331232
return logSettings
12341233
}
12351234

1236-
if ls.LogFormat.Format != "" || ls.AccessLog.Path != "" {
1237-
// only set LoggingSettings if at least one of LogFormat or AccessLog is configured
1238-
logSettings.LoggingSettings = &ls
1235+
if logFormat != nil && logFormat.Format != "" && accessLog != nil && accessLog.Path != "" {
1236+
// only update logSettings if both LogFormat and AccessLog path are configured
1237+
logSettings.LogFormat = logFormat
1238+
logSettings.AccessLog = accessLog
12391239
}
12401240
}
12411241

12421242
return logSettings
12431243
}
12441244

1245-
func buildLogFormat(srcLogSettings *ngfAPIv1alpha2.NginxLogging) LogFormat {
1245+
func buildLogFormat(srcLogSettings *ngfAPIv1alpha2.NginxLogging) *LogFormat {
12461246
var logFormat LogFormat
12471247
// Current API exposes a single LogFormat value whose fields are pointers; include only if both set and non-empty.
12481248
if srcLogSettings.LogFormat != nil &&
@@ -1256,10 +1256,10 @@ func buildLogFormat(srcLogSettings *ngfAPIv1alpha2.NginxLogging) LogFormat {
12561256
}
12571257
}
12581258

1259-
return logFormat
1259+
return &logFormat
12601260
}
12611261

1262-
func buildAccessLog(srcLogSettings *ngfAPIv1alpha2.NginxLogging) AccessLog {
1262+
func buildAccessLog(srcLogSettings *ngfAPIv1alpha2.NginxLogging) *AccessLog {
12631263
var accessLog AccessLog
12641264
// Current API exposes a singular *string AccessLog path (no format yet) – only dev/stdout or "off".
12651265
if srcLogSettings.AccessLog != nil &&
@@ -1280,7 +1280,7 @@ func buildAccessLog(srcLogSettings *ngfAPIv1alpha2.NginxLogging) AccessLog {
12801280
}
12811281
}
12821282

1283-
return accessLog
1283+
return &accessLog
12841284
}
12851285

12861286
func buildWorkerConnections(gateway *graph.Gateway) int32 {

internal/controller/state/dataplane/configuration_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4897,30 +4897,28 @@ func TestBuildLogging(t *testing.T) {
48974897
'"$http_referer" "$http_user_agent" '`),
48984898
},
48994899
AccessLog: &ngfAPIv1alpha2.AccessLog{
4900-
Path: helpers.GetPointer("logs/access.log"),
4900+
Path: helpers.GetPointer("dev/stdout"),
49014901
Format: helpers.GetPointer("custom_format"),
49024902
},
49034903
},
49044904
},
49054905
},
49064906
expLoggingSettings: Logging{
49074907
ErrorLevel: "info",
4908-
LoggingSettings: &LoggingSettings{
4909-
LogFormat: LogFormat{
4910-
Name: "custom_format",
4911-
Format: `'$remote_addr - $remote_user [$time_local] '
4908+
LogFormat: &LogFormat{
4909+
Name: "custom_format",
4910+
Format: `'$remote_addr - $remote_user [$time_local] '
49124911
'"$request" $status $body_bytes_sent '
49134912
'"$http_referer" "$http_user_agent" '`,
4914-
},
4915-
AccessLog: AccessLog{
4916-
Path: "dev/stdout",
4917-
Format: "custom_format",
4918-
},
4913+
},
4914+
AccessLog: &AccessLog{
4915+
Path: "dev/stdout",
4916+
Format: "custom_format",
49194917
},
49204918
},
49214919
},
49224920
{
4923-
msg: "Only AccessLog set if LogFormat is not configured properly (no name given)",
4921+
msg: "No AccessLog setting if LogFormat is not configured properly (no name given)",
49244922
gw: &graph.Gateway{
49254923
EffectiveNginxProxy: &graph.EffectiveNginxProxy{
49264924
Logging: &ngfAPIv1alpha2.NginxLogging{
@@ -4939,12 +4937,6 @@ func TestBuildLogging(t *testing.T) {
49394937
},
49404938
expLoggingSettings: Logging{
49414939
ErrorLevel: "info",
4942-
LoggingSettings: &LoggingSettings{
4943-
AccessLog: AccessLog{
4944-
Path: "dev/stdout",
4945-
Format: "custom_format",
4946-
},
4947-
},
49484940
},
49494941
},
49504942
{
@@ -4962,10 +4954,8 @@ func TestBuildLogging(t *testing.T) {
49624954
},
49634955
expLoggingSettings: Logging{
49644956
ErrorLevel: "info",
4965-
LoggingSettings: &LoggingSettings{
4966-
AccessLog: AccessLog{
4967-
Path: "off",
4968-
},
4957+
AccessLog: &AccessLog{
4958+
Path: "off",
49694959
},
49704960
},
49714961
},
@@ -4975,6 +4965,12 @@ func TestBuildLogging(t *testing.T) {
49754965
EffectiveNginxProxy: &graph.EffectiveNginxProxy{
49764966
Logging: &ngfAPIv1alpha2.NginxLogging{
49774967
ErrorLevel: helpers.GetPointer(ngfAPIv1alpha2.NginxLogLevelInfo),
4968+
LogFormat: &ngfAPIv1alpha2.LogFormat{
4969+
Name: helpers.GetPointer("custom_format"),
4970+
Format: helpers.GetPointer(`'$remote_addr - $remote_user [$time_local] '
4971+
'"$request" $status $body_bytes_sent '
4972+
'"$http_referer" "$http_user_agent" '`),
4973+
},
49784974
AccessLog: &ngfAPIv1alpha2.AccessLog{
49794975
Path: helpers.GetPointer("logs/access.log"),
49804976
Format: helpers.GetPointer("custom_format"),
@@ -4984,11 +4980,15 @@ func TestBuildLogging(t *testing.T) {
49844980
},
49854981
expLoggingSettings: Logging{
49864982
ErrorLevel: "info",
4987-
LoggingSettings: &LoggingSettings{
4988-
AccessLog: AccessLog{
4989-
Path: "dev/stdout",
4990-
Format: "custom_format",
4991-
},
4983+
LogFormat: &LogFormat{
4984+
Name: "custom_format",
4985+
Format: `'$remote_addr - $remote_user [$time_local] '
4986+
'"$request" $status $body_bytes_sent '
4987+
'"$http_referer" "$http_user_agent" '`,
4988+
},
4989+
AccessLog: &AccessLog{
4990+
Path: "dev/stdout",
4991+
Format: "custom_format",
49924992
},
49934993
},
49944994
},

internal/controller/state/dataplane/types.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,9 @@ type Ratio struct {
474474

475475
// Logging defines logging related settings for NGINX.
476476
type Logging struct {
477-
LoggingSettings *LoggingSettings
478-
ErrorLevel string
477+
AccessLog *AccessLog
478+
LogFormat *LogFormat
479+
ErrorLevel string
479480
}
480481

481482
// NginxPlus specifies NGINX Plus additional settings.
@@ -497,14 +498,6 @@ type DeploymentContext struct {
497498
Integration string `json:"integration"`
498499
}
499500

500-
// LoggingSettings defines logging related settings for NGINX.
501-
type LoggingSettings struct {
502-
// LogFormat is the custom log format.
503-
LogFormat LogFormat
504-
// AccessLog is a configuration for access logs
505-
AccessLog AccessLog
506-
}
507-
508501
// LogFormat represents a custom log format.
509502
type LogFormat struct {
510503
// Name is the name of the log format.

0 commit comments

Comments
 (0)