8000 Add passive health checks to TransportServer by LorcanMcVeigh · Pull Request #1382 · nginx/kubernetes-ingress · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ spec:
description: Upstream defines an upstream.
type: object
properties:
failTimeout:
type: string
maxFails:
type: integer
name:
type: string
port:
Expand Down
4 changes: 4 additions & 0 deletions deployments/common/crds/k8s.nginx.org_transportservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ spec:
description: Upstream defines an upstream.
type: object
properties:
failTimeout:
type: string
maxFails:
type: integer
name:
type: string
port:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ spec:
description: Upstream defines an upstream.
type: object
properties:
failTimeout:
type: string
maxFails:
type: integer
name:
type: string
port:
Expand Down
11 changes: 11 additions & 0 deletions docs-web/configuration/transportserver-resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ The upstream defines a destination for the TransportServer. For example:
name: secure-app
service: secure-app
port: 8443
maxFails: 3
failTimeout: 30s
```

```eval_rst
Expand All @@ -179,6 +181,15 @@ port: 8443
- The port of the service. If the service doesn't define that port, NGINX will assume the service has zero endpoints and close client connections/ignore datagrams. The port must fall into the range ``1..65535``.
- ``int``
- Yes
* - ``maxFails``
- Sets the `number <https://nginx.org/en/docs/stream/ngx_stream_upstream_module.html#max_fails>`_ of unsuccessful attempts to communicate with the server that should happen in the duration set by the failTimeout parameter to consider the server unavailable. The default ``1``.
- ``int``
- No
* - ``failTimeout``
- Sets the `time <https://nginx.org/en/docs/stream/ngx_stream_upstream_module.html#fail_timeout>`_ during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server unavailable and the period of time the server will be considered unavailable. The default is ``10s``.
- ``string``
- No

