in pkg/lbcontroller/lbcontroller.go [110:153]
func (c *LBController) AssignIPToNode(ctx context.Context, nodeName, volumeID string) (string, error) {
node, err := c.nodeLister.Get(nodeName)
if err != nil {
return "", err
}
c.mutex.Lock()
defer c.mutex.Unlock()
if ip, exists := node.Annotations[NodeAnnotation]; exists {
klog.Infof("Node %q already have IP %q assigned", node.Name, ip)
if _, exists := c.ipMap[ip]; exists {
return ip, nil
}
klog.V(5).Infof("IP %q not found among the NFS server IP list. Reassigning a new IP to node %q", ip, node.Name)
}
// Sort IPs by their current count.
ips := make([]string, 0, len(c.ipMap))
for ip := range c.ipMap {
ips = append(ips, ip)
}
sort.Slice(ips, func(i, j int) bool {
return c.ipMap[ips[i]] < c.ipMap[ips[j]]
})
selectedIP := ips[0]
nodeCopy := node.DeepCopy()
if node.Annotations == nil {
nodeCopy.Annotations = make(map[string]string)
}
nodeCopy.Annotations[NodeAnnotation] = selectedIP
klog.V(5).Infof("Assigning IP %q to node %q for volume %q", selectedIP, node.Name, volumeID)
_, err = c.clientset.CoreV1().Nodes().Update(ctx, nodeCopy, metav1.UpdateOptions{})
if err != nil {
return "", fmt.Errorf("failed to assign IP %q to node %q: %v", selectedIP, node.Name, err)
}
c.ipMap[selectedIP]++
klog.V(6).Infof("AssignIPToNode: For volume %q, node %q, IP updated %q, LB controller IP map %v", volumeID, nodeName, selectedIP, c.ipMap)
return selectedIP, nil
}