Skip to content

Commit 9a5eb22

Browse files
Vighneswar Rao Bojjabvighnesha
Vighneswar Rao Bojja
authored andcommitted
Add Additional Upstream Server Parameters
1 parent 73ba1f5 commit 9a5eb22

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

client/nginx.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ type UpstreamServer struct {
3333
MaxFails *int `json:"max_fails,omitempty"`
3434
FailTimeout string `json:"fail_timeout,omitempty"`
3535
SlowStart string `json:"slow_start,omitempty"`
36+
Route string `json:"route"`
37+
Backup bool `json:"backup"`
38+
Down bool `json:"down"`
39+
Drain bool `json:"drain,omitempty"`
40+
Weight *int `json:"weight,omitempty"`
41+
Service string `json:"service,omitempty"`
3642
}
3743

3844
// StreamUpstreamServer lets you configure Stream upstreams.
@@ -43,6 +49,10 @@ type StreamUpstreamServer struct {
4349
MaxFails *int `json:"max_fails,omitempty"`
4450
FailTimeout string `json:"fail_timeout,omitempty"`
4551
SlowStart string `json:"slow_start,omitempty"`
52+
Backup bool `json:"backup"`
53+
Down bool `json:"down"`
54+
Weight *int `json:"weight,omitempty"`
55+
Service string `json:"service,omitempty"`
4656
}
4757

4858
type apiErrorResponse struct {

docker/test.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ server {
1818
health_check interval=10 fails=3 passes=1;
1919
}
2020
status_zone test;
21+
22+
}
23+
24+
upstream test-drain {
25+
zone test-drain 64k;
26+
27+
server 127.0.0.1:9001 drain;
2128
}

tests/client_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
)
1818

1919
var defaultMaxFails = 1
20+
var defaultWeight = 1
2021

2122
func TestStreamClient(t *testing.T) {
2223
httpClient := &http.Client{}
@@ -171,12 +172,17 @@ func TestStreamUpstreamServer(t *testing.T) {
171172
}
172173

173174
maxFails := 64
175+
weight := 10
176+
174177
streamServer := client.StreamUpstreamServer{
175178
Server: "127.0.0.1:2000",
176179
MaxConns: 321,
177180
MaxFails: &maxFails,
178181
FailTimeout: "21s",
179182
SlowStart: "12s",
183+
Weight: &weight,
184+
Backup: true,
185+
Down: true,
180186
}
181187
err = c.AddStreamServer(streamUpstream, streamServer)
182188
if err != nil {
@@ -363,12 +369,17 @@ func TestUpstreamServer(t *testing.T) {
363369
}
364370

365371
maxFails := 64
372+
weight := 10
366373
server := client.UpstreamServer{
367374
Server: "127.0.0.1:2000",
368375
MaxConns: 321,
369376
MaxFails: &maxFails,
370377
FailTimeout: "21s",
371378
SlowStart: "12s",
379+
Weight: &weight,
380+
Route: "test",
381+
Backup: true,
382+
Down: true,
372383
}
373384
err = c.AddHTTPServer(upstream, server)
374385
if err != nil {
@@ -496,6 +507,7 @@ func TestUpstreamServerDefaultParameters(t *testing.T) {
496507
SlowStart: "0s",
497508
MaxFails: &defaultMaxFails,
498509
FailTimeout: "10s",
510+
Weight: &defaultWeight,
499511
}
500512
err = c.AddHTTPServer(upstream, server)
501513
if err != nil {
@@ -608,6 +620,7 @@ func TestStreamUpstreamServerDefaultParameters(t *testing.T) {
608620
SlowStart: "0s",
609621
MaxFails: &defaultMaxFails,
610622
FailTimeout: "10s",
623+
Weight: &defaultWeight,
611624
}
612625
err = c.AddStreamServer(streamUpstream, streamServer)
613626
if err != nil {
@@ -955,3 +968,36 @@ func compareStreamUpstreamServers(x []client.StreamUpstreamServer, y []client.St
955968

956969
return reflect.DeepEqual(xServers, yServers)
957970
}
971+
972+
func TestUpstreamServerWithDrain(t *testing.T) {
973+
httpClient := &http.Client{}
974+
c, err := client.NewNginxClient(httpClient, "http://127.0.0.1:8080/api")
975+
if err != nil {
976+
t.Fatalf("Error connecting to nginx: %v", err)
977+
}
978+
979+
server := client.UpstreamServer{
980+
Server: "127.0.0.1:9001",
981+
MaxFails: &defaultMaxFails,
982+
FailTimeout: "10s",
983+
SlowStart: "0s",
984+
Weight: &defaultWeight,
985+
Drain: true,
986+
}
987+
988+
// Get existing upstream servers
989+
servers, err := c.GetHTTPServers("test-drain")
990+
if err != nil {
991+
t.Fatalf("Error getting HTTPServers: %v", err)
992+
}
993+
994+
if len(servers) != 1 {
995+
t.Errorf("Too many servers")
996+
}
997+
998+
servers[0].ID = 0
999+
1000+
if !reflect.DeepEqual(server, servers[0]) {
1001+
t.Errorf("Expected: %v Got: %v", server, servers[0])
1002+
}
1003+
}

0 commit comments

Comments
 (0)