Skip to content

Commit 11d996e

Browse files
authored
Merge pull request #194 from nginxinc/keepalive
Add keepalive configmap key and annotation
2 parents 84d7163 + 714fd99 commit 11d996e

File tree

9 files changed

+38
-3
lines changed

9 files changed

+38
-3
lines changed

examples/customization/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ The table below summarizes some of the options. More options (extensions) are av
3939
| `nginx.org/listen-ports-ssl` | N/A | Configures HTTPS ports that NGINX will listen on. | `[443]` |
4040
| N/A | `worker-processes` | Sets the value of the [worker_processes](http://nginx.org/en/docs/ngx_core_module.html#worker_processes) directive. | `auto` |
4141
| N/A | `worker-cpu-affinity` | Sets the value of the [worker_cpu_affinity](http://nginx.org/en/docs/ngx_core_module.html#worker_cpu_affinity) directive. | N/A |
42+
| `nginx.org/keepalive` | `keepalive` | Sets the value of the [keepalive](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive) directive. Note that `proxy_set_header Connection "";` is added to the generated configuration when the value > 0. | `0` |
4243

4344
## Using ConfigMaps
4445

examples/customization/nginx-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ data:
4646
charset koi8-r;
4747
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
4848
worker-cpu-affinity: "auto" # No default. Sets the value of the worker_cpu_affinity directive. See http://nginx.org/en/docs/ngx_core_module.html#worker_cpu_affinity
49+
keepalive: "32" # default is 0. When > 0, sets the value of the keepalive directive and adds 'proxy_set_header Connection "";' to a location block. See http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive

nginx-controller/controller/controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,13 @@ func (lbc *LoadBalancerController) syncCfgm(task Task) {
575575
if workerCPUAffinity, exists := cfgm.Data["worker-cpu-affinity"]; exists {
576576
cfg.MainWorkerCPUAffinity = workerCPUAffinity
577577
}
578+
if keepalive, exists, err := nginx.GetMapKeyAsInt(cfgm.Data, "keepalive", cfgm); exists {
579+
if err != nil {
580+
glog.Error(err)
581+
} else {
582+
cfg.Keepalive = keepalive
583+
}
584+
}
578585
}
579586

580587
var ingExes []*nginx.IngressEx

nginx-controller/nginx/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Config struct {
2727
LBMethod string
2828
MainWorkerProcesses string
2929
MainWorkerCPUAffinity string
30+
Keepalive int64
3031

3132
// http://nginx.org/en/docs/http/ngx_http_realip_module.html
3233
RealIPHeader string

nginx-controller/nginx/configurator.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,16 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
179179
servers = append(servers, server)
180180
}
181181

182-
return IngressNginxConfig{Upstreams: upstreamMapToSlice(upstreams), Servers: servers}
182+
var keepalive string
183+
if ingCfg.Keepalive > 0 {
184+
keepalive = strconv.FormatInt(ingCfg.Keepalive, 10)
185+
}
186+
187+
return IngressNginxConfig{
188+
Upstreams: upstreamMapToSlice(upstreams),
189+
Servers: servers,
190+
Keepalive: keepalive,
191+
}
183192
}
184193

185194
func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
@@ -323,6 +332,14 @@ func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
323332
ingCfg.SSLPorts = sslPorts
324333
}
325334

335+
if keepalive, exists, err := GetMapKeyAsInt(ingEx.Ingress.Annotations, "nginx.org/keepalive", ingEx.Ingress); exists {
336+
if err != nil {
337+
glog.Error(err)
338+
} else {
339+
ingCfg.Keepalive = keepalive
340+
}
341+
}
342+
326343
return ingCfg
327344
}
328345

nginx-controller/nginx/nginx.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type NginxController struct {
3232
type IngressNginxConfig struct {
3333
Upstreams []Upstream
3434
Servers []Server
35+
Keepalive string
3536
}
3637

3738
// Upstream describes an NGINX upstream

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ upstream {{$upstream.Name}} {
77
{{if $upstream.StickyCookie}}
88
sticky cookie {{$upstream.StickyCookie}};
99
{{end}}
10+
{{if $.Keepalive}}keepalive {{$.Keepalive}};{{end}}
1011
}{{end}}
1112

1213
{{range $server := .Servers}}
@@ -74,7 +75,9 @@ server {
7475
{{if $location.Websocket}}
7576
proxy_set_header Upgrade $http_upgrade;
7677
proxy_set_header Connection $connection_upgrade;
77-
{{end}}
78+
{{- else}}
79+
{{- if $.Keepalive}}proxy_set_header Connection "";{{end}}
80+
{{- end}}
7881

7982
{{- if $location.LocationSnippets}}
8083
{{range $value := $location.LocationSnippets}}

nginx-controller/nginx/templates/nginx.ingress.tmpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ upstream {{$upstream.Name}} {
33
{{if $upstream.LBMethod }}{{$upstream.LBMethod}};{{end}}
44
{{range $server := $upstream.UpstreamServers}}
55
server {{$server.Address}}:{{$server.Port}};{{end}}
6+
{{if $.Keepalive}}keepalive {{$.Keepalive}};{{end}}
67
}{{end}}
78

89
{{range $server := .Servers}}
@@ -55,7 +56,9 @@ server {
5556
{{if $location.Websocket}}
5657
proxy_set_header Upgrade $http_upgrade;
5758
proxy_set_header Connection $connection_upgrade;
58-
{{end}}
59+
{{- else}}
60+
{{- if $.Keepalive}}proxy_set_header Connection "";{{end}}
61+
{{- end}}
5962

6063
{{- if $location.LocationSnippets}}
6164
{{range $value := $location.LocationSnippets}}

nginx-controller/nginx/templates/templates_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var ingCfg = nginx.IngressNginxConfig{
4343
},
4444
},
4545
Upstreams: []nginx.Upstream{testUps},
46+
Keepalive: "16",
4647
}
4748

4849
var mainCfg = nginx.NginxMainConfig{

0 commit comments

Comments
 (0)