Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Cleanup container networking on vm rm
Browse files Browse the repository at this point in the history
Fixes #450
  • Loading branch information
michaelbeaumont committed Jan 30, 2020
1 parent 27161ce commit 1479676
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
13 changes: 8 additions & 5 deletions pkg/network/cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func cniToIgniteResult(r *gocni.CNIResult) *network.Result {
return result
}

func (plugin *cniNetworkPlugin) RemoveContainerNetwork(containerID string) (err error) {
func (plugin *cniNetworkPlugin) RemoveContainerNetwork(containerID string, isRunning bool) (err error) {
if err = plugin.initialize(); err != nil {
return err
}
Expand All @@ -208,10 +208,13 @@ func (plugin *cniNetworkPlugin) RemoveContainerNetwork(containerID string) (err
return nil
}

netnsPath := fmt.Sprintf(netNSPathFmt, c.PID)
if c.PID == 0 {
log.Info("CNI failed to retrieve network namespace path, PID was 0")
return nil
netnsPath := ""
if isRunning {
netnsPath = fmt.Sprintf(netNSPathFmt, c.PID)
if c.PID == 0 {
log.Info("CNI failed to retrieve network namespace path, PID was 0")
return nil
}
}

return plugin.cni.Remove(context.Background(), containerID, netnsPath)
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (plugin *dockerNetworkPlugin) SetupContainerNetwork(containerID string, _ .
}, nil
}

func (*dockerNetworkPlugin) RemoveContainerNetwork(_ string) error {
func (*dockerNetworkPlugin) RemoveContainerNetwork(_ string, _ bool) error {
// no-op for docker, this is handled automatically
return nil
}
2 changes: 1 addition & 1 deletion pkg/network/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Plugin interface {
SetupContainerNetwork(containerID string, portmappings ...meta.PortMapping) (*Result, error)

// RemoveContainerNetwork is the method called before a container using the network plugin can be deleted
RemoveContainerNetwork(containerID string) error
RemoveContainerNetwork(containerID string, isRunning bool) error
}

type Result struct {
Expand Down
11 changes: 8 additions & 3 deletions pkg/operations/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func CleanupVM(vm *api.VM) error {
if err := StopVM(vm, true, true); err != nil {
return err
}
} else {
// Try to cleanup VM networking
if err := removeNetworking(util.NewPrefixer().Prefix(vm.GetUID()), false); err != nil {
log.Warnf("Failed to cleanup networking for stopped container %s %q: %v", vm.GetKind(), vm.GetUID(), err)
}
}

// Remove the VM container if it exists
Expand Down Expand Up @@ -79,7 +84,7 @@ func StopVM(vm *api.VM, kill, silent bool) error {
action := "stop"

// Remove VM networking
if err = removeNetworking(util.NewPrefixer().Prefix(vm.GetUID())); err != nil {
if err = removeNetworking(util.NewPrefixer().Prefix(vm.GetUID()), true); err != nil {
return err
}

Expand Down Expand Up @@ -108,7 +113,7 @@ func StopVM(vm *api.VM, kill, silent bool) error {
return nil
}

func removeNetworking(containerID string) error {
func removeNetworking(containerID string, isRunning bool) error {
log.Infof("Removing the container with ID %q from the %q network", containerID, providers.NetworkPlugin.Name())
return providers.NetworkPlugin.RemoveContainerNetwork(containerID)
return providers.NetworkPlugin.RemoveContainerNetwork(containerID, isRunning)
}

0 comments on commit 1479676

Please sign in to comment.