Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix podman network rm --force when network is used by a pod #7793

Merged
merged 2 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 12 additions & 3 deletions pkg/domain/infra/abi/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,21 @@ func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, o
// We need to iterate containers looking to see if they belong to the given network
for _, c := range containers {
if util.StringInSlice(name, c.Config().Networks) {
// if user passes force, we nuke containers
// if user passes force, we nuke containers and pods
if !options.Force {
// Without the force option, we return an error
return reports, errors.Errorf("%q has associated containers with it. Use -f to forcibly delete containers", name)
return reports, errors.Errorf("%q has associated containers with it. Use -f to forcibly delete containers and pods", name)
}
if err := ic.Libpod.RemoveContainer(ctx, c, true, true); err != nil {
if c.IsInfra() {
// if we have a infra container we need to remove the pod
pod, err := ic.Libpod.GetPod(c.PodID())
if err != nil {
return reports, err
}
if err := ic.Libpod.RemovePod(ctx, pod, true, true); err != nil {
return reports, err
}
} else if err := ic.Libpod.RemoveContainer(ctx, c, true, true); err != nil {
return reports, err
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/e2e/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,35 @@ var _ = Describe("Podman network", func() {
rmAll.WaitWithDefaultTimeout()
Expect(rmAll.ExitCode()).To(BeZero())
})

It("podman network remove --force with pod", func() {
netName := "testnet"
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())

session = podmanTest.Podman([]string{"pod", "create", "--network", netName})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
podID := session.OutputToString()

session = podmanTest.Podman([]string{"create", "--pod", podID, ALPINE})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())

session = podmanTest.Podman([]string{"network", "rm", "--force", netName})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())

// check if pod is deleted
session = podmanTest.Podman([]string{"pod", "exists", podID})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(1))

// check if net is deleted
session = podmanTest.Podman([]string{"network", "ls"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
Expect(session.OutputToString()).To(Not(ContainSubstring(netName)))
})
})