Merge pull request #10321 from BLasan/issue-8040

Move DeletePossibleKicLeftOver function to delete pkg
pull/10613/head
priyawadhwa 2021-02-23 17:15:50 -08:00 committed by GitHub
commit 2ef49ae05c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 73 deletions

View File

@ -40,6 +40,7 @@ import (
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/delete"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/kubeconfig"
@ -185,9 +186,8 @@ func runDelete(cmd *cobra.Command, args []string) {
}
if orphan {
// TODO: generalize for non-KIC drivers: #8040
deletePossibleKicLeftOver(delCtx, cname, driver.Docker)
deletePossibleKicLeftOver(delCtx, cname, driver.Podman)
delete.PossibleLeftOvers(delCtx, cname, driver.Docker)
delete.PossibleLeftOvers(delCtx, cname, driver.Podman)
}
}
@ -229,74 +229,6 @@ func DeleteProfiles(profiles []*config.Profile) []error {
return errs
}
// TODO: remove and/or move to delete package: #8040
func deletePossibleKicLeftOver(ctx context.Context, cname string, driverName string) {
bin := ""
switch driverName {
case driver.Docker:
bin = oci.Docker
case driver.Podman:
bin = oci.Podman
default:
return
}
if _, err := exec.LookPath(bin); err != nil {
klog.Infof("skipping deletePossibleKicLeftOver for %s: %v", bin, err)
return
}
klog.Infof("deleting possible KIC leftovers for %s (driver=%s) ...", cname, driverName)
delLabel := fmt.Sprintf("%s=%s", oci.ProfileLabelKey, cname)
cs, err := oci.ListContainersByLabel(ctx, bin, delLabel)
if err == nil && len(cs) > 0 {
for _, c := range cs {
out.Step(style.DeletingHost, `Deleting container "{{.name}}" ...`, out.V{"name": cname})
err := oci.DeleteContainer(ctx, bin, c)
if err != nil { // it will error if there is no container to delete
klog.Errorf("error deleting container %q. You may want to delete it manually :\n%v", cname, err)
}
}
}
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)
}
if bin == oci.Podman {
// podman prune does not support --filter
return
}
errs = oci.PruneAllVolumesByLabel(ctx, bin, delLabel)
if len(errs) > 0 { // it will not error if there is nothing to delete
klog.Warningf("error pruning volume (might be okay):\n%v", errs)
}
}
func deleteProfile(ctx context.Context, profile *config.Profile) error {
klog.Infof("Deleting %s", profile.Name)
register.Reg.SetStep(register.Deleting)
@ -310,7 +242,7 @@ func deleteProfile(ctx context.Context, profile *config.Profile) error {
out.Step(style.DeletingHost, `Deleting "{{.profile_name}}" in {{.driver_name}} ...`, out.V{"profile_name": profile.Name, "driver_name": profile.Config.Driver})
for _, n := range profile.Config.Nodes {
machineName := config.MachineName(*profile.Config, n)
deletePossibleKicLeftOver(ctx, machineName, profile.Config.Driver)
delete.PossibleLeftOvers(ctx, machineName, profile.Config.Driver)
}
}
} else {

View File

@ -22,6 +22,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/delete"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/mustload"
@ -53,7 +54,7 @@ var nodeDeleteCmd = &cobra.Command{
machineName := config.MachineName(*co.Config, *n)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
deletePossibleKicLeftOver(ctx, machineName, co.Config.Driver)
delete.PossibleLeftOvers(ctx, machineName, co.Config.Driver)
}
out.Step(style.Deleted, "Node {{.name}} was successfully deleted.", out.V{"name": name})

View File

@ -0,0 +1,81 @@
/*
Copyright 2021 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package delete
import (
"context"
"fmt"
"os/exec"
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/drivers/kic/oci"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/style"
)
//PossibleLeftOvers deletes KIC & non-KIC drivers left
func PossibleLeftOvers(ctx context.Context, cname string, driverName string) {
bin := ""
switch driverName {
case driver.Docker:
bin = oci.Docker
case driver.Podman:
bin = oci.Podman
default:
return
}
if _, err := exec.LookPath(bin); err != nil {
klog.Infof("skipping deletePossibleLeftOvers for %s: %v", bin, err)
return
}
klog.Infof("deleting possible leftovers for %s (driver=%s) ...", cname, driverName)
delLabel := fmt.Sprintf("%s=%s", oci.ProfileLabelKey, cname)
cs, err := oci.ListContainersByLabel(ctx, bin, delLabel)
if err == nil && len(cs) > 0 {
for _, c := range cs {
out.Step(style.DeletingHost, `Deleting container "{{.name}}" ...`, out.V{"name": cname})
err := oci.DeleteContainer(ctx, bin, c)
if err != nil { // it will error if there is no container to delete
klog.Errorf("error deleting container %q. You may want to delete it manually :\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)
}
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)
}
if bin == oci.Podman {
// podman prune does not support --filter
return
}
errs = oci.PruneAllVolumesByLabel(ctx, bin, delLabel)
if len(errs) > 0 { // it will not error if there is nothing to delete
klog.Warningf("error pruning volume (might be okay):\n%v", errs)
}
}