Skip to content

Commit 2243b60

Browse files
committed
reset: remove external containers on podman system reset
[NO TESTS NEEDED] Signed-off-by: flouthoc <[email protected]>
1 parent 0a0ade3 commit 2243b60

File tree

5 files changed

+53
-13
lines changed

5 files changed

+53
-13
lines changed

cmd/podman/system/reset.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,29 @@ func init() {
4545
}
4646

4747
func reset(cmd *cobra.Command, args []string) {
48+
// Get all the external containers in use
49+
listCtn, _ := registry.ContainerEngine().ContainerListExternal(registry.Context())
50+
listCtnIds := make([]string, 0, len(listCtn))
51+
for _, externalCtn := range listCtn {
52+
listCtnIds = append(listCtnIds, externalCtn.ID)
53+
}
4854
// Prompt for confirmation if --force is not set
4955
if !forceFlag {
5056
reader := bufio.NewReader(os.Stdin)
51-
fmt.Print(`
57+
fmt.Println(`
5258
WARNING! This will remove:
5359
- all containers
5460
- all pods
5561
- all images
56-
- all build cache
57-
Are you sure you want to continue? [y/N] `)
62+
- all build cache`)
63+
if len(listCtn) > 0 {
64+
fmt.Println(`WARNING! The following external containers will be purged:`)
65+
// print first 12 characters of ID and first configured name alias
66+
for _, externalCtn := range listCtn {
67+
fmt.Printf(" - %s (%s)\n", externalCtn.ID[0:12], externalCtn.Names[0])
68+
}
69+
}
70+
fmt.Print(`Are you sure you want to continue? [y/N] `)
5871
answer, err := reader.ReadString('\n')
5972
if err != nil {
6073
logrus.Error(err)
@@ -65,6 +78,8 @@ Are you sure you want to continue? [y/N] `)
6578
}
6679
}
6780

81+
// Purge all the external containers with storage
82+
registry.ContainerEngine().ContainerRm(registry.Context(), listCtnIds, entities.RmOptions{Force: true, All: true, Ignore: true, Volumes: true})
6883
// Shutdown all running engines, `reset` will hijack repository
6984
registry.ContainerEngine().Shutdown(registry.Context())
7085
registry.ImageEngine().Shutdown(registry.Context())

pkg/domain/entities/engine_container.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type ContainerEngine interface {
3232
ContainerInspect(ctx context.Context, namesOrIds []string, options InspectOptions) ([]*ContainerInspectReport, []error, error)
3333
ContainerKill(ctx context.Context, namesOrIds []string, options KillOptions) ([]*KillReport, error)
3434
ContainerList(ctx context.Context, options ContainerListOptions) ([]ListContainer, error)
35+
ContainerListExternal(ctx context.Context) ([]ListContainer, error)
3536
ContainerLogs(ctx context.Context, containers []string, options ContainerLogsOptions) error
3637
ContainerMount(ctx context.Context, nameOrIDs []string, options ContainerMountOptions) ([]*ContainerMountReport, error)
3738
ContainerPause(ctx context.Context, namesOrIds []string, options PauseUnPauseOptions) ([]*PauseUnpauseReport, error)

pkg/domain/infra/abi/containers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,10 @@ func (ic *ContainerEngine) ContainerList(ctx context.Context, options entities.C
854854
return ps.GetContainerLists(ic.Libpod, options)
855855
}
856856

857+
func (ic *ContainerEngine) ContainerListExternal(ctx context.Context) ([]entities.ListContainer, error) {
858+
return ps.GetExternalContainerLists(ic.Libpod)
859+
}
860+
857861
// ContainerDiff provides changes to given container
858862
func (ic *ContainerEngine) ContainerDiff(ctx context.Context, nameOrID string, opts entities.DiffOptions) (*entities.DiffReport, error) {
859863
if opts.Latest {

pkg/domain/infra/tunnel/containers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,12 @@ func (ic *ContainerEngine) ContainerList(ctx context.Context, opts entities.Cont
646646
return containers.List(ic.ClientCtx, options)
647647
}
648648

649+
func (ic *ContainerEngine) ContainerListExternal(ctx context.Context) ([]entities.ListContainer, error) {
650+
options := new(containers.ListOptions).WithAll(true)
651+
options.WithNamespace(true).WithSize(true).WithSync(true).WithExternal(true)
652+
return containers.List(ic.ClientCtx, options)
653+
}
654+
649655
func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.ContainerRunOptions) (*entities.ContainerRunReport, error) {
650656
con, err := containers.CreateWithSpec(ic.ClientCtx, opts.Spec, nil)
651657
if err != nil {

pkg/ps/ps.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,11 @@ func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOp
7171
}
7272

7373
if options.All && options.External {
74-
externCons, err := runtime.StorageContainers()
74+
listCon, err := GetExternalContainerLists(runtime)
7575
if err != nil {
7676
return nil, err
7777
}
78-
79-
for _, con := range externCons {
80-
listCon, err := ListStorageContainer(runtime, con, options)
81-
if err != nil {
82-
return nil, err
83-
}
84-
pss = append(pss, listCon)
85-
}
78+
pss = append(pss, listCon...)
8679
}
8780

8881
// Sort the containers we got
@@ -97,6 +90,27 @@ func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOp
9790
return pss, nil
9891
}
9992

93+
// GetExternalContainerLists returns list of external containers for e.g created by buildah
94+
func GetExternalContainerLists(runtime *libpod.Runtime) ([]entities.ListContainer, error) {
95+
var (
96+
pss = []entities.ListContainer{}
97+
)
98+
99+
externCons, err := runtime.StorageContainers()
100+
if err != nil {
101+
return nil, err
102+
}
103+
104+
for _, con := range externCons {
105+
listCon, err := ListStorageContainer(runtime, con)
106+
if err != nil {
107+
return nil, err
108+
}
109+
pss = append(pss, listCon)
110+
}
111+
return pss, nil
112+
}
113+
100114
// BatchContainerOp is used in ps to reduce performance hits by "batching"
101115
// locks.
102116
func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities.ContainerListOptions) (entities.ListContainer, error) {
@@ -231,7 +245,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
231245
return ps, nil
232246
}
233247

234-
func ListStorageContainer(rt *libpod.Runtime, ctr storage.Container, opts entities.ContainerListOptions) (entities.ListContainer, error) {
248+
func ListStorageContainer(rt *libpod.Runtime, ctr storage.Container) (entities.ListContainer, error) {
235249
name := "unknown"
236250
if len(ctr.Names) > 0 {
237251
name = ctr.Names[0]

0 commit comments

Comments
 (0)