Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3d4db9c
Update CRDS and types
shaun-nx Jul 24, 2023
85f41d5
Add custom HTTP and HTTPs listeners
shaun-nx Jul 24, 2023
1218590
Add configuration problems for listeners
shaun-nx Jul 26, 2023
11cc3bf
Update calls to vs listener function
shaun-nx Jul 27, 2023
637c77c
Update `addProblemsForVSListeners` function
shaun-nx Aug 1, 2023
1cd1328
Ensure VirtualServer config is updated when changes are made to Globa…
shaun-nx Aug 2, 2023
b068954
Report correct warnings for missconfigured listeners
shaun-nx Aug 3, 2023
3e9f651
Remove unused function
shaun-nx Aug 10, 2023
222654a
Add new unit tests for configuration.go
shaun-nx Aug 10, 2023
f79e6c4
Move validateListenerProtocol function to validation/globalconfigurat…
shaun-nx Aug 11, 2023
3105884
Add unit tests for virtual server when using custom listeners
shaun-nx Aug 11, 2023
b45c503
Add additional unit test. Create map of listener name to listener struct
shaun-nx Aug 11, 2023
e98d171
Add unit test for empty http and https listeners
shaun-nx Aug 14, 2023
8b412d6
Refactor test to use mustInitGlobalConfiguration()
shaun-nx Aug 14, 2023
90d3f2d
Add additional unit tests
shaun-nx Aug 14, 2023
553ac36
Add test for Deleted GlobalConfiguration with default VirtualSErver d…
shaun-nx Aug 14, 2023
dee1fa2
Update VirtualServer template for nginx plus
shaun-nx Aug 14, 2023
f43bb81
Add example files for custom listeners
shaun-nx Aug 15, 2023
692ed7f
Add sample README file
shaun-nx Aug 15, 2023
004f183
Update GlobalConfiguration to ensure VS and TS can't listen on the sa…
shaun-nx Aug 17, 2023
45606fa
add template tests
haywoodsh Aug 17, 2023
8f2738d
add initial functional test
vepatel Aug 18, 2023
db1dc8a
Remove debug lines
shaun-nx Aug 21, 2023
5df07bb
Merge branch 'main' into exp-vs-custom-listeners
shaun-nx Aug 21, 2023
d39ac81
Add test for deleting gc
shaun-nx Aug 21, 2023
9d7557f
Revert "Add test for deleting gc"
shaun-nx Aug 21, 2023
a3f1109
Revert "add initial functional test"
shaun-nx Aug 21, 2023
91522fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 21, 2023
791e9b1
Fix linter error. Refactor configuration tests
shaun-nx Aug 22, 2023
e4fa635
Merge branch 'main' into feat/custom-listeners
shaun-nx Aug 22, 2023
5a9c98c
Remove webapp vs and secret from example directory
shaun-nx Aug 22, 2023
1c8578b
Fix CRDs and README
shaun-nx Aug 22, 2023
d445d70
Merge branch 'main' into feat/custom-listeners
shaun-nx Aug 23, 2023
87ed313
Update README to include NodePort/Loadbalancer update examples.
shaun-nx Aug 24, 2023
336bd86
Merge branch 'main' into feat/custom-listeners
shaun-nx Aug 24, 2023
772d3c6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 24, 2023
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 @@ -45,5 +45,7 @@ spec:
type: integer
protocol:
type: string
ssl:
type: boolean
served: true
storage: true
8 changes: 8 additions & 0 deletions deployments/common/crds/k8s.nginx.org_virtualservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ spec:
internalRoute:
description: InternalRoute allows for the configuration of internal routing.
type: boolean
listener:
description: Listener references a custom http and/or https listener defined in GlobalConfiguration.
type: object
properties:
http:
type: string
https:
type: string
policies:
type: array
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ spec:
type: integer
protocol:
type: string
ssl:
type: boolean
served: true
storage: true
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ spec:
internalRoute:
description: InternalRoute allows for the configuration of internal routing.
type: boolean
listener:
description: Listener references a custom http and/or https listener defined in GlobalConfiguration.
type: object
properties:
http:
type: string
https:
type: string
policies:
type: array
items:
Expand Down
181 changes: 181 additions & 0 deletions examples/custom-resources/custom-listeners/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# Custom HTTP Listeners

