Merge pull request #10435 from afbjorklund/podman-cleanup

Explicitly remove podman volume and network
pull/10450/head
Medya Ghazizadeh 2021-02-11 10:27:50 -08:00 committed by GitHub
commit 3512e88020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 0 deletions

View File

@ -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)

View File

@ -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")

View File

@ -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 {