-
Notifications
You must be signed in to change notification settings - Fork 118
Support cross-namespace BackendRefs in HTTPRoutes #806
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c2e727d
Add support for cross-namespace backendRefs
f60b3ba
Update examples/cross-namespace-routing/README.md
kate-osborn a5706b3
Update examples/cross-namespace-routing/README.md
kate-osborn 96eb13d
Use GetPointer
f435550
Update internal/state/graph/reference_grant.go
kate-osborn f9ab9a3
Remove unnecessary newlines
e8ce30f
Fix lint error
7f51037
Simplify unit tests
7d20e2c
Add missing test case
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
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,128 @@ | ||
# Example | ||
|
||
In this example, we expand on the simple [cafe-example](../cafe-example) by using a ReferenceGrant to route to backends | ||
in a different namespace from our HTTPRoutes. | ||
|
||
## Running the Example | ||
|
||
## 1. Deploy NGINX Kubernetes Gateway | ||
|
||
1. Follow the [installation instructions](/docs/installation.md) 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 cafe namespace and cafe application: | ||
|
||
``` | ||
kubectl apply -f cafe-ns-and-app.yaml | ||
``` | ||
|
||
1. Check that the Pods are running in the `cafe` namespace: | ||
|
||
``` | ||
kubectl -n cafe 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 `Gateway`: | ||
|
||
``` | ||
kubectl apply -f gateway.yaml | ||
``` | ||
|
||
1. Create the `HTTPRoute` resources: | ||
|
||
``` | ||
kubectl apply -f cafe-routes.yaml | ||
``` | ||
1. Create the `ReferenceGrant`: | ||
|
||
``` | ||
kubectl apply -f reference-grant.yaml | ||
``` | ||
This ReferenceGrant allows all HTTPRoutes in the `default` namespace to reference all Services in the `cafe` | ||
namespace. | ||
|
||
## 4. Test the Application | ||
|
||
To access the application, we will use `curl` to send requests to the `coffee` and `tea` Services. | ||
|
||
To get coffee: | ||
|
||
``` | ||
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee | ||
Server address: 10.12.0.18:80 | ||
Server name: coffee-7586895968-r26zn | ||
``` | ||
|
||
To get tea: | ||
|
||
``` | ||
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea | ||
Server address: 10.12.0.19:80 | ||
Server name: tea-7cd44fcb4d-xfw2x | ||
``` | ||
|
||
## 5. Remove the ReferenceGrant | ||
|
||
To restrict access to Services in the `cafe` Namespace, we can delete the ReferenceGrant we created in | ||
Step 3: | ||
|
||
``` | ||
kubectl delete -f reference-grant.yaml | ||
``` | ||
|
||
Now, if we try to access the application over HTTP, we will get an internal server error: | ||
``` | ||
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea | ||
|
||
<html> | ||
<head><title>500 Internal Server Error</title></head> | ||
<body> | ||
<center><h1>500 Internal Server Error</h1></center> | ||
<hr><center>nginx/1.25.1</center> | ||
</body> | ||
</html> | ||
``` | ||
|
||
You can also check the conditions of the HTTPRoutes `coffee` and `tea` to verify that the reference is not permitted: | ||
|
||
``` | ||
kubectl describe httproute coffee | ||
|
||
Condtions: | ||
Message: Backend ref to Service cafe/coffee not permitted by any ReferenceGrant | ||
Observed Generation: 1 | ||
Reason: RefNotPermitted | ||
Status: False | ||
Type: ResolvedRefs | ||
Controller Name: k8s-gateway.nginx.org/nginx-gateway-controller | ||
``` | ||
|
||
``` | ||
kubectl describe httproute tea | ||
|
||
Condtions: | ||
Message: Backend ref to Service cafe/tea not permitted by any ReferenceGrant | ||
Observed Generation: 1 | ||
Reason: RefNotPermitted | ||
Status: False | ||
Type: ResolvedRefs | ||
Controller Name: k8s-gateway.nginx.org/nginx-gateway-controller | ||
``` |
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,74 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: cafe | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: coffee | ||
namespace: cafe | ||
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 | ||
namespace: cafe | ||
spec: | ||
ports: | ||
- port: 80 | ||
targetPort: 8080 | ||
protocol: TCP | ||
name: http | ||
selector: | ||
app: coffee | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: tea | ||
namespace: cafe | ||
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 | ||
namespace: cafe | ||
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
apiVersion: gateway.networking.k8s.io/v1beta1 | ||
kind: HTTPRoute | ||
metadata: | ||
name: coffee | ||
spec: | ||
parentRefs: | ||
- name: gateway | ||
sectionName: http | ||
hostnames: | ||
- "cafe.example.com" | ||
rules: | ||
- matches: | ||
- path: | ||
type: PathPrefix | ||
value: /coffee | ||
backendRefs: | ||
- name: coffee | ||
namespace: cafe | ||
port: 80 | ||
--- | ||
apiVersion: gateway.networking.k8s.io/v1beta1 | ||
kind: HTTPRoute | ||
metadata: | ||
name: tea | ||
spec: | ||
parentRefs: | ||
- name: gateway | ||
sectionName: http | ||
hostnames: | ||
- "cafe.example.com" | ||
rules: | ||
- matches: | ||
- path: | ||
type: Exact | ||
value: /tea | ||
backendRefs: | ||
- name: tea | ||
namespace: cafe | ||
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,13 @@ | ||
apiVersion: gateway.networking.k8s.io/v1beta1 | ||
kind: Gateway | ||
metadata: | ||
name: gateway | ||
labels: | ||
domain: k8s-gateway.nginx.org | ||
spec: | ||
gatewayClassName: nginx | ||
listeners: | ||
- name: http | ||
port: 80 | ||
protocol: HTTP | ||
hostname: "*.example.com" |
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,13 @@ | ||
apiVersion: gateway.networking.k8s.io/v1beta1 | ||
kind: ReferenceGrant | ||
metadata: | ||
name: access-to-cafe-services | ||
namespace: cafe | ||
spec: | ||
to: | ||
- group: "" | ||
kind: Service | ||
from: | ||
- group: gateway.networking.k8s.io | ||
kind: HTTPRoute | ||
namespace: default |
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
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.