Only delete leftovers for the appropriate OCI if known
parent
269b938945
commit
64e47992e4
|
@ -20,6 +20,7 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
|
@ -88,7 +89,15 @@ func init() {
|
|||
RootCmd.AddCommand(deleteCmd)
|
||||
}
|
||||
|
||||
// shotgun cleanup to delete orphaned docker container data
|
||||
func deleteContainersAndVolumes() {
|
||||
if _, err := exec.LookPath(oci.Docker); err != nil {
|
||||
glog.Infof("skipping deleteContainersAndVolumes for %s: %v", oci.Docker, err)
|
||||
return
|
||||
}
|
||||
|
||||
glog.Infof("deleting containers and volumes ...")
|
||||
|
||||
delLabel := fmt.Sprintf("%s=%s", oci.CreatedByLabelKey, "true")
|
||||
errs := oci.DeleteContainersByLabel(oci.Docker, delLabel)
|
||||
if len(errs) > 0 { // it will error if there is no container to delete
|
||||
|
@ -143,16 +152,22 @@ func runDelete(cmd *cobra.Command, args []string) {
|
|||
|
||||
cname := ClusterFlagValue()
|
||||
profile, err := config.LoadProfile(cname)
|
||||
orphan := false
|
||||
|
||||
if err != nil {
|
||||
out.ErrT(out.Meh, `"{{.name}}" profile does not exist, trying anyways.`, out.V{"name": cname})
|
||||
orphan = true
|
||||
}
|
||||
|
||||
deletePossibleKicLeftOver(cname)
|
||||
|
||||
errs := DeleteProfiles([]*config.Profile{profile})
|
||||
if len(errs) > 0 {
|
||||
HandleDeletionErrors(errs)
|
||||
}
|
||||
|
||||
if orphan {
|
||||
// TODO: generalize for non-KIC drivers
|
||||
deletePossibleKicLeftOver(cname, driver.Docker)
|
||||
}
|
||||
}
|
||||
|
||||
// If the purge flag is set, go ahead and delete the .minikube directory.
|
||||
|
@ -171,6 +186,7 @@ func purgeMinikubeDirectory() {
|
|||
|
||||
// DeleteProfiles deletes one or more profiles
|
||||
func DeleteProfiles(profiles []*config.Profile) []error {
|
||||
glog.Infof("DeleteProfiles")
|
||||
var errs []error
|
||||
for _, profile := range profiles {
|
||||
err := deleteProfile(profile)
|
||||
|
@ -191,41 +207,57 @@ func DeleteProfiles(profiles []*config.Profile) []error {
|
|||
return errs
|
||||
}
|
||||
|
||||
func deletePossibleKicLeftOver(name string) {
|
||||
delLabel := fmt.Sprintf("%s=%s", oci.ProfileLabelKey, name)
|
||||
for _, bin := range []string{oci.Docker, oci.Podman} {
|
||||
cs, err := oci.ListContainersByLabel(bin, delLabel)
|
||||
if err == nil && len(cs) > 0 {
|
||||
for _, c := range cs {
|
||||
out.T(out.DeletingHost, `Deleting container "{{.name}}" ...`, out.V{"name": name})
|
||||
err := oci.DeleteContainer(bin, c)
|
||||
if err != nil { // it will error if there is no container to delete
|
||||
glog.Errorf("error deleting container %q. you might want to delete that manually :\n%v", name, err)
|
||||
}
|
||||
// TODO: remove and/or move to delete package.
|
||||
func deletePossibleKicLeftOver(cname string, driverName string) {
|
||||
glog.Infof("deleting possible KIC leftovers for %s (driver=%s) ...", cname, driverName)
|
||||
|
||||
bin := ""
|
||||
switch driverName {
|
||||
case driver.Docker:
|
||||
bin = oci.Docker
|
||||
case driver.Podman:
|
||||
bin = oci.Podman
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
delLabel := fmt.Sprintf("%s=%s", oci.ProfileLabelKey, cname)
|
||||
cs, err := oci.ListContainersByLabel(bin, delLabel)
|
||||
if err == nil && len(cs) > 0 {
|
||||
for _, c := range cs {
|
||||
out.T(out.DeletingHost, `Deleting container "{{.name}}" ...`, out.V{"name": cname})
|
||||
err := oci.DeleteContainer(bin, c)
|
||||
if err != nil { // it will error if there is no container to delete
|
||||
glog.Errorf("error deleting container %q. You may want to delete it manually :\n%v", cname, err)
|
||||
}
|
||||
}
|
||||
|
||||
errs := oci.DeleteAllVolumesByLabel(bin, delLabel)
|
||||
if errs != nil { // it will not error if there is nothing to delete
|
||||
glog.Warningf("error deleting volumes (might be okay).\nTo see the list of volumes run: 'docker volume ls'\n:%v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
errs = oci.PruneAllVolumesByLabel(bin, delLabel)
|
||||
if len(errs) > 0 { // it will not error if there is nothing to delete
|
||||
glog.Warningf("error pruning volume (might be okay):\n%v", errs)
|
||||
}
|
||||
errs := oci.DeleteAllVolumesByLabel(bin, delLabel)
|
||||
if errs != nil { // it will not error if there is nothing to delete
|
||||
glog.Warningf("error deleting volumes (might be okay).\nTo see the list of volumes run: 'docker volume ls'\n:%v", errs)
|
||||
}
|
||||
|
||||
errs = oci.PruneAllVolumesByLabel(bin, delLabel)
|
||||
if len(errs) > 0 { // it will not error if there is nothing to delete
|
||||
glog.Warningf("error pruning volume (might be okay):\n%v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteProfile(profile *config.Profile) error {
|
||||
glog.Infof("Deleting %s", profile.Name)
|
||||
viper.Set(config.ProfileName, profile.Name)
|
||||
if profile.Config != nil {
|
||||
glog.Infof("%s configuration: %+v", profile.Name, profile.Config)
|
||||
|
||||
// if driver is oci driver, delete containers and volumes
|
||||
if driver.IsKIC(profile.Config.Driver) {
|
||||
out.T(out.DeletingHost, `Deleting "{{.profile_name}}" in {{.driver_name}} ...`, out.V{"profile_name": profile.Name, "driver_name": profile.Config.Driver})
|
||||
deletePossibleKicLeftOver(profile.Name)
|
||||
deletePossibleKicLeftOver(profile.Name, profile.Config.Driver)
|
||||
}
|
||||
} else {
|
||||
glog.Infof("%s has no configuration, will try to make it work anyways", profile.Name)
|
||||
}
|
||||
|
||||
api, err := machine.NewAPIClient()
|
||||
|
|
Loading…
Reference in New Issue