@@ -159,13 +159,16 @@ func (r *Actor) selectCN(ctx *recon.Context[*v1alpha1.CNClaim], orphans []corev1
159159 }
160160
161161 sortCNByPriority (c , idleCNs )
162+ // build index once: podName -> claimName for all other CNClaims
163+ claimIndex , err := buildPodClaimIndex (ctx , c .Namespace , c .Name )
164+ if err != nil {
165+ return nil , errors .WrapPrefix (err , "error building pod claim index" , 0 )
166+ }
162167 for i := range idleCNs {
163168 pod := & idleCNs [i ]
164169 // skip pod already referenced by another CNClaim's spec.podName
165- if claimed , err := podClaimedByOthers (ctx , c .Namespace , pod .Name , c .Name ); err != nil {
166- return nil , errors .WrapPrefix (err , "error checking pod claims" , 0 )
167- } else if claimed {
168- ctx .Log .Info ("skip pod claimed by other CNClaim" , "podName" , pod .Name )
170+ if holder , ok := claimIndex [pod .Name ]; ok {
171+ ctx .Log .Info ("skip pod claimed by other CNClaim" , "podName" , pod .Name , "holder" , holder )
169172 continue
170173 }
171174 if err := r .ensureOwnership (ctx , pod ); err != nil {
@@ -347,13 +350,16 @@ func (r *Actor) Finalize(ctx *recon.Context[*v1alpha1.CNClaim]) (bool, error) {
347350 if len (ownedCNs ) == 0 {
348351 return true , nil
349352 }
353+ // build index once: podName -> claimName for all other CNClaims
354+ claimIndex , err := buildPodClaimIndex (ctx , c .Namespace , c .Name )
355+ if err != nil {
356+ return false , errors .WrapPrefix (err , "error building pod claim index" , 0 )
357+ }
350358 for i := range ownedCNs {
351359 cn := ownedCNs [i ]
352360 // skip reclaim if another CNClaim still references this pod via spec.podName
353- if claimed , err := podClaimedByOthers (ctx , c .Namespace , cn .Name , c .Name ); err != nil {
354- return false , errors .WrapPrefix (err , "error checking pod claims" , 0 )
355- } else if claimed {
356- ctx .Log .Info ("skip reclaim, pod still claimed by other CNClaim" , "pod" , cn .Name )
361+ if holder , ok := claimIndex [cn .Name ]; ok {
362+ ctx .Log .Info ("skip reclaim, pod still claimed by other CNClaim" , "pod" , cn .Name , "holder" , holder )
357363 continue
358364 }
359365 ctx .Log .Info ("finalize CNClaim, reclaim bound CN" , "cn" , cn .Name )
@@ -366,14 +372,15 @@ func (r *Actor) Finalize(ctx *recon.Context[*v1alpha1.CNClaim]) (bool, error) {
366372
367373// podClaimedByOthers checks if the given pod is referenced by any CNClaim's
368374// spec.podName other than excludeClaim in the same namespace.
375+ // It skips CNClaims that are being deleted (DeletionTimestamp != nil).
369376func podClaimedByOthers (cli recon.KubeClient , namespace , podName , excludeClaim string ) (bool , error ) {
370377 claimList := & v1alpha1.CNClaimList {}
371378 if err := cli .List (claimList , client .InNamespace (namespace )); err != nil {
372379 return false , err
373380 }
374381 for i := range claimList .Items {
375382 claim := & claimList .Items [i ]
376- if claim .Name == excludeClaim {
383+ if claim .Name == excludeClaim || claim . DeletionTimestamp != nil {
377384 continue
378385 }
379386 if claim .Spec .PodName == podName {
@@ -383,6 +390,25 @@ func podClaimedByOthers(cli recon.KubeClient, namespace, podName, excludeClaim s
383390 return false , nil
384391}
385392
393+ // buildPodClaimIndex lists all CNClaims in the namespace and returns a map
394+ // from podName to the claiming CNClaim name, excluding the given claim and
395+ // CNClaims that are being deleted.
396+ func buildPodClaimIndex (cli recon.KubeClient , namespace , excludeClaim string ) (map [string ]string , error ) {
397+ claimList := & v1alpha1.CNClaimList {}
398+ if err := cli .List (claimList , client .InNamespace (namespace )); err != nil {
399+ return nil , err
400+ }
401+ index := make (map [string ]string , len (claimList .Items ))
402+ for i := range claimList .Items {
403+ claim := & claimList .Items [i ]
404+ if claim .Name == excludeClaim || claim .DeletionTimestamp != nil || claim .Spec .PodName == "" {
405+ continue
406+ }
407+ index [claim .Spec .PodName ] = claim .Name
408+ }
409+ return index , nil
410+ }
411+
386412func (r * Actor ) patchStore (ctx * recon.Context [* v1alpha1.CNClaim ], pod * corev1.Pod , req logpb.CNStateLabel ) (* metadata.CNService , error ) {
387413 cs , err := common .ResolveCNSet (ctx , pod )
388414 if err != nil {
0 commit comments