Skip to content

Commit 490a4a5

Browse files
committed
Documetation for application source files
1 parent eedb6de commit 490a4a5

File tree

7 files changed

+68
-8
lines changed

7 files changed

+68
-8
lines changed

internal/application/application_constants.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,24 @@
55

66
package application
77

8+
// These constants are intended for use in the Annotations field of the Service definition.
9+
// They determine which Border Server client will be used.
10+
// To use these values, add the following annotation to the Service definition:
11+
//
12+
// annotations:
13+
// nginxinc.io/nkl-<upstream name>: <value>
14+
//
15+
// where <upstream name> is the name of the upstream in the NGINX Plus configuration and <value> is one of the constants below.
16+
//
17+
// Note, this is an extensibility point. To add a Border Server client...
18+
// 1. Create a module that implements the BorderClient interface;
19+
// 2. Add a new constant to this group that acts as a key for selecting the client;
20+
// 3. Update the NewBorderClient factory method in border_client.go that returns the client;
821
const (
9-
ClientTypeTcp = "tcp"
22+
23+
// ClientTypeTcp creates a TcpBorderClient that uses the Stream* methods of the NGINX Plus client.
24+
ClientTypeTcp = "tcp"
25+
26+
// ClientTypeHttp creates an HttpBorderClient that uses the HTTP* methods of the NGINX Plus client.
1027
ClientTypeHttp = "http"
1128
)

internal/application/border_client.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,30 @@ import (
1111
"github.com/sirupsen/logrus"
1212
)
1313

14+
// Interface defines the functions required to implement a Border Client.
1415
type Interface interface {
1516
Update(*core.ServerUpdateEvent) error
1617
Delete(*core.ServerUpdateEvent) error
1718
}
1819

20+
// BorderClient defines any state need by the Border Client.
1921
type BorderClient struct {
2022
}
2123

