Skip to content

Commit

Permalink
Merge pull request #10435 from afbjorklund/podman-cleanup
Browse files Browse the repository at this point in the history
Explicitly remove podman volume and network
  • Loading branch information
medyagh authored Feb 11, 2021
2 parents 0cbc73e + b7de621 commit 3512e88
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions pkg/drivers/kic/oci/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
42 changes: 42 additions & 0 deletions pkg/drivers/kic/oci/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"os/exec"
"runtime"
Expand All @@ -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 {
Expand Down

0 comments on commit 3512e88

Please sign in to comment.