mirror of
https://github.com/containers/podman.git
synced 2025-06-20 17:13:43 +08:00
Merge pull request #7793 from Luap99/network-force
Fix podman network rm --force when network is used by a pod
This commit is contained in:
@ -60,7 +60,7 @@ func Remove(ctx context.Context, nameOrID string, force *bool) ([]*entities.Netw
|
||||
}
|
||||
params := url.Values{}
|
||||
if force != nil {
|
||||
params.Set("size", strconv.FormatBool(*force))
|
||||
params.Set("force", strconv.FormatBool(*force))
|
||||
}
|
||||
response, err := conn.DoRequest(nil, http.MethodDelete, "/networks/%s", params, nil, nameOrID)
|
||||
if err != nil {
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -26,11 +26,16 @@ func (ic *ContainerEngine) NetworkInspect(ctx context.Context, namesOrIds []stri
|
||||
func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, options entities.NetworkRmOptions) ([]*entities.NetworkRmReport, error) {
|
||||
reports := make([]*entities.NetworkRmReport, 0, len(namesOrIds))
|
||||
for _, name := range namesOrIds {
|
||||
report, err := network.Remove(ic.ClientCxt, name, &options.Force)
|
||||
response, err := network.Remove(ic.ClientCxt, name, &options.Force)
|
||||
if err != nil {
|
||||
report[0].Err = err
|
||||
report := &entities.NetworkRmReport{
|
||||
Name: name,
|
||||
Err: err,
|
||||
}
|
||||
reports = append(reports, report)
|
||||
} else {
|
||||
reports = append(reports, response...)
|
||||
}
|
||||
reports = append(reports, report...)
|
||||
}
|
||||
return reports, nil
|
||||
}
|
||||
|
@ -263,4 +263,54 @@ 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)))
|
||||
})
|
||||
|
||||
It("podman network remove with two networks", func() {
|
||||
netName1 := "net1"
|
||||
session := podmanTest.Podman([]string{"network", "create", netName1})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session.ExitCode()).To(BeZero())
|
||||
|
||||
netName2 := "net2"
|
||||
session = podmanTest.Podman([]string{"network", "create", netName2})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session.ExitCode()).To(BeZero())
|
||||
|
||||
session = podmanTest.Podman([]string{"network", "rm", netName1, netName2})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session.ExitCode()).To(BeZero())
|
||||
lines := session.OutputToStringArray()
|
||||
Expect(lines[0]).To(Equal(netName1))
|
||||
Expect(lines[1]).To(Equal(netName2))
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user