diff --git a/cns/NetworkContainerContract.go b/cns/NetworkContainerContract.go index 9462ca3053..a0bd6756d7 100644 --- a/cns/NetworkContainerContract.go +++ b/cns/NetworkContainerContract.go @@ -180,6 +180,7 @@ type podInfoScheme int const ( KubernetesPodInfoScheme podInfoScheme = iota InterfaceIDPodInfoScheme + InfraIDPodInfoScheme ) // PodInfo represents the object that we are providing network for. @@ -249,11 +250,18 @@ func (p *podInfo) InterfaceID() string { // orchestrator pod name and namespace. if the Version is interfaceID, key is // composed of the CNI interfaceID, which is generated from the CRI infra // container ID and the pod net ns primary interface name. +// If the version in InfraContainerID then the key is containerID. func (p *podInfo) Key() string { - if p.Version == InterfaceIDPodInfoScheme { + switch p.Version { + case InfraIDPodInfoScheme: + return p.PodInfraContainerID + case InterfaceIDPodInfoScheme: return p.PodInterfaceID + case KubernetesPodInfoScheme: + return p.PodName + ":" + p.PodNamespace + default: + return p.PodName + ":" + p.PodNamespace } - return p.PodName + ":" + p.PodNamespace } func (p *podInfo) Name() string { diff --git a/cns/restserver/ipam.go b/cns/restserver/ipam.go index a328ebaa5e..fc7be99059 100644 --- a/cns/restserver/ipam.go +++ b/cns/restserver/ipam.go @@ -713,7 +713,7 @@ func (service *HTTPRestService) releaseIPConfigs(podInfo cns.PodInfo) error { service.Lock() defer service.Unlock() ipsToBeReleased := make([]cns.IPConfigurationStatus, 0) - + logger.Printf("[releaseIPConfigs] Releasing pod with key %s", podInfo.Key()) for i, ipID := range service.PodIPIDByPodInterfaceKey[podInfo.Key()] { if ipID != "" { if ipconfig, isExist := service.PodIPConfigState[ipID]; isExist { diff --git a/cns/service/main.go b/cns/service/main.go index 3847c2c733..43af60290d 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -843,7 +843,7 @@ func main() { // in this case, cns maintains state with containerid as key and so in-memory cache can lookup // and update based on container id. if cnsconfig.ManageEndpointState { - cns.GlobalPodInfoScheme = cns.InterfaceIDPodInfoScheme + cns.GlobalPodInfoScheme = cns.InfraIDPodInfoScheme } logger.Printf("Set GlobalPodInfoScheme %v (InitializeFromCNI=%t)", cns.GlobalPodInfoScheme, cnsconfig.InitializeFromCNI) @@ -1244,6 +1244,10 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn if err = PopulateCNSEndpointState(httpRestServiceImplementation.EndpointStateStore); err != nil { return errors.Wrap(err, "failed to create CNS EndpointState From CNI") } + // endpoint state needs tobe loaded in memory so the subsequent Delete calls remove the state and release the IPs. + if err = httpRestServiceImplementation.EndpointStateStore.Read(restserver.EndpointStoreKey, &httpRestServiceImplementation.EndpointState); err != nil { + return errors.Wrap(err, "failed to restore endpoint state") + } } var podInfoByIPProvider cns.PodInfoByIPProvider