Skip to content

Commit 161d27b

Browse files
Arjun Baindurxagent003
authored andcommitted
Reconciler optimization
1 parent 0f434e4 commit 161d27b

File tree

1 file changed

+46
-40
lines changed

1 file changed

+46
-40
lines changed

pkg/reconciler/iploop.go

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (rl *ReconcileLooper) findOrphanedIPsPerPool(ipPools []storage.IPPool) erro
7575
_ = logging.Errorf("pod ref missing for Allocations: %s", ipReservation)
7676
continue
7777
}
78-
if !rl.isOrphanedIP(ipReservation.PodRef, ipReservation.IP.String()) {
78+
if rl.isOrphanedIP(ipReservation.PodRef, ipReservation.IP.String()) {
7979
logging.Debugf("pod ref %s is not listed in the live pods list", ipReservation.PodRef)
8080
orphanIP.Allocations = append(orphanIP.Allocations, ipReservation)
8181
}
@@ -88,49 +88,55 @@ func (rl *ReconcileLooper) findOrphanedIPsPerPool(ipPools []storage.IPPool) erro
8888
return nil
8989
}
9090

91-
func (rl ReconcileLooper) isOrphanedIP(podRef string, ip string) bool {
92-
for livePodRef, livePod := range rl.liveWhereaboutsPods {
93-
if podRef == livePodRef {
94-
isFound := isIpOnPod(&livePod, podRef, ip)
95-
if !isFound && (livePod.phase == v1.PodPending) {
96-
/* Sometimes pods are still coming up, and may not yet have Multus
97-
* annotation added to it yet. We don't want to check the IPs yet
98-
* so re-fetch the Pod 5x
99-
*/
100-
podToMatch := &livePod
101-
retries := 0
102-
103-
logging.Debugf("Re-fetching Pending Pod: %s IP-to-match: %s", livePodRef, ip)
104-
105-
for retries < storage.PodRefreshRetries {
106-
retries += 1
107-
podToMatch = rl.refreshPod(livePodRef)
108-
if podToMatch == nil {
109-
logging.Debugf("Cleaning up...")
110-
return false
111-
} else if podToMatch.phase != v1.PodPending {
112-
logging.Debugf("Pending Pod is now in phase: %s", podToMatch.phase)
113-
break
114-
} else {
115-
isFound = isIpOnPod(podToMatch, podRef, ip)
116-
// Short-circuit - Pending Pod may have IP now
117-
if isFound {
118-
logging.Debugf("Pod now has IP annotation while in Pending")
119-
return true
120-
}
121-
time.Sleep(time.Duration(250) * time.Millisecond)
122-
}
91+
func (rl *ReconcileLooper) isOrphanedIP(podRef string, ip string) bool {
92+
livePod, exists := rl.liveWhereaboutsPods[podRef]
93+
if !exists {
94+
logging.Debugf("Pod %s not found in live pod list, IP %s is orphaned", podRef, ip)
95+
return true
96+
}
97+
98+
isIPFoundOnPod := isIpOnPod(&livePod, podRef, ip)
99+
if isIPFoundOnPod {
100+
return false
101+
}
102+
103+
if livePod.phase == v1.PodPending {
104+
podToMatch := &livePod
105+
retries := 0
106+
107+
logging.Debugf("Re-fetching Pending Pod: %s IP-to-match: %s", podRef, ip)
108+
109+
for retries < storage.PodRefreshRetries {
110+
retries++
111+
podToMatch = rl.refreshPod(podRef)
112+
if podToMatch == nil {
113+
logging.Debugf("Pod refresh returned nil, IP is orphaned")
114+
return true
115+
} else if podToMatch.phase != v1.PodPending {
116+
logging.Debugf("Pending Pod is now in phase: %s", podToMatch.phase)
117+
break
118+
} else {
119+
isIPFoundOnPod = isIpOnPod(podToMatch, podRef, ip)
120+
if isIPFoundOnPod {
121+
logging.Debugf("Found IP on refreshed pending pod, not orphaned")
122+
return false
123123
}
124-
isFound = isIpOnPod(podToMatch, podRef, ip)
124+
time.Sleep(time.Duration(500) * time.Millisecond)
125125
}
126+
}
126127

127-
return isFound
128+
isIPFoundOnPod = isIpOnPod(podToMatch, podRef, ip)
129+
if isIPFoundOnPod {
130+
// IP is found on pod after retries, so it's NOT orphaned
131+
return false
128132
}
129133
}
130-
return false
134+
135+
// IP is not found on pod, so it IS orphaned
136+
return true
131137
}
132138

133-
func (rl ReconcileLooper) refreshPod(podRef string) *podWrapper {
139+
func (rl *ReconcileLooper) refreshPod(podRef string) *podWrapper {
134140
namespace, podName := splitPodRef(podRef)
135141
if namespace == "" || podName == "" {
136142
logging.Errorf("Invalid podRef format: %s", podRef)
@@ -162,7 +168,7 @@ func composePodRef(pod v1.Pod) string {
162168
return fmt.Sprintf("%s/%s", pod.GetNamespace(), pod.GetName())
163169
}
164170

165-
func (rl ReconcileLooper) ReconcileIPPools() ([]net.IP, error) {
171+
func (rl *ReconcileLooper) ReconcileIPPools() ([]net.IP, error) {
166172
findAllocationIndex := func(reservation types.IPReservation, reservations []types.IPReservation) int {
167173
for idx, r := range reservations {
168174
if r.PodRef == reservation.PodRef && r.IP.Equal(reservation.IP) {
@@ -225,7 +231,7 @@ func (rl *ReconcileLooper) findClusterWideIPReservations() error {
225231

226232
podRef := clusterWideIPReservation.Spec.PodRef
227233

228-
if !rl.isOrphanedIP(podRef, denormalizedip) {
234+
if rl.isOrphanedIP(podRef, denormalizedip) {
229235
logging.Debugf("pod ref %s is not listed in the live pods list", podRef)
230236
rl.orphanedClusterWideIPs = append(rl.orphanedClusterWideIPs, clusterWideIPReservation)
231237
}
@@ -234,7 +240,7 @@ func (rl *ReconcileLooper) findClusterWideIPReservations() error {
234240
return nil
235241
}
236242

237-
func (rl ReconcileLooper) ReconcileOverlappingIPAddresses() error {
243+
func (rl *ReconcileLooper) ReconcileOverlappingIPAddresses() error {
238244
var failedReconciledClusterWideIPs []string
239245

240246
for _, overlappingIPStruct := range rl.orphanedClusterWideIPs {

0 commit comments

Comments
 (0)