In this example, we will configure a VirtualServer resource with custom HTTP listeners.
This will allow HTTP and/or HTTPs based requests to be made on non-default ports.

## Prerequisites

1. Follow the [installation](https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-manifests/)
instructions to deploy the Ingress Controller with custom resources enabled.
2. Ensure the Ingress Controller is configured with the `-global-configuration` argument:
```console
args:
- -global-configuration=$(POD_NAMESPACE)/nginx-configuration
```

3. Save the public IP address of the Ingress Controller into a shell variable:
```console
IC_IP=XXX.YYY.ZZZ.III
```

4. If you have a NodePort or Loadbalancer service deployed, ensure they are updated to include the custom listener ports.
Example YAML for a LoadBalancer:
```yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-ingress
namespace: nginx-ingress
spec:
type: LoadBalancer
ports:
- port: 8083
targetPort: 8083
protocol: TCP
name: http-8083
- port: 8443
targetPort: 8443
protocol: TCP
name: https-8443
selector:
app: nginx-ingress
```
## Step 1 - Deploy the GlobalConfiguration resource
Similar to how listeners are configured in our [basic-tcp-udp](../../examples/custom-resource/basic-tcp-udp) examples,
here we deploy a GlobalConfiguration resource with the listeners we want to use in our VirtualServer.
```yaml
apiVersion: k8s.nginx.org/v1alpha1
kind: GlobalConfiguration
metadata:
name: nginx-configuration
namespace: nginx-ingress
spec:
listeners:
- name: http-8083
port: 8083
protocol: HTTP
- name: https-8443
port: 8443
protocol: HTTP
ssl: true
```

```console
kubectl create -f global-configuration.yaml
```

## Step 2 - Save the custom port numbers
Save the custom HTTP and/or HTTPS ports into a shell variables for later:

```console
IC_HTTP_PORT=8083
IC_HTTPS_PORT=8443
```

## Step 3 - Deploy the Cafe Application

Create the coffee and the tea deployments and services:

```console
kubectl create -f cafe.yaml
```

## Step 4 - Deploy the VirtualServer with custom listeners
The VirtualServer in this example is set to use the listeners defined in the GlobalConfiguration resource
that was deployed in Step 1. Below is the yaml of this example VirtualServer:

```yaml
apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
name: cafe
spec:
listener:
http: http-8083
https: https-8443
host: cafe.example.com
tls:
secret: cafe-secret
upstreams:
- name: tea
service: tea-svc
port: 80
- name: coffee
service: coffee-svc
port: 80
routes:
- path: /tea
action:
pass: tea
- path: /coffee
action:
pass: coffee
```

1. Create the secret with the TLS certificate and key:

```console
kubectl create -f cafe-secret.yaml
```

2. Create the VirtualServer resource:

```console
kubectl create -f cafe-virtual-server.yaml
```

## Step 5 - Test the Configuration

1. Check that the configuration has been successfully applied by inspecting the events of the VirtualServer:

```console
kubectl describe virtualserver cafe
```

Below you will see the events as well as the new `Listeners` field
```console
. . .
Spec:
Host: cafe.example.com
Listener:
Http: http-8083
Https: https-8443
. . .
Routes:
. . .
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal AddedOrUpdated 7s nginx-ingress-controller Configuration for default/cafe was added or updated
```

