From ddeccee02e097783d3552b1ea696258cfcfac8c1 Mon Sep 17 00:00:00 2001 From: Sridhar Gaddam Date: Tue, 29 Aug 2023 20:49:56 +0530 Subject: [PATCH] Improve podCIDR detection In K8s, each node is typically assigned a unique PodCIDR range for the pods that run on that node. Each node's PodCIDR is used to allocate IP addresses to the pods scheduled on that node. Only if the cluster is a single node deployment, we should rely on the node.Spec.PodCIDR as podCIDR of the cluster. Related to: https://github.com/submariner-io/submariner/issues/2128 Signed-off-by: Sridhar Gaddam --- pkg/discovery/network/generic.go | 9 +++++---- pkg/discovery/network/generic_test.go | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pkg/discovery/network/generic.go b/pkg/discovery/network/generic.go index 05199755a..6e669f34f 100644 --- a/pkg/discovery/network/generic.go +++ b/pkg/discovery/network/generic.go @@ -191,10 +191,11 @@ func findPodIPRangeFromNodeSpec(ctx context.Context, client controllerClient.Cli } func parseToPodCidr(nodes []corev1.Node) (string, error) { - for i := range nodes { - if nodes[i].Spec.PodCIDR != "" { - return nodes[i].Spec.PodCIDR, nil - } + // In K8s, each node is typically assigned a unique PodCIDR range for the pods that run on that node. + // Each node's PodCIDR is used to allocate IP addresses to the pods scheduled on that node. Only if + // the cluster is a single node deployment, we should rely on the node.Spec.PodCIDR as podCIDR of the cluster. + if len(nodes) == 1 { + return nodes[0].Spec.PodCIDR, nil } return "", nil diff --git a/pkg/discovery/network/generic_test.go b/pkg/discovery/network/generic_test.go index ab7136471..2cfa9fa8e 100644 --- a/pkg/discovery/network/generic_test.go +++ b/pkg/discovery/network/generic_test.go @@ -222,13 +222,12 @@ var _ = Describe("Generic Network", func() { }) }) - When("Pod CIDR information exists on a node", func() { + When("Pod CIDR information exists on a single node cluster", func() { var clusterNet *network.ClusterNetwork BeforeEach(func() { clusterNet = testDiscoverGenericWith( - fakeNode("node1", ""), - fakeNode("node2", testPodCIDR), + fakeNode("node1", testPodCIDR), ) }) @@ -245,6 +244,21 @@ var _ = Describe("Generic Network", func() { }) }) + When("Pod CIDR information exists on a multi node cluster", func() { + var clusterNet *network.ClusterNetwork + + BeforeEach(func() { + clusterNet = testDiscoverGenericWith( + fakeNode("node1", testPodCIDR), + fakeNode("node2", testPodCIDR), + ) + }) + + It("Should return an empty ClusterNetwork structure with the pod CIDR", func() { + Expect(clusterNet.PodCIDRs).To(BeEmpty()) + }) + }) + When("Both pod and service CIDR information exists", func() { var clusterNet *network.ClusterNetwork