From a007c0790c7713c5319739bc7970011ef8433fc0 Mon Sep 17 00:00:00 2001
From: Medya Gh <medya@google.com>
Date: Fri, 14 Feb 2020 01:39:16 -0800
Subject: [PATCH] Delete volumes created by kic

---
 cmd/minikube/cmd/delete.go     | 12 ++++++--
 pkg/drivers/kic/oci/oci.go     | 14 ---------
 pkg/drivers/kic/oci/volumes.go | 52 ++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+), 17 deletions(-)
 create mode 100644 pkg/drivers/kic/oci/volumes.go

diff --git a/cmd/minikube/cmd/delete.go b/cmd/minikube/cmd/delete.go
index f58bd5440e..7523b9a0e0 100644
--- a/cmd/minikube/cmd/delete.go
+++ b/cmd/minikube/cmd/delete.go
@@ -32,6 +32,7 @@ import (
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
 	cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
+	"k8s.io/minikube/pkg/drivers/kic/oci"
 	"k8s.io/minikube/pkg/minikube/cluster"
 	"k8s.io/minikube/pkg/minikube/config"
 	pkg_config "k8s.io/minikube/pkg/minikube/config"
@@ -97,7 +98,7 @@ func runDelete(cmd *cobra.Command, args []string) {
 
 	validProfiles, invalidProfiles, err := pkg_config.ListProfiles()
 	profilesToDelete := append(validProfiles, invalidProfiles...)
-
+	glog.Infof("error listing profiless %v", err)
 	// If the purge flag is set, go ahead and delete the .minikube directory.
 	if purge && len(profilesToDelete) > 1 && !deleteAll {
 		out.ErrT(out.Notice, "Multiple minikube profiles were found - ")
@@ -112,8 +113,9 @@ func runDelete(cmd *cobra.Command, args []string) {
 			exit.UsageT("usage: minikube delete --all")
 		}
 
-		if err != nil {
-			exit.WithError("Error getting profiles to delete", err)
+		err := oci.DeleteAllVolumesByLabel(oci.Docker, fmt.Sprintf("%s=%s", oci.CreatedByLabelKey, "=true"))
+		if err != nil { // if there is no volume there won't be any error
+			glog.Warningf("error deleting left docker volumes. To see the list of volumes run: 'docker volume ls' \n:", err)
 		}
 
 		errs := DeleteProfiles(profilesToDelete)
@@ -177,6 +179,10 @@ func DeleteProfiles(profiles []*pkg_config.Profile) []error {
 
 func deleteProfile(profile *pkg_config.Profile) error {
 	viper.Set(pkg_config.MachineProfile, profile.Name)
+	err := oci.DeleteAllVolumesByLabel(oci.Docker, fmt.Sprintf("%s=%s", oci.ProfileLabelKey, profile.Name))
+	if err != nil { // if there is no volume there wont be any error
+		glog.Warningf("error deleting left docker volumes. To see the list of volumes run: 'docker volume ls' \n:%v", err)
+	}
 
 	api, err := machine.NewAPIClient()
 	if err != nil {
diff --git a/pkg/drivers/kic/oci/oci.go b/pkg/drivers/kic/oci/oci.go
index 7786b80755..6380fd34b5 100644
--- a/pkg/drivers/kic/oci/oci.go
+++ b/pkg/drivers/kic/oci/oci.go
@@ -113,20 +113,6 @@ func CreateContainerNode(p CreateParams) error {
 	return nil
 }
 
-// createDockerVolume creates a docker volume to be attached to the container with correct labels and prefixes based on profile name
-// Caution ! if volume already exists does NOT return an error and will not apply the minikube labels on it.
-// TODO: this should be fixed as a part of https://github.com/kubernetes/minikube/issues/6530
-func createDockerVolume(name string) error {
-	if err := PointToHostDockerDaemon(); err != nil {
-		return errors.Wrap(err, "point host docker-daemon")
-	}
-	cmd := exec.Command(Docker, "volume", "create", name, "--label", "name.minikube.sigs.k8s.io="+name, "--label", "craeted_by_minikube.minikube.sigs.k8s.io=true")
-	if out, err := cmd.CombinedOutput(); err != nil {
-		return errors.Wrapf(err, "output %s", string(out))
-	}
-	return nil
-}
-
 // CreateContainer creates a container with "docker/podman run"
 func createContainer(ociBinary string, image string, opts ...createOpt) ([]string, error) {
 	if err := PointToHostDockerDaemon(); err != nil {
diff --git a/pkg/drivers/kic/oci/volumes.go b/pkg/drivers/kic/oci/volumes.go
new file mode 100644
index 0000000000..5aedd6aff5
--- /dev/null
+++ b/pkg/drivers/kic/oci/volumes.go
@@ -0,0 +1,52 @@
+/*
+Copyright 2020 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 oci
+
+import (
+	"os/exec"
+
+	"github.com/pkg/errors"
+)
+
+// DeleteAllVolumesByLabel delets all volumes that have a specific label
+// example: docker volume prune -f --filter label=name.minikube.sigs.k8s.io=minikube
+func DeleteAllVolumesByLabel(ociBin string, label string) error {
+	if ociBin == Docker {
+		if err := PointToHostDockerDaemon(); err != nil {
+			return errors.Wrap(err, "point host docker-daemon")
+		}
+	}
+	cmd := exec.Command(ociBin, "volume", "prune", "-f", "--filter", label)
+	if out, err := cmd.CombinedOutput(); err != nil {
+		return errors.Wrapf(err, "output %s", string(out))
+	}
+	return nil
+}
+
+// createDockerVolume creates a docker volume to be attached to the container with correct labels and prefixes based on profile name
+// Caution ! if volume already exists does NOT return an error and will not apply the minikube labels on it.
+// TODO: this should be fixed as a part of https://github.com/kubernetes/minikube/issues/6530
+func createDockerVolume(name string) error {
+	if err := PointToHostDockerDaemon(); err != nil {
+		return errors.Wrap(err, "point host docker-daemon")
+	}
+	cmd := exec.Command(Docker, "volume", "create", name, "--label", "name.minikube.sigs.k8s.io="+name, "--label", "craeted_by_minikube.minikube.sigs.k8s.io=true")
+	if out, err := cmd.CombinedOutput(); err != nil {
+		return errors.Wrapf(err, "output %s", string(out))
+	}
+	return nil
+}