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

Cleanup container networking on vm rm #515

Merged
merged 1 commit into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}