From 7ce509a8f692df7a93660cdb720ff85eded694b3 Mon Sep 17 00:00:00 2001 From: jerryzhuang Date: Thu, 25 Apr 2019 02:00:58 +0800 Subject: [PATCH] refactor: get pod network status only if necessary Signed-off-by: zhuangqh --- cri/v1alpha2/cri.go | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/cri/v1alpha2/cri.go b/cri/v1alpha2/cri.go index 27357dcc1..aeb0eafe5 100644 --- a/cri/v1alpha2/cri.go +++ b/cri/v1alpha2/cri.go @@ -380,30 +380,32 @@ func (c *CriManager) StartPodSandbox(ctx context.Context, r *runtime.StartPodSan } sandboxMeta := res.(*metatypes.SandboxMeta) - ip, _ := c.CniMgr.GetPodNetworkStatus(sandboxMeta.NetNS) - - if mgr.IsNetNS(sandbox.HostConfig.NetworkMode) && ip == "" { - if err := c.CniMgr.RecoverNetNS(sandboxMeta.NetNS); err != nil { - return nil, fmt.Errorf("failed to recover netns %s for sandbox %q: %v", sandboxMeta.NetNS, podSandboxID, err) - } - defer func() { - if retErr != nil { - if err := c.CniMgr.RemoveNetNS(sandboxMeta.NetNS); err != nil { - logrus.Errorf("failed to remove net ns for sandbox %q: %v", podSandboxID, err) - } + if mgr.IsNetNS(sandbox.HostConfig.NetworkMode) { + ip, _ := c.CniMgr.GetPodNetworkStatus(sandboxMeta.NetNS) + // recover network if it is down. + if ip == "" { + if err := c.CniMgr.RecoverNetNS(sandboxMeta.NetNS); err != nil { + return nil, fmt.Errorf("failed to recover netns %s for sandbox %q: %v", sandboxMeta.NetNS, podSandboxID, err) } - }() - - if err = c.setupPodNetwork(podSandboxID, sandboxMeta.NetNS, sandboxMeta.Config); err != nil { - return nil, err - } - defer func() { - if retErr != nil { - if err := c.teardownNetwork(podSandboxID, sandboxMeta.NetNS, sandboxMeta.Config); err != nil { - logrus.Errorf("failed to teardown pod network for sandbox %q: %v", podSandboxID, err) + defer func() { + if retErr != nil { + if err := c.CniMgr.RemoveNetNS(sandboxMeta.NetNS); err != nil { + logrus.Errorf("failed to remove net ns for sandbox %q: %v", podSandboxID, err) + } } + }() + + if err = c.setupPodNetwork(podSandboxID, sandboxMeta.NetNS, sandboxMeta.Config); err != nil { + return nil, err } - }() + defer func() { + if retErr != nil { + if err := c.teardownNetwork(podSandboxID, sandboxMeta.NetNS, sandboxMeta.Config); err != nil { + logrus.Errorf("failed to teardown pod network for sandbox %q: %v", podSandboxID, err) + } + } + }() + } } // start PodSandbox.