Skip to content

Commit

Permalink
Add keepalive configmap key and annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
pleshakov committed Oct 3, 2017
1 parent 84d7163 commit 714fd99
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/customization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The table below summarizes some of the options. More options (extensions) are av
| `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` |
| 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 |
| `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` |

## Using ConfigMaps

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 @@ -46,3 +46,4 @@ data:
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
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
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
7 changes: 7 additions & 0 deletions nginx-controller/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,13 @@ func (lbc *LoadBalancerController) syncCfgm(task Task) {
if workerCPUAffinity, exists := cfgm.Data["worker-cpu-affinity"]; exists {
cfg.MainWorkerCPUAffinity = workerCPUAffinity
}
if keepalive, exists, err := nginx.GetMapKeyAsInt(cfgm.Data, "keepalive", cfgm); exists {
if err != nil {
glog.Error(err)
} else {
cfg.Keepalive = keepalive
}
}
}

var ingExes []*nginx.IngressEx
Expand Down
1 change: 1 addition & 0 deletions nginx-controller/nginx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {
LBMethod string
MainWorkerProcesses string
MainWorkerCPUAffinity string
Keepalive int64

// http://nginx.org/en/docs/http/ngx_http_realip_module.html
RealIPHeader string
Expand Down
19 changes: 18 additions & 1 deletion nginx-controller/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,16 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
servers = append(servers, server)
}

return IngressNginxConfig{Upstreams: upstreamMapToSlice(upstreams), Servers: servers}
var keepalive string
if ingCfg.Keepalive > 0 {
keepalive = strconv.FormatInt(ingCfg.Keepalive, 10)
}

return IngressNginxConfig{
Upstreams: upstreamMapToSlice(upstreams),
Servers: servers,
Keepalive: keepalive,
}
}

func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
Expand Down Expand Up @@ -323,6 +332,14 @@ func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
ingCfg.SSLPorts = sslPorts
}

if keepalive, exists, err := GetMapKeyAsInt(ingEx.Ingress.Annotations, "nginx.org/keepalive", ingEx.Ingress); exists {
if err != nil {
glog.Error(err)
} else {
ingCfg.Keepalive = keepalive
}
}

return ingCfg
}

Expand Down
1 change: 1 addition & 0 deletions nginx-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type NginxController struct {
type IngressNginxConfig struct {
Upstreams []Upstream
Servers []Server
Keepalive string
}

// Upstream describes an NGINX upstream
Expand Down
5 changes: 4 additions & 1 deletion nginx-controller/nginx/templates/nginx-plus.ingress.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ upstream {{$upstream.Name}} {
{{if $upstream.StickyCookie}}
sticky cookie {{$upstream.StickyCookie}};
{{end}}
{{if $.Keepalive}}keepalive {{$.Keepalive}};{{end}}
}{{end}}

{{range $server := .Servers}}
Expand Down Expand Up @@ -74,7 +75,9 @@ server {
{{if $location.Websocket}}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
{{end}}
{{- else}}
{{- if $.Keepalive}}proxy_set_header Connection "";{{end}}
{{- end}}

{{- if $location.LocationSnippets}}
{{range $value := $location.LocationSnippets}}
Expand Down
5 changes: 4 additions & 1 deletion nginx-controller/nginx/templates/nginx.ingress.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ upstream {{$upstream.Name}} {
{{if $upstream.LBMethod }}{{$upstream.LBMethod}};{{end}}
{{range $server := $upstream.UpstreamServers}}
server {{$server.Address}}:{{$server.Port}};{{end}}
{{if $.Keepalive}}keepalive {{$.Keepalive}};{{end}}
}{{end}}

{{range $server := .Servers}}
Expand Down Expand Up @@ -55,7 +56,9 @@ server {
{{if $location.Websocket}}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
{{end}}
{{- else}}
{{- if $.Keepalive}}proxy_set_header Connection "";{{end}}
{{- end}}

{{- if $location.LocationSnippets}}
{{range $value := $location.LocationSnippets}}
Expand Down
1 change: 1 addition & 0 deletions nginx-controller/nginx/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var ingCfg = nginx.IngressNginxConfig{
},
},
Upstreams: []nginx.Upstream{testUps},
Keepalive: "16",
}

var mainCfg = nginx.NginxMainConfig{
Expand Down

0 comments on commit 714fd99

Please sign in to comment.