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

+2
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

+1-1
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

+1-1
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

+1-1
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

+1-2
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

+5-4
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

+3-3
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

+3-3
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

+2-2
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

+15-12
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

internal/configs/virtualserver_test.go

+115-6
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ func TestGenerateVirtualServerConfig(t *testing.T) {
240240
Address: "10.0.0.20:80",
241241
},
242242
},
243+
Keepalive: 16,
243244
},
244245
{
245246
Name: "vs_default_cafe_vsr_default_coffee_coffee",
@@ -248,6 +249,7 @@ func TestGenerateVirtualServerConfig(t *testing.T) {
248249
Address: "10.0.0.30:80",
249250
},
250251
},
252+
Keepalive: 16,
251253
},
252254
},
253255
Server: version2.Server{
@@ -261,16 +263,17 @@ func TestGenerateVirtualServerConfig(t *testing.T) {
261263
Snippets: []string{"# server snippet"},
262264
Locations: []version2.Location{
263265
{
264-
Path: "/tea",
265-
ProxyPass: "http://vs_default_cafe_tea",
266+
Path: "/tea",
267+
ProxyPass: "http://vs_default_cafe_tea",
268+
HasKeepalive: true,
266269
},
267270
{
268-
Path: "/coffee",
269-
ProxyPass: "http://vs_default_cafe_vsr_default_coffee_coffee",
271+
Path: "/coffee",
272+
ProxyPass: "http://vs_default_cafe_vsr_default_coffee_coffee",
273+
HasKeepalive: true,
270274
},
271275
},
272276
},
273-
Keepalive: "16",
274277
}
275278

276279
isPlus := false
@@ -739,6 +742,7 @@ func TestGenerateUpstream(t *testing.T) {
739742
LBMethod: "random",
740743
MaxFails: 1,
741744
FailTimeout: "10s",
745+
Keepalive: 21,
742746
}
743747

744748
expected := version2.Upstream{
@@ -750,7 +754,8 @@ func TestGenerateUpstream(t *testing.T) {
750754
FailTimeout: "10s",
751755
},
752756
},
753-
LBMethod: "random",
757+
LBMethod: "random",
758+
Keepalive: 21,
754759
}
755760

756761
result := generateUpstream(name, upstream, endpoints, isPlus, &cfgParams)
@@ -759,6 +764,72 @@ func TestGenerateUpstream(t *testing.T) {
759764
}
760765
}
761766

767+
func TestGenerateUpstreamWithKeepalive(t *testing.T) {
768+
name := "test-upstream"
769+
noKeepalive := 0
770+
keepalive := 32
771+
endpoints := []string{
772+
"192.168.10.10:8080",
773+
}
774+
isPlus := false
775+
776+
tests := []struct {
777+
upstream conf_v1alpha1.Upstream
778+
cfgParams *ConfigParams
779+
expected version2.Upstream
780+
msg string
781+
}{
782+
{
783+
conf_v1alpha1.Upstream{Keepalive: &keepalive},
784+
&ConfigParams{Keepalive: 21},
785+
version2.Upstream{
786+
Name: "test-upstream",
787+
Servers: []version2.UpstreamServer{
788+
{
789+
Address: "192.168.10.10:8080",
790+
},
791+
},
792+
Keepalive: 32,
793+
},
794+
"upstream keepalive set, configparam set",
795+
},
796+
{
797+
conf_v1alpha1.Upstream{},
798+
&ConfigParams{Keepalive: 21},
799+
version2.Upstream{
800+
Name: "test-upstream",
801+
Servers: []version2.UpstreamServer{
802+
{
803+
Address: "192.168.10.10:8080",
804+
},
805+
},
806+
Keepalive: 21,
807+
},
808+
"upstream keepalive not set, configparam set",
809+
},
810+
{
811+
conf_v1alpha1.Upstream{Keepalive: &noKeepalive},
812+
&ConfigParams{Keepalive: 21},
813+
version2.Upstream{
814+
Name: "test-upstream",
815+
Servers: []version2.UpstreamServer{
816+
{
817+
Address: "192.168.10.10:8080",
818+
},
819+
},
820+
},
821+
"upstream keepalive set to 0, configparam set",
822+
},
823+
}
824+
825+
for _, test := range tests {
826+
result := generateUpstream(name, test.upstream, endpoints, isPlus, test.cfgParams)
827+
if !reflect.DeepEqual(result, test.expected) {
828+
t.Errorf("generateUpstream() returned %v but expected %v for the case of %v", result, test.expected, test.msg)
829+
}
830+
}
831+
}
832+
762833
func TestGenerateUpstreamForZeroEndpoints(t *testing.T) {
763834
name := "test-upstream"
764835
upstream := conf_v1alpha1.Upstream{}
@@ -1466,3 +1537,41 @@ func TestGenerateLBMethod(t *testing.T) {
14661537
}
14671538
}
14681539
}
1540+
1541+
func TestUpstreamHasKeepalive(t *testing.T) {
1542+
noKeepalive := 0
1543+
keepalive := 32
1544+
1545+
tests := []struct {
1546+
upstream conf_v1alpha1.Upstream
1547+
cfgParams *ConfigParams
1548+
expected bool
1549+
msg string
1550+
}{
1551+
{
1552+
conf_v1alpha1.Upstream{},
1553+
&ConfigParams{Keepalive: keepalive},
1554+
true,
1555+
"upstream keepalive not set, configparam keepalive set",
1556+
},
1557+
{
1558+
conf_v1alpha1.Upstream{Keepalive: &noKeepalive},
1559+
&ConfigParams{Keepalive: keepalive},
1560+
false,
1561+
"upstream keepalive set to 0, configparam keepive set",
1562+
},
1563+
{
1564+
conf_v1alpha1.Upstream{Keepalive: &keepalive},
1565+
&ConfigParams{Keepalive: noKeepalive},
1566+
true,
1567+
"upstream keepalive set, configparam keepalive set to 0",
1568+
},
1569+
}
1570+
1571+
for _, test := range tests {
1572+
result := upstreamHasKeepalive(test.upstream, test.cfgParams)
1573+
if result != test.expected {
1574+
t.Errorf("upstreamHasKeepalive() returned %v, but expected %v for the case of %v", result, test.expected, test.msg)
1575+
}
1576+
}
1577+
}

0 commit comments

Comments
 (0)