Skip to content

Commit 253e3be

Browse files
committed
Remove LoggingSettings structure
1 parent a946480 commit 253e3be

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
@@ -8609,9 +8609,6 @@ spec:
86098609
type: string
86108610
path:
86118611
type: string
8612-
required:
8613-
- format
8614-
- path
86158612
type: object
86168613
agentLevel:
86178614
default: info
@@ -8651,9 +8648,6 @@ spec:
86518648
type: string
86528649
name:
86538650
type: string
8654-
required:
8655-
- format
8656-
- name
86578651
type: object
86588652
type: object
86598653
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
@@ -1209,26 +1209,26 @@ func buildLogging(gateway *graph.Gateway) Logging {
12091209
}
12101210

12111211
srcLogSettings := ngfProxy.Logging
1212-
ls := LoggingSettings{}
1213-
ls.LogFormat = buildLogFormat(srcLogSettings)
1214-
ls.AccessLog = buildAccessLog(srcLogSettings)
1212+
logFormat := buildLogFormat(srcLogSettings)
1213+
accessLog := buildAccessLog(srcLogSettings)
12151214

1216-
if ls.AccessLog.Path == "off" {
1217-
logSettings.LoggingSettings = &LoggingSettings{AccessLog: AccessLog{Path: "off"}}
1215+
if accessLog.Path == "off" {
1216+
logSettings.AccessLog = &AccessLog{Path: "off"}
12181217

12191218
return logSettings
12201219
}
12211220

1222-
if ls.LogFormat.Format != "" || ls.AccessLog.Path != "" {
1223-
// only set LoggingSettings if at least one of LogFormat or AccessLog is configured
1224-
logSettings.LoggingSettings = &ls
1221+
if logFormat != nil && logFormat.Format != "" && accessLog != nil && accessLog.Path != "" {
1222+
// only update logSettings if both LogFormat and AccessLog path are configured
1223+
logSettings.LogFormat = logFormat
1224+
logSettings.AccessLog = accessLog
12251225
}
12261226
}
12271227

12281228
return logSettings
12291229
}
12301230

1231-
func buildLogFormat(srcLogSettings *ngfAPIv1alpha2.NginxLogging) LogFormat {
1231+
func buildLogFormat(srcLogSettings *ngfAPIv1alpha2.NginxLogging) *LogFormat {
12321232
var logFormat LogFormat
12331233
// Current API exposes a single LogFormat value whose fields are pointers; include only if both set and non-empty.
12341234
if srcLogSettings.LogFormat != nil &&
@@ -1242,10 +1242,10 @@ func buildLogFormat(srcLogSettings *ngfAPIv1alpha2.NginxLogging) LogFormat {
12421242
}
12431243
}
12441244

1245-
return logFormat
1245+
return &logFormat
12461246
}
12471247

1248-
func buildAccessLog(srcLogSettings *ngfAPIv1alpha2.NginxLogging) AccessLog {
1248+
func buildAccessLog(srcLogSettings *ngfAPIv1alpha2.NginxLogging) *AccessLog {
12491249
var accessLog AccessLog
12501250
// Current API exposes a singular *string AccessLog path (no format yet) – only dev/stdout or "off".
12511251
if srcLogSettings.AccessLog != nil &&
@@ -1266,7 +1266,7 @@ func buildAccessLog(srcLogSettings *ngfAPIv1alpha2.NginxLogging) AccessLog {
12661266
}
12671267
}
12681268

1269-
return accessLog
1269+
return &accessLog
12701270
}
12711271

