Skip to content

Commit adcc255

Browse files
combine methods
Signed-off-by: Mladjan Gadzic <[email protected]>
1 parent be88301 commit adcc255

File tree

2 files changed

+407
-40
lines changed

2 files changed

+407
-40
lines changed

pkg/daemon/daemon.go

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ func NewDaemon() (Daemon, error) {
154154
guidPodNetworkMap: make(map[string]string),
155155
}
156156

157-
// Reset guid pool with already allocated guids to avoid collisions
158-
err = daemonInstance.syncGUIDPool()
157+
// Initialize guid pool with existing pods and sync with subnet manager
158+
err = daemonInstance.initGUIDPool()
159159
if err != nil {
160160
return nil, err
161161
}
@@ -469,7 +469,7 @@ func (d *daemon) processNetworkGUID(
469469
switch err {
470470
// If the guid pool is exhausted, need to sync with SM in case there are unsynced changes
471471
case guid.ErrGUIDPoolExhausted:
472-
err = d.syncGUIDPool()
472+
err = d.syncWithSubnetManager()
473473
if err != nil {
474474
return err
475475
}
@@ -541,37 +541,6 @@ func (d *daemon) removeStaleGUID(allocatedGUID, existingPkey string) error {
541541
return nil
542542
}
543543

544-
func (d *daemon) syncGUIDPool() error {
545-
usedGuids, err := d.smClient.ListGuidsInUse()
546-
if err != nil {
547-
return err
548-
}
549-
550-
// Reset guid pool with already allocated guids to avoid collisions
551-
err = d.guidPool.Reset(usedGuids)
552-
if err != nil {
553-
return err
554-
}
555-
556-
// Remove stale GUIDs that are no longer in use by the subnet manager
557-
// This handles cleanup of GUIDs from deleted/finished pods
558-
for allocatedGUID, podNetworkID := range d.guidPodNetworkMap {
559-
if _, found := usedGuids[allocatedGUID]; !found {
560-
// If GUID is not found in the subnet manager's list of used GUIDs,
561-
// it means the pod was deleted/finished and we should clean it up
562-
log.Info().Msgf("removing stale GUID %s for pod network %s", allocatedGUID, podNetworkID)
563-
if err = d.guidPool.ReleaseGUID(allocatedGUID); err != nil {
564-
log.Warn().Msgf("failed to release stale guid \"%s\" with error: %v", allocatedGUID, err)
565-
} else {
566-
delete(d.guidPodNetworkMap, allocatedGUID)
567-
log.Info().Msgf("successfully cleaned up stale GUID %s", allocatedGUID)
568-
}
569-
}
570-
}
571-
572-
return nil
573-
}
574-
575544
// Update and set Pod's network annotation.
576545
// If failed to update annotation, pod's GUID added into the list to be removed from Pkey.
577546
func (d *daemon) updatePodNetworkAnnotation(pi *podNetworkInfo, removedList *[]net.HardwareAddr, pkey string) error {
@@ -835,11 +804,12 @@ func (d *daemon) DeletePeriodicUpdate() {
835804
log.Info().Msg("delete periodic update finished")
836805
}
837806

838-
// initPool check the guids that are already allocated by the running pods
839-
func (d *daemon) initPool() error {
807+
// initGUIDPool initializes the GUID pool by first populating guidPodNetworkMap with existing pods,
808+
// then syncing with subnet manager and cleaning up stale GUIDs
809+
func (d *daemon) initGUIDPool() error {
840810
log.Info().Msg("Initializing GUID pool.")
841811

842-
// Try to get pod list from k8s client in backoff loop
812+
// First populate guidPodNetworkMap with existing pods
843813
var pods *kapi.PodList
844814
if err := wait.ExponentialBackoff(backoffValues, func() (bool, error) {
845815
var err error
@@ -894,5 +864,67 @@ func (d *daemon) initPool() error {
894864
}
895865
}
896866

867+
// Now sync with subnet manager and clean up stale GUIDs
868+
usedGuids, err := d.smClient.ListGuidsInUse()
869+
if err != nil {
870+
return err
871+
}
872+
873+
// Reset guid pool with already allocated guids to avoid collisions
874+
err = d.guidPool.Reset(usedGuids)
875+
if err != nil {
876+
return err
877+
}
878+
879+
// Remove stale GUIDs that are no longer in use by the subnet manager
880+
// This handles cleanup of GUIDs from deleted/finished pods
881+
// Now guidPodNetworkMap is populated, so this cleanup will work correctly
882+
for allocatedGUID, podNetworkID := range d.guidPodNetworkMap {
883+
if _, found := usedGuids[allocatedGUID]; !found {
884+
// If GUID is not found in the subnet manager's list of used GUIDs,
885+
// it means the pod was deleted/finished and we should clean it up
886+
log.Info().Msgf("removing stale GUID %s for pod network %s", allocatedGUID, podNetworkID)
887+
if err = d.guidPool.ReleaseGUID(allocatedGUID); err != nil {
888+
log.Warn().Msgf("failed to release stale guid \"%s\" with error: %v", allocatedGUID, err)
889+
} else {
890+
delete(d.guidPodNetworkMap, allocatedGUID)
891+
log.Info().Msgf("successfully cleaned up stale GUID %s", allocatedGUID)
892+
}
893+
}
894+
}
895+
896+
return nil
897+
}
898+
899+
// syncWithSubnetManager syncs the GUID pool with the subnet manager during runtime
900+
// This is used when the pool is exhausted and we need to check for available GUIDs
901+
func (d *daemon) syncWithSubnetManager() error {
902+
usedGuids, err := d.smClient.ListGuidsInUse()
903+
if err != nil {
904+
return err
905+
}
906+
907+
// Reset guid pool with already allocated guids to avoid collisions
908+
err = d.guidPool.Reset(usedGuids)
909+
if err != nil {
910+
return err
911+
}
912+
913+
// Remove stale GUIDs that are no longer in use by the subnet manager
914+
// This handles cleanup of GUIDs from deleted/finished pods during runtime
915+
for allocatedGUID, podNetworkID := range d.guidPodNetworkMap {
916+
if _, found := usedGuids[allocatedGUID]; !found {
917+
// If GUID is not found in the subnet manager's list of used GUIDs,
918+
// it means the pod was deleted/finished and we should clean it up
919+
log.Info().Msgf("removing stale GUID %s for pod network %s", allocatedGUID, podNetworkID)
920+
if err = d.guidPool.ReleaseGUID(allocatedGUID); err != nil {
921+
log.Warn().Msgf("failed to release stale guid \"%s\" with error: %v", allocatedGUID, err)
922+
} else {
923+
delete(d.guidPodNetworkMap, allocatedGUID)
924+
log.Info().Msgf("successfully cleaned up stale GUID %s", allocatedGUID)
925+
}
926+
}
927+
}
928+
897929
return nil
898930
}

0 commit comments

Comments
 (0)