Skip to content

Commit

Permalink
virtcontainers: add rollback to remove sandbox network
Browse files Browse the repository at this point in the history
If error occurs after sandbox network created successfully, we need to rollback
to remove the created sandbox network

Fixes: clearcontainers#297

Signed-off-by: flyflypeng <[email protected]>
  • Loading branch information
flyflypeng committed Jul 24, 2018
1 parent b244410 commit daebbd1
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,38 @@ 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 {
return nil, err
}

// 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
}

Expand Down

0 comments on commit daebbd1

Please sign in to comment.