@@ -86,8 +86,8 @@ var serviceLog = logf.Log.WithName("server")
8686var _ rpc.TerwayBackendServer = (* networkService )(nil )
8787
8888// return resource relation in db, or return nil.
89- func (n * networkService ) getPodResource (info * daemon. PodInfo ) (daemon.PodResources , error ) {
90- obj , err := n .resourceDB .Get (utils . PodInfoKey ( info . Namespace , info . Name ) )
89+ func (n * networkService ) getPodResource (podID string ) (daemon.PodResources , error ) {
90+ obj , err := n .resourceDB .Get (podID )
9191 if err == nil {
9292 return obj .(daemon.PodResources ), nil
9393 }
@@ -98,9 +98,8 @@ func (n *networkService) getPodResource(info *daemon.PodInfo) (daemon.PodResourc
9898 return daemon.PodResources {}, err
9999}
100100
101- func (n * networkService ) deletePodResource (info * daemon.PodInfo ) error {
102- key := utils .PodInfoKey (info .Namespace , info .Name )
103- return n .resourceDB .Delete (key )
101+ func (n * networkService ) deletePodResource (podID string ) error {
102+ return n .resourceDB .Delete (podID )
104103}
105104
106105func (n * networkService ) AllocIP (ctx context.Context , r * rpc.AllocIPRequest ) (* rpc.AllocIPReply , error ) {
@@ -163,7 +162,7 @@ func (n *networkService) AllocIP(ctx context.Context, r *rpc.AllocIPRequest) (*r
163162 }
164163
165164 // 2. Find old resource info
166- oldRes , err := n .getPodResource (pod )
165+ oldRes , err := n .getPodResource (podID )
167166 if err != nil {
168167 return nil , & types.Error {
169168 Code : types .ErrInternalError ,
@@ -331,30 +330,31 @@ func (n *networkService) ReleaseIP(ctx context.Context, r *rpc.ReleaseIPRequest)
331330 // 0. Get pod Info
332331 pod , err := n .k8s .GetPod (ctx , r .K8SPodNamespace , r .K8SPodName , true )
333332 if err != nil {
334- if k8sErr .IsNotFound (err ) {
335- return reply , nil
333+ if ! k8sErr .IsNotFound (err ) {
334+ l .Error (err , "get pod failed" )
335+ // ignore error, do not block delete
336336 }
337- return nil , err
338337 }
339338
340339 cni := & daemon.CNI {
341340 PodName : r .K8SPodName ,
342341 PodNamespace : r .K8SPodNamespace ,
343342 PodID : podID ,
344- PodUID : pod .PodUID ,
343+ }
344+
345+ var podIpStickTime time.Duration
346+ if pod != nil {
347+ cni .PodUID = pod .PodUID
348+ podIpStickTime = pod .IPStickTime
345349 }
346350
347351 // 1. Init Context
348352
349- oldRes , err := n .getPodResource (pod )
353+ oldRes , err := n .getPodResource (podID )
350354 if err != nil {
351355 return nil , err
352356 }
353357
354- if ! n .verifyPodNetworkType (pod .PodNetworkType ) {
355- return nil , fmt .Errorf ("unexpect pod network type allocate, maybe daemon mode changed: %+v" , pod .PodNetworkType )
356- }
357-
358358 if oldRes .ContainerID != nil {
359359 if r .K8SPodInfraContainerId != * oldRes .ContainerID {
360360 l .Info ("cni request not match stored resource, ignored" , "old" , * oldRes .ContainerID )
@@ -365,7 +365,7 @@ func (n *networkService) ReleaseIP(ctx context.Context, r *rpc.ReleaseIPRequest)
365365 cni .PodUID = oldRes .PodInfo .PodUID
366366 }
367367
368- if n .ipamType == types .IPAMTypeCRD || pod . IPStickTime = = 0 {
368+ if n .ipamType == types .IPAMTypeCRD || podIpStickTime < = 0 {
369369 for _ , resource := range oldRes .Resources {
370370 res := parseNetworkResource (resource )
371371 if res == nil {
@@ -379,7 +379,7 @@ func (n *networkService) ReleaseIP(ctx context.Context, r *rpc.ReleaseIPRequest)
379379 return nil , err
380380 }
381381 }
382- err = n .deletePodResource (pod )
382+ err = n .deletePodResource (podID )
383383 if err != nil {
384384 return nil , fmt .Errorf ("error delete pod resource: %w" , err )
385385 }
@@ -388,6 +388,7 @@ func (n *networkService) ReleaseIP(ctx context.Context, r *rpc.ReleaseIPRequest)
388388 return reply , nil
389389}
390390
391+ // GetIPInfo return cached alloc ip info
391392func (n * networkService ) GetIPInfo (ctx context.Context , r * rpc.GetInfoRequest ) (* rpc.GetInfoReply , error ) {
392393 podID := utils .PodInfoKey (r .K8SPodNamespace , r .K8SPodName )
393394 log := logf .FromContext (ctx )
@@ -409,27 +410,17 @@ func (n *networkService) GetIPInfo(ctx context.Context, r *rpc.GetInfoRequest) (
409410
410411 var err error
411412
412- // 0. Get pod Info
413- pod , err := n .k8s .GetPod (ctx , r .K8SPodNamespace , r .K8SPodName , true )
414- if err != nil {
415- return nil , & types.Error {
416- Code : types .ErrInvalidArgsErrCode ,
417- Msg : err .Error (),
418- R : err ,
419- }
420- }
421-
422413 // 1. Init Context
423414 reply := & rpc.GetInfoReply {
424415 Success : true ,
425416 IPv4 : n .enableIPv4 ,
426417 IPv6 : n .enableIPv6 ,
427418 }
428419
429- switch pod . PodNetworkType {
430- case daemon .PodNetworkTypeENIMultiIP :
420+ switch n . daemonMode {
421+ case daemon .ModeENIMultiIP :
431422 reply .IPType = rpc .IPType_TypeENIMultiIP
432- case daemon .PodNetworkTypeVPCENI :
423+ case daemon .ModeENIOnly :
433424 reply .IPType = rpc .IPType_TypeVPCENI
434425 default :
435426 return nil , & types.Error {
@@ -439,7 +430,7 @@ func (n *networkService) GetIPInfo(ctx context.Context, r *rpc.GetInfoRequest) (
439430 }
440431
441432 // 2. Find old resource info
442- oldRes , err := n .getPodResource (pod )
433+ oldRes , err := n .getPodResource (podID )
443434 if err != nil {
444435 return nil , & types.Error {
445436 Code : types .ErrInternalError ,
@@ -448,12 +439,6 @@ func (n *networkService) GetIPInfo(ctx context.Context, r *rpc.GetInfoRequest) (
448439 }
449440 }
450441
451- if ! n .verifyPodNetworkType (pod .PodNetworkType ) {
452- return nil , & types.Error {
453- Code : types .ErrInvalidArgsErrCode ,
454- Msg : "Unexpected network type, maybe daemon mode changed" ,
455- }
456- }
457442 if oldRes .ContainerID != nil {
458443 if r .K8SPodInfraContainerId != * oldRes .ContainerID {
459444 log .Info ("cni request not match stored resource, ignored" , "old" , * oldRes .ContainerID )
@@ -640,7 +625,7 @@ func (n *networkService) gcPods(ctx context.Context) error {
640625 }
641626 }
642627
643- err = n .deletePodResource (podRes . PodInfo )
628+ err = n .deletePodResource (podID )
644629 if err != nil {
645630 return err
646631 }
0 commit comments