Skip to content

Commit 16d676c

Browse files
committed
add worker connections field to nginxproxy
1 parent bb036ef commit 16d676c

File tree

12 files changed

+176
-18
lines changed

12 files changed

+176
-18
lines changed

apis/v1alpha2/nginxproxy_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ type NginxProxySpec struct {
7676
//
7777
// +optional
7878
Kubernetes *KubernetesSpec `json:"kubernetes,omitempty"`
79+
// WorkerConnections specifies the maximum number of simultaneous connections that can be opened by a worker process.
80+
// Default is 1024.
81+
//
82+
// +optional
83+
// +kubebuilder:validation:Minimum=1
84+
// +kubebuilder:validation:Maximum=65535
85+
WorkerConnections *int32 `json:"workerConnections,omitempty"`
7986
}
8087

8188
// Telemetry specifies the OpenTelemetry configuration.

apis/v1alpha2/zz_generated.deepcopy.go

Lines changed: 5 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7187,6 +7187,14 @@ spec:
71877187
- key
71887188
x-kubernetes-list-type: map
71897189
type: object
7190+
workerConnections:
7191+
description: |-
7192+
WorkerConnections specifies the maximum number of simultaneous connections that can be opened by a worker process.
7193+
Default is 1024.
7194+
format: int32
7195+
maximum: 65535
7196+
minimum: 1
7197+
type: integer
71907198
type: object
71917199
required:
71927200
- spec

deploy/crds.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7772,6 +7772,14 @@ spec:
77727772
- key
77737773
x-kubernetes-list-type: map
77747774
type: object
7775+
workerConnections:
7776+
description: |-
7777+
WorkerConnections specifies the maximum number of simultaneous connections that can be opened by a worker process.
7778+
Default is 1024.
7779+
format: int32
7780+
maximum: 65535
7781+
minimum: 1
7782+
type: integer
77757783
type: object
77767784
required:
77777785
- spec

internal/controller/nginx/conf/nginx-plus.conf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ worker_processes auto;
55

66
pid /var/run/nginx/nginx.pid;
77

