Skip to content

Commit 8f5ab98

Browse files
authored
Merge pull request #192 from nginxinc/worker-processes
Add worker-processes configmap key
2 parents 90b8f75 + e87c20e commit 8f5ab98

File tree

9 files changed

+78
-27
lines changed

9 files changed

+78
-27
lines changed

examples/customization/README.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The table below summarizes some of the options. More options (extensions) are av
3737
| `nginx.org/lb-method` | `lb-method` | Sets the [load balancing method](https://www.nginx.com/resources/admin-guide/load-balancer/#method). The default `""` specifies the round-robin method. | `""` |
3838
| `nginx.org/listen-ports` | N/A | Configures HTTP ports that NGINX will listen on. | `[80]` |
3939
| `nginx.org/listen-ports-ssl` | N/A | Configures HTTPS ports that NGINX will listen on. | `[443]` |
40+
| N/A | `worker-processes` | Sets the value of the [worker_processes](http://nginx.org/en/docs/ngx_core_module.html#worker_processes) directive. | `auto` |
4041

4142
## Using ConfigMaps
4243

@@ -47,29 +48,29 @@ the config map to use with the following format: `<namespace>/<name>`. See [ngin
4748

4849
1. Create a configmaps file with the name *nginx-config.yaml* and set the values
4950
that make sense for your setup:
50-
```yaml
51-
kind: ConfigMap
52-
apiVersion: v1
53-
metadata:
54-
name: nginx-config
55-
data:
56-
proxy-connect-timeout: "10s"
57-
proxy-read-timeout: "10s"
58-
client-max-body-size: "2m"
59-
```
60-
See the **nginx-config.yaml** from this directory for a complete example.
51+
```yaml
52+
kind: ConfigMap
53+
apiVersion: v1
54+
metadata:
55+
name: nginx-config
56+
data:
57+
proxy-connect-timeout: "10s"
58+
proxy-read-timeout: "10s"
59+
client-max-body-size: "2m"
60+
```
61+
See the **nginx-config.yaml** from this directory for a complete example.
6162

62-
2. Create a configmaps resource:
63-
```
64-
$ kubectl create -f nginx-config.yaml
65-
```
66-
The NGINX configuration will be updated.
63+
1. Create a configmaps resource:
64+
```
65+
$ kubectl create -f nginx-config.yaml
66+
```
67+
The NGINX configuration will be updated.
6768
68-
3. If you want to update the configmaps, update the file and replace the config map:
69-
```
70-
$ kubectl replace -f nginx-config.yaml
71-
```
72-
The NGINX configuration will be updated.
69+
1. If you want to update the configmaps, update the file and replace the config map:
70+
```
71+
$ kubectl replace -f nginx-config.yaml
72+
```
73+
The NGINX configuration will be updated.
7374
7475
## Using Annotations
7576

examples/customization/nginx-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ data:
4444
location-snippets: | # No default. Pipe is used for multiple line snippets. Make sure the snippet is not a default value, in order to avoid duplication.
4545
proxy_temp_path /var/nginx/proxy_temp;
4646
charset koi8-r;
47+
worker-processes: "1" # default is "auto". Sets the value of the worker_processes directive. See http://nginx.org/en/docs/ngx_core_module.html#worker_processes

nginx-controller/controller/controller.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,13 @@ func (lbc *LoadBalancerController) syncCfgm(task Task) {
559559
cfg.ServerSnippets = serverSnippets
560560
}
561561
}
562-
562+
if _, exists, err := nginx.GetMapKeyAsInt(cfgm.Data, "worker-processes", cfgm); exists {
563+
if err != nil && cfgm.Data["worker-processes"] != "auto" {
564+
glog.Errorf("Configmap %s/%s: Invalid value for worker-processes key: must be an integer or the string 'auto', got %q", cfgm.GetNamespace(), cfgm.GetName(), cfgm.Data["worker-processes"])
565+
} else {
566+
cfg.MainWorkerProcesses = cfgm.Data["worker-processes"]
567+
}
568+
}
563569
}
564570

565571
var ingExes []*nginx.IngressEx

nginx-controller/nginx/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Config struct {
2525
HSTSMaxAge int64
2626
HSTSIncludeSubdomains bool
2727
LBMethod string
28+
MainWorkerProcesses string
2829

2930
// http://nginx.org/en/docs/http/ngx_http_realip_module.html
3031
RealIPHeader string
@@ -55,6 +56,7 @@ func NewDefaultConfig() *Config {
5556
ClientMaxBodySize: "1m",
5657
MainServerNamesHashMaxSize: "512",
5758
ProxyBuffering: true,
59+
MainWorkerProcesses: "auto",
5860
HSTSMaxAge: 2592000,
5961
Ports: []int{80},
6062
SSLPorts: []int{443},

nginx-controller/nginx/configurator.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,10 @@ func (cnf *Configurator) UpdateConfig(config *Config, ingExes []*IngressEx) erro
663663
SSLCiphers: config.MainServerSSLCiphers,
664664
SSLDHParam: config.MainServerSSLDHParam,
665665
SSLPreferServerCiphers: config.MainServerSSLPreferServerCiphers,
666-
HTTP2: config.HTTP2,
667-
ServerTokens: config.ServerTokens,
668-
ProxyProtocol: config.ProxyProtocol,
666+
HTTP2: config.HTTP2,
667+
ServerTokens: config.ServerTokens,
668+
ProxyProtocol: config.ProxyProtocol,
669+
WorkerProcesses: config.MainWorkerProcesses,
669670
}
670671

671672
cnf.nginx.UpdateMainConfigFile(mainCfg)

nginx-controller/nginx/nginx.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ type NginxMainConfig struct {
113113
HTTP2 bool
114114
ServerTokens string
115115
ProxyProtocol bool
116+
WorkerProcesses string
116117
}
117118

118119
// NewUpstreamWithDefaultServer creates an upstream with the default server.
@@ -139,6 +140,7 @@ func NewNginxController(nginxConfPath string, local bool, healthStatus bool, ngi
139140
cfg := &NginxMainConfig{
140141
ServerNamesHashMaxSize: NewDefaultConfig().MainServerNamesHashMaxSize,
141142
ServerTokens: NewDefaultConfig().ServerTokens,
143+
WorkerProcesses: NewDefaultConfig().MainWorkerProcesses,
142144
}
143145
ngxc.UpdateMainConfigFile(cfg)
144146

nginx-controller/nginx/templates/nginx-plus.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
user nginx;
3-
worker_processes auto;
3+
worker_processes {{.WorkerProcesses}};
44

55
daemon off;
66

nginx-controller/nginx/templates/nginx.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
user nginx;
3-
worker_processes auto;
3+
worker_processes {{.WorkerProcesses}};
44

55
daemon off;
66

nginx-controller/nginx/templates/templates_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
)
1010

1111
const nginxIngressTmpl = "nginx.ingress.tmpl"
12+
const nginxMainTmpl = "nginx.tmpl"
1213
const nginxPlusIngressTmpl = "nginx-plus.ingress.tmpl"
14+
const nginxPlusMainTmpl = "nginx-plus.tmpl"
1315

1416
var testUps = nginx.Upstream{
1517
Name: "test",
@@ -43,6 +45,12 @@ var ingCfg = nginx.IngressNginxConfig{
4345
Upstreams: []nginx.Upstream{testUps},
4446
}
4547

48+
var mainCfg = nginx.NginxMainConfig{
49+
ServerNamesHashMaxSize: "512",
50+
ServerTokens: "off",
51+
WorkerProcesses: "auto",
52+
}
53+
4654
func TestIngressForNGINXPlus(t *testing.T) {
4755
tmpl, err := template.New(nginxPlusIngressTmpl).ParseFiles(nginxPlusIngressTmpl)
4856
if err != nil {
@@ -72,3 +80,33 @@ func TestIngressForNGINX(t *testing.T) {
7280
t.Fatalf("Failed to write template %v", err)
7381
}
7482
}
83+
84+
func TestMainForNGINXPlus(t *testing.T) {
85+
tmpl, err := template.New(nginxPlusMainTmpl).ParseFiles(nginxPlusMainTmpl)
86+
if err != nil {
87+
t.Fatalf("Failed to parse template file: %v", err)
88+
}
89+
90+
var buf bytes.Buffer
91+
92+
err = tmpl.Execute(&buf, mainCfg)
93+
t.Log(string(buf.Bytes()))
94+
if err != nil {
95+
t.Fatalf("Failed to write template %v", err)
96+
}
97+
}
98+
99+
func TestMainForNGINX(t *testing.T) {
100+
tmpl, err := template.New(nginxMainTmpl).ParseFiles(nginxMainTmpl)
101+
if err != nil {
102+
t.Fatalf("Failed to parse template file: %v", err)
103+
}
104+
105+
var buf bytes.Buffer
106+
107+
err = tmpl.Execute(&buf, mainCfg)
108+
t.Log(string(buf.Bytes()))
109+
if err != nil {
110+
t.Fatalf("Failed to write template %v", err)
111+
}
112+
}

0 commit comments

Comments
 (0)