Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtconainers: rollback the NetNs when createNetwork failed
Browse files Browse the repository at this point in the history
When createNetwork failed, cleanup the NetNs if it created.

Fixes: #508

Signed-off-by: fupan <[email protected]>
  • Loading branch information
lifupan authored and Eric Ernst committed Aug 23, 2018
1 parent 3bbcdbc commit 35a9430
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,20 @@ func (s *Sandbox) Delete() error {
}

func (s *Sandbox) createNetwork() error {
var netNsPath string
var netNsCreated bool
var networkNS NetworkNamespace
var err error

//rollback the NetNs when createNetwork failed
defer func() {
if err != nil && netNsPath != "" && netNsCreated {
deleteNetNS(netNsPath)
}
}()

// Initialize the network.
netNsPath, netNsCreated, err := s.network.init(s.config.NetworkConfig)
netNsPath, netNsCreated, err = s.network.init(s.config.NetworkConfig)
if err != nil {
return err
}
Expand All @@ -919,14 +931,16 @@ func (s *Sandbox) createNetwork() error {
}

// Add the network
networkNS, err := s.network.add(s, s.config.NetworkConfig, netNsPath, netNsCreated)
networkNS, err = s.network.add(s, s.config.NetworkConfig, netNsPath, netNsCreated)
if err != nil {
return err
}
s.networkNS = networkNS

// Store the network
return s.storage.storeSandboxNetwork(s.id, networkNS)
err = s.storage.storeSandboxNetwork(s.id, networkNS)

return err
}

func (s *Sandbox) removeNetwork() error {
Expand Down

0 comments on commit 35a9430

Please sign in to comment.