diff --git a/cmd/minikube/cmd/delete.go b/cmd/minikube/cmd/delete.go index 09c8ae4d5831..0728d2c2536c 100644 --- a/cmd/minikube/cmd/delete.go +++ b/cmd/minikube/cmd/delete.go @@ -260,11 +260,27 @@ func deletePossibleKicLeftOver(ctx context.Context, cname string, driverName str } } + if bin == oci.Podman { + // podman volume does not support --filter + err := oci.RemoveVolume(bin, cname) + if err != nil { + klog.Warningf("error deleting volume %s (might be okay).'\n:%v", cname, err) + } + } + errs := oci.DeleteAllVolumesByLabel(ctx, bin, delLabel) if errs != nil { // it will not error if there is nothing to delete klog.Warningf("error deleting volumes (might be okay).\nTo see the list of volumes run: 'docker volume ls'\n:%v", errs) } + if bin == oci.Podman { + // podman network does not support --filter + err := oci.RemoveNetwork(bin, cname) + if err != nil { + klog.Warningf("error deleting network %s (might be okay).'\n:%v", cname, err) + } + } + errs = oci.DeleteKICNetworks(bin) if errs != nil { klog.Warningf("error deleting leftover networks (might be okay).\nTo see the list of networks: 'docker network ls'\n:%v", errs) diff --git a/pkg/drivers/kic/oci/errors.go b/pkg/drivers/kic/oci/errors.go index 138c8cae7cd0..c4d806fc06ff 100644 --- a/pkg/drivers/kic/oci/errors.go +++ b/pkg/drivers/kic/oci/errors.go @@ -54,6 +54,9 @@ var ErrDaemonInfo = errors.New("daemon info not responding") // ErrInsufficientDockerStorage is thrown when there is not more storage for docker var ErrInsufficientDockerStorage = &FailFastError{errors.New("insufficient docker storage, no space left on device")} +// ErrVolumeNotFound is when given volume was not found +var ErrVolumeNotFound = errors.New("kic volume not found") + // ErrNetworkSubnetTaken is thrown when a subnet is taken by another network var ErrNetworkSubnetTaken = errors.New("subnet is taken") diff --git a/pkg/drivers/kic/oci/volumes.go b/pkg/drivers/kic/oci/volumes.go index a32492892ed7..7c5260cd82b4 100644 --- a/pkg/drivers/kic/oci/volumes.go +++ b/pkg/drivers/kic/oci/volumes.go @@ -20,6 +20,7 @@ import ( "bufio" "bytes" "context" + "encoding/json" "fmt" "os/exec" "runtime" @@ -30,6 +31,47 @@ import ( "k8s.io/klog/v2" ) +// RemoveVolume removes a volume +func RemoveVolume(ociBin string, name string) error { + if !volumeExists(ociBin, name) { + return nil + } + rr, err := runCmd(exec.Command(ociBin, "volume", "rm", name)) + if err != nil { + if strings.Contains(rr.Output(), "No such volume") || + strings.Contains(rr.Output(), "no such volume") { + return ErrVolumeNotFound + } + } + + return err +} + +func volumeExists(ociBin string, name string) bool { + _, err := containerVolumeInspect(ociBin, name) + if err != nil && !errors.Is(err, ErrVolumeNotFound) { // log unexpected error + klog.Warningf("Error inspecting docker volume %s: %v", name, err) + } + return err == nil +} + +func containerVolumeInspect(ociBin string, name string) (interface{}, error) { + var info interface{} + cmd := exec.Command(ociBin, "volume", "inspect", name) + rr, err := runCmd(cmd) + if err != nil { + if strings.Contains(rr.Output(), "No such volume") || + strings.Contains(rr.Output(), "no such volume") { + return info, ErrVolumeNotFound + } + return info, err + } + + err = json.Unmarshal(rr.Stdout.Bytes(), &info) + + return info, err +} + // DeleteAllVolumesByLabel deletes all volumes that have a specific label // if there is no volume to delete it will return nil func DeleteAllVolumesByLabel(ctx context.Context, ociBin string, label string, warnSlow ...bool) []error {