Skip to content

Commit

Permalink
Fixed bugs, updated tests, removed invalid options
Browse files Browse the repository at this point in the history
  • Loading branch information
soneillf5 committed Mar 24, 2021
1 parent eb9fdf1 commit 0bab691
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 22 deletions.
1 change: 1 addition & 0 deletions docs-web/configuration/transportserver-resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ loadBalancingMethod: least_conn
- No
* - ``loadBalancingMethod``
- The method used to load balance the upstream servers. By default, connections are distributed between the servers using a weighted round-robin balancing method. See the `upstream <http://nginx.org/en/docs/stream/ngx_stream_upstream_module.html#upstream>`_ section for available methods and their details.
- ``string``
- No

```
Expand Down
15 changes: 14 additions & 1 deletion internal/configs/transportserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ func generateStreamUpstream(upstream *conf_v1alpha1.Upstream, upstreamNamer *ups
return version2.StreamUpstream{
Name: name,
Servers: upsServers,
LoadBalancingMethod: upstream.LoadBalancingMethod,
LoadBalancingMethod: generateLoadBalancingMethod(upstream.LoadBalancingMethod),
}
}

func generateLoadBalancingMethod(method string) string {
if method == "" {
// By default, if unspecified, Nginx uses the 'round_robin' load balancing method.
// We override this default which suits the Ingress Controller better.
return "random two least_conn"
}
if method == "round_robin" {
// By default, Nginx uses round robin. We select this method by not specifying any method.
return ""
}
return method
}
4 changes: 4 additions & 0 deletions internal/configs/transportserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestGenerateTransportServerConfigForTCPSnippets(t *testing.T) {
ResourceNamespace: "default",
Service: "tcp-app-svc",
},
LoadBalancingMethod: "random two least_conn",
},
},
Server: version2.StreamServer{
Expand Down Expand Up @@ -196,6 +197,7 @@ func TestGenerateTransportServerConfigForTCP(t *testing.T) {
ResourceNamespace: "default",
Service: "tcp-app-svc",
},
LoadBalancingMethod: "random two least_conn",
},
},
Server: version2.StreamServer{
Expand Down Expand Up @@ -279,6 +281,7 @@ func TestGenerateTransportServerConfigForTLSPasstrhough(t *testing.T) {
ResourceNamespace: "default",
Service: "tcp-app-svc",
},
LoadBalancingMethod: "random two least_conn",
},
},
Server: version2.StreamServer{
Expand Down Expand Up @@ -368,6 +371,7 @@ func TestGenerateTransportServerConfigForUDP(t *testing.T) {
ResourceNamespace: "default",
Service: "udp-app-svc",
},
LoadBalancingMethod: "random two least_conn",
},
},
Server: version2.StreamServer{
Expand Down
2 changes: 1 addition & 1 deletion internal/configs/version2/nginx-plus.transportserver.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ upstream {{ $u.Name }} {
zone {{ $u.Name }} 256k;

{{ if $u.LoadBalancingMethod }}
$u.LoadBalancingMethod;
{{ $u.LoadBalancingMethod }};
{{ end }}

{{ range $s := $u.Servers }}
Expand Down
2 changes: 1 addition & 1 deletion internal/configs/version2/nginx.transportserver.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ upstream {{ $u.Name }} {
zone {{ $u.Name }} 256k;

{{ if $u.LoadBalancingMethod }}
$u.LoadBalancingMethod;
{{ $u.LoadBalancingMethod }};
{{ end }}

{{ range $s := $u.Servers }}
Expand Down
33 changes: 16 additions & 17 deletions pkg/apis/configuration/validation/transportserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,36 +208,35 @@ func validateLoadBalancingMethod(method string, fieldPath *field.Path, isPlus bo
}

var nginxStreamLoadBalanceValidInput = map[string]bool{
"round_robin": true,
"least_conn": true,
"random": true,
"random two": true,
"random two least_conn": true,
}

var nginxPlusStreamLoadBalanceValidInput = map[string]bool{
"least_conn": true,
"random": true,
"random two": true,
"random two least_conn": true,
"random two least_time": true,
"random two least_time=connect": true,
"random two least_time=first_byte": true,
"random two least_time=last_byte": true,
"random least_conn": true,
"random least_time": true,
"random least_time=connect": true,
"random least_time=first_byte": true,
"random least_time=last_byte": true,
"least_time connect": true,
"least_time first_byte": true,
"least_time last_byte": true,
"least_time last_byte inflight": true,
"round_robin": true,
"least_conn": true,
"random": true,
"random two": true,
"random two least_conn": true,
"random least_conn": true,
"least_time connect": true,
"least_time first_byte": true,
"least_time last_byte": true,
"least_time last_byte inflight": true,
}

func validateHashLoadBalancingMethod(method string) error {
keyWords := strings.Split(method, " ")
if len(keyWords) > 0 && keyWords[0] == "hash" {
if len(keyWords) == 2 || (len(keyWords) == 3 && keyWords[2] == "consistent") {
value := keyWords[1]
if !escapedStringsFmtRegexp.MatchString(value) {
return fmt.Errorf("invalid value for hash: %v", validation.RegexError(escapedStringsErrMsg, escapedStringsFmt))
}

return nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/configuration/validation/transportserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ func TestValidateTransportServerLoadBalancingMethod(t *testing.T) {
{
method: "random two least_time",
isPlus: true,
hasError: false,
hasError: true,
}, {
method: "random two least_time=connect",
isPlus: true,
hasError: false,
hasError: true,
},
}

Expand Down

0 comments on commit 0bab691

Please sign in to comment.