From c3aafaeeb4541b0cd5303afe211fe281dbf5e4f3 Mon Sep 17 00:00:00 2001 From: Thomas Stromberg Date: Fri, 31 Jan 2020 14:19:08 -0800 Subject: [PATCH] Add IsNotExist() method to the config package, so that we can have meaningful error messages and make them comparable --- cmd/minikube/cmd/config/profile.go | 3 ++- cmd/minikube/cmd/config/profile_list.go | 2 +- cmd/minikube/cmd/dashboard.go | 2 +- cmd/minikube/cmd/delete.go | 4 ++-- cmd/minikube/cmd/kubectl.go | 5 ++--- cmd/minikube/cmd/pause.go | 2 +- cmd/minikube/cmd/start.go | 4 ++-- cmd/minikube/cmd/unpause.go | 2 +- pkg/addons/addons.go | 3 +-- pkg/addons/kubectl.go | 3 +-- pkg/minikube/config/config.go | 19 ++++++++++++++++++- 11 files changed, 32 insertions(+), 17 deletions(-) diff --git a/cmd/minikube/cmd/config/profile.go b/cmd/minikube/cmd/config/profile.go index 03eb016426c3..67203ebdbf69 100644 --- a/cmd/minikube/cmd/config/profile.go +++ b/cmd/minikube/cmd/config/profile.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" pkgConfig "k8s.io/minikube/pkg/minikube/config" + pkg_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" @@ -78,7 +79,7 @@ var ProfileCmd = &cobra.Command{ } cc, err := pkgConfig.Load(profile) // might err when loading older version of cfg file that doesn't have KeepContext field - if err != nil && !os.IsNotExist(err) { + if err != nil && !pkg_config.IsNotExist(err) { out.ErrT(out.Sad, `Error loading profile config: {{.error}}`, out.V{"error": err}) } if err == nil { diff --git a/cmd/minikube/cmd/config/profile_list.go b/cmd/minikube/cmd/config/profile_list.go index 8df06f2d2d8c..d3b90721a81a 100644 --- a/cmd/minikube/cmd/config/profile_list.go +++ b/cmd/minikube/cmd/config/profile_list.go @@ -141,7 +141,7 @@ var printProfilesJSON = func() { var body = map[string]interface{}{} - if err == nil || os.IsNotExist(err) { + if err == nil || config.IsNotExist(err) { body["valid"] = valid body["invalid"] = invalid jsonString, _ := json.Marshal(body) diff --git a/cmd/minikube/cmd/dashboard.go b/cmd/minikube/cmd/dashboard.go index 4c465e265e59..f247b365d764 100644 --- a/cmd/minikube/cmd/dashboard.go +++ b/cmd/minikube/cmd/dashboard.go @@ -60,7 +60,7 @@ var dashboardCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { profileName := viper.GetString(pkg_config.MachineProfile) cc, err := pkg_config.Load(profileName) - if err != nil && !os.IsNotExist(err) { + if err != nil && !pkg_config.IsNotExist(err) { exit.WithError("Error loading profile config", err) } diff --git a/cmd/minikube/cmd/delete.go b/cmd/minikube/cmd/delete.go index 5efe7e2ee3bb..73af19bd666c 100644 --- a/cmd/minikube/cmd/delete.go +++ b/cmd/minikube/cmd/delete.go @@ -188,7 +188,7 @@ func deleteProfile(profile *pkg_config.Profile) error { } defer api.Close() cc, err := pkg_config.Load(profile.Name) - if err != nil && !os.IsNotExist(err) { + if err != nil && !pkg_config.IsNotExist(err) { delErr := profileDeletionErr(profile.Name, fmt.Sprintf("error loading profile config: %v", err)) return DeletionError{Err: delErr, Errtype: MissingProfile} } @@ -223,7 +223,7 @@ func deleteProfile(profile *pkg_config.Profile) error { deleteProfileDirectory(profile.Name) if err := pkg_config.DeleteProfile(profile.Name); err != nil { - if os.IsNotExist(err) { + if pkg_config.IsNotExist(err) { delErr := profileDeletionErr(profile.Name, fmt.Sprintf("\"%s\" profile does not exist", profile.Name)) return DeletionError{Err: delErr, Errtype: MissingProfile} } diff --git a/cmd/minikube/cmd/kubectl.go b/cmd/minikube/cmd/kubectl.go index 706a9703e288..0a5eaea6596c 100644 --- a/cmd/minikube/cmd/kubectl.go +++ b/cmd/minikube/cmd/kubectl.go @@ -27,7 +27,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" "k8s.io/minikube/pkg/minikube/config" - pkg_config "k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/exit" "k8s.io/minikube/pkg/minikube/machine" @@ -51,8 +50,8 @@ minikube kubectl -- get pods --namespace kube-system`, } defer api.Close() - cc, err := pkg_config.Load(viper.GetString(config.MachineProfile)) - if err != nil && !os.IsNotExist(err) { + cc, err := config.Load(viper.GetString(config.MachineProfile)) + if err != nil && !config.IsNotExist(err) { out.ErrLn("Error loading profile config: %v", err) } diff --git a/cmd/minikube/cmd/pause.go b/cmd/minikube/cmd/pause.go index 493c2c0990b2..1e7c68a01dbb 100644 --- a/cmd/minikube/cmd/pause.go +++ b/cmd/minikube/cmd/pause.go @@ -53,7 +53,7 @@ func runPause(cmd *cobra.Command, args []string) { defer api.Close() cc, err := config.Load(cname) - if err != nil && !os.IsNotExist(err) { + if err != nil && !config.IsNotExist(err) { exit.WithError("Error loading profile config", err) } diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index c7246f43947f..df4916aaf0c4 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -295,7 +295,7 @@ func runStart(cmd *cobra.Command, args []string) { } existing, err := config.Load(viper.GetString(config.MachineProfile)) - if err != nil && !os.IsNotExist(err) { + if err != nil && !config.IsNotExist(err) { exit.WithCodeT(exit.Data, "Unable to load config: {{.error}}", out.V{"error": err}) } @@ -751,7 +751,7 @@ func validateUser(drvName string) { os.Exit(exit.Permissions) } _, err = config.Load(viper.GetString(config.MachineProfile)) - if err == nil || !os.IsNotExist(err) { + if err == nil || !config.IsNotExist(err) { out.T(out.Tip, "Tip: To remove this root owned cluster, run: sudo {{.cmd}} delete", out.V{"cmd": minikubeCmd()}) } if !useForce { diff --git a/cmd/minikube/cmd/unpause.go b/cmd/minikube/cmd/unpause.go index f2d86f8d9e12..4a3356edffbd 100644 --- a/cmd/minikube/cmd/unpause.go +++ b/cmd/minikube/cmd/unpause.go @@ -45,7 +45,7 @@ var unpauseCmd = &cobra.Command{ defer api.Close() cc, err := config.Load(cname) - if err != nil && !os.IsNotExist(err) { + if err != nil && !config.IsNotExist(err) { exit.WithError("Error loading profile config", err) } diff --git a/pkg/addons/addons.go b/pkg/addons/addons.go index 29c58029fc3f..06cf51d7dcd5 100644 --- a/pkg/addons/addons.go +++ b/pkg/addons/addons.go @@ -18,7 +18,6 @@ package addons import ( "fmt" - "os" "path" "strconv" "strings" @@ -140,7 +139,7 @@ func enableOrDisableAddon(name, val, profile string) error { defer api.Close() cfg, err := config.Load(profile) - if err != nil && !os.IsNotExist(err) { + if err != nil && !config.IsNotExist(err) { exit.WithCodeT(exit.Data, "Unable to load config: {{.error}}", out.V{"error": err}) } diff --git a/pkg/addons/kubectl.go b/pkg/addons/kubectl.go index 5cc96383f874..1e3fbe2a978e 100644 --- a/pkg/addons/kubectl.go +++ b/pkg/addons/kubectl.go @@ -17,7 +17,6 @@ limitations under the License. package addons import ( - "os" "os/exec" "path" @@ -53,7 +52,7 @@ func kubectlCommand(profile string, files []string, enable bool) (*exec.Cmd, err func kubernetesVersion(profile string) (string, error) { cc, err := config.Load(profile) - if err != nil && !os.IsNotExist(err) { + if err != nil && !config.IsNotExist(err) { return "", err } version := constants.DefaultKubernetesVersion diff --git a/pkg/minikube/config/config.go b/pkg/minikube/config/config.go index 134c969721f0..a5d16cc0c119 100644 --- a/pkg/minikube/config/config.go +++ b/pkg/minikube/config/config.go @@ -54,6 +54,23 @@ var ( ErrKeyNotFound = errors.New("specified key could not be found in config") ) +type ErrNotExist struct { + s string +} + +func (e *ErrNotExist) Error() string { + return e.s +} + +// IsNotExist returns whether the error means a nonexistent configuration +func IsNotExist(err error) bool { + switch err.(type) { + case *ErrNotExist: + return true + } + return false +} + // MinikubeConfig represents minikube config type MinikubeConfig map[string]interface{} @@ -151,7 +168,7 @@ func (c *simpleConfigLoader) LoadConfigFromFile(profileName string, miniHome ... if _, err := os.Stat(path); err != nil { if os.IsNotExist(err) { - return nil, fmt.Errorf("cluster %q does not exist", profileName) + return nil, &ErrNotExist{fmt.Sprintf("cluster %q does not exist", profileName)} } return nil, errors.Wrap(err, "stat") }