From 14796768181fe7562e0d705b8cb9fefced726b99 Mon Sep 17 00:00:00 2001 From: Michael Beaumont Date: Thu, 30 Jan 2020 21:11:10 +0100 Subject: [PATCH] Cleanup container networking on vm rm Fixes #450 --- pkg/network/cni/cni.go | 13 ++++++++----- pkg/network/docker/docker.go | 2 +- pkg/network/types.go | 2 +- pkg/operations/remove.go | 11 ++++++++--- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pkg/network/cni/cni.go b/pkg/network/cni/cni.go index 5f255c7d5..a2a3a5f35 100644 --- a/pkg/network/cni/cni.go +++ b/pkg/network/cni/cni.go @@ -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 } @@ -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) diff --git a/pkg/network/docker/docker.go b/pkg/network/docker/docker.go index 4d4192672..e3377baa1 100644 --- a/pkg/network/docker/docker.go +++ b/pkg/network/docker/docker.go @@ -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 } diff --git a/pkg/network/types.go b/pkg/network/types.go index f91ce053c..89273292c 100644 --- a/pkg/network/types.go +++ b/pkg/network/types.go @@ -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 { diff --git a/pkg/operations/remove.go b/pkg/operations/remove.go index 382ae230b..ba0d94abe 100644 --- a/pkg/operations/remove.go +++ b/pkg/operations/remove.go @@ -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 @@ -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 } @@ -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) }