12721272
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
@@ -4899,30 +4899,28 @@ func TestBuildLogging(t *testing.T) {
48994899
'"$http_referer" "$http_user_agent" '`),
49004900
},
49014901
AccessLog: &ngfAPIv1alpha2.AccessLog{
4902-
Path: helpers.GetPointer("logs/access.log"),
4902+
Path: helpers.GetPointer("dev/stdout"),
49034903
Format: helpers.GetPointer("custom_format"),
49044904
},
49054905
},
49064906
},
49074907
},
49084908
expLoggingSettings: Logging{
49094909
ErrorLevel: "info",
4910-
LoggingSettings: &LoggingSettings{
4911-
LogFormat: LogFormat{
4912-
Name: "custom_format",
4913-
Format: `'$remote_addr - $remote_user [$time_local] '
4910+
LogFormat: &LogFormat{
4911+
Name: "custom_format",
4912+
Format: `'$remote_addr - $remote_user [$time_local] '
49144913
'"$request" $status $body_bytes_sent '
49154914
'"$http_referer" "$http_user_agent" '`,
4916-
},
4917-
AccessLog: AccessLog{
4918-
Path: "dev/stdout",
4919-
Format: "custom_format",
4920-
},
4915+
},
4916+
AccessLog: &AccessLog{
4917+
Path: "dev/stdout",
4918+
Format: "custom_format",
49214919
},
49224920
},
49234921
},
49244922
{
4925-
msg: "Only AccessLog set if LogFormat is not configured properly (no name given)",
4923+
msg: "No AccessLog setting if LogFormat is not configured properly (no name given)",
49264924
gw: &graph.Gateway{
49274925
EffectiveNginxProxy: &graph.EffectiveNginxProxy{
49284926
Logging: &ngfAPIv1alpha2.NginxLogging{
@@ -4941,12 +4939,6 @@ func TestBuildLogging(t *testing.T) {
49414939
},
49424940
expLoggingSettings: Logging{
49434941
ErrorLevel: "info",
4944-
LoggingSettings: &LoggingSettings{
4945-
AccessLog: AccessLog{
4946-
Path: "dev/stdout",
4947-
Format: "custom_format",
4948-
},
4949-
},
49504942
},
49514943
},
49524944
{
@@ -4964,10 +4956,8 @@ func TestBuildLogging(t *testing.T) {
49644956
},
49654957
expLoggingSettings: Logging{
49664958
ErrorLevel: "info",
4967-
LoggingSettings: &LoggingSettings{
4968-
AccessLog: AccessLog{
4969-
Path: "off",
4970-
},
4959+
AccessLog: &AccessLog{
4960+
Path: "off",
49714961
},
49724962
},
49734963
},
@@ -4977,6 +4967,12 @@ func TestBuildLogging(t *testing.T) {
49774967
EffectiveNginxProxy: &graph.EffectiveNginxProxy{
49784968
Logging: &ngfAPIv1alpha2.NginxLogging{
49794969
ErrorLevel: helpers.GetPointer(ngfAPIv1alpha2.NginxLogLevelInfo),
4970+
LogFormat: &ngfAPIv1alpha2.LogFormat{
4971+
Name: helpers.GetPointer("custom_format"),
4972+
Format: helpers.GetPointer(`'$remote_addr - $remote_user [$time_local] '
4973+
'"$request" $status $body_bytes_sent '
4974+
'"$http_referer" "$http_user_agent" '`),
4975+
},
49804976
AccessLog: &ngfAPIv1alpha2.AccessLog{
49814977
Path: helpers.GetPointer("logs/access.log"),
49824978
Format: helpers.GetPointer("custom_format"),
@@ -4986,11 +4982,15 @@ func TestBuildLogging(t *testing.T) {
49864982
},
49874983
expLoggingSettings: Logging{
49884984
ErrorLevel: "info",
4989-
LoggingSettings: &LoggingSettings{
4990-
AccessLog: AccessLog{
4991-
Path: "dev/stdout",
4992-
Format: "custom_format",
4993-
},
4985+
LogFormat: &LogFormat{
4986+
Name: "custom_format",
4987+
Format: `'$remote_addr - $remote_user [$time_local] '
4988+
'"$request" $status $body_bytes_sent '
4989+
'"$http_referer" "$http_user_agent" '`,
4990+
},
4991+
AccessLog: &AccessLog{
4992+
Path: "dev/stdout",
4993+
Format: "custom_format",
49944994
},
49954995
},
49964996
},

internal/controller/state/dataplane/types.go

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

470470
// Logging defines logging related settings for NGINX.
471471
type Logging struct {
472-
LoggingSettings *LoggingSettings
473-
ErrorLevel string
472+
AccessLog *AccessLog
473+
LogFormat *LogFormat
474+
ErrorLevel string
474475
}
475476

476477
// NginxPlus specifies NGINX Plus additional settings.
@@ -492,14 +493,6 @@ type DeploymentContext struct {
492493
Integration string `json:"integration"`
493494
}
494495

495-
// LoggingSettings defines logging related settings for NGINX.
496-
type LoggingSettings struct {
497-
// LogFormat is the custom log format.
498-
LogFormat LogFormat
499-
// AccessLog is a configuration for access logs
500-
AccessLog AccessLog
501-
}
502-
503496
// LogFormat represents a custom log format.
504497
type LogFormat struct {
505498
// Name is the name of the log format.

0 commit comments

Comments
 (0)