Skip to content

Commit

Permalink
User podRef as indentifier in IP allocation/deallocation
Browse files Browse the repository at this point in the history
The containerID of a pod will change after node reboot. It caused
the IP allocation/deallocation operation cannot find the IP that
has been reserved by the pod.

Signed-off-by: Peng Liu <[email protected]>
  • Loading branch information
pliurh committed Mar 7, 2024
1 parent 6354e57 commit 3a84167
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions pkg/allocate/allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func AssignIP(ipamConf types.RangeConfiguration, reservelist []types.IPReservati
}

// DeallocateIP assigns an IP using a range and a reserve list.
func DeallocateIP(reservelist []types.IPReservation, containerID string) ([]types.IPReservation, net.IP, error) {
func DeallocateIP(reservelist []types.IPReservation, podRef string) ([]types.IPReservation, net.IP, error) {

updatedreservelist, hadip, err := IterateForDeallocation(reservelist, containerID, getMatchingIPReservationIndex)
updatedreservelist, hadip, err := IterateForDeallocation(reservelist, podRef, getMatchingIPReservationIndex)
if err != nil {
return nil, nil, err
}
Expand All @@ -52,13 +52,13 @@ func DeallocateIP(reservelist []types.IPReservation, containerID string) ([]type
// IterateForDeallocation iterates overs currently reserved IPs and the deallocates given the container id.
func IterateForDeallocation(
reservelist []types.IPReservation,
containerID string,
podRef string,
matchingFunction func(reservation []types.IPReservation, id string) int) ([]types.IPReservation, net.IP, error) {

foundidx := matchingFunction(reservelist, containerID)
foundidx := matchingFunction(reservelist, podRef)
// Check if it's a valid index
if foundidx < 0 {
return reservelist, nil, fmt.Errorf("did not find reserved IP for container %v", containerID)
return reservelist, nil, fmt.Errorf("did not find reserved IP for container %v", podRef)
}

returnip := reservelist[foundidx].IP
Expand All @@ -70,7 +70,7 @@ func IterateForDeallocation(
func getMatchingIPReservationIndex(reservelist []types.IPReservation, id string) int {
foundidx := -1
for idx, v := range reservelist {
if v.ContainerID == id {
if v.PodRef == id {
foundidx = idx
break
}
Expand Down Expand Up @@ -100,9 +100,9 @@ func IterateForAssignment(ipnet net.IPNet, rangeStart net.IP, rangeEnd net.IP, r
rangeStart, rangeEnd, ipnet, firstIP, lastIP)

// Build reserved map.
reserved := make(map[string]bool)
reserved := make(map[string]string)
for _, r := range reserveList {
reserved[r.IP.String()] = true
reserved[r.IP.String()] = r.PodRef
}

// Build excluded list, "192.168.2.229/30", "192.168.1.229/30".
Expand All @@ -118,8 +118,13 @@ func IterateForAssignment(ipnet net.IPNet, rangeStart net.IP, rangeEnd net.IP, r
// Iterate over every IP address in the range, accounting for reserved IPs and exclude ranges. Make sure that ip is
// within ipnet, and make sure that ip is smaller than lastIP.
for ip := firstIP; ipnet.Contains(ip) && iphelpers.CompareIPs(ip, lastIP) <= 0; ip = iphelpers.IncIP(ip) {
// If already reserved, skip it.
if reserved[ip.String()] {
if podRefReserved, ok := reserved[ip.String()]; ok {
if podRefReserved == podRef {
// If this IP has been reserved for this pod, return it.
logging.Debugf("Returning reserved IP: |%v|", ip.String()+" "+podRef)
return ip, reserveList, nil
}
// If already reserved, skip it.
continue
}
// If this IP is within the range of one of the excluded subnets, jump to the exluded subnet's broadcast address
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/kubernetes/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func IPManagementKubernetesUpdate(ctx context.Context, mode int, ipam *Kubernete
}

case whereaboutstypes.Deallocate:
updatedreservelist, ipforoverlappingrangeupdate, err = allocate.DeallocateIP(reservelist, containerID)
updatedreservelist, ipforoverlappingrangeupdate, err = allocate.DeallocateIP(reservelist, podRef)
if err != nil {
logging.Errorf("Error deallocating IP: %v", err)
return newips, err
Expand Down

0 comments on commit 3a84167

Please sign in to comment.