@@ -42,24 +42,24 @@ func (np *NetworkDriver) Synchronize(_ context.Context, pods []*api.PodSandbox,
4242 klog .Infof ("Synchronized state with the runtime (%d pods, %d containers)..." ,
4343 len (pods ), len (containers ))
4444
45- livePods := set .New [types.UID ]()
45+ // livePodNetNs map tracks live pods by UID and their network namespace paths.
46+ livePodNetNs := make (map [types.UID ]string )
4647 for _ , pod := range pods {
4748 klog .Infof ("Synchronize Pod %s/%s UID %s" , pod .Namespace , pod .Name , pod .Uid )
4849 klog .V (2 ).Infof ("pod %s/%s: namespace=%s ips=%v" , pod .GetNamespace (), pod .GetName (), getNetworkNamespace (pod ), pod .GetIps ())
49- livePods .Insert (types .UID (pod .Uid ))
50- // get the pod network namespace
51- ns := getNetworkNamespace (pod )
52- // host network pods are skipped
53- if ns != "" {
54- // store the Pod metadata in the db
55- np .netdb .AddPodNetNs (podKey (pod ), ns )
56- }
50+ livePodNetNs [types .UID (pod .Uid )] = getNetworkNamespace (pod )
5751 }
5852
59- // Prune persisted configs for pods that no longer exist in the runtime.
60- // This handles the case where pods were deleted while the driver was down .
53+ // Process stored pods: update NetNS for live pods, and prune configurations
54+ // for pods that no longer exist in the runtime .
6155 for _ , storedUID := range np .podConfigStore .ListPods () {
62- if ! livePods .Has (storedUID ) {
56+ ns , isLive := livePodNetNs [storedUID ]
57+
58+ if isLive {
59+ if ns != "" {
60+ np .podConfigStore .SetPodNetNs (storedUID , ns )
61+ }
62+ } else {
6363 klog .Infof ("Synchronize: pruning stale config for pod %s" , storedUID )
6464 np .podConfigStore .DeletePod (storedUID )
6565 }
@@ -152,8 +152,8 @@ func (np *NetworkDriver) runPodSandbox(_ context.Context, pod *api.PodSandbox, p
152152 if ns == "" {
153153 return fmt .Errorf ("RunPodSandbox pod %s/%s using host network can not claim host devices" , pod .Namespace , pod .Name )
154154 }
155- // store the Pod metadata in the db
156- np .netdb . AddPodNetNs ( podKey (pod ), ns )
155+ // store the Pod network namespace in the pod config store
156+ np .podConfigStore . SetPodNetNs ( types . UID (pod . GetUid () ), ns )
157157
158158 // Track all the status updates needed for the resource claims of the pod.
159159 statusUpdates := map [types.NamespacedName ]* resourceapply.ResourceClaimStatusApplyConfiguration {}
@@ -363,18 +363,23 @@ func (np *NetworkDriver) StopPodSandbox(ctx context.Context, pod *api.PodSandbox
363363}
364364
365365func (np * NetworkDriver ) stopPodSandbox (_ context.Context , pod * api.PodSandbox , podConfig PodConfig ) error {
366- defer func () {
367- np .netdb .RemovePodNetNs (podKey (pod ))
368- }()
369366 // get the pod network namespace
370367 ns := getNetworkNamespace (pod )
371368 if ns == "" {
372369 // some version of containerd does not send the network namespace information on this hook so
373370 // we workaround it using the local copy we have in the db to associate interfaces with Pods via
374371 // the network namespace id.
375- ns = np .netdb .GetPodNetNs (podKey (pod ))
376- if ns == "" {
377- klog .Infof ("StopPodSandbox pod %s/%s using host network ... skipping" , pod .Namespace , pod .Name )
372+ storedNs , ok := np .podConfigStore .GetPodNetNs (types .UID (pod .GetUid ()))
373+ if ok {
374+ if storedNs != "" {
375+ ns = storedNs
376+ } else {
377+ // Pod is not configured for DRAnet (host network or other driver)
378+ klog .Infof ("StopPodSandbox pod %s/%s using host network ... skipping" , pod .Namespace , pod .Name )
379+ return nil
380+ }
381+ } else {
382+ klog .Warningf ("StopPodSandbox: pod %s/%s (UID %s) not found in podConfigStore when fetching fallback NetNS" , pod .Namespace , pod .Name , pod .Uid )
378383 return nil
379384 }
380385 }
@@ -452,7 +457,6 @@ func (np *NetworkDriver) RemovePodSandbox(ctx context.Context, pod *api.PodSandb
452457}
453458
454459func (np * NetworkDriver ) removePodSandbox (_ context.Context , pod * api.PodSandbox ) error {
455- np .netdb .RemovePodNetNs (podKey (pod ))
456460 return nil
457461}
458462
0 commit comments