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

move delete logic to a pkg #5693

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6bfe4e8
Added delete and delete_test in pkg/minikube/config
mschwrz Oct 21, 2019
73f6608
Removed unnecessary import alias "pkg_config"
mschwrz Oct 21, 2019
f2c156c
Introduced delete package
mschwrz Oct 21, 2019
3e5c096
Moved UninstallKubernetes to delete pkg
mschwrz Oct 21, 2019
842a924
Moved KillMountProcess to delete pkg
mschwrz Oct 21, 2019
cb8c72d
Moved delete logic and error handling logic to delete pkg
mschwrz Oct 21, 2019
6268013
Moved delete_test to delete pkg
mschwrz Oct 21, 2019
35e1de3
Bugfix for delete_test
mschwrz Oct 21, 2019
536e439
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 22, 2019
69568d1
Integrated changes from master
mschwrz Oct 22, 2019
42426f2
Fixed lint errors
mschwrz Oct 22, 2019
05597f0
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 23, 2019
17cc3c5
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 23, 2019
66a2f0f
Renamed delete package to profile
mschwrz Oct 23, 2019
f40a479
Moved KillMountProcess to pkg/minikube/cluster/mount.go
mschwrz Oct 23, 2019
c59193b
Removed unused import
mschwrz Oct 23, 2019
c51173e
Organized imports
mschwrz Oct 23, 2019
9ea4ee7
Moved UninstallKubernetes to cluster.go
mschwrz Oct 23, 2019
a083fac
Merge branch 'DELETE_LOGIC_TO_PKG' of https://github.com/marekschwarz…
mschwrz Oct 24, 2019
d1639c2
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 24, 2019
3378a94
Renamed DeleteDirectoryOfProfile to DeleteMachineDirectory
mschwrz Oct 24, 2019
bcc70de
Corrected renaming issues
mschwrz Oct 24, 2019
c13e885
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 25, 2019
a829218
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 25, 2019
fe4c64c
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 26, 2019
b1d1fb8
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 26, 2019
dd05eb5
Renamed delete to deleteOne
mschwrz Oct 27, 2019
62d1f28
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 28, 2019
afe28e3
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 28, 2019
f543a4c
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 29, 2019
351456d
Resolved conflicts in mount.go
mschwrz Oct 30, 2019
09bb0ea
Merge branch 'DELETE_LOGIC_TO_PKG' of https://github.com/marekschwarz…
mschwrz Oct 30, 2019
9f737c5
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Oct 31, 2019
533c0ba
Fixed review findings
mschwrz Oct 31, 2019
5731a7e
Merge remote-tracking branch 'marekschwarz/DELETE_LOGIC_TO_PKG' into …
mschwrz Oct 31, 2019
3db4f26
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Nov 3, 2019
7b0ece7
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Nov 5, 2019
d31cc21
Merge branch 'master' into DELETE_LOGIC_TO_PKG
mschwrz Dec 23, 2019
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
Prev Previous commit
Next Next commit
Removed unnecessary import alias "pkg_config"
Moved DeleteProfileDirectory to pkg
  • Loading branch information
mschwrz committed Oct 21, 2019
commit 73f66081e1573ddb7cf9daff1c26036533e422a5
43 changes: 16 additions & 27 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"github.com/spf13/viper"
cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
"k8s.io/minikube/pkg/minikube/cluster"
pkg_config "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/kubeconfig"
Expand Down Expand Up @@ -85,7 +85,7 @@ func runDelete(cmd *cobra.Command, args []string) {
exit.UsageT("usage: minikube delete --all")
}

validProfiles, invalidProfiles, err := pkg_config.ListProfiles()
validProfiles, invalidProfiles, err := config.ListProfiles()
profilesToDelete := append(validProfiles, invalidProfiles...)

