Skip to content

Commit 722e910

Browse files
committed
Allow upstream parameters update
1 parent 5ed42c5 commit 722e910

File tree

210 files changed

+24014
-8385
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

210 files changed

+24014
-8385
lines changed

Gopkg.lock

Lines changed: 19 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
[[constraint]]
4141
name = "github.com/nginxinc/nginx-plus-go-client"
42-
version = "0.4.0"
42+
version = "0.6.0"
4343

4444
# https://github.com/Azure/go-autorest/issues/439#issuecomment-521732075
4545
[[override]]

cmd/sync/aws.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ func (client *AWSClient) GetUpstreams() []Upstream {
6767
Port: awsU.Port,
6868
Kind: awsU.Kind,
6969
ScalingGroup: awsU.AutoscalingGroup,
70+
MaxConns: &awsU.MaxConns,
71+
MaxFails: &awsU.MaxFails,
72+
FailTimeout: awsU.FailTimeout,
73+
SlowStart: awsU.SlowStart,
7074
}
7175
upstreams = append(upstreams, u)
7276
}
@@ -108,7 +112,7 @@ func parseAWSConfig(data []byte) (*awsConfig, error) {
108112
func (client *AWSClient) CheckIfScalingGroupExists(name string) (bool, error) {
109113
params := &ec2.DescribeInstancesInput{
110114
Filters: []*ec2.Filter{
111-
&ec2.Filter{
115+
{
112116
Name: aws.String("tag:aws:autoscaling:groupName"),
113117
Values: []*string{
114118
aws.String(name),
@@ -129,7 +133,7 @@ func (client *AWSClient) CheckIfScalingGroupExists(name string) (bool, error) {
129133
func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, error) {
130134
params := &ec2.DescribeInstancesInput{
131135
Filters: []*ec2.Filter{
132-
&ec2.Filter{
136+
{
133137
Name: aws.String("tag:aws:autoscaling:groupName"),
134138
Values: []*string{
135139
aws.String(name),
@@ -171,6 +175,10 @@ type awsUpstream struct {
171175
AutoscalingGroup string `yaml:"autoscaling_group"`
172176
Port int
173177
Kind string
178+
MaxConns int `yaml:"max_conns"`
179+
MaxFails int `yaml:"max_fails"`
180+
FailTimeout string `yaml:"fail_timeout"`
181+
SlowStart string `yaml:"slow_start"`
174182
}
175183

176184
func validateAWSConfig(cfg *awsConfig) error {
@@ -195,6 +203,18 @@ func validateAWSConfig(cfg *awsConfig) error {
195203
if ups.Kind == "" || !(ups.Kind == "http" || ups.Kind == "stream") {
196204
return fmt.Errorf(upstreamKindErrorMsgFormat, ups.Name)
197205
}
206+
if ups.MaxConns < 0 {
207+
return fmt.Errorf(upstreamMaxConnsErrorMsgFmt, ups.MaxConns)
208+
}
209+
if ups.MaxFails < 0 {
210+
return fmt.Errorf(upstreamMaxFailsErrorMsgFmt, ups.MaxFails)
211+
}
212+
if !isValidTime(ups.FailTimeout) {
213+
return fmt.Errorf(upstreamFailTimeoutErrorMsgFmt, ups.FailTimeout)
214+
}
215+
if !isValidTime(ups.SlowStart) {
216+
return fmt.Errorf(upstreamSlowStartErrorMsgFmt, ups.SlowStart)
217+
}
198218
}
199219

200220
return nil

cmd/sync/aws_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ func getInvalidAWSConfigInput() []*testInputAWS {
5151
invalidUpstreamKindCfg.Upstreams[0].Kind = ""
5252
input = append(input, &testInputAWS{invalidUpstreamKindCfg, "invalid kind of the upstream"})
5353

54+
invalidUpstreamMaxConnsCfg := getValidAWSConfig()
55+
invalidUpstreamMaxConnsCfg.Upstreams[0].MaxConns = -10
56+
input = append(input, &testInputAWS{invalidUpstreamMaxConnsCfg, "invalid max_conns of the upstream"})
57+
58+
invalidUpstreamMaxFailsCfg := getValidAWSConfig()
59+
invalidUpstreamMaxFailsCfg.Upstreams[0].MaxFails = -10
60+
input = append(input, &testInputAWS{invalidUpstreamMaxFailsCfg, "invalid max_fails of the upstream"})
61+
62+
invalidUpstreamFailTimeoutCfg := getValidAWSConfig()
63+
invalidUpstreamFailTimeoutCfg.Upstreams[0].FailTimeout = "-10s"
64+
input = append(input, &testInputAWS{invalidUpstreamFailTimeoutCfg, "invalid fail_timeout of the upstream"})
65+
66+
invalidUpstreamSlowStartCfg := getValidAWSConfig()
67+
invalidUpstreamSlowStartCfg.Upstreams[0].SlowStart = "-10s"
68+
input = append(input, &testInputAWS{invalidUpstreamSlowStartCfg, "invalid slow_start of the upstream"})
69+
5470
return input
5571
}
5672

cmd/sync/azure.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ func (client *AzureClient) GetUpstreams() []Upstream {
136136
Port: azureU.Port,
137137
Kind: azureU.Kind,
138138
ScalingGroup: azureU.VMScaleSet,
139+
MaxConns: &azureU.MaxConns,
140+
MaxFails: &azureU.MaxFails,
141+
FailTimeout: azureU.FailTimeout,
142+
SlowStart: azureU.SlowStart,
139143
}
140144
upstreams = append(upstreams, u)
141145
}
@@ -149,10 +153,14 @@ type azureConfig struct {
149153
}
150154

151155
type azureUpstream struct {
152-
Name string
153-
VMScaleSet string `yaml:"virtual_machine_scale_set"`
154-
Port int
155-
Kind string
156+
Name string
157+
VMScaleSet string `yaml:"virtual_machine_scale_set"`
158+
Port int
159+
Kind string
160+
MaxConns int `yaml:"max_conns"`
161+
MaxFails int `yaml:"max_fails"`
162+
FailTimeout string `yaml:"fail_timeout"`
163+
SlowStart string `yaml:"slow_start"`
156164
}
157165

158166
func validateAzureConfig(cfg *azureConfig) error {
@@ -165,7 +173,7 @@ func validateAzureConfig(cfg *azureConfig) error {
165173
}
166174

167175
if len(cfg.Upstreams) == 0 {
168-
return fmt.Errorf("There is no upstreams found in the config file")
176+
return fmt.Errorf("There are no upstreams found in the config file")
169177
}
170178

171179
for _, ups := range cfg.Upstreams {
@@ -181,6 +189,18 @@ func validateAzureConfig(cfg *azureConfig) error {
181189
if ups.Kind == "" || !(ups.Kind == "http" || ups.Kind == "stream") {
182190
return fmt.Errorf(upstreamKindErrorMsgFormat, ups.Name)
183191
}
192+
if ups.MaxConns < 0 {
193+
return fmt.Errorf(upstreamMaxConnsErrorMsgFmt, ups.MaxConns)
194+
}
195+
if ups.MaxFails < 0 {
196+
return fmt.Errorf(upstreamMaxFailsErrorMsgFmt, ups.MaxFails)
197+
}
198+
if !isValidTime(ups.FailTimeout) {
199+
return fmt.Errorf(upstreamFailTimeoutErrorMsgFmt, ups.FailTimeout)
200+
}
201+
if !isValidTime(ups.SlowStart) {
202+
return fmt.Errorf(upstreamSlowStartErrorMsgFmt, ups.SlowStart)
203+
}
184204
}
185205
return nil
186206
}

cmd/sync/azure_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ func getInvalidAzureConfigInput() []*testInputAzure {
6060
invalidUpstreamKindCfg.Upstreams[0].Kind = ""
6161
input = append(input, &testInputAzure{invalidUpstreamKindCfg, "invalid kind of the upstream"})
6262

63+
invalidUpstreamMaxConnsCfg := getValidAzureConfig()
64+
invalidUpstreamMaxConnsCfg.Upstreams[0].MaxConns = -10
65+
input = append(input, &testInputAzure{invalidUpstreamMaxConnsCfg, "invalid max_conns of the upstream"})
66+
67+
invalidUpstreamMaxFailsCfg := getValidAzureConfig()
68+
invalidUpstreamMaxFailsCfg.Upstreams[0].MaxFails = -10
69+
input = append(input, &testInputAzure{invalidUpstreamMaxFailsCfg, "invalid max_fails of the upstream"})
70+
71+
invalidUpstreamFailTimeoutCfg := getValidAzureConfig()
72+
invalidUpstreamFailTimeoutCfg.Upstreams[0].FailTimeout = "-10s"
73+
input = append(input, &testInputAzure{invalidUpstreamFailTimeoutCfg, "invalid fail_timeout of the upstream"})
74+
75+
invalidUpstreamSlowStartCfg := getValidAzureConfig()
76+
invalidUpstreamSlowStartCfg.Upstreams[0].SlowStart = "-10s"
77+
input = append(input, &testInputAzure{invalidUpstreamSlowStartCfg, "invalid slow_start of the upstream"})
78+
6379
return input
6480
}
6581

cmd/sync/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ type Upstream struct {
5555
Port int
5656
ScalingGroup string
5757
Kind string
58+
MaxConns *int
59+
MaxFails *int
60+
FailTimeout string
61+
SlowStart string
5862
}

cmd/sync/errormessages.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ const upstreamNameErrorMsg = "The mandatory field name is either empty or missin
88
const upstreamErrorMsgFormat = "The mandatory field %v is either empty or missing for the upstream %v in the config file"
99
const upstreamPortErrorMsgFormat = "The mandatory field port is either zero or missing for the upstream %v in the config file"
1010
const upstreamKindErrorMsgFormat = "The mandatory field kind is either not equal to http or tcp or missing for the upstream %v in the config file"
11+
const upstreamMaxConnsErrorMsgFmt = "The field max_conns has invalid value %v in the config file"
12+
const upstreamMaxFailsErrorMsgFmt = "The field max_fails has invalid value %v in the config file"
13+
const upstreamFailTimeoutErrorMsgFmt = "The field fail_timeout has invalid value %v in the config file"
14+
const upstreamSlowStartErrorMsgFmt = "The field slow_start has invalid value %v in the config file"

cmd/sync/main.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,38 +113,44 @@ func main() {
113113
for _, ip := range ips {
114114
backend := fmt.Sprintf("%v:%v", ip, upstream.Port)
115115
upsServers = append(upsServers, nginx.UpstreamServer{
116-
Server: backend,
117-
MaxFails: 1,
116+
Server: backend,
117+
MaxConns: upstream.MaxConns,
118+
MaxFails: upstream.MaxFails,
119+
FailTimeout: upstream.FailTimeout,
120+
SlowStart: upstream.SlowStart,
118121
})
119122
}
120123

121-
added, removed, err := nginxClient.UpdateHTTPServers(upstream.Name, upsServers)
124+
added, removed, updated, err := nginxClient.UpdateHTTPServers(upstream.Name, upsServers)
122125
if err != nil {
123126
log.Printf("Couldn't update HTTP servers in NGINX: %v", err)
124127
continue
125128
}
126129

127-
if len(added) > 0 || len(removed) > 0 {
128-
log.Printf("Updated HTTP servers of %v; Added: %v, Removed: %v", upstream, added, removed)
130+
if len(added) > 0 || len(removed) > 0 || len(updated) > 0 {
131+
log.Printf("Updated HTTP servers of %v; Added: %+v, Removed: %+v, Updated: %+v", upstream, added, removed, updated)
129132
}
130133
} else {
131134
var upsServers []nginx.StreamUpstreamServer
132135
for _, ip := range ips {
133136
backend := fmt.Sprintf("%v:%v", ip, upstream.Port)
134137
upsServers = append(upsServers, nginx.StreamUpstreamServer{
135-
Server: backend,
136-
MaxFails: 1,
138+
Server: backend,
139+
MaxConns: upstream.MaxConns,
140+
MaxFails: upstream.MaxFails,
141+
FailTimeout: upstream.FailTimeout,
142+
SlowStart: upstream.SlowStart,
137143
})
138144
}
139145

140-
added, removed, err := nginxClient.UpdateStreamServers(upstream.Name, upsServers)
146+
added, removed, updated, err := nginxClient.UpdateStreamServers(upstream.Name, upsServers)
141147
if err != nil {
142148
log.Printf("Couldn't update Steam servers in NGINX: %v", err)
143149
continue
144150
}
145151

146-
if len(added) > 0 || len(removed) > 0 {
147-
log.Printf("Updated Stream servers of %v; Added: %v, Removed: %v", upstream, added, removed)
152+
if len(added) > 0 || len(removed) > 0 || len(updated) > 0 {
153+
log.Printf("Updated Stream servers of %v; Added: %+v, Removed: %+v, Updated: %+v", upstream, added, removed, updated)
148154
}
149155
}
150156

cmd/sync/validation.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
)
7+
8+
// http://nginx.org/en/docs/syntax.html
9+
var validTimeSuffixes = []string{
10+
"ms",
11+
"s",
12+
"m",
13+
"h",
14+
"d",
15+
"w",
16+
"M",
17+
"y",
18+
}
19+
20+
var durationEscaped = strings.Join(validTimeSuffixes, "|")
21+
var validNginxTime = regexp.MustCompile(`^([0-9]+([` + durationEscaped + `]?){0,1} *)+$`)
22+
23+
func isValidTime(time string) bool {
24+
if time == "" {
25+
return true
26+
}
27+
28+
time = strings.TrimSpace(time)
29+
30+
return validNginxTime.MatchString(time)
31+
}

0 commit comments

Comments
 (0)