Skip to content

Commit

Permalink
make error message more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
norshtein committed Feb 18, 2023
1 parent 4f14f64 commit b381671
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
7 changes: 5 additions & 2 deletions cmd/minikube/cmd/config/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ var addonsDisableCmd = &cobra.Command{
if len(args) != 1 {
exit.Message(reason.Usage, "usage: minikube addons disable ADDON_NAME")
}

err := addons.CheckPaused(ClusterFlagValue(), false)
if err != nil {
exit.Error(reason.InternalAddonDisablePaused, "disable failed", err)
}
addon := args[0]
if addon == "heapster" {
exit.Message(reason.AddonUnsupported, "The heapster addon is depreciated. please try to disable metrics-server instead")
}
err := addons.SetAndSave(ClusterFlagValue(), addon, "false")
err = addons.SetAndSave(ClusterFlagValue(), addon, "false")
if err != nil {
exit.Error(reason.InternalAddonDisable, "disable failed", err)
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/minikube/cmd/config/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ var addonsEnableCmd = &cobra.Command{
if len(args) != 1 {
exit.Message(reason.Usage, "usage: minikube addons enable ADDON_NAME")
}
err := addons.CheckPaused(ClusterFlagValue(), true)
if err != nil {
exit.Error(reason.InternalAddonEnablePaused, "enabled failed", err)
}
addon := args[0]
isDeprecated, replacement, msg := addons.Deprecations(addon)
if isDeprecated && replacement == "" {
Expand Down Expand Up @@ -68,7 +72,7 @@ You can view the list of minikube maintainers at: https://github.com/kubernetes/
}
viper.Set(config.AddonImages, images)
viper.Set(config.AddonRegistries, registries)
err := addons.SetAndSave(ClusterFlagValue(), addon, "true")
err = addons.SetAndSave(ClusterFlagValue(), addon, "true")
if err != nil && !errors.Is(err, addons.ErrSkipThisAddon) {
exit.Error(reason.InternalAddonEnable, "enable failed", err)
}
Expand Down
69 changes: 51 additions & 18 deletions pkg/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,24 +281,6 @@ func EnableOrDisableAddon(cc *config.ClusterConfig, name string, val string) err
return errors.Wrap(err, "command runner")
}

crName := cc.KubernetesConfig.ContainerRuntime
cr, err := cruntime.New(cruntime.Config{Type: crName, Runner: runner})
if err != nil {
return errors.Wrap(err, "container runtime")
}
runtimePaused, err := cluster.CheckIfPaused(cr, []string{"kube-system"})
if err != nil {
return errors.Wrap(err, "check paused")
}
if runtimePaused {
action := "disable"
if enable {
action = "enable"
}
msg := fmt.Sprintf("can't %s addon on a paused cluster, please unpause the cluster firstly.", action)
out.Styled(style.Shrug, msg)
return errors.New(msg)
}
bail, err := addonSpecificChecks(cc, name, enable, runner)
if err != nil {
return err
Expand Down Expand Up @@ -598,3 +580,54 @@ func UpdateConfigToDisable(cc *config.ClusterConfig) {
}
}
}

// CheckPaused checks whether the cluster is paused before enable/disable an addon.
func CheckPaused(profile string, enable bool) error {
klog.Info("checking whether the cluster is paused")

cc, err := config.Load(profile)
if err != nil {
return errors.Wrap(err, "loading profile")
}

api, err := machine.NewAPIClient()
if err != nil {
return errors.Wrap(err, "machine client")
}
defer api.Close()

cp, err := config.PrimaryControlPlane(cc)
if err != nil {
return errors.Wrap(err, "control plane")
}

host, err := machine.LoadHost(api, config.MachineName(*cc, cp))
if err != nil {
return errors.Wrap(err, "get host")
}

runner, err := machine.CommandRunner(host)
if err != nil {
return errors.Wrap(err, "command runner")
}

crName := cc.KubernetesConfig.ContainerRuntime
cr, err := cruntime.New(cruntime.Config{Type: crName, Runner: runner})
if err != nil {
return errors.Wrap(err, "container runtime")
}
runtimePaused, err := cluster.CheckIfPaused(cr, []string{"kube-system"})
if err != nil {
return errors.Wrap(err, "check paused")
}
if runtimePaused {
action := "disable"
if enable {
action = "enable"
}
msg := fmt.Sprintf("can't %s addon on a paused cluster, please unpause the cluster firstly.", action)
out.Styled(style.Shrug, msg)
return errors.New(msg)
}
return nil
}
5 changes: 5 additions & 0 deletions pkg/minikube/reason/reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ var (
InternalAddonDisable = Kind{ID: "MK_ADDON_DISABLE", ExitCode: ExProgramError}
// minikube could not enable an addon, e.g. dashboard addon
InternalAddonEnable = Kind{ID: "MK_ADDON_ENABLE", ExitCode: ExProgramError}
// minikube could not enable an addon on a paused cluster
InternalAddonEnablePaused = Kind{ID: "MK_ADDON_ENABLE_PAUSED", ExitCode: ExProgramConflict}
// minikube could not disable an addon on a paused cluster
InternalAddonDisablePaused = Kind{ID: "MK_ADDON_DISABLE_PAUSED", ExitCode: ExProgramConflict}

// minikube failed to update internal configuration, such as the cached images config map
InternalAddConfig = Kind{ID: "MK_ADD_CONFIG", ExitCode: ExProgramError}
// minikube failed to create a cluster bootstrapper
Expand Down

0 comments on commit b381671

Please sign in to comment.