Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the appropriate OCI for deletePossibleKicLeftOver when known #8038

Merged
merged 2 commits into from
May 8, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 55 additions & 23 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
tstromberg marked this conversation as resolved.
Show resolved Hide resolved
deletePossibleKicLeftOver(cname, driver.Docker)
}
}

// If the purge flag is set, go ahead and delete the .minikube directory.
Expand All @@ -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)
Expand All @@ -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()
Expand Down