Skip to content

Commit a0ba554

Browse files
committed
move template AccessLog to separate structure
1 parent c19544c commit a0ba554

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

internal/controller/nginx/config/base_http_config.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ import (
1010

1111
var baseHTTPTemplate = gotemplate.Must(gotemplate.New("baseHttp").Parse(baseHTTPTemplateText))
1212

13+
type AccessLog struct {
14+
Format string // User's format string
15+
Path string // Where to write logs (/dev/stdout)
16+
FormatName string // Internal format name (ngf_user_defined_log_format)
17+
Disabled bool // User's disabled flag
18+
}
1319
type httpConfig struct {
1420
DNSResolver *dataplane.DNSResolverConfig
15-
AccessLog *dataplane.AccessLog
21+
AccessLog *AccessLog
1622
DefaultAccessLogPath string
1723
DefaultLogFormatName string
1824
Includes []shared.Include
@@ -30,9 +36,7 @@ func executeBaseHTTPConfig(conf dataplane.Configuration) []executeResult {
3036
NginxReadinessProbePort: conf.BaseHTTPConfig.NginxReadinessProbePort,
3137
IPFamily: getIPFamily(conf.BaseHTTPConfig),
3238
DNSResolver: conf.BaseHTTPConfig.DNSResolver,
33-
AccessLog: conf.Logging.AccessLog,
34-
DefaultAccessLogPath: dataplane.DefaultAccessLogPath,
35-
DefaultLogFormatName: dataplane.DefaultLogFormatName,
39+
AccessLog: buildAccessLog(conf.Logging.AccessLog),
3640
}
3741

3842
results := make([]executeResult, 0, len(includes)+1)
@@ -44,3 +48,22 @@ func executeBaseHTTPConfig(conf dataplane.Configuration) []executeResult {
4448

4549
return results
4650
}
51+
52+
func buildAccessLog(accessLogConfig *dataplane.AccessLog) *AccessLog {
53+
if accessLogConfig != nil {
54+
accessLog := &AccessLog{
55+
Path: dataplane.DefaultAccessLogPath,
56+
FormatName: dataplane.DefaultLogFormatName,
57+
}
58+
if accessLogConfig.Format != "" {
59+
accessLog.Format = accessLogConfig.Format
60+
}
61+
62+
if accessLogConfig.Disabled {
63+
accessLog.Disabled = accessLogConfig.Disabled
64+
}
65+
66+
return accessLog
67+
}
68+
return nil
69+
}

internal/controller/nginx/config/base_http_config_template.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ server {
5252
{{- /* Access log directives for AccessLog. If path is "off" we disable logging. */ -}}
5353
{{- /* We use a fixed name for user-defined log format to avoid complexity of passing the name around. */ -}}
5454
{{- if .AccessLog }}
55-
{{- if .AccessLog.Disabled }}
55+
{{- if .AccessLog.Disabled }}
5656
access_log off;
57-
{{- else }}
58-
{{- if .AccessLog.Format }}
59-
log_format {{ .DefaultLogFormatName }} '{{ .AccessLog.Format }}';
60-
access_log {{ .DefaultAccessLogPath }} {{ .DefaultLogFormatName }};
61-
{{- end }}
62-
{{- end }}
57+
{{- else }}
58+
{{- if .AccessLog.Format }}
59+
log_format {{ .AccessLog.FormatName }} '{{ .AccessLog.Format }}';
60+
access_log {{ .AccessLog.Path }} {{ .AccessLog.FormatName }};
61+
{{- end }}
62+
{{- end }}
6363
{{- end }}
6464
6565
{{ range $i := .Includes -}}

internal/controller/nginx/config/base_http_config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestLoggingSettingsTemplate(t *testing.T) {
2323
unexpectedOutputs []string
2424
}{
2525
{
26-
name: "Log format and access log with custom path and custom format name",
26+
name: "Log format and access log with custom format",
2727
accessLog: &dataplane.AccessLog{Format: logFormat},
2828
expectedOutputs: []string{
2929
fmt.Sprintf("log_format %s '%s'", dataplane.DefaultLogFormatName, logFormat),

internal/controller/provisioner/objects.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,11 @@ func (p *NginxProvisioner) buildNginxConfigMaps(
406406
workerConnections = *nProxyCfg.WorkerConnections
407407
}
408408

409-
// Add LogFormats and AccessLogs to mainFields
410-
accessLog := addAccessLogsToNginxConfig(logging)
411-
412409
mainFields := map[string]interface{}{
413410
"ErrorLevel": logLevel,
414411
"WorkerConnections": workerConnections,
415412
}
416413

417-
if accessLog != nil {
418-
mainFields["AccessLog"] = accessLog
419-
}
420-
421414
// Create events ConfigMap data using template
422415
eventsFields := map[string]interface{}{
423416
"WorkerConnections": workerConnections,
@@ -1443,19 +1436,3 @@ func DetermineNginxImageName(
14431436

14441437
return fmt.Sprintf("%s:%s", image, tag), pullPolicy
14451438
}
1446-
1447-
func addAccessLogsToNginxConfig(logging *ngfAPIv1alpha2.NginxLogging) *ngfAPIv1alpha2.NginxAccessLog {
1448-
accessLog := &ngfAPIv1alpha2.NginxAccessLog{}
1449-
if logging == nil {
1450-
return accessLog
1451-
}
1452-
1453-
if logging.AccessLog != nil {
1454-
accessLog = &ngfAPIv1alpha2.NginxAccessLog{
1455-
Disabled: logging.AccessLog.Disabled,
1456-
Format: logging.AccessLog.Format,
1457-
}
1458-
}
1459-
1460-
return accessLog
1461-
}

0 commit comments

Comments
 (0)