8-
events {
9-
worker_connections 1024;
10-
}
11-
128
http {
139
include /etc/nginx/conf.d/*.conf;
1410
include /etc/nginx/mime.types;

internal/controller/nginx/conf/nginx.conf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ worker_processes auto;
55

66
pid /var/run/nginx/nginx.pid;
77

8-
events {
9-
worker_connections 1024;
10-
}
11-
128
http {
139
include /etc/nginx/conf.d/*.conf;
1410
include /etc/nginx/mime.types;

internal/controller/nginx/config/main_config_template.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ load_module modules/ngx_otel_module.so;
77
88
error_log stderr {{ .Conf.Logging.ErrorLevel }};
99
10+
events {
11+
worker_connections {{ .Conf.WorkerConnections }};
12+
}
13+
1014
{{ range $i := .Includes -}}
1115
include {{ $i.Name }};
1216
{{ end -}}

internal/controller/nginx/config/main_config_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,55 @@ func TestGenerateMgmtFiles_Panic(t *testing.T) {
147147
gen.generateMgmtFiles(dataplane.Configuration{})
148148
}).To(Panic())
149149
}
150+
151+
func TestExecuteMainConfig_WorkerConnections(t *testing.T) {
152+
t.Parallel()
153+
154+
tests := []struct {
155+
name string
156+
expWorkerConnections string
157+
conf dataplane.Configuration
158+
}{
159+
{
160+
name: "default worker connections",
161+
conf: dataplane.Configuration{
162+
WorkerConnections: 1024,
163+
},
164+
expWorkerConnections: "worker_connections 1024;",
165+
},
166+
{
167+
name: "custom worker connections",
168+
conf: dataplane.Configuration{
169+
WorkerConnections: 2048,
170+
},
171+
expWorkerConnections: "worker_connections 2048;",
172+
},
173+
{
174+
name: "minimum worker connections",
175+
conf: dataplane.Configuration{
176+
WorkerConnections: 1,
177+
},
178+
expWorkerConnections: "worker_connections 1;",
179+
},
180+
{
181+
name: "maximum worker connections",
182+
conf: dataplane.Configuration{
183+
WorkerConnections: 65535,
184+
},
185+
expWorkerConnections: "worker_connections 65535;",
186+
},
187+
}
188+
189+
for _, test := range tests {
190+
t.Run(test.name, func(t *testing.T) {
191+
t.Parallel()
192+
g := NewWithT(t)
193+
194+
res := executeMainConfig(test.conf)
195+
g.Expect(res).To(HaveLen(1))
196+
g.Expect(res[0].dest).To(Equal(mainIncludesConfigFile))
197+
g.Expect(string(res[0].data)).To(ContainSubstring(test.expWorkerConnections))
198+
g.Expect(string(res[0].data)).To(ContainSubstring("events {"))
199+
})
200+
}
201+
}

internal/controller/provisioner/templates.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ var (
99
)
1010

1111
const mainTemplateText = `
12-
error_log stderr {{ .ErrorLevel }};`
12+
error_log stderr {{ .ErrorLevel }};
13+
14+
events {
15+
worker_connections 1024;
16+
}`
1317

1418
const mgmtTemplateText = `mgmt {
1519
{{- if .UsageEndpoint }}

internal/controller/state/dataplane/configuration.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ func BuildConfiguration(
7676
buildRefCertificateBundles(g.ReferencedSecrets, g.ReferencedCaCertConfigMaps),
7777
backendGroups,
7878
),
79-
Telemetry: buildTelemetry(g, gateway),
80-
BaseHTTPConfig: baseHTTPConfig,
81-
Logging: buildLogging(gateway),
82-
NginxPlus: nginxPlus,
83-
MainSnippets: buildSnippetsForContext(gatewaySnippetsFilters, ngfAPIv1alpha1.NginxContextMain),
84-
AuxiliarySecrets: buildAuxiliarySecrets(g.PlusSecrets),
79+
Telemetry: buildTelemetry(g, gateway),
80+
BaseHTTPConfig: baseHTTPConfig,
81+
Logging: buildLogging(gateway),
82+
NginxPlus: nginxPlus,
83+
MainSnippets: buildSnippetsForContext(gatewaySnippetsFilters, ngfAPIv1alpha1.NginxContextMain),
84+
AuxiliarySecrets: buildAuxiliarySecrets(g.PlusSecrets),
85+
WorkerConnections: buildWorkerConnections(gateway),
8586
}
8687

8788
return config
@@ -1105,6 +1106,21 @@ func buildLogging(gateway *graph.Gateway) Logging {
11051106
return logSettings
11061107
}
11071108

1109+
func buildWorkerConnections(gateway *graph.Gateway) int32 {
1110+
defaultWorkerConnections := int32(1024)
1111+
1112+
if gateway == nil || gateway.EffectiveNginxProxy == nil {
1113+
return defaultWorkerConnections
1114+
}
1115+
1116+
ngfProxy := gateway.EffectiveNginxProxy
1117+
if ngfProxy.WorkerConnections != nil {
1118+
return *ngfProxy.WorkerConnections
1119+
}
1120+
1121+
return defaultWorkerConnections
1122+
}
1123+
11081124
func buildAuxiliarySecrets(
11091125
secrets map[types.NamespacedName][]graph.PlusSecretFile,
11101126
) map[graph.SecretFileType][]byte {
@@ -1143,8 +1159,9 @@ func buildNginxPlus(gateway *graph.Gateway) NginxPlus {
11431159

11441160
func GetDefaultConfiguration(g *graph.Graph, gateway *graph.Gateway) Configuration {
11451161
return Configuration{
1146-
Logging: buildLogging(gateway),
1147-
NginxPlus: NginxPlus{},
1148-
AuxiliarySecrets: buildAuxiliarySecrets(g.PlusSecrets),
1162+
Logging: buildLogging(gateway),
1163+
NginxPlus: NginxPlus{},
1164+
AuxiliarySecrets: buildAuxiliarySecrets(g.PlusSecrets),
1165+
WorkerConnections: buildWorkerConnections(gateway),
11491166
}
11501167
}

0 commit comments

Comments
 (0)