Skip to content

Commit 7ce509a

Browse files
zhuangqhfuweid
authored andcommitted
refactor: get pod network status only if necessary
Signed-off-by: zhuangqh <[email protected]>
1 parent 7f5e511 commit 7ce509a

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

cri/v1alpha2/cri.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -380,30 +380,32 @@ func (c *CriManager) StartPodSandbox(ctx context.Context, r *runtime.StartPodSan
380380
}
381381
sandboxMeta := res.(*metatypes.SandboxMeta)
382382

383-
ip, _ := c.CniMgr.GetPodNetworkStatus(sandboxMeta.NetNS)
384-
385-
if mgr.IsNetNS(sandbox.HostConfig.NetworkMode) && ip == "" {
386-
if err := c.CniMgr.RecoverNetNS(sandboxMeta.NetNS); err != nil {
387-
return nil, fmt.Errorf("failed to recover netns %s for sandbox %q: %v", sandboxMeta.NetNS, podSandboxID, err)
388-
}
389-
defer func() {
390-
if retErr != nil {
391-
if err := c.CniMgr.RemoveNetNS(sandboxMeta.NetNS); err != nil {
392-
logrus.Errorf("failed to remove net ns for sandbox %q: %v", podSandboxID, err)
393-
}
383+
if mgr.IsNetNS(sandbox.HostConfig.NetworkMode) {
384+
ip, _ := c.CniMgr.GetPodNetworkStatus(sandboxMeta.NetNS)
385+
// recover network if it is down.
386+
if ip == "" {
387+
if err := c.CniMgr.RecoverNetNS(sandboxMeta.NetNS); err != nil {
388+
return nil, fmt.Errorf("failed to recover netns %s for sandbox %q: %v", sandboxMeta.NetNS, podSandboxID, err)
394389
}
395-
}()
396-
397-
if err = c.setupPodNetwork(podSandboxID, sandboxMeta.NetNS, sandboxMeta.Config); err != nil {
398-
return nil, err
399-
}
400-
defer func() {
401-
if retErr != nil {
402-
if err := c.teardownNetwork(podSandboxID, sandboxMeta.NetNS, sandboxMeta.Config); err != nil {
403-
logrus.Errorf("failed to teardown pod network for sandbox %q: %v", podSandboxID, err)
390+
defer func() {
391+
if retErr != nil {
392+
if err := c.CniMgr.RemoveNetNS(sandboxMeta.NetNS); err != nil {
393+
logrus.Errorf("failed to remove net ns for sandbox %q: %v", podSandboxID, err)
394+
}
404395
}
396+
}()
397+
398+
if err = c.setupPodNetwork(podSandboxID, sandboxMeta.NetNS, sandboxMeta.Config); err != nil {
399+
return nil, err
405400
}
406-
}()
401+
defer func() {
402+
if retErr != nil {
403+
if err := c.teardownNetwork(podSandboxID, sandboxMeta.NetNS, sandboxMeta.Config); err != nil {
404+
logrus.Errorf("failed to teardown pod network for sandbox %q: %v", podSandboxID, err)
405+
}
406+
}
407+
}()
408+
}
407409
}
408410

409411
// start PodSandbox.

0 commit comments

Comments
 (0)