From daebbd1e9314e5ff35a141b5ed04ac50257ed718 Mon Sep 17 00:00:00 2001 From: flyflypeng Date: Wed, 20 Jun 2018 07:33:45 +0800 Subject: [PATCH] virtcontainers: add rollback to remove sandbox network If error occurs after sandbox network created successfully, we need to rollback to remove the created sandbox network Fixes: #297 Signed-off-by: flyflypeng --- virtcontainers/api.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/virtcontainers/api.go b/virtcontainers/api.go index abaebb50ed..ddbb16801d 100644 --- a/virtcontainers/api.go +++ b/virtcontainers/api.go @@ -39,6 +39,8 @@ func CreateSandbox(sandboxConfig SandboxConfig, factory Factory) (VCSandbox, err } func createSandboxFromConfig(sandboxConfig SandboxConfig, factory Factory) (*Sandbox, error) { + var err error + // Create the sandbox. s, err := createSandbox(sandboxConfig, factory) if err != nil { @@ -46,22 +48,29 @@ func createSandboxFromConfig(sandboxConfig SandboxConfig, factory Factory) (*San } // Create the sandbox network - if err := s.createNetwork(); err != nil { + if err = s.createNetwork(); err != nil { return nil, err } + // network rollback + defer func() { + if err != nil && s.networkNS.NetNsCreated { + s.removeNetwork() + } + }() + // Start the VM - if err := s.startVM(); err != nil { + if err = s.startVM(); err != nil { return nil, err } // Create Containers - if err := s.createContainers(); err != nil { + if err = s.createContainers(); err != nil { return nil, err } // The sandbox is completely created now, we can store it. - if err := s.storeSandbox(); err != nil { + if err = s.storeSandbox(); err != nil { return nil, err }