Skip to content

Commit 3512e88

Browse files
authored
Merge pull request #10435 from afbjorklund/podman-cleanup
Explicitly remove podman volume and network
2 parents 0cbc73e + b7de621 commit 3512e88

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

cmd/minikube/cmd/delete.go

+16
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,27 @@ func deletePossibleKicLeftOver(ctx context.Context, cname string, driverName str
260260
}
261261
}
262262

263+
if bin == oci.Podman {
264+
// podman volume does not support --filter
265+
err := oci.RemoveVolume(bin, cname)
266+
if err != nil {
267+
klog.Warningf("error deleting volume %s (might be okay).'\n:%v", cname, err)
268+
}
269+
}
270+
263271
errs := oci.DeleteAllVolumesByLabel(ctx, bin, delLabel)
264272
if errs != nil { // it will not error if there is nothing to delete
265273
klog.Warningf("error deleting volumes (might be okay).\nTo see the list of volumes run: 'docker volume ls'\n:%v", errs)
266274
}
267275

276+
if bin == oci.Podman {
277+
// podman network does not support --filter
278+
err := oci.RemoveNetwork(bin, cname)
279+
if err != nil {
280+
klog.Warningf("error deleting network %s (might be okay).'\n:%v", cname, err)
281+
}
282+
}
283+
268284
errs = oci.DeleteKICNetworks(bin)
269285
if errs != nil {
270286
klog.Warningf("error deleting leftover networks (might be okay).\nTo see the list of networks: 'docker network ls'\n:%v", errs)

pkg/drivers/kic/oci/errors.go

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ var ErrDaemonInfo = errors.New("daemon info not responding")
5454
// ErrInsufficientDockerStorage is thrown when there is not more storage for docker
5555
var ErrInsufficientDockerStorage = &FailFastError{errors.New("insufficient docker storage, no space left on device")}
5656

57+
// ErrVolumeNotFound is when given volume was not found
58+
var ErrVolumeNotFound = errors.New("kic volume not found")
59+
5760
// ErrNetworkSubnetTaken is thrown when a subnet is taken by another network
5861
var ErrNetworkSubnetTaken = errors.New("subnet is taken")
5962

pkg/drivers/kic/oci/volumes.go

+42
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bufio"
2121
"bytes"
2222
"context"
23+
"encoding/json"
2324
"fmt"
2425
"os/exec"
2526
"runtime"
@@ -30,6 +31,47 @@ import (
3031
"k8s.io/klog/v2"
3132
)
3233

34+
// RemoveVolume removes a volume
35+
func RemoveVolume(ociBin string, name string) error {
36+
if !volumeExists(ociBin, name) {
37+
return nil
38+
}
39+
rr, err := runCmd(exec.Command(ociBin, "volume", "rm", name))
40+
if err != nil {
41+
if strings.Contains(rr.Output(), "No such volume") ||
42+
strings.Contains(rr.Output(), "no such volume") {
43+
return ErrVolumeNotFound
44+
}
45+
}
46+
47+
return err
48+
}
49+
50+
func volumeExists(ociBin string, name string) bool {
51+
_, err := containerVolumeInspect(ociBin, name)
52+
if err != nil && !errors.Is(err, ErrVolumeNotFound) { // log unexpected error
53+
klog.Warningf("Error inspecting docker volume %s: %v", name, err)
54+
}
55+
return err == nil
56+
}
57+
58+
func containerVolumeInspect(ociBin string, name string) (interface{}, error) {
59+
var info interface{}
60+
cmd := exec.Command(ociBin, "volume", "inspect", name)
61+
rr, err := runCmd(cmd)
62+
if err != nil {
63+
if strings.Contains(rr.Output(), "No such volume") ||
64+
strings.Contains(rr.Output(), "no such volume") {
65+
return info, ErrVolumeNotFound
66+
}
67+
return info, err
68+
}
69+
70+
err = json.Unmarshal(rr.Stdout.Bytes(), &info)
71+
72+
return info, err
73+
}
74+
3375
// DeleteAllVolumesByLabel deletes all volumes that have a specific label
3476
// if there is no volume to delete it will return nil
3577
func DeleteAllVolumesByLabel(ctx context.Context, ociBin string, label string, warnSlow ...bool) []error {

0 commit comments

Comments
 (0)