Skip to content

Commit 37f4b10

Browse files
committed
add x-forwarded-for to envoy
1 parent 7cee5d8 commit 37f4b10

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

examples/ingress_foo_bar.yaml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
kind: Pod
2+
apiVersion: v1
3+
metadata:
4+
name: foo-app
5+
labels:
6+
app: foo
7+
spec:
8+
containers:
9+
- command:
10+
- /agnhost
11+
- netexec
12+
- --http-port=8080
13+
image: registry.k8s.io/e2e-test-images/agnhost:2.59
14+
name: foo-app
15+
---
16+
kind: Service
17+
apiVersion: v1
18+
metadata:
19+
name: foo-service
20+
spec:
21+
selector:
22+
app: foo
23+
ports:
24+
- port: 8080
25+
---
26+
kind: Pod
27+
apiVersion: v1
28+
metadata:
29+
name: bar-app
30+
labels:
31+
app: bar
32+
spec:
33+
containers:
34+
- command:
35+
- /agnhost
36+
- netexec
37+
- --http-port=8080
38+
image: registry.k8s.io/e2e-test-images/agnhost:2.59
39+
name: bar-app
40+
---
41+
kind: Service
42+
apiVersion: v1
43+
metadata:
44+
name: bar-service
45+
spec:
46+
selector:
47+
app: bar
48+
ports:
49+
- port: 8080
50+
---
51+
apiVersion: networking.k8s.io/v1
52+
kind: Ingress
53+
metadata:
54+
name: example-ingress
55+
spec:
56+
rules:
57+
- host: foo.example.com
58+
http:
59+
paths:
60+
- pathType: Prefix
61+
path: /
62+
backend:
63+
service:
64+
name: foo-service
65+
port:
66+
number: 8080
67+
- host: bar.example.com
68+
http:
69+
paths:
70+
- pathType: Prefix
71+
path: /
72+
backend:
73+
service:
74+
name: bar-service
75+
port:
76+
number: 8080
77+
---
78+
kind: Pod
79+
apiVersion: v1
80+
metadata:
81+
name: curl-pod
82+
spec:
83+
containers:
84+
- name: curl
85+
image: registry.k8s.io/e2e-test-images/agnhost:2.59

pkg/gateway/listener.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
1818

1919
"google.golang.org/protobuf/types/known/anypb"
20+
"google.golang.org/protobuf/types/known/wrapperspb"
2021

2122
corev1 "k8s.io/api/core/v1"
2223
"k8s.io/apimachinery/pkg/api/meta"
@@ -223,6 +224,9 @@ func (c *Controller) translateListenerToFilterChain(gateway *gatewayv1.Gateway,
223224

224225
hcmConfig := &hcm.HttpConnectionManager{
225226
StatPrefix: string(lis.Name),
227+
// Enable X-Forwarded-For header
228+
// https://github.com/kubernetes-sigs/cloud-provider-kind/issues/296
229+
UseRemoteAddress: &wrapperspb.BoolValue{Value: true},
226230
RouteSpecifier: &hcm.HttpConnectionManager_Rds{
227231
Rds: &hcm.Rds{
228232
ConfigSource: &corev3.ConfigSource{

tests/tests.bats

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,45 @@
118118
# Cleanup: Delete the applied manifests
119119
kubectl delete --ignore-not-found -f "$BATS_TEST_DIRNAME"/../examples/gateway_httproute_simple.yaml
120120
}
121+
122+
123+
@test "Ingress to Gateway Migration and X-Forwarded-For Header" {
124+
# Apply the Gateway and HTTPRoute manifests
125+
kubectl apply -f "$BATS_TEST_DIRNAME"/../examples/ingress_foo_bar.yaml
126+
127+
# Wait for the backend application pod to be ready
128+
kubectl wait --for=condition=ready pods -l app=foo --timeout=60s
129+
kubectl wait --for=condition=ready pods -l app=foo --timeout=60s
130+
131+
# Give the controller time to reconcile
132+
echo "Waiting for reconciliation..."
133+
sleep 5
134+
135+
echo "Finding Ingress Loadbalancer IP ..."
136+
run kubectl get ingress example-ingress -o jsonpath='{.status.loadBalancer.ingress[0].ip}'
137+
[ "$status" -eq 0 ]
138+
export INGRESS_SVC_IP="$output"
139+
echo "Ingress LoadBalancer IP: $INGRESS_SVC_IP"
140+
141+
# Test /foo prefix
142+
echo "Testing /foo prefix (should match foo-app)..."
143+
run kubectl exec curl-pod -- curl -H "Host: foo.example.com" -s "http://$INGRESS_SVC_IP/hostname"
144+
[ "$status" -eq 0 ]
145+
[[ "$output" == "foo-app" ]]
146+
147+
# Test /bar prefix
148+
echo "Testing /bar prefix (should match bar-app)..."
149+
run kubectl exec curl-pod -- curl -H "Host: bar.example.com" -s "http://$INGRESS_SVC_IP/hostname"
150+
[ "$status" -eq 0 ]
151+
[[ "$output" == "bar-app" ]]
152+
153+
# Test X-Forwarded-For header
154+
echo "Testing X-Forwarded-For header..."
155+
run kubectl exec curl-pod -- curl -H "Host: foo.example.com" -s "http://$INGRESS_SVC_IP/header?key=X-Forwarded-For"
156+
[ "$status" -eq 0 ]
157+
echo "X-Forwarded-For header value: $output"
158+
[[ ! -z "$output" ]]
159+
160+
# Cleanup: Delete the applied manifests
161+
kubectl delete --ignore-not-found -f "$BATS_TEST_DIRNAME"/../examples/ingress_foo_bar.yaml
162+
}

0 commit comments

Comments
 (0)