Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve podCIDR detection #2770

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/discovery/network/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 17 additions & 3 deletions pkg/discovery/network/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
})

Expand All @@ -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

Expand Down
Loading