if err != nil {
Expand All @@ -103,13 +103,13 @@ func runDelete(cmd *cobra.Command, args []string) {
exit.UsageT("usage: minikube delete")
}

profileName := viper.GetString(pkg_config.MachineProfile)
profile, err := pkg_config.LoadProfile(profileName)
profileName := viper.GetString(config.MachineProfile)
profile, err := config.LoadProfile(profileName)
if err != nil {
out.ErrT(out.Meh, `"{{.name}}" profile does not exist`, out.V{"name": profileName})
}

errs := DeleteProfiles([]*pkg_config.Profile{profile})
errs := DeleteProfiles([]*config.Profile{profile})
if len(errs) > 0 {
HandleDeletionErrors(errs)
} else {
Expand All @@ -119,7 +119,7 @@ func runDelete(cmd *cobra.Command, args []string) {
}

// Deletes one or more profiles
func DeleteProfiles(profiles []*pkg_config.Profile) []error {
func DeleteProfiles(profiles []*config.Profile) []error {
var errs []error
for _, profile := range profiles {
err := deleteProfile(profile)
Expand All @@ -140,8 +140,8 @@ func DeleteProfiles(profiles []*pkg_config.Profile) []error {
return errs
}

func deleteProfile(profile *pkg_config.Profile) error {
viper.Set(pkg_config.MachineProfile, profile.Name)
func deleteProfile(profile *config.Profile) error {
viper.Set(config.MachineProfile, profile.Name)

api, err := machine.NewAPIClient()
if err != nil {
Expand All @@ -150,7 +150,7 @@ func deleteProfile(profile *pkg_config.Profile) error {
}
defer api.Close()

cc, err := pkg_config.Load()
cc, err := config.Load()
if err != nil && !os.IsNotExist(err) {
out.ErrT(out.Sad, "Error loading profile {{.name}}: {{.error}}", out.V{"name": profile, "error": err})
delErr := profileDeletionErr(profile.Name, fmt.Sprintf("error loading profile config: %v", err))
Expand Down Expand Up @@ -185,9 +185,9 @@ func deleteProfile(profile *pkg_config.Profile) error {
}

// In case DeleteHost didn't complete the job.
deleteProfileDirectory(profile.Name)
config.DeleteProfileDirectory(profile.Name)

if err := pkg_config.DeleteProfile(profile.Name); err != nil {
if err := config.DeleteProfile(profile.Name); err != nil {
if os.IsNotExist(err) {
delErr := profileDeletionErr(profile.Name, fmt.Sprintf("\"%s\" profile does not exist", profile.Name))
return DeletionError{Err: delErr, Errtype: MissingProfile}
Expand All @@ -198,22 +198,22 @@ func deleteProfile(profile *pkg_config.Profile) error {

out.T(out.Crushed, `The "{{.name}}" cluster has been deleted.`, out.V{"name": profile.Name})

machineName := pkg_config.GetMachineName()
machineName := config.GetMachineName()
if err := kubeconfig.DeleteContext(constants.KubeconfigPath, machineName); err != nil {
return DeletionError{Err: fmt.Errorf("update config: %v", err), Errtype: Fatal}
}

if err := cmdcfg.Unset(pkg_config.MachineProfile); err != nil {
if err := cmdcfg.Unset(config.MachineProfile); err != nil {
return DeletionError{Err: fmt.Errorf("unset minikube profile: %v", err), Errtype: Fatal}
}
return nil
}

func deleteInvalidProfile(profile *pkg_config.Profile) []error {
func deleteInvalidProfile(profile *config.Profile) []error {
out.T(out.DeletingHost, "Trying to delete invalid profile {{.profile}}", out.V{"profile": profile.Name})

var errs []error
pathToProfile := pkg_config.ProfileFolderPath(profile.Name, localpath.MiniPath())
pathToProfile := config.ProfileFolderPath(profile.Name, localpath.MiniPath())
if _, err := os.Stat(pathToProfile); !os.IsNotExist(err) {
err := os.RemoveAll(pathToProfile)
if err != nil {
Expand All @@ -235,7 +235,7 @@ func profileDeletionErr(profileName string, additionalInfo string) error {
return fmt.Errorf("error deleting profile \"%s\": %s", profileName, additionalInfo)
}

func uninstallKubernetes(api libmachine.API, kc pkg_config.KubernetesConfig, bsName string) error {
func uninstallKubernetes(api libmachine.API, kc config.KubernetesConfig, bsName string) error {
out.T(out.Resetting, "Uninstalling Kubernetes {{.kubernetes_version}} using {{.bootstrapper_name}} ...", out.V{"kubernetes_version": kc.KubernetesVersion, "bootstrapper_name": bsName})
clusterBootstrapper, err := getClusterBootstrapper(api, bsName)
if err != nil {
Expand Down Expand Up @@ -288,17 +288,6 @@ func handleMultipleDeletionErrors(errors []error) {
}
}

func deleteProfileDirectory(profile string) {
machineDir := filepath.Join(localpath.MiniPath(), "machines", profile)
if _, err := os.Stat(machineDir); err == nil {
out.T(out.DeletingHost, `Removing {{.directory}} ...`, out.V{"directory": machineDir})
err := os.RemoveAll(machineDir)
if err != nil {
exit.WithError("Unable to remove machine directory: %v", err)
}
}
}

// killMountProcess kills the mount process, if it is running
func killMountProcess() error {
pidPath := filepath.Join(localpath.MiniPath(), constants.MountProcessFileName)
Expand Down
20 changes: 20 additions & 0 deletions pkg/minikube/config/delete.go
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
package config

import (
"os"
"path/filepath"

"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/out"
)

func DeleteProfileDirectory(profile string) {
machineDir := filepath.Join(localpath.MiniPath(), "machines", profile)
if _, err := os.Stat(machineDir); err == nil {
out.T(out.DeletingHost, `Removing {{.directory}} ...`, out.V{"directory": machineDir})
err := os.RemoveAll(machineDir)
if err != nil {
exit.WithError("Unable to remove machine directory: %v", err)
}
}
}