diff --git a/cmd/minikube/cmd/config/util.go b/cmd/minikube/cmd/config/util.go index 1186325503bc..9f01482a5598 100644 --- a/cmd/minikube/cmd/config/util.go +++ b/cmd/minikube/cmd/config/util.go @@ -27,6 +27,7 @@ import ( "k8s.io/minikube/pkg/minikube/bootstrapper" "k8s.io/minikube/pkg/minikube/cluster" "k8s.io/minikube/pkg/minikube/config" + "k8s.io/minikube/pkg/minikube/console" "k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/exit" "k8s.io/minikube/pkg/minikube/machine" @@ -136,8 +137,31 @@ func EnableOrDisableAddon(name string, val string) error { return enableOrDisableAddonInternal(addon, cmd, data, enable) } +func isAddonAlreadySet(addon *assets.Addon, enable bool) error { + + addonStatus, err := addon.IsEnabled() + + if err != nil { + return errors.Wrap(err, "get the addon status") + } + + if addonStatus && enable { + return fmt.Errorf("addon %s was already enabled", addon.Name()) + } else if !addonStatus && !enable { + return fmt.Errorf("addon %s was already disabled", addon.Name()) + } + + return nil +} + func enableOrDisableAddonInternal(addon *assets.Addon, cmd bootstrapper.CommandRunner, data interface{}, enable bool) error { var err error + // check addon status before enabling/disabling it + if err := isAddonAlreadySet(addon, enable); err != nil { + console.ErrStyle(console.Conflict, "%v", err) + os.Exit(0) + } + if enable { for _, addon := range addon.Assets { var addonFile assets.CopyableFile diff --git a/cmd/minikube/cmd/config/util_test.go b/cmd/minikube/cmd/config/util_test.go index 274b1525d6f6..081533203396 100644 --- a/cmd/minikube/cmd/config/util_test.go +++ b/cmd/minikube/cmd/config/util_test.go @@ -19,6 +19,7 @@ package config import ( "testing" + "k8s.io/minikube/pkg/minikube/assets" pkgConfig "k8s.io/minikube/pkg/minikube/config" ) @@ -79,3 +80,33 @@ func TestSetBool(t *testing.T) { t.Fatalf("SetBool set wrong value") } } + +func TestIsAddonAlreadySet(t *testing.T) { + testCases := []struct { + addonName string + expectErr string + }{ + { + addonName: "ingress", + expectErr: "addon ingress was already ", + }, + { + addonName: "heapster", + expectErr: "addon heapster was already ", + }, + } + + for _, test := range testCases { + addon := assets.Addons[test.addonName] + addonStatus, _ := addon.IsEnabled() + + expectMsg := test.expectErr + "enabled" + if !addonStatus { + expectMsg = test.expectErr + "disabled" + } + err := isAddonAlreadySet(addon, addonStatus) + if err.Error() != expectMsg { + t.Errorf("Did not get expected error, \n\n expected: %+v \n\n actual: %+v", expectMsg, err) + } + } +} diff --git a/cmd/minikube/cmd/dashboard.go b/cmd/minikube/cmd/dashboard.go index 200d285d03c1..1f15b8b7009f 100644 --- a/cmd/minikube/cmd/dashboard.go +++ b/cmd/minikube/cmd/dashboard.go @@ -31,6 +31,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" configcmd "k8s.io/minikube/cmd/minikube/cmd/config" + "k8s.io/minikube/pkg/minikube/assets" "k8s.io/minikube/pkg/minikube/cluster" "k8s.io/minikube/pkg/minikube/config" pkg_config "k8s.io/minikube/pkg/minikube/config" @@ -81,12 +82,17 @@ var dashboardCmd = &cobra.Command{ } cluster.EnsureMinikubeRunningOrExit(api, 1) - // Send status messages to stderr for folks re-using this output. - console.ErrStyle(console.Enabling, "Enabling dashboard ...") - // Enable the dashboard add-on - err = configcmd.Set("dashboard", "true") - if err != nil { - exit.WithError("Unable to enable dashboard", err) + // Check dashboard status before enabling it + dashboardAddon := assets.Addons["dashboard"] + dashboardStatus, _ := dashboardAddon.IsEnabled() + if !dashboardStatus { + // Send status messages to stderr for folks re-using this output. + console.ErrStyle(console.Enabling, "Enabling dashboard ...") + // Enable the dashboard add-on + err = configcmd.Set("dashboard", "true") + if err != nil { + exit.WithError("Unable to enable dashboard", err) + } } ns := "kube-system" diff --git a/pkg/minikube/assets/addons.go b/pkg/minikube/assets/addons.go index f3d7a56307ae..03beb52abde3 100644 --- a/pkg/minikube/assets/addons.go +++ b/pkg/minikube/assets/addons.go @@ -47,6 +47,11 @@ func NewAddon(assets []*BinAsset, enabled bool, addonName string) *Addon { return a } +// Name get the addon name +func (a *Addon) Name() string { + return a.addonName +} + // IsEnabled checks if an Addon is enabled func (a *Addon) IsEnabled() (bool, error) { addonStatusText, err := config.Get(a.addonName)