```

### UpstreamParameters
Expand Down
19 changes: 13 additions & 6 deletions internal/configs/transportserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ func generateStreamUpstreams(transportServerEx *TransportServerEx, upstreamNamer
var upstreams []version2.StreamUpstream

for _, u := range transportServerEx.TransportServer.Spec.Upstreams {
name := upstreamNamer.GetNameForUpstream(u.Name)

// subselector is not supported yet in TransportServer upstreams. That's why we pass "nil" here
endpointsKey := GenerateEndpointsKey(transportServerEx.TransportServer.Namespace, u.Service, nil, uint16(u.Port))
endpoints := transportServerEx.Endpoints[endpointsKey]

ups := generateStreamUpstream(name, endpoints, isPlus)
ups := generateStreamUpstream(&u, upstreamNamer, endpoints, isPlus)

ups.UpstreamLabels.Service = u.Service
ups.UpstreamLabels.ResourceType = "transportserver"
Expand All @@ -116,25 +115,33 @@ func generateStreamUpstreams(transportServerEx *TransportServerEx, upstreamNamer
return upstreams
}

func generateStreamUpstream(upstreamName string, endpoints []string, isPlus bool) version2.StreamUpstream {
func generateStreamUpstream(upstream *conf_v1alpha1.Upstream, upstreamNamer *upstreamNamer, endpoints []string, isPlus bool) version2.StreamUpstream {
var upsServers []version2.StreamUpstreamServer

name := upstreamNamer.GetNameForUpstream(upstream.Name)
maxFails := generateIntFromPointer(upstream.MaxFails, 1)
failTimeout := generateTime(upstream.FailTimeout, "10s")

for _, e := range endpoints {
s := version2.StreamUpstreamServer{
Address: e,
Address: e,
MaxFails: maxFails,
FailTimeout: failTimeout,
}

upsServers = append(upsServers, s)
}

if !isPlus && len(endpoints) == 0 {
upsServers = append(upsServers, version2.StreamUpstreamServer{
Address: nginxNonExistingUnixSocket,
Address: nginxNonExistingUnixSocket,
MaxFails: maxFails,
FailTimeout: failTimeout,
})
}

return version2.StreamUpstream{
Name: upstreamName,
Name: name,
Servers: upsServers,
}
}
24 changes: 18 additions & 6 deletions internal/configs/transportserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ func TestGenerateTransportServerConfigForTCP(t *testing.T) {
},
Upstreams: []conf_v1alpha1.Upstream{
{
Name: "tcp-app",
Service: "tcp-app-svc",
Port: 5001,
Name: "tcp-app",
Service: "tcp-app-svc",
Port: 5001,
MaxFails: intPointer(3),
FailTimeout: "40s",
},
},
UpstreamParameters: &conf_v1alpha1.UpstreamParameters{
Expand Down Expand Up @@ -107,7 +109,9 @@ func TestGenerateTransportServerConfigForTCP(t *testing.T) {
Name: "ts_default_tcp-server_tcp-app",
Servers: []version2.StreamUpstreamServer{
{
Address: "10.0.0.20:5001",
Address: "10.0.0.20:5001",
MaxFails: 3,
FailTimeout: "40s",
},
},
UpstreamLabels: version2.UpstreamLabels{
Expand Down Expand Up @@ -186,7 +190,9 @@ func TestGenerateTransportServerConfigForTLSPasstrhough(t *testing.T) {
Name: "ts_default_tcp-server_tcp-app",
Servers: []version2.StreamUpstreamServer{
{
Address: "10.0.0.20:5001",
Address: "10.0.0.20:5001",
MaxFails: 1,
FailTimeout: "10s",
},
},
UpstreamLabels: version2.UpstreamLabels{
Expand Down Expand Up @@ -271,7 +277,9 @@ func TestGenerateTransportServerConfigForUDP(t *testing.T) {
Name: "ts_default_udp-server_udp-app",
Servers: []version2.StreamUpstreamServer{
{
Address: "10.0.0.20:5001",
Address: "10.0.0.20:5001",
MaxFails: 1,
FailTimeout: "10s",
},
},
UpstreamLabels: version2.UpstreamLabels{
Expand Down Expand Up @@ -336,3 +344,7 @@ func TestGenerateUnixSocket(t *testing.T) {
t.Errorf("generateUnixSocket() returned %q but expected %q", result, expected)
}
}

func intPointer(value int) *int {
return &value
}
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 @@ -5,7 +5,7 @@ upstream {{ $u.Name }} {
random two least_conn;

{{ range $s := $u.Servers }}
server {{ $s.Address }};
server {{ $s.Address }} max_fails={{ $s.MaxFails }} fail_timeout={{ $s.FailTimeout }};
{{ end }}
}
{{ end }}
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 @@ -5,7 +5,7 @@ upstream {{ $u.Name }} {
random two least_conn;

{{ range $s := $u.Servers }}
server {{ $s.Address }};
server {{ $s.Address }} max_fails={{ $s.MaxFails }} fail_timeout={{ $s.FailTimeout }};
{{ end }}
}
{{ end }}
Expand Down
4 changes: 3 additions & 1 deletion internal/configs/version2/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type StreamUpstream struct {

// StreamUpstreamServer defines a stream upstream server.
type StreamUpstreamServer struct {
Address string
Address string
MaxFails int
FailTimeout string
}

// StreamServer defines a server in the stream module.
Expand Down
8 changes: 5 additions & 3 deletions pkg/apis/configuration/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ type TransportServerListener struct {

// Upstream defines an upstream.
type Upstream struct {
Name string `json:"name"`
Service string `json:"service"`
Port int `json:"port"`
Name string `json:"name"`
Service string `json:"service"`
Port int `json:"port"`
FailTimeout string `json:"failTimeout"`
MaxFails *int `json:"maxFails"`
}

// UpstreamParameters defines parameters for an upstream.
Expand Down
9 changes: 8 additions & 1 deletion pkg/apis/configuration/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/apis/configuration/validation/transportserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ func validateTransportServerUpstreams(upstreams []v1alpha1.Upstream, fieldPath *
}

allErrs = append(allErrs, validateServiceName(u.Service, idxPath.Child("service"))...)
allErrs = append(allErrs, validatePositiveIntOrZeroFromPointer(u.MaxFails, idxPath.Child("maxFails"))...)
allErrs = append(allErrs, validateTime((u.FailTimeout), idxPath.Child("failTimeout"))...)

for _, msg := range validation.IsValidPortNum(u.Port) {
allErrs = append(allErrs, field.Invalid(idxPath.Child("port"), u.Port, msg))
Expand Down
0