Merge pull request #13116 from Luap99/reset-networks

system prune: remove all networks
This commit is contained in:
OpenShift Merge Robot
2022-02-03 08:43:16 -05:00
committed by GitHub
4 changed files with 46 additions and 8 deletions

View File

@ -21,7 +21,7 @@ import (
var ( var (
systemResetDescription = `Reset podman storage back to default state" systemResetDescription = `Reset podman storage back to default state"
All containers will be stopped and removed, and all images, volumes and container content will be removed. All containers will be stopped and removed, and all images, volumes, networks and container content will be removed.
` `
systemResetCommand = &cobra.Command{ systemResetCommand = &cobra.Command{
Annotations: map[string]string{registry.EngineMode: registry.ABIMode}, Annotations: map[string]string{registry.EngineMode: registry.ABIMode},
@ -55,11 +55,11 @@ func reset(cmd *cobra.Command, args []string) {
// Prompt for confirmation if --force is not set // Prompt for confirmation if --force is not set
if !forceFlag { if !forceFlag {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
fmt.Println(` fmt.Println(`WARNING! This will remove:
WARNING! This will remove:
- all containers - all containers
- all pods - all pods
- all images - all images
- all networks
- all build cache`) - all build cache`)
if len(listCtn) > 0 { if len(listCtn) > 0 {
fmt.Println(`WARNING! The following external containers will be purged:`) fmt.Println(`WARNING! The following external containers will be purged:`)

View File

@ -7,7 +7,7 @@ podman\-system\-reset - Reset storage back to initial state
**podman system reset** [*options*] **podman system reset** [*options*]
## DESCRIPTION ## DESCRIPTION
**podman system reset** removes all pods, containers, images and volumes. **podman system reset** removes all pods, containers, images, networks and volumes.
This command must be run **before** changing any of the following fields in the This command must be run **before** changing any of the following fields in the
`containers.conf` or `storage.conf` files: `driver`, `static_dir`, `tmp_dir` `containers.conf` or `storage.conf` files: `driver`, `static_dir`, `tmp_dir`
@ -28,6 +28,17 @@ Print usage statement
## EXAMPLES ## EXAMPLES
```
$ podman system reset
WARNING! This will remove:
- all containers
- all pods
- all images
- all networks
- all build cache
Are you sure you want to continue? [y/N] y
```
### Switching rootless user from VFS driver to overlay with fuse-overlayfs ### Switching rootless user from VFS driver to overlay with fuse-overlayfs
If the user ran rootless containers without having the `fuse-overlayfs` program If the user ran rootless containers without having the `fuse-overlayfs` program

View File

@ -7,6 +7,7 @@ import (
"path/filepath" "path/filepath"
"github.com/containers/common/libimage" "github.com/containers/common/libimage"
"github.com/containers/common/libnetwork/types"
"github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/errorhandling" "github.com/containers/podman/v4/pkg/errorhandling"
"github.com/containers/podman/v4/pkg/rootless" "github.com/containers/podman/v4/pkg/rootless"
@ -70,6 +71,22 @@ func (r *Runtime) Reset(ctx context.Context) error {
} }
} }
// remove all networks
nets, err := r.network.NetworkList()
if err != nil {
return err
}
for _, net := range nets {
// do not delete the default network
if net.Name == r.network.DefaultNetworkName() {
continue
}
// ignore not exists errors because of the TOCTOU problem
if err := r.network.NetworkRemove(net.Name); err != nil && !errors.Is(err, types.ErrNoSuchNetwork) {
logrus.Errorf("Removing network %s: %v", net.Name, err)
}
}
xdgRuntimeDir := filepath.Clean(os.Getenv("XDG_RUNTIME_DIR")) xdgRuntimeDir := filepath.Clean(os.Getenv("XDG_RUNTIME_DIR"))
_, prevError := r.store.Shutdown(true) _, prevError := r.store.Shutdown(true)
graphRoot := filepath.Clean(r.store.GraphRoot()) graphRoot := filepath.Clean(r.store.GraphRoot())

View File

@ -38,6 +38,10 @@ var _ = Describe("podman system reset", func() {
SkipIfRemote("system reset not supported on podman --remote") SkipIfRemote("system reset not supported on podman --remote")
// system reset will not remove additional store images, so need to grab length // system reset will not remove additional store images, so need to grab length
// change the network dir so that we do not conflict with other tests
// that would use the same network dir and cause unnecessary flakes
podmanTest.NetworkConfigDir = tempdir
session := podmanTest.Podman([]string{"rmi", "--force", "--all"}) session := podmanTest.Podman([]string{"rmi", "--force", "--all"})
session.WaitWithDefaultTimeout() session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0)) Expect(session).Should(Exit(0))
@ -56,16 +60,16 @@ var _ = Describe("podman system reset", func() {
session.WaitWithDefaultTimeout() session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0)) Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"network", "create"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"system", "reset", "-f"}) session = podmanTest.Podman([]string{"system", "reset", "-f"})
session.WaitWithDefaultTimeout() session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0)) Expect(session).Should(Exit(0))
Expect(session.ErrorToString()).To(Not(ContainSubstring("Failed to add pause process"))) Expect(session.ErrorToString()).To(Not(ContainSubstring("Failed to add pause process")))
// If remote then the API service should have exited
// On local tests this is a noop
podmanTest.StartRemoteService()
session = podmanTest.Podman([]string{"images", "-n"}) session = podmanTest.Podman([]string{"images", "-n"})
session.WaitWithDefaultTimeout() session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0)) Expect(session).Should(Exit(0))
@ -80,5 +84,11 @@ var _ = Describe("podman system reset", func() {
session.WaitWithDefaultTimeout() session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0)) Expect(session).Should(Exit(0))
Expect(session.OutputToStringArray()).To(BeEmpty()) Expect(session.OutputToStringArray()).To(BeEmpty())
session = podmanTest.Podman([]string{"network", "ls", "-q"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
// default network should exists
Expect(session.OutputToStringArray()).To(HaveLen(1))
}) })
}) })