Skip to content

Commit 2626e8d

Browse files
committed
Reapply "e2e: connect to host-networked pod from localnet"
This reverts commit 936e621. Signed-off-by: Riccardo Ravaioli <[email protected]>
1 parent 3de7ead commit 2626e8d

File tree

2 files changed

+95
-16
lines changed

2 files changed

+95
-16
lines changed

test/e2e/multihoming.go

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,31 @@ var _ = Describe("Multi Homing", feature.MultiHoming, func() {
333333
kickstartPod(cs, clientPodConfig)
334334

335335
// Check that the client pod can reach the server pod on the server localnet interface
336-
serverIPs, err := podIPsForAttachment(cs, f.Namespace.Name, serverPod.GetName(), netConfig.name)
336+
var serverIPs []string
337+
if serverPodConfig.hostNetwork {
338+
serverIPs, err = podIPsFromStatus(cs, serverPodConfig.namespace, serverPodConfig.name)
339+
} else {
340+
serverIPs, err = podIPsForAttachment(cs, serverPod.Namespace, serverPod.Name, netConfig.name)
341+
342+
}
337343
Expect(err).NotTo(HaveOccurred())
344+
338345
for _, serverIP := range serverIPs {
339346
By(fmt.Sprintf("asserting the *client* can contact the server pod exposed endpoint: %q on port %q", serverIP, port))
347+
curlArgs := []string{}
348+
pingArgs := []string{}
349+
if clientPodConfig.attachments != nil {
350+
// When the client is attached to a localnet, send probes from the localnet interface
351+
curlArgs = []string{"--interface", "net1"}
352+
pingArgs = []string{"-I", "net1"}
353+
}
340354
Eventually(func() error {
341-
return reachServerPodFromClient(cs, serverPodConfig, clientPodConfig, serverIP, port)
355+
return reachServerPodFromClient(cs, serverPodConfig, clientPodConfig, serverIP, port, curlArgs...)
342356
}, 2*time.Minute, 6*time.Second).Should(Succeed())
343357

344358
By(fmt.Sprintf("asserting the *client* can ping the server pod exposed endpoint: %q", serverIP))
345359
Eventually(func() error {
346-
return pingServerPodFromClient(cs, serverPodConfig, clientPodConfig, serverIP)
360+
return pingServerPodFromClient(cs, serverPodConfig, clientPodConfig, serverIP, pingArgs...)
347361
}, 2*time.Minute, 6*time.Second).Should(Succeed())
348362
}
349363
},
@@ -391,6 +405,52 @@ var _ = Describe("Multi Homing", feature.MultiHoming, func() {
391405
},
392406
Label("BUG", "OCPBUGS-43004"),
393407
),
408+
ginkgo.Entry(
409+
"can reach a host-networked pod on a different node",
410+
networkAttachmentConfigParams{
411+
name: secondaryNetworkName,
412+
topology: "localnet",
413+
},
414+
podConfiguration{ // client on localnet
415+
attachments: []nadapi.NetworkSelectionElement{{
416+
Name: secondaryNetworkName,
417+
}},
418+
name: clientPodName,
419+
nodeSelector: map[string]string{nodeHostnameKey: workerOneNodeName},
420+
isPrivileged: true,
421+
needsIPRequestFromHostSubnet: true,
422+
},
423+
podConfiguration{ // server on default network, pod is host-networked
424+
name: podName,
425+
containerCmd: httpServerContainerCmd(port),
426+
nodeSelector: map[string]string{nodeHostnameKey: workerTwoNodeName},
427+
hostNetwork: true,
428+
},
429+
Label("STORY", "SDN-5345"),
430+
),
431+
ginkgo.Entry(
432+
"can reach a host-networked pod on the same node",
433+
networkAttachmentConfigParams{
434+
name: secondaryNetworkName,
435+
topology: "localnet",
436+
},
437+
podConfiguration{ // client on localnet
438+
attachments: []nadapi.NetworkSelectionElement{{
439+
Name: secondaryNetworkName,
440+
}},
441+
name: clientPodName,
442+
nodeSelector: map[string]string{nodeHostnameKey: workerTwoNodeName},
443+
isPrivileged: true,
444+
needsIPRequestFromHostSubnet: true,
445+
},
446+
podConfiguration{ // server on default network, pod is host-networked
447+
name: podName,
448+
containerCmd: httpServerContainerCmd(port),
449+
nodeSelector: map[string]string{nodeHostnameKey: workerTwoNodeName},
450+
hostNetwork: true,
451+
},
452+
Label("STORY", "SDN-5345"),
453+
),
394454
)
395455
})
396456

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 uint16) error {
257-
_, err := e2ekubectl.RunKubectl(
258-
clientPodConfig.namespace,
258+
func connectToServer(clientPodConfig podConfiguration, serverIP string, port uint16, 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 uint16) error {
632+
func reachServerPodFromClient(cs clientset.Interface, serverConfig podConfiguration, clientConfig podConfiguration, serverIP string, serverPort uint16, 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)