Skip to content

Commit bdc1047

Browse files
authored
Add worker connections field to NginxProxy (#3611)
Add worker connections field to NginxProxy Problem: Users were unable to configure worker connections Solution: Add field to nginxproxy that allows users to configure worker connections. Read that field and put it into the main conf. Testing: Manual testing and unit tests for the new code
1 parent 947cfdd commit bdc1047

File tree

14 files changed

+191
-22
lines changed

14 files changed

+191
-22
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,34 @@ 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: "custom worker connections",
161+
conf: dataplane.Configuration{
162+
WorkerConnections: 2048,
163+
},
164+
expWorkerConnections: "worker_connections 2048;",
165+
},
166+
}
167+
168+
for _, test := range tests {
169+
t.Run(test.name, func(t *testing.T) {
170+
t.Parallel()
171+
g := NewWithT(t)
172+
173+
res := executeMainConfig(test.conf)
174+
g.Expect(res).To(HaveLen(1))
175+
g.Expect(res[0].dest).To(Equal(mainIncludesConfigFile))
176+
g.Expect(string(res[0].data)).To(ContainSubstring(test.expWorkerConnections))
177+
g.Expect(string(res[0].data)).To(ContainSubstring("events {"))
178+
})
179+
}
180+
}

internal/controller/provisioner/objects.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
ngfAPIv1alpha2 "github.com/nginx/nginx-gateway-fabric/apis/v1alpha2"
2222
"github.com/nginx/nginx-gateway-fabric/internal/controller/config"
23+
"github.com/nginx/nginx-gateway-fabric/internal/controller/state/dataplane"
2324
"github.com/nginx/nginx-gateway-fabric/internal/controller/state/graph"
2425
"github.com/nginx/nginx-gateway-fabric/internal/framework/controller"
2526
"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers"
@@ -317,8 +318,14 @@ func (p *NginxProvisioner) buildNginxConfigMaps(
317318
logLevel = string(*nProxyCfg.Logging.ErrorLevel)
318319
}
319320

321+
workerConnections := dataplane.DefaultWorkerConnections
322+
if nProxyCfg != nil && nProxyCfg.WorkerConnections != nil {
323+
workerConnections = *nProxyCfg.WorkerConnections
324+
}
325+
320326
mainFields := map[string]interface{}{
321-
"ErrorLevel": logLevel,
327+
"ErrorLevel": logLevel,
328+
"WorkerConnections": workerConnections,
322329
}
323330

324331
bootstrapCM := &corev1.ConfigMap{

internal/controller/provisioner/objects_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,3 +1014,47 @@ func TestSetIPFamily(t *testing.T) {
10141014
g.Expect(svc.Spec.IPFamilyPolicy).To(Equal(helpers.GetPointer(corev1.IPFamilyPolicySingleStack)))
10151015
g.Expect(svc.Spec.IPFamilies).To(Equal([]corev1.IPFamily{corev1.IPv6Protocol}))
10161016
}
1017+
1018+
func TestBuildNginxConfigMaps_WorkerConnections(t *testing.T) {
1019+
t.Parallel()
1020+
g := NewWithT(t)
1021+
1022+
provisioner := &NginxProvisioner{
1023+
cfg: Config{
1024+
GatewayPodConfig: &config.GatewayPodConfig{
1025+
Namespace: "default",
1026+
ServiceName: "test-service",
1027+
},
1028+
},
1029+
}
1030+
objectMeta := metav1.ObjectMeta{Name: "test", Namespace: "default"}
1031+
1032+
// Test with default worker connections (nil NginxProxy config)
1033+
configMaps := provisioner.buildNginxConfigMaps(objectMeta, nil, "test-bootstrap", "test-agent", false, false)
1034+
g.Expect(configMaps).To(HaveLen(2))
1035+
1036+
bootstrapCM, ok := configMaps[0].(*corev1.ConfigMap)
1037+
g.Expect(ok).To(BeTrue())
1038+
g.Expect(bootstrapCM.Data["main.conf"]).To(ContainSubstring("worker_connections 1024;"))
1039+
1040+
// Test with default worker connections (empty NginxProxy config)
1041+
nProxyCfgEmpty := &graph.EffectiveNginxProxy{}
1042+
configMaps = provisioner.buildNginxConfigMaps(objectMeta, nProxyCfgEmpty, "test-bootstrap", "test-agent", false, false)
1043+
g.Expect(configMaps).To(HaveLen(2))
1044+
1045+
bootstrapCM, ok = configMaps[0].(*corev1.ConfigMap)
1046+
g.Expect(ok).To(BeTrue())
1047+
g.Expect(bootstrapCM.Data["main.conf"]).To(ContainSubstring("worker_connections 1024;"))
1048+
1049+
// Test with custom worker connections
1050+
nProxyCfg := &graph.EffectiveNginxProxy{
1051+
WorkerConnections: helpers.GetPointer(int32(2048)),
1052+
}
1053+
1054+
configMaps = provisioner.buildNginxConfigMaps(objectMeta, nProxyCfg, "test-bootstrap", "test-agent", false, false)
1055+
g.Expect(configMaps).To(HaveLen(2))
1056+
1057+
bootstrapCM, ok = configMaps[0].(*corev1.ConfigMap)
1058+
g.Expect(ok).To(BeTrue())
1059+
g.Expect(bootstrapCM.Data["main.conf"]).To(ContainSubstring("worker_connections 2048;"))
1060+
}

0 commit comments

Comments
 (0)