From 808477ae1ea6ca715a2328471f3d0e165396f8dd Mon Sep 17 00:00:00 2001 From: Marek Schwarz Date: Fri, 23 Aug 2019 19:47:41 +0200 Subject: [PATCH] Moved GetMachinePath to machine.go and renamed it to MachinePath Added comments to public functions --- cmd/minikube/cmd/delete.go | 4 +++- cmd/minikube/cmd/delete_test.go | 17 +++++++++-------- pkg/minikube/cluster/machine.go | 13 +++++++++++++ pkg/minikube/constants/constants.go | 9 --------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/cmd/minikube/cmd/delete.go b/cmd/minikube/cmd/delete.go index 3bef814c42..3e388b2c43 100644 --- a/cmd/minikube/cmd/delete.go +++ b/cmd/minikube/cmd/delete.go @@ -113,6 +113,7 @@ func runDelete(cmd *cobra.Command, args []string) { } } +// Deletes one or more profiles func DeleteProfiles(profiles []*pkg_config.Profile) []error { var errs []error for _, profile := range profiles { @@ -223,7 +224,7 @@ func deleteInvalidProfile(profile *pkg_config.Profile) []error { } } - pathToMachine := constants.GetMachinePath(profile.Name, constants.GetMinipath()) + pathToMachine := cluster.MachinePath(profile.Name, constants.GetMinipath()) if _, err := os.Stat(pathToMachine); !os.IsNotExist(err) { err := os.RemoveAll(pathToMachine) if err != nil { @@ -248,6 +249,7 @@ func uninstallKubernetes(api libmachine.API, kc pkg_config.KubernetesConfig, bsN return nil } +// Handles deletion error from DeleteProfiles func HandleDeletionErrors(errors []error) { if len(errors) == 1 { handleSingleDeletionError(errors[0]) diff --git a/cmd/minikube/cmd/delete_test.go b/cmd/minikube/cmd/delete_test.go index a377c3135e..d76e7e6148 100644 --- a/cmd/minikube/cmd/delete_test.go +++ b/cmd/minikube/cmd/delete_test.go @@ -22,6 +22,7 @@ import ( "path/filepath" "testing" + "k8s.io/minikube/pkg/minikube/cluster" "k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/constants" ) @@ -60,7 +61,7 @@ func TestDeleteProfileWithValidConfig(t *testing.T) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } - pathToMachine := constants.GetMachinePath(profile.Name, constants.GetMinipath()) + pathToMachine := cluster.MachinePath(profile.Name, constants.GetMinipath()) if _, err := os.Stat(pathToMachine); !os.IsNotExist(err) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } @@ -108,7 +109,7 @@ func TestDeleteProfileWithEmptyProfileConfig(t *testing.T) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } - pathToMachine := constants.GetMachinePath(profile.Name, constants.GetMinipath()) + pathToMachine := cluster.MachinePath(profile.Name, constants.GetMinipath()) if _, err := os.Stat(pathToMachine); !os.IsNotExist(err) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } @@ -156,7 +157,7 @@ func TestDeleteProfileWithInvalidProfileConfig(t *testing.T) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } - pathToMachine := constants.GetMachinePath(profile.Name, constants.GetMinipath()) + pathToMachine := cluster.MachinePath(profile.Name, constants.GetMinipath()) if _, err := os.Stat(pathToMachine); !os.IsNotExist(err) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } @@ -204,7 +205,7 @@ func TestDeleteProfileWithPartialProfileConfig(t *testing.T) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } - pathToMachine := constants.GetMachinePath(profile.Name, constants.GetMinipath()) + pathToMachine := cluster.MachinePath(profile.Name, constants.GetMinipath()) if _, err := os.Stat(pathToMachine); !os.IsNotExist(err) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } @@ -252,7 +253,7 @@ func TestDeleteProfileWithMissingMachineConfig(t *testing.T) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } - pathToMachine := constants.GetMachinePath(profile.Name, constants.GetMinipath()) + pathToMachine := cluster.MachinePath(profile.Name, constants.GetMinipath()) if _, err := os.Stat(pathToMachine); !os.IsNotExist(err) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } @@ -300,7 +301,7 @@ func TestDeleteProfileWithEmptyMachineConfig(t *testing.T) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } - pathToMachine := constants.GetMachinePath(profile.Name, constants.GetMinipath()) + pathToMachine := cluster.MachinePath(profile.Name, constants.GetMinipath()) if _, err := os.Stat(pathToMachine); !os.IsNotExist(err) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } @@ -348,7 +349,7 @@ func TestDeleteProfileWithInvalidMachineConfig(t *testing.T) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } - pathToMachine := constants.GetMachinePath(profile.Name, constants.GetMinipath()) + pathToMachine := cluster.MachinePath(profile.Name, constants.GetMinipath()) if _, err := os.Stat(pathToMachine); !os.IsNotExist(err) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } @@ -396,7 +397,7 @@ func TestDeleteProfileWithPartialMachineConfig(t *testing.T) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } - pathToMachine := constants.GetMachinePath(profile.Name, constants.GetMinipath()) + pathToMachine := cluster.MachinePath(profile.Name, constants.GetMinipath()) if _, err := os.Stat(pathToMachine); !os.IsNotExist(err) { t.Fatalf("Profile folder of profile \"%s\" was not deleted", profile.Name) } diff --git a/pkg/minikube/cluster/machine.go b/pkg/minikube/cluster/machine.go index cd28295ee2..e674bfe095 100644 --- a/pkg/minikube/cluster/machine.go +++ b/pkg/minikube/cluster/machine.go @@ -30,6 +30,7 @@ type Machine struct { *host.Host } +// IsValid checks if the machine has the essential info needed for a machine func (h *Machine) IsValid() bool { if h == nil { return false @@ -57,6 +58,8 @@ func (h *Machine) IsValid() bool { return true } +// ListsMachines return all valid and invalid machines +// If a machine is valid or invalid is determined by the cluster.IsValid function func ListMachines(miniHome ...string) (validMachines []*Machine, inValidMachines []*Machine, err error) { pDirs, err := machineDirs(miniHome...) if err != nil { @@ -77,6 +80,7 @@ func ListMachines(miniHome ...string) (validMachines []*Machine, inValidMachines return validMachines, inValidMachines, nil } +// Loads a machine or throws an error if the machine could not be loadedG func LoadMachine(name string) (*Machine, error) { api, err := machine.NewAPIClient() if err != nil { @@ -112,3 +116,12 @@ func machineDirs(miniHome ...string) (dirs []string, err error) { } return dirs, err } + +// MachinePath returns the Minikube machine path of a machine +func MachinePath(machine string, miniHome ...string) string { + miniPath := constants.GetMinipath() + if len(miniHome) > 0 { + miniPath = miniHome[0] + } + return filepath.Join(miniPath, "machines", machine) +} diff --git a/pkg/minikube/constants/constants.go b/pkg/minikube/constants/constants.go index 4378af264a..6bc072b91c 100644 --- a/pkg/minikube/constants/constants.go +++ b/pkg/minikube/constants/constants.go @@ -210,15 +210,6 @@ func GetProfilePath(profile string, miniHome ...string) string { return filepath.Join(miniPath, "profiles", profile) } -// GetMachinePath returns the Minikube machine path of a machine -func GetMachinePath(machine string, miniHome ...string) string { - miniPath := GetMinipath() - if len(miniHome) > 0 { - miniPath = miniHome[0] - } - return filepath.Join(miniPath, "machines", machine) -} - // AddonsPath is the default path of the addons configuration const AddonsPath = "/etc/kubernetes/addons"