Skip to content

Commit

Permalink
Merge pull request #10321 from BLasan/issue-8040
Browse files Browse the repository at this point in the history
Move DeletePossibleKicLeftOver function to delete pkg
  • Loading branch information
priyawadhwa authored Feb 24, 2021
2 parents e1f8373 + b757573 commit 2ef49ae
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 73 deletions.
76 changes: 4 additions & 72 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/minikube/cmd/node_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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})
Expand Down
81 changes: 81 additions & 0 deletions pkg/minikube/delete/delete.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 2ef49ae

Please sign in to comment.