Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add worker-processes configmap key #192

Merged
merged 1 commit into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions examples/customization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The table below summarizes some of the options. More options (extensions) are av
| `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. | `""` |
| `nginx.org/listen-ports` | N/A | Configures HTTP ports that NGINX will listen on. | `[80]` |
| `nginx.org/listen-ports-ssl` | N/A | Configures HTTPS ports that NGINX will listen on. | `[443]` |
| N/A | `worker-processes` | Sets the value of the [worker_processes](http://nginx.org/en/docs/ngx_core_module.html#worker_processes) directive. | `auto` |

## Using ConfigMaps

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

1. Create a configmaps file with the name *nginx-config.yaml* and set the values
that make sense for your setup:
```yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-config
data:
proxy-connect-timeout: "10s"
proxy-read-timeout: "10s"
client-max-body-size: "2m"
```
See the **nginx-config.yaml** from this directory for a complete example.
```yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-config
data:
proxy-connect-timeout: "10s"
proxy-read-timeout: "10s"
client-max-body-size: "2m"
```
See the **nginx-config.yaml** from this directory for a complete example.

2. Create a configmaps resource:
```
$ kubectl create -f nginx-config.yaml
```
The NGINX configuration will be updated.
1. Create a configmaps resource:
```
$ kubectl create -f nginx-config.yaml
```
The NGINX configuration will be updated.

3. If you want to update the configmaps, update the file and replace the config map:
```
$ kubectl replace -f nginx-config.yaml
```
The NGINX configuration will be updated.
1. If you want to update the configmaps, update the file and replace the config map:
```
$ kubectl replace -f nginx-config.yaml
```
The NGINX configuration will be updated.

## Using Annotations

Expand Down
1 change: 1 addition & 0 deletions examples/customization/nginx-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ data:
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.
proxy_temp_path /var/nginx/proxy_temp;
charset koi8-r;
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
8 changes: 7 additions & 1 deletion nginx-controller/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,13 @@ func (lbc *LoadBalancerController) syncCfgm(task Task) {
cfg.ServerSnippets = serverSnippets
}
}

if _, exists, err := nginx.GetMapKeyAsInt(cfgm.Data, "worker-processes", cfgm); exists {
if err != nil && cfgm.Data["worker-processes"] != "auto" {
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"])
} else {
cfg.MainWorkerProcesses = cfgm.Data["worker-processes"]
}
}
}

var ingExes []*nginx.IngressEx
Expand Down
2 changes: 2 additions & 0 deletions nginx-controller/nginx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
HSTSMaxAge int64
HSTSIncludeSubdomains bool
LBMethod string
MainWorkerProcesses string

// http://nginx.org/en/docs/http/ngx_http_realip_module.html
RealIPHeader string
Expand Down Expand Up @@ -55,6 +56,7 @@ func NewDefaultConfig() *Config {
ClientMaxBodySize: "1m",
MainServerNamesHashMaxSize: "512",
ProxyBuffering: true,
MainWorkerProcesses: "auto",
HSTSMaxAge: 2592000,
Ports: []int{80},
SSLPorts: []int{443},
Expand Down
7 changes: 4 additions & 3 deletions nginx-controller/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,10 @@ func (cnf *Configurator) UpdateConfig(config *Config, ingExes []*IngressEx) erro
SSLCiphers: config.MainServerSSLCiphers,
SSLDHParam: config.MainServerSSLDHParam,
SSLPreferServerCiphers: config.MainServerSSLPreferServerCiphers,
HTTP2: config.HTTP2,
ServerTokens: config.ServerTokens,
ProxyProtocol: config.ProxyProtocol,
HTTP2: config.HTTP2,
ServerTokens: config.ServerTokens,
ProxyProtocol: config.ProxyProtocol,
WorkerProcesses: config.MainWorkerProcesses,
}

cnf.nginx.UpdateMainConfigFile(mainCfg)
Expand Down
2 changes: 2 additions & 0 deletions nginx-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type NginxMainConfig struct {
HTTP2 bool
ServerTokens string
ProxyProtocol bool
WorkerProcesses string
}

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

Expand Down
2 changes: 1 addition & 1 deletion nginx-controller/nginx/templates/nginx-plus.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

user nginx;
worker_processes auto;
worker_processes {{.WorkerProcesses}};

daemon off;

Expand Down
2 changes: 1 addition & 1 deletion nginx-controller/nginx/templates/nginx.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

user nginx;
worker_processes auto;
worker_processes {{.WorkerProcesses}};

daemon off;

Expand Down
38 changes: 38 additions & 0 deletions nginx-controller/nginx/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
)

const nginxIngressTmpl = "nginx.ingress.tmpl"
const nginxMainTmpl = "nginx.tmpl"
const nginxPlusIngressTmpl = "nginx-plus.ingress.tmpl"
const nginxPlusMainTmpl = "nginx-plus.tmpl"

var testUps = nginx.Upstream{
Name: "test",
Expand Down Expand Up @@ -43,6 +45,12 @@ var ingCfg = nginx.IngressNginxConfig{
Upstreams: []nginx.Upstream{testUps},
}

var mainCfg = nginx.NginxMainConfig{
ServerNamesHashMaxSize: "512",
ServerTokens: "off",
WorkerProcesses: "auto",
}

func TestIngressForNGINXPlus(t *testing.T) {
tmpl, err := template.New(nginxPlusIngressTmpl).ParseFiles(nginxPlusIngressTmpl)
if err != nil {
Expand Down Expand Up @@ -72,3 +80,33 @@ func TestIngressForNGINX(t *testing.T) {
t.Fatalf("Failed to write template %v", err)
}
}

func TestMainForNGINXPlus(t *testing.T) {
tmpl, err := template.New(nginxPlusMainTmpl).ParseFiles(nginxPlusMainTmpl)
if err != nil {
t.Fatalf("Failed to parse template file: %v", err)
}

var buf bytes.Buffer

err = tmpl.Execute(&buf, mainCfg)
t.Log(string(buf.Bytes()))
if err != nil {
t.Fatalf("Failed to write template %v", err)
}
}

func TestMainForNGINX(t *testing.T) {
tmpl, err := template.New(nginxMainTmpl).ParseFiles(nginxMainTmpl)
if err != nil {
t.Fatalf("Failed to parse template file: %v", err)
}

var buf bytes.Buffer

err = tmpl.Execute(&buf, mainCfg)
t.Log(string(buf.Bytes()))
if err != nil {
t.Fatalf("Failed to write template %v", err)
}
}