added remove capabilities for pod

This commit is contained in:
christophe-duc 2026-01-08 13:42:54 -04:00
parent 8ed81e4ec9
commit 64297ad1d5
7 changed files with 79 additions and 1 deletions

View file

@ -3,10 +3,12 @@ package commands
import (
"context"
"fmt"
"strings"
"time"
"github.com/sasha-s/go-deadlock"
"github.com/sirupsen/logrus"
"golang.org/x/xerrors"
)
// Pod represents a Podman pod with its containers.
@ -69,6 +71,24 @@ func (p *Pod) Unpause() error {
return p.Runtime.UnpausePod(ctx, p.ID)
}
// Remove removes the pod
func (p *Pod) Remove(force bool) error {
p.Log.Warn(fmt.Sprintf("removing pod %s", p.Name))
ctx := context.Background()
if err := p.Runtime.RemovePod(ctx, p.ID, force); err != nil {
if strings.Contains(err.Error(), "pod is running") ||
strings.Contains(err.Error(), "stop the pod before attempting removal") {
return ComplexError{
Code: MustStopContainer,
Message: err.Error(),
frame: xerrors.Caller(1),
}
}
return err
}
return nil
}
// HasContainers returns true if the pod has non-infra containers.
func (p *Pod) HasContainers() bool {
return len(p.Containers) > 0

View file

@ -45,6 +45,7 @@ type ContainerRuntime interface {
StopPod(ctx context.Context, id string, timeout *int) error
PausePod(ctx context.Context, id string) error
UnpausePod(ctx context.Context, id string) error
RemovePod(ctx context.Context, id string, force bool) error
// Events streams container/image/volume/network events
Events(ctx context.Context) (<-chan Event, <-chan error)

View file

@ -493,6 +493,16 @@ func (r *LibpodRuntime) UnpausePod(ctx context.Context, id string) error {
return err
}
// RemovePod removes a pod from the system.
func (r *LibpodRuntime) RemovePod(ctx context.Context, id string, force bool) error {
pod, err := r.runtime.LookupPod(id)
if err != nil {
return err
}
_, err = r.runtime.RemovePod(ctx, pod, true, force, 0)
return err
}
// Events streams container runtime events.
// For libpod, we use a polling approach since direct event streaming requires more setup.
func (r *LibpodRuntime) Events(ctx context.Context) (<-chan Event, <-chan error) {

View file

@ -165,6 +165,10 @@ func (r *LibpodRuntime) UnpausePod(ctx context.Context, id string) error {
return ErrLibpodNotAvailable
}
func (r *LibpodRuntime) RemovePod(ctx context.Context, id string, force bool) error {
return ErrLibpodNotAvailable
}
// Events returns an error channel on non-Linux platforms.
func (r *LibpodRuntime) Events(ctx context.Context) (<-chan Event, <-chan error) {
eventsChan := make(chan Event)

View file

@ -309,6 +309,11 @@ func (m *MockRuntime) UnpausePod(ctx context.Context, id string) error {
return ErrMockNotImplemented
}
func (m *MockRuntime) RemovePod(ctx context.Context, id string, force bool) error {
m.recordCall("RemovePod", id, force)
return ErrMockNotImplemented
}
// Events
func (m *MockRuntime) Events(ctx context.Context) (<-chan Event, <-chan error) {

View file

@ -374,6 +374,13 @@ func (r *SocketRuntime) UnpausePod(ctx context.Context, id string) error {
return err
}
// RemovePod removes a pod from the system.
func (r *SocketRuntime) RemovePod(ctx context.Context, id string, force bool) error {
opts := &pods.RemoveOptions{Force: &force}
_, err := pods.Remove(r.conn, id, opts)
return err
}
// aggregatePodStats combines stats from all containers in a pod into a single entry.
func aggregatePodStats(reports []*types.PodStatsReport) PodStatsEntry {
if len(reports) == 0 {

View file

@ -473,7 +473,38 @@ func (gui *Gui) handleContainersRemoveMenu(g *gocui.Gui, v *gocui.View) error {
}
if item.IsPod {
return gui.createErrorPanel("Remove not yet supported for pods")
pod := item.Pod
handleMenuPress := func(force bool) error {
return gui.WithWaitingStatus(gui.Tr.RemovingStatus, func() error {
if err := pod.Remove(force); err != nil {
if commands.HasErrorCode(err, commands.MustStopContainer) {
return gui.createConfirmationPanel(gui.Tr.Confirm, gui.Tr.MustForceToRemoveContainer, func(g *gocui.Gui, v *gocui.View) error {
return gui.WithWaitingStatus(gui.Tr.RemovingStatus, func() error {
return pod.Remove(true)
})
}, nil)
}
return gui.createErrorPanel(err.Error())
}
return nil
})
}
menuItems := []*types.MenuItem{
{
LabelColumns: []string{gui.Tr.Remove, "podman pod rm " + pod.ID[1:10]},
OnPress: func() error { return handleMenuPress(false) },
},
{
LabelColumns: []string{gui.Tr.ForceRemove, "podman pod rm -f " + pod.ID[1:10]},
OnPress: func() error { return handleMenuPress(true) },
},
}
return gui.Menu(CreateMenuOptions{
Title: "",
Items: menuItems,
})
}
ctr := item.Container