Skip to content

Commit dfc14b4

Browse files
committed
e2e: refactor podIPOfFamilyOnPrimaryNetwork into more reusable code
Signed-off-by: Jaime Caamaño Ruiz <[email protected]>
1 parent 5ece846 commit dfc14b4

File tree

7 files changed

+85
-76
lines changed

7 files changed

+85
-76
lines changed

test/e2e/egressip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ spec:
972972
if isClusterDefaultNetwork(netConfigParams) {
973973
pod2IP = getPodAddress(pod2Name, f.Namespace.Name)
974974
} else {
975-
pod2IP, err = podIPsForUserDefinedPrimaryNetwork(
975+
pod2IP, err = getPodAnnotationIPsForAttachmentByIndex(
976976
f.ClientSet,
977977
f.Namespace.Name,
978978
pod2Name,

test/e2e/multihoming_utils.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,3 +704,39 @@ func getNetworkGateway(cli *client.Client, networkName string) (string, error) {
704704

705705
return "", fmt.Errorf("Gateway not found for network %q", networkName)
706706
}
707+
708+
func getPodAnnotationForAttachment(pod *v1.Pod, attachmentName string) (PodAnnotation, error) {
709+
podAnnotation, err := unmarshalPodAnnotation(pod.Annotations, attachmentName)
710+
if err != nil {
711+
return PodAnnotation{}, fmt.Errorf("failed to unmarshall annotations for pod %q: %v", pod.Name, err)
712+
}
713+
714+
return *podAnnotation, nil
715+
}
716+
717+
func getPodAnnotationIPsForAttachment(k8sClient clientset.Interface, podNamespace string, podName string, attachmentName string) ([]*net.IPNet, error) {
718+
pod, err := k8sClient.CoreV1().Pods(podNamespace).Get(context.Background(), podName, metav1.GetOptions{})
719+
if err != nil {
720+
return nil, err
721+
}
722+
podAnnotation, err := getPodAnnotationForAttachment(pod, attachmentName)
723+
if err != nil {
724+
return nil, err
725+
}
726+
return podAnnotation.IPs, nil
727+
}
728+
729+
// podIPsForNetworkByIndex returns the v4 or v6 IPs for a pod on the UDN
730+
func getPodAnnotationIPsForAttachmentByIndex(k8sClient clientset.Interface, podNamespace string, podName string, attachmentName string, index int) (string, error) {
731+
ipnets, err := getPodAnnotationIPsForAttachment(k8sClient, podNamespace, podName, attachmentName)
732+
if err != nil {
733+
return "", err
734+
}
735+
if index >= len(ipnets) {
736+
return "", fmt.Errorf("no IP at index %d for attachment %s on pod %s", index, attachmentName, namespacedName(podNamespace, podName))
737+
}
738+
if len(ipnets) > 2 {
739+
return "", fmt.Errorf("attachment for network %q with more than two IPs", attachmentName)
740+
}
741+
return ipnets[index].IP.String(), nil
742+
}

test/e2e/network_segmentation.go

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ var _ = Describe("Network Segmentation", feature.NetworkSegmentation, func() {
175175
for i, cidr := range strings.Split(netConfig.cidr, ",") {
176176
if cidr != "" {
177177
By("asserting the server pod has an IP from the configured range")
178-
serverIP, err = podIPsForUserDefinedPrimaryNetwork(
178+
serverIP, err = getPodAnnotationIPsForAttachmentByIndex(
179179
cs,
180180
f.Namespace.Name,
181181
serverPodConfig.name,
@@ -610,7 +610,7 @@ var _ = Describe("Network Segmentation", feature.NetworkSegmentation, func() {
610610
By("creating pod " + podConfig.name + " in " + podConfig.namespace)
611611
pod := runUDNPod(cs, podConfig.namespace, podConfig, nil)
612612
pods = append(pods, pod)
613-
podIP, err := podIPsForUserDefinedPrimaryNetwork(
613+
podIP, err := getPodAnnotationIPsForAttachmentByIndex(
614614
cs,
615615
pod.Namespace,
616616
pod.Name,
@@ -792,7 +792,7 @@ var _ = Describe("Network Segmentation", feature.NetworkSegmentation, func() {
792792
By(fmt.Sprintf("asserting network works in namespace %s", config.namespace))
793793
for i, cidr := range strings.Split(config.cidr, ",") {
794794
if cidr != "" {
795-
serverIP, err = podIPsForUserDefinedPrimaryNetwork(
795+
serverIP, err = getPodAnnotationIPsForAttachmentByIndex(
796796
cs,
797797
config.namespace,
798798
serverPodConfig.name,
@@ -1756,7 +1756,7 @@ spec:
17561756
clientPodConfig.nodeSelector = map[string]string{nodeHostnameKey: node2Name}
17571757
runUDNPod(cs, f.Namespace.Name, serverPodConfig, nil)
17581758
runUDNPod(cs, f.Namespace.Name, clientPodConfig, nil)
1759-
serverIP, err := podIPsForUserDefinedPrimaryNetwork(cs, f.Namespace.Name, serverPodConfig.name, namespacedName(f.Namespace.Name, netConfig.name), 0)
1759+
serverIP, err := getPodAnnotationIPsForAttachmentByIndex(cs, f.Namespace.Name, serverPodConfig.name, namespacedName(f.Namespace.Name, netConfig.name), 0)
17601760
Expect(err).ShouldNot(HaveOccurred(), "UDN pod IP must be retrieved")
17611761
By("restart OVNKube node pods on client and server Nodes and ensure connectivity")
17621762
serverPod := getPod(f, serverPodConfig.name)
@@ -2275,26 +2275,6 @@ func withNetworkAttachment(networks []nadapi.NetworkSelectionElement) podOption
22752275
}
22762276
}
22772277

2278-
// podIPsForUserDefinedPrimaryNetwork returns the v4 or v6 IPs for a pod on the UDN
2279-
func podIPsForUserDefinedPrimaryNetwork(k8sClient clientset.Interface, podNamespace string, podName string, attachmentName string, index int) (string, error) {
2280-
pod, err := k8sClient.CoreV1().Pods(podNamespace).Get(context.Background(), podName, metav1.GetOptions{})
2281-
if err != nil {
2282-
return "", err
2283-
}
2284-
netStatus, err := userDefinedNetworkStatus(pod, attachmentName)
2285-
if err != nil {
2286-
return "", err
2287-
}
2288-
2289-
if len(netStatus.IPs) == 0 {
2290-
return "", fmt.Errorf("attachment for network %q without IPs", attachmentName)
2291-
}
2292-
if len(netStatus.IPs) > 2 {
2293-
return "", fmt.Errorf("attachment for network %q with more than two IPs", attachmentName)
2294-
}
2295-
return netStatus.IPs[index].IP.String(), nil
2296-
}
2297-
22982278
func podIPsForDefaultNetwork(k8sClient clientset.Interface, podNamespace string, podName string) (string, string, error) {
22992279
pod, err := k8sClient.CoreV1().Pods(podNamespace).Get(context.Background(), podName, metav1.GetOptions{})
23002280
if err != nil {
@@ -2304,15 +2284,6 @@ func podIPsForDefaultNetwork(k8sClient clientset.Interface, podNamespace string,
23042284
return ipv4, ipv6, nil
23052285
}
23062286

2307-
func userDefinedNetworkStatus(pod *v1.Pod, networkName string) (PodAnnotation, error) {
2308-
netStatus, err := unmarshalPodAnnotation(pod.Annotations, networkName)
2309-
if err != nil {
2310-
return PodAnnotation{}, fmt.Errorf("failed to unmarshall annotations for pod %q: %v", pod.Name, err)
2311-
}
2312-
2313-
return *netStatus, nil
2314-
}
2315-
23162287
func runUDNPod(cs clientset.Interface, namespace string, serverPodConfig podConfiguration, podSpecTweak func(*v1.Pod)) *v1.Pod {
23172288
By(fmt.Sprintf("instantiating the UDN pod %s", serverPodConfig.name))
23182289
podSpec := generatePodSpec(serverPodConfig)

test/e2e/network_segmentation_policy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ var _ = ginkgo.Describe("Network Segmentation: Network Policies", feature.Networ
103103
for i, cidr := range strings.Split(netConfig.cidr, ",") {
104104
if cidr != "" {
105105
ginkgo.By("asserting the server pod has an IP from the configured range")
106-
serverIP, err = podIPsForUserDefinedPrimaryNetwork(
106+
serverIP, err = getPodAnnotationIPsForAttachmentByIndex(
107107
cs,
108108
f.Namespace.Name,
109109
serverPodConfig.name,
@@ -231,12 +231,12 @@ var _ = ginkgo.Describe("Network Segmentation: Network Policies", feature.Networ
231231
}
232232
subnet, err := getNetCIDRSubnet(cidr)
233233
gomega.Expect(err).NotTo(gomega.HaveOccurred())
234-
allowServerPodIP, err = podIPsForUserDefinedPrimaryNetwork(cs, namespaceYellow, allowServerPodConfig.name,
234+
allowServerPodIP, err = getPodAnnotationIPsForAttachmentByIndex(cs, namespaceYellow, allowServerPodConfig.name,
235235
namespacedName(namespaceYellow, netConfName), i)
236236
gomega.Expect(err).NotTo(gomega.HaveOccurred())
237237
ginkgo.By(fmt.Sprintf("asserting the allow server pod IP %v is from the configured range %v", allowServerPodIP, cidr))
238238
gomega.Expect(inRange(subnet, allowServerPodIP)).To(gomega.Succeed())
239-
denyServerPodIP, err = podIPsForUserDefinedPrimaryNetwork(cs, namespaceYellow, denyServerPodConfig.name,
239+
denyServerPodIP, err = getPodAnnotationIPsForAttachmentByIndex(cs, namespaceYellow, denyServerPodConfig.name,
240240
namespacedName(namespaceYellow, netConfName), i)
241241
gomega.Expect(err).NotTo(gomega.HaveOccurred())
242242
ginkgo.By(fmt.Sprintf("asserting the deny server pod IP %v is from the configured range %v", denyServerPodIP, cidr))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package e2e
2+
3+
import (
4+
"k8s.io/client-go/kubernetes"
5+
"k8s.io/utils/net"
6+
)
7+
8+
// podIPsForUserDefinedPrimaryNetwork returns the v4 or v6 IPs for a pod on the UDN
9+
func getPodAnnotationIPsForPrimaryNetworkByIPFamily(k8sClient kubernetes.Interface, podNamespace string, podName string, networkName string, family net.IPFamily) (string, error) {
10+
if networkName != "default" {
11+
networkName = namespacedName(podNamespace, networkName)
12+
}
13+
ipnets, err := getPodAnnotationIPsForAttachment(k8sClient, podNamespace, podName, networkName)
14+
if err != nil {
15+
return "", err
16+
}
17+
ipnet := getFirstCIDROfFamily(family, ipnets)
18+
if ipnet == nil {
19+
return "", nil
20+
}
21+
return ipnet.IP.String(), nil
22+
}

test/e2e/route_advertisements.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ var _ = ginkgo.Describe("BGP: Pod to external server when CUDN network is advert
407407

408408
ginkgo.By("queries to the external server are not SNATed (uses podIP)")
409409
for _, serverContainerIP := range serverContainerIPs {
410-
podIP, err := podIPsForUserDefinedPrimaryNetwork(f.ClientSet, f.Namespace.Name, clientPod.Name, namespacedName(f.Namespace.Name, cUDN.Name), 0)
410+
podIP, err := getPodAnnotationIPsForAttachmentByIndex(f.ClientSet, f.Namespace.Name, clientPod.Name, namespacedName(f.Namespace.Name, cUDN.Name), 0)
411411
gomega.Expect(err).NotTo(gomega.HaveOccurred())
412412
framework.ExpectNoError(err, fmt.Sprintf("Getting podIPs for pod %s failed: %v", clientPod.Name, err))
413413
framework.Logf("Client pod IP address=%s", podIP)
@@ -426,7 +426,7 @@ var _ = ginkgo.Describe("BGP: Pod to external server when CUDN network is advert
426426
60*time.Second)
427427
framework.ExpectNoError(err, fmt.Sprintf("Testing pod to external traffic failed: %v", err))
428428
if isIPv6Supported(f.ClientSet) && utilnet.IsIPv6String(serverContainerIP) {
429-
podIP, err = podIPsForUserDefinedPrimaryNetwork(f.ClientSet, f.Namespace.Name, clientPod.Name, namespacedName(f.Namespace.Name, cUDN.Name), 1)
429+
podIP, err = getPodAnnotationIPsForAttachmentByIndex(f.ClientSet, f.Namespace.Name, clientPod.Name, namespacedName(f.Namespace.Name, cUDN.Name), 1)
430430
// For IPv6 addresses, need to handle the brackets in the output
431431
outputIP := strings.TrimPrefix(strings.Split(stdout, "]:")[0], "[")
432432
gomega.Expect(outputIP).To(gomega.Equal(podIP),
@@ -838,9 +838,9 @@ var _ = ginkgo.DescribeTableSubtree("BGP: isolation between advertised networks"
838838
clientPod := podsNetA[0]
839839
srvPod := podsNetA[1]
840840

841-
clientPodStatus, err := userDefinedNetworkStatus(clientPod, namespacedName(clientPod.Namespace, cudnATemplate.Name))
841+
clientPodStatus, err := getPodAnnotationForAttachment(clientPod, namespacedName(clientPod.Namespace, cudnATemplate.Name))
842842
framework.ExpectNoError(err)
843-
srvPodStatus, err := userDefinedNetworkStatus(srvPod, namespacedName(srvPod.Namespace, cudnATemplate.Name))
843+
srvPodStatus, err := getPodAnnotationForAttachment(srvPod, namespacedName(srvPod.Namespace, cudnATemplate.Name))
844844
framework.ExpectNoError(err)
845845
return clientPod.Name, clientPod.Namespace, net.JoinHostPort(srvPodStatus.IPs[ipFamilyIndex].IP.String(), "8080") + "/clientip", clientPodStatus.IPs[ipFamilyIndex].IP.String(), false
846846
}),
@@ -850,9 +850,9 @@ var _ = ginkgo.DescribeTableSubtree("BGP: isolation between advertised networks"
850850
clientPod := podsNetA[0]
851851
srvPod := podsNetA[2]
852852

853-
clientPodStatus, err := userDefinedNetworkStatus(clientPod, namespacedName(clientPod.Namespace, cudnATemplate.Name))
853+
clientPodStatus, err := getPodAnnotationForAttachment(clientPod, namespacedName(clientPod.Namespace, cudnATemplate.Name))
854854
framework.ExpectNoError(err)
855-
srvPodStatus, err := userDefinedNetworkStatus(srvPod, namespacedName(srvPod.Namespace, cudnATemplate.Name))
855+
srvPodStatus, err := getPodAnnotationForAttachment(srvPod, namespacedName(srvPod.Namespace, cudnATemplate.Name))
856856
framework.ExpectNoError(err)
857857
return clientPod.Name, clientPod.Namespace, net.JoinHostPort(srvPodStatus.IPs[ipFamilyIndex].IP.String(), "8080") + "/clientip", clientPodStatus.IPs[ipFamilyIndex].IP.String(), false
858858
}),
@@ -862,7 +862,7 @@ var _ = ginkgo.DescribeTableSubtree("BGP: isolation between advertised networks"
862862
clientPod := podsNetA[2]
863863
srvPod := podNetB
864864

865-
srvPodStatus, err := userDefinedNetworkStatus(srvPod, namespacedName(srvPod.Namespace, cudnBTemplate.Name))
865+
srvPodStatus, err := getPodAnnotationForAttachment(srvPod, namespacedName(srvPod.Namespace, cudnBTemplate.Name))
866866
framework.ExpectNoError(err)
867867
return clientPod.Name, clientPod.Namespace, net.JoinHostPort(srvPodStatus.IPs[ipFamilyIndex].IP.String(), "8080") + "/clientip", curlConnectionTimeoutCode, true
868868
}),
@@ -873,7 +873,7 @@ var _ = ginkgo.DescribeTableSubtree("BGP: isolation between advertised networks"
873873
clientPod := podsNetA[0]
874874
srvPod := podNetB
875875

876-
srvPodStatus, err := userDefinedNetworkStatus(srvPod, namespacedName(srvPod.Namespace, cudnBTemplate.Name))
876+
srvPodStatus, err := getPodAnnotationForAttachment(srvPod, namespacedName(srvPod.Namespace, cudnBTemplate.Name))
877877
framework.ExpectNoError(err)
878878
return clientPod.Name, clientPod.Namespace, net.JoinHostPort(srvPodStatus.IPs[ipFamilyIndex].IP.String(), "8080") + "/clientip", curlConnectionTimeoutCode, true
879879
}),
@@ -883,7 +883,7 @@ var _ = ginkgo.DescribeTableSubtree("BGP: isolation between advertised networks"
883883
clientPod := podNetDefault
884884
srvPod := podNetB
885885

886-
srvPodStatus, err := userDefinedNetworkStatus(srvPod, namespacedName(srvPod.Namespace, cudnBTemplate.Name))
886+
srvPodStatus, err := getPodAnnotationForAttachment(srvPod, namespacedName(srvPod.Namespace, cudnBTemplate.Name))
887887
framework.ExpectNoError(err)
888888
return clientPod.Name, clientPod.Namespace, net.JoinHostPort(srvPodStatus.IPs[ipFamilyIndex].IP.String(), "8080") + "/clientip", curlConnectionTimeoutCode, true
889889
}),
@@ -893,7 +893,7 @@ var _ = ginkgo.DescribeTableSubtree("BGP: isolation between advertised networks"
893893
clientPod := podNetDefault
894894
srvPod := podsNetA[0]
895895

896-
srvPodStatus, err := userDefinedNetworkStatus(srvPod, namespacedName(srvPod.Namespace, cudnATemplate.Name))
896+
srvPodStatus, err := getPodAnnotationForAttachment(srvPod, namespacedName(srvPod.Namespace, cudnATemplate.Name))
897897
framework.ExpectNoError(err)
898898
return clientPod.Name, clientPod.Namespace, net.JoinHostPort(srvPodStatus.IPs[ipFamilyIndex].IP.String(), "8080") + "/clientip", curlConnectionTimeoutCode, true
899899
}),
@@ -922,7 +922,7 @@ var _ = ginkgo.DescribeTableSubtree("BGP: isolation between advertised networks"
922922
clientNode := podsNetA[0].Spec.NodeName
923923
srvPod := podsNetA[0]
924924

925-
srvPodStatus, err := userDefinedNetworkStatus(srvPod, namespacedName(srvPod.Namespace, cudnATemplate.Name))
925+
srvPodStatus, err := getPodAnnotationForAttachment(srvPod, namespacedName(srvPod.Namespace, cudnATemplate.Name))
926926
framework.ExpectNoError(err)
927927
return clientNode, "", net.JoinHostPort(srvPodStatus.IPs[ipFamilyIndex].IP.String(), "8080") + "/clientip", curlConnectionTimeoutCode, true
928928
}),
@@ -932,7 +932,7 @@ var _ = ginkgo.DescribeTableSubtree("BGP: isolation between advertised networks"
932932
clientNode := podsNetA[2].Spec.NodeName
933933
srvPod := podsNetA[0]
934934

935-
srvPodStatus, err := userDefinedNetworkStatus(srvPod, namespacedName(srvPod.Namespace, cudnATemplate.Name))
935+
srvPodStatus, err := getPodAnnotationForAttachment(srvPod, namespacedName(srvPod.Namespace, cudnATemplate.Name))
936936
framework.ExpectNoError(err)
937937
return clientNode, "", net.JoinHostPort(srvPodStatus.IPs[ipFamilyIndex].IP.String(), "8080") + "/clientip", curlConnectionTimeoutCode, true
938938
}),
@@ -1301,7 +1301,7 @@ var _ = ginkgo.Describe("BGP: For a VRF-Lite configured network", func() {
13011301
testPodToHostnameAndExpect(testPod, serverIP, bgpServerName)
13021302

13031303
ginkgo.By("Ensuring a request from the pod is not SNATed")
1304-
testPodIP, err := podIPOfFamilyOnPrimaryNetwork(
1304+
testPodIP, err := getPodAnnotationIPsForPrimaryNetworkByIPFamily(
13051305
f.ClientSet,
13061306
testPod.Namespace,
13071307
testPod.Name,
@@ -1328,7 +1328,7 @@ var _ = ginkgo.Describe("BGP: For a VRF-Lite configured network", func() {
13281328
gomega.Expect(err).NotTo(gomega.HaveOccurred())
13291329
serverIP := getFirstIPStringOfFamily(family, []string{iface.IPv4, iface.IPv6})
13301330
gomega.Expect(serverIP).NotTo(gomega.BeEmpty())
1331-
podIP, err := podIPOfFamilyOnPrimaryNetwork(
1331+
podIP, err := getPodAnnotationIPsForPrimaryNetworkByIPFamily(
13321332
f.ClientSet,
13331333
testPod.Namespace,
13341334
testPod.Name,
@@ -1381,7 +1381,7 @@ var _ = ginkgo.Describe("BGP: For a VRF-Lite configured network", func() {
13811381
e2eskipper.Skipf("IP family %v not supported", family)
13821382
}
13831383
ginkgo.By("Ensuring a request from the external server cannot reach the pod")
1384-
podIP, err := podIPOfFamilyOnPrimaryNetwork(
1384+
podIP, err := getPodAnnotationIPsForPrimaryNetworkByIPFamily(
13851385
f.ClientSet,
13861386
testPod.Namespace,
13871387
testPod.Name,
@@ -1405,7 +1405,7 @@ var _ = ginkgo.Describe("BGP: For a VRF-Lite configured network", func() {
14051405
e2eskipper.Skipf("IP family %v not supported", family)
14061406
}
14071407
ginkgo.By("Ensuring a request from the node cannot reach the tested network pod")
1408-
podIP, err := podIPOfFamilyOnPrimaryNetwork(
1408+
podIP, err := getPodAnnotationIPsForPrimaryNetworkByIPFamily(
14091409
f.ClientSet,
14101410
testPod.Namespace,
14111411
testPod.Name,
@@ -1448,7 +1448,7 @@ var _ = ginkgo.Describe("BGP: For a VRF-Lite configured network", func() {
14481448
e2eskipper.Skipf("IP family %v not supported", family)
14491449
}
14501450
ginkgo.By("Ensuring a request from the first pod can reach the second pod")
1451-
otherPodIP, err := podIPOfFamilyOnPrimaryNetwork(
1451+
otherPodIP, err := getPodAnnotationIPsForPrimaryNetworkByIPFamily(
14521452
f.ClientSet,
14531453
otherPod.Namespace,
14541454
otherPod.Name,
@@ -1597,7 +1597,7 @@ var _ = ginkgo.Describe("BGP: For a VRF-Lite configured network", func() {
15971597
e2eskipper.Skipf("IP family %v not supported", family)
15981598
}
15991599
ginkgo.By("Ensuring a request from the tested network pod cannot reach the other network pod")
1600-
otherPodIP, err := podIPOfFamilyOnPrimaryNetwork(
1600+
otherPodIP, err := getPodAnnotationIPsForPrimaryNetworkByIPFamily(
16011601
f.ClientSet,
16021602
otherPod.Namespace,
16031603
otherPod.Name,
@@ -1618,7 +1618,7 @@ var _ = ginkgo.Describe("BGP: For a VRF-Lite configured network", func() {
16181618
e2eskipper.Skipf("IP family %v not supported", family)
16191619
}
16201620
ginkgo.By("Ensuring a request from the other network pod cannot reach the tested network pod")
1621-
testPodIP, err := podIPOfFamilyOnPrimaryNetwork(
1621+
testPodIP, err := getPodAnnotationIPsForPrimaryNetworkByIPFamily(
16221622
f.ClientSet,
16231623
testPod.Namespace,
16241624
testPod.Name,

test/e2e/util.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,23 +1516,3 @@ func executeFileTemplate(templates *template.Template, directory, name string, d
15161516
}
15171517
return nil
15181518
}
1519-
1520-
// podIPsForUserDefinedPrimaryNetwork returns the v4 or v6 IPs for a pod on the UDN
1521-
func podIPOfFamilyOnPrimaryNetwork(k8sClient kubernetes.Interface, podNamespace string, podName string, networkName string, family utilnet.IPFamily) (string, error) {
1522-
pod, err := k8sClient.CoreV1().Pods(podNamespace).Get(context.Background(), podName, metav1.GetOptions{})
1523-
if err != nil {
1524-
return "", err
1525-
}
1526-
if networkName != "default" {
1527-
networkName = namespacedName(podNamespace, networkName)
1528-
}
1529-
netStatus, err := userDefinedNetworkStatus(pod, networkName)
1530-
if err != nil {
1531-
return "", err
1532-
}
1533-
ipnet := getFirstCIDROfFamily(family, netStatus.IPs)
1534-
if ipnet == nil {
1535-
return "", nil
1536-
}
1537-
return ipnet.IP.String(), nil
1538-
}

0 commit comments

Comments
 (0)