Skip to content

Commit eb3c8f8

Browse files
committed
test: verify source IP is preserved
1 parent 800a8a9 commit eb3c8f8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

test/e2e/serviceloadbalancer_helpers.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"fmt"
1111
"io"
12+
"net"
1213
"net/http"
1314
"net/url"
1415
"strings"
@@ -170,6 +171,28 @@ func EnsureLoadBalancerService(
170171
}
171172
output := testServiceLoadBalancer(ctx, getClientIPURL, input.ServiceIntervals)
172173
Expect(output).ToNot(BeEmpty())
174+
175+
By("Verifying that the source IP is not part of the Cluster's Service subnet")
176+
// It is not simple to get the source IP of the runner because its possible connect through a VPN.
177+
//
178+
// When source IP preservation is not enabled,
179+
// the source IP that the LoadBalancer Service responds with would be part of the Cluster's Service subnet.
180+
// In this case we test the source IP is different from the Service IP.
181+
// The output will be something like:
182+
// 192.168.1.141:32768 - when source IP preservation is not enabled.
183+
// 10.22.24.12:32768 - when source IP preservation is enabled.
184+
// Get the source IP from the output.
185+
sourceIPStr := strings.Split(output, ":")[0]
186+
sourceIP := net.ParseIP(sourceIPStr)
187+
Expect(sourceIP).ToNot(BeNil())
188+
// Get the Cluster's Service subnet.
189+
serviceCIDRStr := input.WorkloadCluster.Spec.ClusterNetwork.Services.CIDRBlocks[0]
190+
_, serviceCIDR, err := net.ParseCIDR(serviceCIDRStr)
191+
Expect(err).ToNot(HaveOccurred())
192+
Expect(sourceIP).ToNot(BeNil())
193+
// Verify that the source IP is not part of the Cluster's Service subnet,
194+
// i.e. it will be the external client's IP.
195+
Expect(serviceCIDR.Contains(sourceIP)).To(BeFalse())
173196
}
174197

175198
func createTestService(

0 commit comments

Comments
 (0)