99 "context"
1010 "fmt"
1111 "io"
12+ "net"
1213 "net/http"
1314 "net/url"
1415 "strings"
@@ -20,12 +21,14 @@ import (
2021 corev1 "k8s.io/api/core/v1"
2122 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223 "k8s.io/apimachinery/pkg/util/intstr"
24+ "k8s.io/klog/v2"
2325 "k8s.io/utils/ptr"
2426 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2527 "sigs.k8s.io/cluster-api/test/framework"
2628 "sigs.k8s.io/controller-runtime/pkg/client"
2729
2830 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
31+ capiutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/utils"
2932 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/serviceloadbalancer/metallb"
3033)
3134
@@ -168,8 +171,34 @@ func EnsureLoadBalancerService(
168171 Host : getLoadBalancerAddress (svc ),
169172 Path : "/clientip" ,
170173 }
174+ klog .Infof ("Testing the LoadBalancer Service on: %q" , getClientIPURL .String ())
171175 output := testServiceLoadBalancer (ctx , getClientIPURL , input .ServiceIntervals )
172176 Expect (output ).ToNot (BeEmpty ())
177+ klog .Infof ("Kubernetes LoadBalancer Service output: %q" , output )
178+
179+ By ("Verifying that the source IP is as expected" )
180+ // It is not simple to get the source IP of the runner because its possible connect through a VPN.
181+ //
182+ // When source IP preservation is not enabled,
183+ // the source IP that the LoadBalancer Service responds with an IP from the Cluster's Pod subnet.
184+ // When source IP preservation is enabled, we test the source IP is different from the Pod IP.
185+ // The output will be something like:
186+ // 192.168.1.141:32768 - when source IP preservation is not enabled.
187+ // 10.22.24.12:32768 - when source IP preservation is enabled.
188+ // Get the source IP from the output.
189+ sourceIPStr := strings .Split (output , ":" )[0 ]
190+ sourceIP := net .ParseIP (sourceIPStr )
191+ Expect (sourceIP ).ToNot (BeNil ())
192+ // Get the Cluster's Pod subnet.
193+ podCIDRStr := input .WorkloadCluster .Spec .ClusterNetwork .Pods .CIDRBlocks [0 ]
194+ _ , podCIDR , err := net .ParseCIDR (podCIDRStr )
195+ Expect (err ).ToNot (HaveOccurred ())
196+ Expect (podCIDR ).ToNot (BeNil ())
197+
198+ // When skip kube-proxy is false (i.e. kube-proxy is enabled), sourceIP should be from the Pod subnet.
199+ // Otherwise, sourceIP will be some external IP.
200+ expectIPFromPodSubnet := ! capiutils .ShouldSkipKubeProxy (input .WorkloadCluster )
201+ Expect (podCIDR .Contains (sourceIP )).To (Equal (expectIPFromPodSubnet ))
173202}
174203
175204func createTestService (
0 commit comments