2. Since the deployed VirtualServer is using ports `8083` and 8443` in this example. you must explicitly specify these ports
when sending requests to the endpoints of this VirtualServer:

For `/coffee` on `8443`:

```console
curl -k https://cafe.example.com:8443/coffee
```

```text
Server address: 10.32.0.40:8080
Server name: coffee-7dd75bc79b-qmhmv
...
URI: /coffee
...
```

For `/coffee` on `8083`:

```console
curl -k https://cafe.example.com:8083/coffee
```

```text
Server address: 10.32.0.40:8080
Server name: coffee-7dd75bc79b-qmhmv
...
URI: /coffee
...
```
8 changes: 8 additions & 0 deletions examples/custom-resources/custom-listeners/cafe-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Secret
metadata:
name: cafe-secret
type: kubernetes.io/tls
data:
tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURMakNDQWhZQ0NRREFPRjl0THNhWFdqQU5CZ2txaGtpRzl3MEJBUXNGQURCYU1Rc3dDUVlEVlFRR0V3SlYKVXpFTE1Ba0dBMVVFQ0F3Q1EwRXhJVEFmQmdOVkJBb01HRWx1ZEdWeWJtVjBJRmRwWkdkcGRITW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RGcEFOZmlJWEV0LzQ4WGY2ME54OGdXTQp1SHl4Wlp4L05LdER3MFY4dlgxUE9ucTJBNWVpS2ErOGpSQVJZS0pMWU5kZkR1d29seHZHNmJaaGtQaS80RXRUCjNZMThzUUtCZ0h0S2JrKzdsTkpWZXN3WEU1Y1VHNkVEVXNEZS8yVWE3ZlhwN0ZjanFCRW9hcDFMU3crNlRYcDAKWmdybUtFOEFSek00NytFSkhVdmlpcS9udXBFMTVnMGtKVzNzeWhwVTl6WkxPN2x0QjBLSWtPOVpSY21Vam84UQpjcExsSE1BcWJMSjhXWUdKQ2toaVd4eWFsNmhZVHlXWTRjVmtDMHh0VGwvaFVFOUllTktvCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
name: cafe
spec:
listener:
http: http-8083
https: https-8443
host: cafe.example.com
tls:
secret: cafe-secret
upstreams:
- name: tea
service: tea-svc
port: 80
- name: coffee
service: coffee-svc
port: 80
routes:
- path: /tea
action:
pass: tea
- path: /coffee
action:
pass: coffee
65 changes: 65 additions & 0 deletions examples/custom-resources/custom-listeners/cafe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: coffee
spec:
replicas: 2
selector:
matchLabels:
app: coffee
template:
metadata:
labels:
app: coffee
spec:
containers:
- name: coffee
image: nginxdemos/nginx-hello:plain-text
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: coffee-svc
spec:
ports:
- port: 80
targetPort: 8080
protocol: TCP
name: http
selector:
app: coffee
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: tea
spec:
replicas: 1
selector:
matchLabels:
app: tea
template:
metadata:
labels:
app: tea
spec:
containers:
- name: tea
image: nginxdemos/nginx-hello:plain-text
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: tea-svc
spec:
ports:
- port: 80
targetPort: 8080
protocol: TCP
name: http
selector:
app: tea
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: k8s.nginx.org/v1alpha1
kind: GlobalConfiguration
metadata:
name: nginx-configuration
namespace: nginx-ingress
spec:
listeners:
- name: http-8083
port: 8083
protocol: HTTP
- name: https-8443
port: 8443
protocol: HTTP
ssl: true
24 changes: 24 additions & 0 deletions internal/configs/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,30 @@ func (cnf *Configurator) UpdateConfig(cfgParams *ConfigParams, resources Extende
return allWarnings, nil
}

// UpdateVirtualServers updates VirtualServers.
func (cnf *Configurator) UpdateVirtualServers(updatedVSExes []*VirtualServerEx, deletedKeys []string) []error {
var errList []error
for _, vsEx := range updatedVSExes {
_, err := cnf.addOrUpdateVirtualServer(vsEx)
if err != nil {
errList = append(errList, fmt.Errorf("error adding or updating VirtualServer %v/%v: %w", vsEx.VirtualServer.Namespace, vsEx.VirtualServer.Name, err))
}
}

for _, key := range deletedKeys {
err := cnf.DeleteVirtualServer(key, true)
if err != nil {
errList = append(errList, fmt.Errorf("error when removing VirtualServer %v: %w", key, err))
}
}

if err := cnf.reload(nginx.ReloadForOtherUpdate); err != nil {
errList = append(errList, fmt.Errorf("error when updating VirtualServer: %w", err))
}

return errList
}

// UpdateTransportServers updates TransportServers.
func (cnf *Configurator) UpdateTransportServers(updatedTSExes []*TransportServerEx, deletedKeys []string) []error {
var errList []error
Expand Down
3 changes: 3 additions & 0 deletions internal/configs/version2/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type UpstreamServer struct {
type Server struct {
ServerName string
StatusZone string
CustomListeners bool
HTTPPort int
HTTPSPort int
ProxyProtocol bool
SSL *SSL
ServerTokens string
Expand Down
Loading