Skip to content

Commit b1525c3

Browse files
committed
e2e: connect to host-networked pod from localnet
Extend the existing test cases to include: - localnet -> host-networked pod on a different node - localnet -> host-networked pod on the same node Signed-off-by: Riccardo Ravaioli <[email protected]>
1 parent 0ee80bf commit b1525c3

File tree

2 files changed

+95
-17
lines changed

2 files changed

+95
-17
lines changed

test/e2e/multihoming.go

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,17 +325,31 @@ var _ = Describe("Multi Homing", func() {
325325
kickstartPod(cs, clientPodConfig)
326326

327327
// Check that the client pod can reach the server pod on the server localnet interface
328-
serverIPs, err := podIPsForAttachment(cs, f.Namespace.Name, serverPod.GetName(), netConfig.name)
328+
var serverIPs []string
329+
if serverPodConfig.hostNetwork {
330+
serverIPs, err = podIPsFromStatus(cs, serverPodConfig.namespace, serverPodConfig.name)
331+
} else {
332+
serverIPs, err = podIPsForAttachment(cs, serverPod.Namespace, serverPod.Name, netConfig.name)
333+
334+
}
329335
Expect(err).NotTo(HaveOccurred())
336+
330337
for _, serverIP := range serverIPs {
331338
By(fmt.Sprintf("asserting the *client* can contact the server pod exposed endpoint: %q on port %q", serverIP, port))
339+
curlArgs := []string{}
340+
pingArgs := []string{}
341+
if clientPodConfig.attachments != nil {
342+
// When the client is attached to a localnet, send probes from the localnet interface
343+
curlArgs = []string{"--interface", "net1"}
344+
pingArgs = []string{"-I", "net1"}
345+
}
332346
Eventually(func() error {
333-
return reachServerPodFromClient(cs, serverPodConfig, clientPodConfig, serverIP, port)
347+
return reachServerPodFromClient(cs, serverPodConfig, clientPodConfig, serverIP, port, curlArgs...)
334348
}, 2*time.Minute, 6*time.Second).Should(Succeed())
335349

336350
By(fmt.Sprintf("asserting the *client* can ping the server pod exposed endpoint: %q", serverIP))
337351
Eventually(func() error {
338-
return pingServerPodFromClient(cs, serverPodConfig, clientPodConfig, serverIP)
352+
return pingServerPodFromClient(cs, serverPodConfig, clientPodConfig, serverIP, pingArgs...)
339353
}, 2*time.Minute, 6*time.Second).Should(Succeed())
340354
}
341355
},
@@ -383,6 +397,52 @@ var _ = Describe("Multi Homing", func() {
383397
},
384398
Label("BUG", "OCPBUGS-43004"),
385399
),
400+
ginkgo.Entry(
401+
"can reach a host-networked pod on a different node",
402+
networkAttachmentConfigParams{
403+
name: secondaryNetworkName,
404+
topology: "localnet",
405+
},
406+
podConfiguration{ // client on localnet
407+
attachments: []nadapi.NetworkSelectionElement{{
408+
Name: secondaryNetworkName,
409+
}},
410+
name: clientPodName,
411+
nodeSelector: map[string]string{nodeHostnameKey: workerOneNodeName},
412+
isPrivileged: true,
413+
needsIPRequestFromHostSubnet: true,
414+
},
415+
podConfiguration{ // server on default network, pod is host-networked
416+
name: podName,
417+
containerCmd: httpServerContainerCmd(port),
418+
nodeSelector: map[string]string{nodeHostnameKey: workerTwoNodeName},
419+
hostNetwork: true,
420+
},
421+
Label("STORY", "SDN-5345"),
422+
),
423+
ginkgo.Entry(
424+
"can reach a host-networked pod on the same node",
425+
networkAttachmentConfigParams{
426+
name: secondaryNetworkName,
427+
topology: "localnet",
428+
},
429+
podConfiguration{ // client on localnet
430+
attachments: []nadapi.NetworkSelectionElement{{
431+
Name: secondaryNetworkName,
432+
}},
433+
name: clientPodName,
434+
nodeSelector: map[string]string{nodeHostnameKey: workerTwoNodeName},
435+
isPrivileged: true,
436+
needsIPRequestFromHostSubnet: true,
437+
},
438+
podConfiguration{ // server on default network, pod is host-networked
439+
name: podName,
440+
containerCmd: httpServerContainerCmd(port),
441+
nodeSelector: map[string]string{nodeHostnameKey: workerTwoNodeName},
442+
hostNetwork: true,
443+
},
444+
Label("STORY", "SDN-5345"),
445+
),
386446
)
387447
})
388448

