-
Notifications
You must be signed in to change notification settings - Fork 139
Expand HTTPRoute Matching #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0e03511
Expand HTTPRoute Matching
98ff9a8
Pin node to version and rename fmt-njs
3991d2b
Rename volume
c505c6f
Use route idx in the path for match
95d0d8b
Change path to redirectPath
d1e3d54
Update comment about redirectPath
44747b5
Add GetMatch to Route
a2ad131
Convert njs test to table-style; Add more edge case tests
2f511b0
Add njs-fmt to dev-all target
c124aa4
Add advanced routing example
6204e76
Add missing return statement
f8cccb9
Add njs lint and test jobs to the github workflow
769beb9
Reformat YAML
33d5c74
Add subheaders to example
b4a6ba1
Use github ids for fixmes
0b93373
Remove njs-unit-test from unit-test target
ce4d111
Header values should be case-sensitive; fmt changes
9edc36a
Remove warn log messages from njs
32b7ad9
Improve comments on query param matching
83cf1f8
Few more updates to example files
31c16dc
Goimports
7feb8c9
Another example fix
6cc8082
One more fix to example
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,27 @@ jobs: | |
| with: | ||
| args: --timeout ${{ env.GOLANGCI_TIMEOUT }} | ||
|
|
||
| njs-lint: | ||
| name: NJS Lint | ||
| runs-on: ubuntu-20.04 | ||
| steps: | ||
| - name: Checkout Repository | ||
| uses: actions/checkout@v2 | ||
| - name: Run Prettier on NJS code | ||
| id: prettier-run | ||
| uses: rutajdash/[email protected] | ||
| with: | ||
| config_path: ${{ github.workspace }}/internal/nginx/modules/.prettierrc | ||
| file_pattern: ${{ github.workspace }}/internal/nginx/modules/*.js | ||
| prettier_version: 2.6.2 | ||
| - name: Prettier Output | ||
| if: ${{ failure() }} | ||
| shell: bash | ||
| run: | | ||
| echo "The following files are not formatted:" | ||
| echo "${{steps.prettier-run.outputs.prettier_output}}" | ||
| echo "Run \"make njs-fmt\" locally to format the code" | ||
|
|
||
| unit-tests: | ||
| name: Unit Tests | ||
| runs-on: ubuntu-20.04 | ||
|
|
@@ -89,6 +110,19 @@ jobs: | |
| path: ${{ github.workspace }}/cover.html | ||
| if: always() | ||
|
|
||
| njs-unit-tests: | ||
| name: NJS Unit Tests | ||
| runs-on: ubuntu-20.04 | ||
| steps: | ||
| - name: Checkout Repository | ||
| uses: actions/checkout@v3 | ||
| - name: Setup Node.js Environment | ||
| uses: actions/setup-node@v3 | ||
| with: | ||
| node_version: 18 | ||
| - run: npm install mocha@^8.2 esm chai | ||
| - run: npx mocha -r esm ${{ github.workspace }}/internal/nginx/modules/httpmatches_test.js | ||
|
|
||
| binary: | ||
| name: Build Binary | ||
| runs-on: ubuntu-20.04 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,13 @@ delete-kind-cluster: | |
| fmt: ## Run go fmt against code. | ||
| go fmt ./... | ||
|
|
||
| .PHONY: njs-fmt | ||
| njs-fmt: ## Run prettier against the njs httpmatches module. | ||
| docker run --rm \ | ||
kate-osborn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| -v $(PWD)/internal/nginx/modules/:/njs-modules/ \ | ||
| node:18 \ | ||
| npx [email protected] --write njs-modules/ --config=njs-modules/.prettierrc | ||
|
|
||
| .PHONY: vet | ||
| vet: ## Run go vet against code. | ||
| go vet ./... | ||
|
|
@@ -74,9 +81,15 @@ lint: ## Run golangci-lint against code. | |
| docker run --pull always --rm -v $(shell pwd):/nginx-kubernetes-gateway -w /nginx-kubernetes-gateway -v $(shell go env GOCACHE):/cache/go -e GOCACHE=/cache/go -e GOLANGCI_LINT_CACHE=/cache/go -v $(shell go env GOPATH)/pkg:/go/pkg golangci/golangci-lint:latest golangci-lint --color always run | ||
|
|
||
| .PHONY: unit-test | ||
| unit-test: | ||
| unit-test: ## Run unit tests for the go code | ||
| go test ./... -race -coverprofile cover.out | ||
| go tool cover -html=cover.out -o cover.html | ||
|
|
||
| njs-unit-test: ## Run unit tests for the njs httpmatches module. | ||
| docker run --rm -w /src \ | ||
| -v $(PWD)/internal/nginx/modules/:/src/njs-modules/ \ | ||
| node:18 \ | ||
| /bin/bash -c "npm install mocha@^8.2 esm chai && npx mocha -r esm njs-modules/httpmatches_test.js" | ||
|
|
||
| .PHONY: dev-all | ||
| dev-all: deps fmt vet lint unit-test | ||
| dev-all: deps fmt njs-fmt vet lint unit-test njs-unit-test | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| apiVersion: v1 | ||
| kind: Namespace | ||
| metadata: | ||
| name: nginx-gateway |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # Advanced Routing | ||
|
|
||
| In this example we will deploy NGINX Kubernetes Gateway and configure advanced routing rules for a simple cafe application. | ||
| We will use `HTTPRoute` resources to route traffic to the cafe application based on a combination of the request method, headers, and query parameters. | ||
|
|
||
| ## Running the Example | ||
|
|
||
| ## 1. Deploy NGINX Kubernetes Gateway | ||
|
|
||
| 1. Follow the [installation instructions](https://github.com/nginxinc/nginx-kubernetes-gateway/blob/main/README.md#run-nginx-gateway) to deploy NGINX Gateway. | ||
|
|
||
| 1. Save the public IP address of NGINX Kubernetes Gateway into a shell variable: | ||
|
|
||
| ``` | ||
| GW_IP=XXX.YYY.ZZZ.III | ||
| ``` | ||
|
|
||
| 1. Save the port of NGINX Kubernetes Gateway: | ||
|
|
||
| ``` | ||
| GW_PORT=<port number> | ||
| ``` | ||
|
|
||
| ## 2. Deploy the Cafe Application | ||
|
|
||
| 1. Create the coffee and the tea deployments and services: | ||
|
|
||
| ``` | ||
| kubectl apply -f cafe.yaml | ||
| ``` | ||
|
|
||
| 1. Check that the Pods are running in the `default` namespace: | ||
|
|
||
| ``` | ||
| kubectl -n default get pods | ||
| NAME READY STATUS RESTARTS AGE | ||
| coffee-6f4b79b975-2sb28 1/1 Running 0 12s | ||
| tea-6fb46d899f-fm7zr 1/1 Running 0 12s | ||
| ``` | ||
|
|
||
| ## 3. Configure Routing | ||
|
|
||
| 1. Create the `HTTPRoute` resources: | ||
|
|
||
| ``` | ||
| kubectl apply -f cafe-routes.yaml | ||
| ``` | ||
|
|
||
| ## 4. Test the Application | ||
|
|
||
| We will use `curl` to send requests to the `coffee` and `tea` services. | ||
|
|
||
| ### 4.1 Access coffee | ||
|
|
||
| Send a `POST` request to the path `/coffee` with the headers `x-demo-header:demo-x1` and `version:v1`: | ||
|
|
||
| ``` | ||
| curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee -X POST -H "x-demo-header:demo-x1" -H "version:v1" | ||
| Server address: 10.12.0.18:80 | ||
| Server name: coffee-7586895968-r26zn | ||
| ``` | ||
|
|
||
| Header keys and values are case-insensitive, so we can also access coffee with the following request: | ||
|
|
||
| ``` | ||
| curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee -X POST -H "X-DEMO-HEADER:DEMO-X1" -H "VERSION:V1" | ||
| Server address: 10.12.0.18:80 | ||
| Server name: coffee-7586895968-r26zn | ||
| ``` | ||
|
|
||
| Only `POST` requests to the path `/coffee` with the headers `x-demo-header:demo-x1` and `version:v1` will be able to access coffee. | ||
| For example, try sending the following `GET` request: | ||
| ``` | ||
| curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee -H "X-DEMO-HEADER:DEMO-X1" -H "VERSION:V1" | ||
| ``` | ||
|
|
||
| NGINX Kubernetes Gateway returns a 405 since the request method does not match the method defined in the routing rule for `/coffee`. | ||
|
|
||
| ### 4.2 Access tea | ||
|
|
||
| Send a request to the path `/tea` with the query parameter `Great=Example`: | ||
|
|
||
| ``` | ||
| curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea?Great=Example | ||
| Server address: 10.12.0.19:80 | ||
| Server name: tea-7cd44fcb4d-xfw2x | ||
| ``` | ||
|
|
||
| Query parameters are case-sensitive, so the case must match what you specify in the `HTTPRoute` resource. | ||
|
|
||
| Only requests to the path `/tea` with the query parameter `Great=Example` will be able to access tea. | ||
| For example, try sending the following request: | ||
|
|
||
| ``` | ||
| curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea | ||
| ``` | ||
|
|
||
| NGINX Kubernetes Gateway returns a 404 since the request does not satisfy the routing rule configured for `/tea`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| apiVersion: gateway.networking.k8s.io/v1alpha2 | ||
| kind: HTTPRoute | ||
| metadata: | ||
| name: cafe | ||
| spec: | ||
| hostnames: | ||
| - "cafe.example.com" | ||
| rules: | ||
| - backendRefs: | ||
| - name: main | ||
| port: 80 | ||
| --- | ||
| apiVersion: gateway.networking.k8s.io/v1alpha2 | ||
| kind: HTTPRoute | ||
| metadata: | ||
| name: coffee | ||
| spec: | ||
| hostnames: | ||
| - "cafe.example.com" | ||
| rules: | ||
| - matches: | ||
| - path: | ||
| type: PathPrefix | ||
| value: /coffee | ||
| method: POST | ||
| headers: | ||
| - name: X-Demo-Header # header names and values are case-insensitive | ||
| value: Demo-X1 | ||
| - name: version | ||
| value: v1 | ||
| backendRefs: | ||
| - name: coffee | ||
| port: 80 | ||
| --- | ||
| apiVersion: gateway.networking.k8s.io/v1alpha2 | ||
| kind: HTTPRoute | ||
| metadata: | ||
| name: tea | ||
| spec: | ||
| hostnames: | ||
| - "cafe.example.com" | ||
| rules: | ||
| - matches: | ||
| - path: | ||
| type: PathPrefix | ||
| value: /tea | ||
| queryParams: | ||
| - name: Great # query params and values are case-sensitive | ||
| value: Example | ||
| backendRefs: | ||
| - name: tea | ||
| port: 80 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: coffee | ||
| spec: | ||
| replicas: 1 | ||
| 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 | ||
| 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 | ||
| spec: | ||
| ports: | ||
| - port: 80 | ||
| targetPort: 8080 | ||
| protocol: TCP | ||
| name: http | ||
| selector: | ||
| app: tea |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,6 @@ apiVersion: v1 | |
| kind: Service | ||
| metadata: | ||
| name: tea | ||
| labels: | ||
| spec: | ||
| ports: | ||
| - port: 80 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.