Skip to content

Commit 0923130

Browse files
committed
Adding nginx_proxy access_log format ability
1 parent 25e8e5b commit 0923130

File tree

8 files changed

+361
-1
lines changed

8 files changed

+361
-1
lines changed

apis/v1alpha2/nginxproxy_types.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ type NginxLogging struct {
297297
// +optional
298298
// +kubebuilder:default=info
299299
AgentLevel *AgentLogLevel `json:"agentLevel,omitempty"`
300+
301+
// LogFormats defines custom log formats that can be used in access logs.
302+
// Each log format must have a unique name.
303+
//
304+
// +optional
305+
LogFormats []LogFormat `json:"logFormats,omitempty"`
306+
307+
// AccessLogs defines the access log settings, including the log file path, format, and optional parameters.
308+
//
309+
// +optional
310+
AccessLogs []AccessLog `json:"accessLogs,omitempty"`
300311
}
301312

302313
// NginxErrorLogLevel type defines the log level of error logs for NGINX.
@@ -352,6 +363,21 @@ const (
352363
AgentLogLevelFatal AgentLogLevel = "fatal"
353364
)
354365

366+
// LogFormat defines a custom log format for NGINX.
367+
type LogFormat struct {
368+
Name string `json:"name"`
369+
Format string `json:"format"`
370+
}
371+
372+
// AccessLog defines the configuration for an NGINX access log.
373+
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"`
379+
}
380+
355381
// NginxPlus specifies NGINX Plus additional settings. These will only be applied if NGINX Plus is being used.
356382
type NginxPlus struct {
357383
// AllowedAddresses specifies IPAddresses or CIDR blocks to the allow list for accessing the NGINX Plus API.

apis/v1alpha2/zz_generated.deepcopy.go

Lines changed: 40 additions & 0 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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8016,6 +8016,28 @@ spec:
80168016
logging:
80178017
description: Logging defines logging related settings for NGINX.
80188018
properties:
8019+
accessLogs:
8020+
description: AccessLogs defines the access log settings, including
8021+
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
80198041
agentLevel:
80208042
default: info
80218043
description: |-
@@ -8045,6 +8067,22 @@ spec:
80458067
- alert
80468068
- emerg
80478069
type: string
8070+
logFormats:
8071+
description: |-
8072+
LogFormats defines custom log formats that can be used in access logs.
8073+
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
80488086
type: object
80498087
metrics:
80508088
description: |-

deploy/crds.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8601,6 +8601,28 @@ spec:
86018601
logging:
86028602
description: Logging defines logging related settings for NGINX.
86038603
properties:
8604+
accessLogs:
8605+
description: AccessLogs defines the access log settings, including
8606+
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
86048626
agentLevel:
86058627
default: info
86068628
description: |-
@@ -8630,6 +8652,22 @@ spec:
86308652
- alert
86318653
- emerg
86328654
type: string
8655+
logFormats:
8656+
description: |-
8657+
LogFormats defines custom log formats that can be used in access logs.
8658+
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
86338671
type: object
86348672
metrics:
86358673
description: |-

internal/controller/provisioner/objects.go

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

409+
// Add LogFormats and AccessLogs to mainFields
410+
logFormats := addLogFormatToNginxConfig(logging)
411+
accessLogs := addAccessLogsToNginxConfig(logging)
412+
409413
mainFields := map[string]interface{}{
410414
"ErrorLevel": logLevel,
411415
"WorkerConnections": workerConnections,
416+
"LogFormats": logFormats,
417+
"AccessLogs": accessLogs,
412418
}
413419

414420
// Create events ConfigMap data using template
@@ -1427,3 +1433,49 @@ func DetermineNginxImageName(
14271433

14281434
return fmt.Sprintf("%s:%s", image, tag), pullPolicy
14291435
}
1436+
1437+
func addLogFormatToNginxConfig(logging *ngfAPIv1alpha2.NginxLogging) []ngfAPIv1alpha2.LogFormat {
1438+
logFormats := []ngfAPIv1alpha2.LogFormat{}
1439+
if logging == nil {
1440+
return logFormats
1441+
}
1442+
1443+
for _, lf := range logging.LogFormats {
1444+
logFormats = append(logFormats, ngfAPIv1alpha2.LogFormat{
1445+
Name: lf.Name,
1446+
Format: lf.Format,
1447+
})
1448+
}
1449+
1450+
if len(logFormats) == 0 {
1451+
logFormats = append(logFormats, ngfAPIv1alpha2.LogFormat{
1452+
Name: "default",
1453+
Format: "$remote_addr - [$time_local] \"$request\" $status $body_bytes_sent",
1454+
})
1455+
}
1456+
1457+
return logFormats
1458+
}
1459+
1460+
func addAccessLogsToNginxConfig(logging *ngfAPIv1alpha2.NginxLogging) []ngfAPIv1alpha2.AccessLog {
1461+
accessLogs := []ngfAPIv1alpha2.AccessLog{}
1462+
if logging == nil {
1463+
return accessLogs
1464+
}
1465+
1466+
for _, al := range logging.AccessLogs {
1467+
accessLogs = append(accessLogs, ngfAPIv1alpha2.AccessLog{
1468+
Path: al.Path,
1469+
Format: al.Format,
1470+
})
1471+
}
1472+
1473+
if len(accessLogs) == 0 {
1474+
accessLogs = append(accessLogs, ngfAPIv1alpha2.AccessLog{
1475+
Path: "/var/log/nginx/access.log",
1476+
Format: "default",
1477+
})
1478+
}
1479+
1480+
return accessLogs
1481+
}

0 commit comments

Comments
 (0)