@@ -842,7 +902,6 @@ var _ = Describe("Multi Homing", func() {
842902
Context("localnet OVN-K secondary network", func() {
843903
const (
844904
clientPodName = "client-pod"
845-
nodeHostnameKey = "kubernetes.io/hostname"
846905
servicePort = 9000
847906
dockerNetworkName = "underlay"
848907
underlayServiceIP = "60.128.0.1"

test/e2e/multihoming_utils.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ type podConfiguration struct {
161161
isPrivileged bool
162162
labels map[string]string
163163
requiresExtraNamespace bool
164+
hostNetwork bool
164165
needsIPRequestFromHostSubnet bool
165166
}
166167

@@ -171,6 +172,7 @@ func generatePodSpec(config podConfiguration) *v1.Pod {
171172
}
172173
podSpec.Spec.NodeSelector = config.nodeSelector
173174
podSpec.Labels = config.labels
175+
podSpec.Spec.HostNetwork = config.hostNetwork
174176
if config.isPrivileged {
175177
podSpec.Spec.Containers[0].SecurityContext.Privileged = ptr.To(true)
176178
} else {
@@ -253,17 +255,19 @@ func inRange(cidr string, ip string) error {
253255
return fmt.Errorf("ip [%s] is NOT in range %s", ip, cidr)
254256
}
255257

256-
func connectToServer(clientPodConfig podConfiguration, serverIP string, port int) error {
257-
_, err := e2ekubectl.RunKubectl(
258-
clientPodConfig.namespace,
258+
func connectToServer(clientPodConfig podConfiguration, serverIP string, port int, args ...string) error {
259+
target := net.JoinHostPort(serverIP, fmt.Sprintf("%d", port))
260+
baseArgs := []string{
259261
"exec",
260262
clientPodConfig.name,
261263
"--",
262264
"curl",
263265
"--connect-timeout",
264266
"2",
265-
net.JoinHostPort(serverIP, fmt.Sprintf("%d", port)),
266-
)
267+
}
268+
baseArgs = append(baseArgs, args...)
269+
270+
_, err := e2ekubectl.RunKubectl(clientPodConfig.namespace, append(baseArgs, target)...)
267271
return err
268272
}
269273

@@ -308,16 +312,19 @@ func getSecondaryInterfaceMTU(clientPodConfig podConfiguration) (int, error) {
308312
return mtu, nil
309313
}
310314

311-
func pingServer(clientPodConfig podConfiguration, serverIP string) error {
312-
_, err := e2ekubectl.RunKubectl(
313-
clientPodConfig.namespace,
315+
func pingServer(clientPodConfig podConfiguration, serverIP string, args ...string) error {
316+
baseArgs := []string{
314317
"exec",
315318
clientPodConfig.name,
316319
"--",
317320
"ping",
318321
"-c", "1", // send one ICMP echo request
319322
"-W", "2", // timeout after 2 seconds if no response
320-
serverIP)
323+
}
324+
baseArgs = append(baseArgs, args...)
325+
326+
_, err := e2ekubectl.RunKubectl(clientPodConfig.namespace, append(baseArgs, serverIP)...)
327+
321328
return err
322329
}
323330

@@ -381,6 +388,18 @@ func podIPForAttachment(k8sClient clientset.Interface, podNamespace string, podN
381388
return ips[ipIndex], nil
382389
}
383390

391+
func podIPsFromStatus(k8sClient clientset.Interface, podNamespace string, podName string) ([]string, error) {
392+
pod, err := k8sClient.CoreV1().Pods(podNamespace).Get(context.Background(), podName, metav1.GetOptions{})
393+
if err != nil {
394+
return nil, err
395+
}
396+
podIPs := make([]string, 0, len(pod.Status.PodIPs))
397+
for _, podIP := range pod.Status.PodIPs {
398+
podIPs = append(podIPs, podIP.IP)
399+
}
400+
return podIPs, nil
401+
}
402+
384403
func allowedClient(podName string) string {
385404
return "allowed-" + podName
386405
}
@@ -610,27 +629,27 @@ func allowedTCPPortsForPolicy(allowPorts ...int) []mnpapi.MultiNetworkPolicyPort
610629
return portAllowlist
611630
}
612631

613-
func reachServerPodFromClient(cs clientset.Interface, serverConfig podConfiguration, clientConfig podConfiguration, serverIP string, serverPort int) error {
632+
func reachServerPodFromClient(cs clientset.Interface, serverConfig podConfiguration, clientConfig podConfiguration, serverIP string, serverPort int, args ...string) error {
614633
updatedPod, err := cs.CoreV1().Pods(serverConfig.namespace).Get(context.Background(), serverConfig.name, metav1.GetOptions{})
615634
if err != nil {
616635
return err
617636
}
618637

619638
if updatedPod.Status.Phase == v1.PodRunning {
620-
return connectToServer(clientConfig, serverIP, serverPort)
639+
return connectToServer(clientConfig, serverIP, serverPort, args...)
621640
}
622641

623642
return fmt.Errorf("pod not running. /me is sad")
624643
}
625644

626-
func pingServerPodFromClient(cs clientset.Interface, serverConfig podConfiguration, clientConfig podConfiguration, serverIP string) error {
645+
func pingServerPodFromClient(cs clientset.Interface, serverConfig podConfiguration, clientConfig podConfiguration, serverIP string, args ...string) error {
627646
updatedPod, err := cs.CoreV1().Pods(serverConfig.namespace).Get(context.Background(), serverConfig.name, metav1.GetOptions{})
628647
if err != nil {
629648
return err
630649
}
631650

632651
if updatedPod.Status.Phase == v1.PodRunning {
633-
return pingServer(clientConfig, serverIP)
652+
return pingServer(clientConfig, serverIP, args...)
634653
}
635654

636655
return fmt.Errorf("pod not running. /me is sad")

0 commit comments

Comments
 (0)