From 08ea68f728c9b19b31b66f73ce963e898bf1c996 Mon Sep 17 00:00:00 2001 From: YaoZengzeng Date: Fri, 15 Jun 2018 16:20:39 +0800 Subject: [PATCH] bugfix: skip teardown network, if the sandbox has been stopped Signed-off-by: YaoZengzeng --- cri/v1alpha1/cri.go | 24 +++++++++++++++--------- cri/v1alpha2/cri.go | 24 +++++++++++++++--------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/cri/v1alpha1/cri.go b/cri/v1alpha1/cri.go index bc61a0cc1..cfdce9544 100644 --- a/cri/v1alpha1/cri.go +++ b/cri/v1alpha1/cri.go @@ -284,15 +284,21 @@ func (c *CriManager) StopPodSandbox(ctx context.Context, r *runtime.StopPodSandb // Teardown network of the pod, if it is not in host network mode. if !hostNet { - err = c.CniMgr.TearDownPodNetwork(&ocicni.PodNetwork{ - Name: metadata.GetName(), - Namespace: metadata.GetNamespace(), - ID: podSandboxID, - NetNS: sandboxMeta.NetNSPath, - PortMappings: toCNIPortMappings(sandboxMeta.Config.GetPortMappings()), - }) - if err != nil { - return nil, err + _, err = os.Stat(sandboxMeta.NetNSPath) + // If the sandbox has been stopped, the corresponding network namespace will not exist. + if err == nil { + err = c.CniMgr.TearDownPodNetwork(&ocicni.PodNetwork{ + Name: metadata.GetName(), + Namespace: metadata.GetNamespace(), + ID: podSandboxID, + NetNS: sandboxMeta.NetNSPath, + PortMappings: toCNIPortMappings(sandboxMeta.Config.GetPortMappings()), + }) + if err != nil { + return nil, err + } + } else if !os.IsNotExist(err) { + return nil, fmt.Errorf("failed to stat network namespace file %s of sandbox %s: %v", sandboxMeta.NetNSPath, podSandboxID, err) } } diff --git a/cri/v1alpha2/cri.go b/cri/v1alpha2/cri.go index dca1864e8..beff5f602 100644 --- a/cri/v1alpha2/cri.go +++ b/cri/v1alpha2/cri.go @@ -284,15 +284,21 @@ func (c *CriManager) StopPodSandbox(ctx context.Context, r *runtime.StopPodSandb // Teardown network of the pod, if it is not in host network mode. if !hostNet { - err = c.CniMgr.TearDownPodNetwork(&ocicni.PodNetwork{ - Name: metadata.GetName(), - Namespace: metadata.GetNamespace(), - ID: podSandboxID, - NetNS: sandboxMeta.NetNSPath, - PortMappings: toCNIPortMappings(sandboxMeta.Config.GetPortMappings()), - }) - if err != nil { - return nil, err + _, err = os.Stat(sandboxMeta.NetNSPath) + // If the sandbox has been stopped, the corresponding network namespace will not exist. + if err == nil { + err = c.CniMgr.TearDownPodNetwork(&ocicni.PodNetwork{ + Name: metadata.GetName(), + Namespace: metadata.GetNamespace(), + ID: podSandboxID, + NetNS: sandboxMeta.NetNSPath, + PortMappings: toCNIPortMappings(sandboxMeta.Config.GetPortMappings()), + }) + if err != nil { + return nil, err + } + } else if !os.IsNotExist(err) { + return nil, fmt.Errorf("failed to stat network namespace file %s of sandbox %s: %v", sandboxMeta.NetNSPath, podSandboxID, err) } }