func()

in pkg/metrics/collector/pod_ip_metrics.go [351:383]


func (c *podIPMetricsCollector) calculateAssignedIP() error {
	node, err := c.clientset.CoreV1().Nodes().Get(context.Background(), c.nodeName, metav1.GetOptions{})
	if err != nil {
		return fmt.Errorf("error getting node %s: %v", c.nodeName, err)
	}

	podCIDRs := node.Spec.PodCIDRs
	if len(podCIDRs) == 0 {
		if node.Spec.PodCIDR == "" {
			return fmt.Errorf("both podCIDR and podCIDRs are empty")
		}
		podCIDRs = []string{node.Spec.PodCIDR}
	}
	var firstErr error
	for _, podCIDR := range podCIDRs {
		_, subnet, err := net.ParseCIDR(podCIDR)
		if err != nil {
			if firstErr == nil {
				firstErr = fmt.Errorf("error parsing podCIDR %s: %v", podCIDR, err)
			}
			continue
		}
		totalIP, err := c.countIPsFromRange(subnet)
		if err != nil {
			if firstErr == nil {
				firstErr = fmt.Errorf("error calculating total IPs for subnet %s: %v", subnet.IP, err)
			}
			continue
		}
		c.updateAssignedIPs(subnet, totalIP)
	}
	return firstErr
}