From cb484c3d5879153ff7d7b889e868438b6ebf7da9 Mon Sep 17 00:00:00 2001 From: Behzad Mirkhanzadeh Date: Fri, 16 Feb 2024 10:17:56 -0800 Subject: [PATCH] =?UTF-8?q?fix:=20fixing=20CNS=20IP=20releae=20for=20azure?= =?UTF-8?q?=20CNI=20in=20case=20of=20managed=20endpoint=20s=E2=80=A6=20(#2?= =?UTF-8?q?588)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: fixing CNS IP releae for azure CNI in case of managed endpoint state. * fix: changing PodInfo Key to InfraContainerID when Mange Endpoint State is enabled. --- cns/NetworkContainerContract.go | 12 ++++++++++-- cns/restserver/ipam.go | 2 +- cns/service/main.go | 6 +++++- 3 files changed, 16 insertions(+), 4 deletions(-) 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