8000 Add keepalive support to vs/vsr · nginx/kubernetes-ingress@bc2326e · GitHub
[go: up one dir, main page]

Skip to content

Commit bc2326e

Browse files
authored
Add keepalive support to vs/vsr
1 parent 1f4ad60 commit bc2326e

15 files changed

+164
-43
lines changed

docs/virtualserver-and-virtualserverroute.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ port: 80
179179
lb-method: round_robin
180180
fail-timeout: 10s
181181
max-fails: 1
182+
keepalive: 32
182183
connect-timeout: 30s
183184
read-timeout: 30s
184185
send-timeout: 30s
@@ -192,6 +193,7 @@ send-timeout: 30s
192193
| `lb-method` | The load [balancing method](https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/#choosing-a-load-balancing-method). To use the round-robin method, specify `round_robin`. The default is specified in the `lb-method` ConfigMap key. | `string` | No |
193194
| `fail-timeout` | The time during which the specified number of unsuccessful attempts to communicate with an upstream server should happen to consider the server unavailable. See the [fail_timeout](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#fail_timeout) parameter of the server directive. The default is set in the `fail-timeout` ConfigMap key. | `string` | No |
194195
| `max-fails` | The number of unsuccessful attempts to communicate with an upstream server that should happen in the duration set by the `fail-timeout` to consider the server unavailable. See the [max_fails](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#max_fails) parameter of the server directive. The default is set in the `max-fails` ConfgMap key. | `int` | No |
196+
| `keepalive` | Configures the cache for connections to upstream servers. The value `0` disables the cache. See the [keepalive](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive) directive. The default is set in the `keepalive` ConfigMap key. | `int` | No
195197
`connect-timeout` | The timeout for establishing a connection with an upstream server. See the [proxy_connect_timeout](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_connect_timeout) directive. The default is specified in the `proxy-connect-timeout` ConfigMap key. | `string` | No
196198
`read-timeout` | The timeout for reading a response from an upstream server. See the [proxy_read_timeout](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout) directive. The default is specified in the `proxy-read-timeout` ConfigMap key. | `string` | No
197199
`send-timeout` | The timeout for transmitting a request to an upstream server. See the [proxy_send_timeout](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_send_timeout) directive. The default is specified in the `proxy-send-timeout` ConfigMap key. | `string` | No

internal/configs/annotations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func parseAnnotations(ingEx *IngressEx, baseCfgParams *ConfigParams, isPlus bool
275275
cfgParams.SSLPorts = sslPorts
276276
}
277277

278-
if keepalive, exists, err := GetMapKeyAsInt64(ingEx.Ingress.Annotations, "nginx.org/keepalive", ingEx.Ingress); exists {
278+
if keepalive, exists, err := GetMapKeyAsInt(ingEx.Ingress.Annotations, "nginx.org/keepalive", ingEx.Ingress); exists {
279279
if err != nil {
280280
glog.Error(err)
281281
} else {

internal/configs/config_params.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type ConfigParams struct {
3939
MainWorkerShutdownTimeout string
4040
MainWorkerConnections string
4141
MainWorkerRlimitNofile string
42-
Keepalive int64
42+
Keepalive int
4343
MaxFails int
4444
MaxConns int
4545
FailTimeout string

internal/configs/configmaps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool) *ConfigParams {
291291
cfgParams.MainWorkerRlimitNofile = workerRlimitNofile
292292
}
293293

294-
if keepalive, exists, err := GetMapKeyAsInt64(cfgm.Data, "keepalive", cfgm); exists {
294+
if keepalive, exists, err := GetMapKeyAsInt(cfgm.Data, "keepalive", cfgm); exists {
295295
if err != nil {
296296
glog.Error(err)
297297
} else {

internal/configs/ingress.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package configs
33
import (
44
"fmt"
55
"sort"
6-
"strconv"
76
"strings"
87

98
"github.com/golang/glog"
@@ -214,7 +213,7 @@ func generateNginxCfg(ingEx *IngressEx, pems map[string]string, isMinion bool, b
214213

215214
var keepalive string
216215
if cfgParams.Keepalive > 0 {
217-
keepalive = strconv.FormatInt(cfgParams.Keepalive, 10)
216+
keepalive = fmt.Sprint(cfgParams.Keepalive)
218217
}
219218

220219
return version1.IngressNginxConfig{

internal/configs/version2/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ type VirtualServerConfig struct {
66
Upstreams []Upstream
77
SplitClients []SplitClient
88
Maps []Map
9-
Keepalive string
109
}
1110

1211
// Upstream defines an upstream.
1312
type Upstream struct {
14-
Name string
15-
Servers []UpstreamServer
16-
LBMethod string
13+
Name string
14+
Servers []UpstreamServer
15+
LBMethod string
16+
Keepalive int
1717
}
1818

1919
// UpstreamServer defines an upstream server.
@@ -60,6 +60,7 @@ type Location struct {
6060
ProxyBuffers string
6161
ProxyBufferSize string
6262
ProxyPass string
63+
HasKeepalive bool
6364
}
6465

6566
// SplitClient defines a split_clients.

internal/configs/version2/nginx-plus.virtualserver.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ upstream {{ $u.Name }} {
88
server {{ $s.Address }} max_fails={{ $s.MaxFails }} fail_timeout={{ $s.FailTimeout }};
99
{{ end }}
1010

11-
{{ if $.Keepalive }}
12-
keepalive {{ $.Keepalive }};
11+
{{ if $u.Keepalive }}
12+
keepalive {{ $u.Keepalive }};
1313
{{ end }}
1414
}
1515
{{ end }}
@@ -107,7 +107,7 @@ server {
107107

108108
proxy_http_version 1.1;
109109

110-
{{ if $.Keepalive }}
110+
{{ if $l.HasKeepalive }}
111111
proxy_set_header Connection "";
112112
{{ end }}
113113

internal/configs/version2/nginx.virtualserver.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ upstream {{ $u.Name }} {
88
server {{ $s.Address }} max_fails={{ $s.MaxFails }} fail_timeout={{ $s.FailTimeout }};
99
{{ end }}
1010

11-
{{ if $.Keepalive }}
12-
keepalive {{ $.Keepalive }};
11+
{{ if $u.Keepalive }}
12+
keepalive {{ $u.Keepalive }};
1313
{{ end }}
1414
}
1515
{{ end }}
@@ -107,7 +107,7 @@ server {
107107

108108
proxy_http_version 1.1;
109109

110-
{{ if $.Keepalive }}
110+
{{ if $l.HasKeepalive }}
111111
proxy_set_header Connection "";
112112
{{ end }}
113113

internal/configs/version2/templates_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ var virtualServerCfg = VirtualServerConfig{
1616
FailTimeout: "10s",
1717
},
1818
},
19-
LBMethod: "random",
19+
LBMethod: "random",
20+
Keepalive: 32,
2021
},
2122
{
2223
Name: "coffee-v1",
@@ -159,7 +160,6 @@ var virtualServerCfg = VirtualServerConfig{
159160
},
160161
},
161162
},
162-
Keepalive: "10",
163163
}
164164

165165
func TestVirtualServerForNginxPlus(t *testing.T) {

internal/configs/virtualserver.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ func generateVirtualServerConfig(virtualServerEx *VirtualServerEx, tlsPemFileNam
178178
}
179179
}
180180

181-
keepalive := ""
182-
if baseCfgParams.Keepalive > 0 {
183-
keepalive = fmt.Sprint(baseCfgParams.Keepalive)
184-
}
185-
186181
return version2.VirtualServerConfig{
187182
Upstreams: upstreams,
188183
SplitClients: splitClients,
@@ -200,7 +195,6 @@ func generateVirtualServerConfig(virtualServerEx *VirtualServerEx, tlsPemFileNam
200195
InternalRedirectLocations: internalRedirectLocations,
201196
Locations: locations,
202197
},
203-
Keepalive: keepalive,
204198
}
205199
}
206200

@@ -210,7 +204,7 @@ func generateUpstream(upstreamName string, upstream conf_v1alpha1.Upstream, endp
210204
for _, e := range endpoints {
211205
s := version2.UpstreamServer{
212206
Address: e,
213-
MaxFails: generatePositiveIntFromPointer(upstream.MaxFails, cfgParams.MaxFails),
207+
MaxFails: generateIntFromPointer(upstream.MaxFails, cfgParams.MaxFails),
214208
FailTimeout: generateTime(upstream.FailTimeout, cfgParams.FailTimeout),
215209
}
216210
upsServers = append(upsServers, s)
@@ -219,16 +213,17 @@ func generateUpstream(upstreamName string, upstream conf_v1alpha1.Upstream, endp
219213
if !isPlus && len(upsServers) == 0 {
220214
s := version2.UpstreamServer{
221215
Address: nginx502Server,
222-
MaxFails: generatePositiveIntFromPointer(upstream.MaxFails, cfgParams.MaxFails),
216+
MaxFails: generateIntFromPointer(upstream.MaxFails, cfgParams.MaxFails),
223217
FailTimeout: generateTime(upstream.FailTimeout, cfgParams.FailTimeout),
224218
}
225219
upsServers = append(upsServers, s)
226220
}
227221

228222
return version2.Upstream{
229-
Name: upstreamName,
230-
Servers: upsServers,
231-
LBMethod: generateLBMethod(upstream.LBMethod, cfgParams.LBMethod),
223+
Name: upstreamName,
224+
Servers: upsServers,
225+
LBMethod: generateLBMethod(upstream.LBMethod, cfgParams.LBMethod),
226+
Keepalive: generateIntFromPointer(upstream.Keepalive, cfgParams.Keepalive),
232227
}
233228
}
234229

@@ -248,13 +243,20 @@ func generateTime(time string, defaultTime string) string {
248243
return time
249244
}
250245

251-
func generatePositiveIntFromPointer(n *int, defaultN int) int {
246+
func generateIntFromPointer(n *int, defaultN int) int {
252247
if n == nil {
253248
return defaultN
254249
}
255250
return *n
256251
}
257252

253+
func upstreamHasKeepalive(upstream conf_v1alpha1.Upstream, cfgParams *ConfigParams) bool {
254+
if upstream.Keepalive != nil {
255+
return *upstream.Keepalive != 0
256+
}
257+
return cfgParams.Keepalive != 0
258+
}
259+
258260
func generateLocation(path string, upstreamName string, upstream conf_v1alpha1.Upstream, cfgParams *ConfigParams) version2.Location {
259261
return version2.Location{
260262
Path: path,
@@ -268,6 +270,7 @@ func generateLocation(path string, upstreamName string, upstream conf_v1alpha1.U
268270
ProxyBuffers: cfgParams.ProxyBuffers,
269271
ProxyBufferSize: cfgParams.ProxyBufferSize,
270272
ProxyPass: fmt.Sprintf("http://%v", upstreamName),
273+
HasKeepalive: upstreamHasKeepalive(upstream, cfgParams),
271274
}
272275
}
273276

0 commit comments

Comments
 (0)
0