Skip to content

Commit

Permalink
Use /14 for pods and services cidr
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelvl authored Jan 29, 2025
1 parent d18e537 commit 29c80fd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
41 changes: 32 additions & 9 deletions cmd/controller/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,12 @@ func (c *Client) GetClusterInfo(ctx context.Context) (*ClusterInfo, error) {
var res ClusterInfo
// Try to find pods cidr from nodes.
for _, node := range c.index.nodesByName {
res.PodCidr = getPodCidrFromNodeSpec(node)
if len(res.PodCidr) > 0 {
podCidr, err := getPodCidrFromNodeSpec(node)
if err != nil {
return nil, err
}
if len(podCidr) > 0 {
res.PodCidr = podCidr
break
}
}
Expand Down Expand Up @@ -310,12 +314,21 @@ func (c *Client) GetClusterInfo(ctx context.Context) (*ClusterInfo, error) {
return &res, nil
}

func getPodCidrFromNodeSpec(node *corev1.Node) []string {
podCidrs := node.Spec.PodCIDRs
if len(podCidrs) == 0 && node.Spec.PodCIDR != "" {
podCidrs = []string{node.Spec.PodCIDR}
func getPodCidrFromNodeSpec(node *corev1.Node) ([]string, error) {
nodeCidrs := node.Spec.PodCIDRs
if len(nodeCidrs) == 0 && node.Spec.PodCIDR != "" {
nodeCidrs = []string{node.Spec.PodCIDR}
}
return podCidrs
var podCidrs []string
for _, cidr := range nodeCidrs {
subnet, err := netip.ParsePrefix(cidr)
if err != nil {
return nil, fmt.Errorf("parsing pod cidr: %w", err)
}
prefix := prefixLength(subnet.Addr())
podCidrs = append(podCidrs, netip.PrefixFrom(subnet.Addr(), prefix).String())
}
return podCidrs, nil
}

func getPodCidrFromPodSpec(pod *corev1.Pod) ([]string, error) {
Expand Down Expand Up @@ -395,7 +408,16 @@ func discoverServiceCidr(ctx context.Context, client kubernetes.Interface, ip, n
}
return nil, err
}
return strings.Split(match[1], ","), nil
var servicesCidr []string
for _, cidr := range strings.Split(match[1], ",") {
subnet, err := netip.ParsePrefix(cidr)
if err != nil {
return nil, fmt.Errorf("parsing service cidr: %w", err)
}
prefix := prefixLength(subnet.Addr())
servicesCidr = append(servicesCidr, netip.PrefixFrom(subnet.Addr(), prefix).String())
}
return servicesCidr, nil
}

func parseIP(ip string) (string, error) {
Expand All @@ -411,11 +433,12 @@ func parseIP(ip string) (string, error) {
return cidr.String(), nil
}

// prefixLength returns the absolute CIDR for IPv4 and IPv6 addresses
func prefixLength(addr netip.Addr) int {
if addr.Is6() {
return 48
}
return 16
return 14
}

type ImageDetails struct {
Expand Down
6 changes: 3 additions & 3 deletions cmd/controller/kube/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestServer(t *testing.T) {
case "0.0.0.0":
return true, svc, fmt.Errorf("The range of valid IPs is 10.30.0.0/16")
case "::":
return true, svc, fmt.Errorf("The range of valid IPs is fd01::/48")
return true, svc, fmt.Errorf("The range of valid IPs is fd01::/64")
}
return false, nil, nil
})
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestServer(t *testing.T) {
r := require.New(t)
resp, err := srv.GetClusterInfo(ctx, &kubepb.GetClusterInfoRequest{})
r.NoError(err)
r.ElementsMatch([]string{"10.10.10.0/24", "fd00::/64"}, resp.PodsCidr)
r.ElementsMatch([]string{"10.30.0.0/16", "fd01::/48"}, resp.ServiceCidr)
r.ElementsMatch([]string{"10.10.10.0/14", "fd00::/48"}, resp.PodsCidr)
r.ElementsMatch([]string{"10.30.0.0/14", "fd01::/48"}, resp.ServiceCidr)
})
}

0 comments on commit 29c80fd

Please sign in to comment.