22-
// NewBorderClient Returns a NullBorderClient if the type is unknown, this avoids panics due to nil pointer dereferences.
24+
// NewBorderClient is the Factory function for creating a Border Client.
25+
//
26+
// Note, this is an extensibility point. To add a Border Server client...
27+
// 1. Create a module that implements the BorderClient interface;
28+
// 2. Add a new constant in application_constants.go that acts as a key for selecting the client;
29+
// 3. Update the NewBorderClient factory method in border_client.go that returns the client;
2330
func NewBorderClient(clientType string, borderClient interface{}) (Interface, error) {
2431
logrus.Debugf(`NewBorderClient for type: %s`, clientType)
2532

2633
switch clientType {
27-
case "tcp":
34+
case "tcp": // ClientTypeTcp
2835
return NewTcpBorderClient(borderClient)
2936

30-
case "http":
37+
case "http": // ClientTypeHttp
3138
return NewHttpBorderClient(borderClient)
3239

3340
default:

internal/application/doc.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,28 @@
66
/*
77
Package application includes support for applying updates to the Border servers.
88
9-
"Border TcpServers" are the servers that are exposed to the outside world and direct traffic into the cluster.
10-
At this time the only supported Border TcpServers are NGINX Plus servers. The BorderClient module defines
11-
an interface that can be implemented to support other Border Server types.
9+
"Border Servers" are servers that are exposed to the outside world and direct traffic into the cluster.
1210
11+
The BorderClient module defines an interface that can be implemented to support other Border Server types.
12+
To add a Border Server client...
13+
1. Create a module that implements the BorderClient interface;
14+
2. Add a new constant in application_constants.go that acts as a key for selecting the client;
15+
3. Update the NewBorderClient factory method in border_client.go that returns the client;
16+
17+
At this time the only supported Border Servers are NGINX Plus servers.
18+
19+
The two Border Server clients for NGINX Plus are:
1320
- HttpBorderClient: updates NGINX Plus servers using HTTP Upstream methods on the NGINX Plus API.
1421
- TcpBorderClient: updates NGINX Plus servers using Stream Upstream methods on the NGINX Plus API.
1522
16-
Selection of the appropriate client is based on the Annotations present on the NodePort Service definition.
23+
Both of these implementations use the NGINX Plus client module to communicate with the NGINX Plus server.
24+
25+
Selection of the appropriate client is based on the Annotations present on the Service definition, e.g.:
26+
27+
annotations:
28+
nginxinc.io/nkl-<upstream name>: <value>
29+
30+
where <upstream name> is the name of the upstream in the NGINX Plus configuration and <value> is one of the constants in application_constants.go.
1731
*/
1832

1933
package application

internal/application/http_border_client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import (
1111
nginxClient "github.com/nginxinc/nginx-plus-go-client/client"
1212
)
1313

14+
// HttpBorderClient implements the BorderClient interface for HTTP upstreams.
1415
type HttpBorderClient struct {
1516
BorderClient
1617
nginxClient NginxClientInterface
1718
}
1819

20+
// NewHttpBorderClient is the Factory function for creating an HttpBorderClient.
1921
func NewHttpBorderClient(client interface{}) (Interface, error) {
2022
ngxClient, ok := client.(NginxClientInterface)
2123
if !ok {
@@ -27,6 +29,7 @@ func NewHttpBorderClient(client interface{}) (Interface, error) {
2729
}, nil
2830
}
2931

32+
// Update manages the Upstream servers for the Upstream Name given in the ServerUpdateEvent.
3033
func (hbc *HttpBorderClient) Update(event *core.ServerUpdateEvent) error {
3134
httpUpstreamServers := asNginxHttpUpstreamServers(event.UpstreamServers)
3235
_, _, _, err := hbc.nginxClient.UpdateHTTPServers(event.UpstreamName, httpUpstreamServers)
@@ -37,6 +40,7 @@ func (hbc *HttpBorderClient) Update(event *core.ServerUpdateEvent) error {
3740
return nil
3841
}
3942

43+
// Delete deletes the Upstream server for the Upstream Name given in the ServerUpdateEvent.
4044
func (hbc *HttpBorderClient) Delete(event *core.ServerUpdateEvent) error {
4145
err := hbc.nginxClient.DeleteHTTPServer(event.UpstreamName, event.UpstreamServers[0].Host)
4246
if err != nil {
@@ -46,12 +50,14 @@ func (hbc *HttpBorderClient) Delete(event *core.ServerUpdateEvent) error {
4650
return nil
4751
}
4852

53+
// asNginxHttpUpstreamServer converts a core.UpstreamServer to a nginxClient.UpstreamServer.
4954
func asNginxHttpUpstreamServer(server *core.UpstreamServer) nginxClient.UpstreamServer {
5055
return nginxClient.UpstreamServer{
5156
Server: server.Host,
5257
}
5358
}
5459

60+
// asNginxHttpUpstreamServers converts a core.UpstreamServers to a []nginxClient.UpstreamServer.
5561
func asNginxHttpUpstreamServers(servers core.UpstreamServers) []nginxClient.UpstreamServer {
5662
var upstreamServers []nginxClient.UpstreamServer
5763

internal/application/nginx_client_interface.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ package application
77

88
import nginxClient "github.com/nginxinc/nginx-plus-go-client/client"
99

10+
// NginxClientInterface defines the functions used on the NGINX Plus client, abstracting away the full details of that client.
1011
type NginxClientInterface interface {
12+
// DeleteStreamServer is used by the TcpBorderClient.
1113
DeleteStreamServer(upstream string, server string) error
14+
15+
// UpdateStreamServers is used by the TcpBorderClient.
1216
UpdateStreamServers(upstream string, servers []nginxClient.StreamUpstreamServer) ([]nginxClient.StreamUpstreamServer, []nginxClient.StreamUpstreamServer, []nginxClient.StreamUpstreamServer, error)
1317

18+
// DeleteHTTPServer is used by the HttpBorderClient.
1419
DeleteHTTPServer(upstream string, server string) error
20+
21+
// UpdateHTTPServers is used by the HttpBorderClient.
1522
UpdateHTTPServers(upstream string, servers []nginxClient.UpstreamServer) ([]nginxClient.UpstreamServer, []nginxClient.UpstreamServer, []nginxClient.UpstreamServer, error)
1623
}

internal/application/null_border_client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ import (
1010
"github.com/sirupsen/logrus"
1111
)
1212

13+
// NullBorderClient is a BorderClient that does nothing.
14+
// / It serves only to prevent a panic if the BorderClient is not set correctly and errors from the factory methods are ignored.
1315
type NullBorderClient struct {
1416
}
1517

18+
// NewNullBorderClient is the Factory function for creating a NullBorderClient
1619
func NewNullBorderClient() (Interface, error) {
1720
return &NullBorderClient{}, nil
1821
}
1922

23+
// Update logs a Warning. It is, after all, a NullObject Pattern implementation.
2024
func (nbc *NullBorderClient) Update(_ *core.ServerUpdateEvent) error {
2125
logrus.Warn("NullBorderClient.Update called")
2226
return nil
2327
}
2428

29+
// Delete logs a Warning. It is, after all, a NullObject Pattern implementation.
2530
func (nbc *NullBorderClient) Delete(_ *core.ServerUpdateEvent) error {
2631
logrus.Warn("NullBorderClient.Delete called")
2732
return nil

internal/application/tcp_border_client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import (
1111
nginxClient "github.com/nginxinc/nginx-plus-go-client/client"
1212
)
1313

14+
// TcpBorderClient implements the BorderClient interface for TCP upstreams.
1415
type TcpBorderClient struct {
1516
BorderClient
1617
nginxClient NginxClientInterface
1718
}
1819

20+
// NewTcpBorderClient is the Factory function for creating an TcpBorderClient.
1921
func NewTcpBorderClient(client interface{}) (Interface, error) {
2022
ngxClient, ok := client.(NginxClientInterface)
2123
if !ok {
@@ -27,6 +29,7 @@ func NewTcpBorderClient(client interface{}) (Interface, error) {
2729
}, nil
2830
}
2931

32+
// Update manages the Upstream servers for the Upstream Name given in the ServerUpdateEvent.
3033
func (tbc *TcpBorderClient) Update(event *core.ServerUpdateEvent) error {
3134
streamUpstreamServers := asNginxStreamUpstreamServers(event.UpstreamServers)
3235
_, _, _, err := tbc.nginxClient.UpdateStreamServers(event.UpstreamName, streamUpstreamServers)
@@ -37,6 +40,7 @@ func (tbc *TcpBorderClient) Update(event *core.ServerUpdateEvent) error {
3740
return nil
3841
}
3942

43+
// Delete deletes the Upstream server for the Upstream Name given in the ServerUpdateEvent.
4044
func (tbc *TcpBorderClient) Delete(event *core.ServerUpdateEvent) error {
4145
err := tbc.nginxClient.DeleteStreamServer(event.UpstreamName, event.UpstreamServers[0].Host)
4246
if err != nil {

0 commit comments

Comments
 (0)