Skip to content

Commit

Permalink
Add detailed JSON & status codes for all error paths
Browse files Browse the repository at this point in the history
  • Loading branch information
tstromberg committed Aug 31, 2020
1 parent a8b40a7 commit 2c5e8fd
Show file tree
Hide file tree
Showing 122 changed files with 2,881 additions and 1,927 deletions.
11 changes: 6 additions & 5 deletions cmd/minikube/cmd/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"k8s.io/minikube/pkg/minikube/image"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/node"
"k8s.io/minikube/pkg/minikube/reason"
)

// cacheImageConfigKey is the config field name used to store which images we have previously cached
Expand All @@ -43,11 +44,11 @@ var addCacheCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// Cache and load images into docker daemon
if err := machine.CacheAndLoadImages(args); err != nil {
exit.WithError("Failed to cache and load images", err)
exit.Error(reason.InternalCacheLoad, "Failed to cache and load images", err)
}
// Add images to config file
if err := cmdConfig.AddToConfigMap(cacheImageConfigKey, args); err != nil {
exit.WithError("Failed to update config", err)
exit.Error(reason.InternalAddConfig, "Failed to update config", err)
}
},
}
Expand All @@ -60,11 +61,11 @@ var deleteCacheCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// Delete images from config file
if err := cmdConfig.DeleteFromConfigMap(cacheImageConfigKey, args); err != nil {
exit.WithError("Failed to delete images from config", err)
exit.Error(reason.InternalDelConfig, "Failed to delete images from config", err)
}
// Delete images from cache/images directory
if err := image.DeleteFromCacheDir(args); err != nil {
exit.WithError("Failed to delete images", err)
exit.Error(reason.HostDelCache, "Failed to delete images", err)
}
},
}
Expand All @@ -77,7 +78,7 @@ var reloadCacheCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
err := node.CacheAndLoadImagesInConfig()
if err != nil {
exit.WithError("Failed to reload cached images", err)
exit.Error(reason.GuestCacheLoad, "Failed to reload cached images", err)
}
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/minikube/cmd/cache_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/spf13/cobra"
cmdConfig "k8s.io/minikube/cmd/minikube/cmd/config"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/reason"
)

const defaultCacheListFormat = "{{.CacheImage}}\n"
Expand All @@ -42,10 +43,10 @@ var listCacheCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
images, err := cmdConfig.ListConfigMap(cacheImageConfigKey)
if err != nil {
exit.WithError("Failed to get image map", err)
exit.Error(reason.InternalListConfig, "Failed to get image map", err)
}
if err := cacheList(images); err != nil {
exit.WithError("Failed to list cached images", err)
exit.Error(reason.InternalCacheList, "Failed to list cached images", err)
}
},
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/minikube/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
)

const longDescription = `
Expand Down Expand Up @@ -73,27 +74,26 @@ var completionCmd = &cobra.Command{
Long: longDescription,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
exit.UsageT("Usage: minikube completion SHELL")
exit.Message(reason.Usage, "Usage: minikube completion SHELL")
}
if args[0] != "bash" && args[0] != "zsh" && args[0] != "fish" {
exit.UsageT("Sorry, completion support is not yet implemented for {{.name}}", out.V{"name": args[0]})
exit.Message(reason.Usage, "Sorry, completion support is not yet implemented for {{.name}}", out.V{"name": args[0]})
} else if args[0] == "bash" {
err := GenerateBashCompletion(os.Stdout, cmd.Parent())
if err != nil {
exit.WithError("bash completion failed", err)
exit.Error(reason.InternalCompletion, "bash completion failed", err)
}
} else if args[0] == "zsh" {
err := GenerateZshCompletion(os.Stdout, cmd.Parent())
if err != nil {
exit.WithError("zsh completion failed", err)
exit.Error(reason.InternalCompletion, "zsh completion failed", err)
}
} else {
err := GenerateFishCompletion(os.Stdout, cmd.Parent())
if err != nil {
exit.WithError("fish completion failed", err)
exit.Error(reason.InternalCompletion, "fish completion failed", err)
}
}

},
}

Expand Down
8 changes: 5 additions & 3 deletions cmd/minikube/cmd/config/addons_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/mustload"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/style"
)

var addonListOutput string
Expand All @@ -47,7 +49,7 @@ var addonsListCmd = &cobra.Command{
Long: "Lists all available minikube addons as well as their current statuses (enabled/disabled)",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
exit.UsageT("usage: minikube addons list")
exit.Message(reason.Usage, "usage: minikube addons list")
}

_, cc := mustload.Partial(ClusterFlagValue())
Expand All @@ -57,7 +59,7 @@ var addonsListCmd = &cobra.Command{
case "json":
printAddonsJSON(cc)
default:
exit.WithCodeT(exit.BadUsage, fmt.Sprintf("invalid output format: %s. Valid values: 'list', 'json'", addonListOutput))
exit.Message(reason.Usage, fmt.Sprintf("invalid output format: %s. Valid values: 'list', 'json'", addonListOutput))
}
},
}
Expand Down Expand Up @@ -115,7 +117,7 @@ var printAddonsList = func(cc *config.ClusterConfig) {
glog.Errorf("list profiles returned error: %v", err)
}
if len(v) > 1 {
out.T(out.Tip, "To see addons list for other profiles use: `minikube addons -p name list`")
out.T(style.Tip, "To see addons list for other profiles use: `minikube addons -p name list`")
}
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/minikube/cmd/config/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/mustload"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/service"
"k8s.io/minikube/pkg/minikube/style"
)

var addonsConfigureCmd = &cobra.Command{
Expand All @@ -34,7 +36,7 @@ var addonsConfigureCmd = &cobra.Command{
Long: "Configures the addon w/ADDON_NAME within minikube (example: minikube addons configure registry-creds). For a list of available addons use: minikube addons list ",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
exit.UsageT("usage: minikube addons configure ADDON_NAME")
exit.Message(reason.Usage, "usage: minikube addons configure ADDON_NAME")
}

addon := args[0]
Expand Down Expand Up @@ -123,7 +125,6 @@ var addonsConfigureCmd = &cobra.Command{
"cloud": "ecr",
"kubernetes.io/minikube-addons": "registry-creds",
})

if err != nil {
out.FailureT("ERROR creating `registry-creds-ecr` secret: {{.error}}", out.V{"error": err})
}
Expand Down Expand Up @@ -204,7 +205,7 @@ var addonsConfigureCmd = &cobra.Command{
}

if err := config.SaveProfile(profile, cfg); err != nil {
out.ErrT(out.FatalType, "Failed to save config {{.profile}}", out.V{"profile": profile})
out.ErrT(style.Fatal, "Failed to save config {{.profile}}", out.V{"profile": profile})
}

default:
Expand Down
10 changes: 6 additions & 4 deletions cmd/minikube/cmd/config/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"k8s.io/minikube/pkg/addons"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/style"
)

var addonsDisableCmd = &cobra.Command{
Expand All @@ -29,18 +31,18 @@ var addonsDisableCmd = &cobra.Command{
Long: "Disables the addon w/ADDON_NAME within minikube (example: minikube addons disable dashboard). For a list of available addons use: minikube addons list ",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
exit.UsageT("usage: minikube addons disable ADDON_NAME")
exit.Message(reason.Usage, "usage: minikube addons disable ADDON_NAME")
}

addon := args[0]
if addon == "heapster" {
exit.WithCodeT(exit.Unavailable, "The heapster addon is depreciated. please try to disable metrics-server instead")
exit.Message(reason.AddonUnsupported, "The heapster addon is depreciated. please try to disable metrics-server instead")
}
err := addons.SetAndSave(ClusterFlagValue(), addon, "false")
if err != nil {
exit.WithError("disable failed", err)
exit.Error(reason.InternalDisable, "disable failed", err)
}
out.T(out.AddonDisable, `"The '{{.minikube_addon}}' addon is disabled`, out.V{"minikube_addon": addon})
out.T(style.AddonDisable, `"The '{{.minikube_addon}}' addon is disabled`, out.V{"minikube_addon": addon})
},
}

Expand Down
12 changes: 7 additions & 5 deletions cmd/minikube/cmd/config/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/style"
)

var addonsEnableCmd = &cobra.Command{
Expand All @@ -32,32 +34,32 @@ var addonsEnableCmd = &cobra.Command{
Long: "Enables the addon w/ADDON_NAME within minikube (example: minikube addons enable dashboard). For a list of available addons use: minikube addons list ",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
exit.UsageT("usage: minikube addons enable ADDON_NAME")
exit.Message(reason.Usage, "usage: minikube addons enable ADDON_NAME")
}
addon := args[0]
// replace heapster as metrics-server because heapster is deprecated
if addon == "heapster" {
out.T(out.Waiting, "enable metrics-server addon instead of heapster addon because heapster is deprecated")
out.T(style.Waiting, "enable metrics-server addon instead of heapster addon because heapster is deprecated")
addon = "metrics-server"
}
err := addons.SetAndSave(ClusterFlagValue(), addon, "true")
if err != nil {
exit.WithError("enable failed", err)
exit.Error(reason.InternalEnable, "enable failed", err)
}
if addon == "dashboard" {
tipProfileArg := ""
if ClusterFlagValue() != constants.DefaultClusterName {
tipProfileArg = fmt.Sprintf(" -p %s", ClusterFlagValue())
}
out.T(out.Tip, `Some dashboard features require the metrics-server addon. To enable all features please run:
out.T(style.Tip, `Some dashboard features require the metrics-server addon. To enable all features please run:
minikube{{.profileArg}} addons enable metrics-server
`, out.V{"profileArg": tipProfileArg})

}

out.T(out.AddonEnable, "The '{{.addonName}}' addon is enabled", out.V{"addonName": addon})
out.T(style.AddonEnable, "The '{{.addonName}}' addon is enabled", out.V{"addonName": addon})
},
}

Expand Down
20 changes: 11 additions & 9 deletions cmd/minikube/cmd/config/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/mustload"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/service"
"k8s.io/minikube/pkg/minikube/style"
)

var (
Expand All @@ -47,13 +49,13 @@ var addonsOpenCmd = &cobra.Command{
PreRun: func(cmd *cobra.Command, args []string) {
t, err := template.New("addonsURL").Parse(addonsURLFormat)
if err != nil {
exit.UsageT("The value passed to --format is invalid: {{.error}}", out.V{"error": err})
exit.Message(reason.Usage, "The value passed to --format is invalid: {{.error}}", out.V{"error": err})
}
addonsURLTemplate = t
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
exit.UsageT("usage: minikube addons open ADDON_NAME")
exit.Message(reason.Usage, "usage: minikube addons open ADDON_NAME")
}
addonName := args[0]

Expand All @@ -62,14 +64,14 @@ var addonsOpenCmd = &cobra.Command{

addon, ok := assets.Addons[addonName] // validate addon input
if !ok {
exit.WithCodeT(exit.Data, `addon '{{.name}}' is not a valid addon packaged with minikube.
exit.Message(reason.Usage, `addon '{{.name}}' is not a valid addon packaged with minikube.
To see the list of available addons run:
minikube addons list`, out.V{"name": addonName})
}

enabled := addon.IsEnabled(co.Config)
if !enabled {
exit.WithCodeT(exit.Unavailable, `addon '{{.name}}' is currently not enabled.
exit.Message(reason.AddonNotEnabled, `addon '{{.name}}' is currently not enabled.
To enable this addon run:
minikube addons enable {{.name}}`, out.V{"name": addonName})
}
Expand All @@ -79,25 +81,25 @@ minikube addons enable {{.name}}`, out.V{"name": addonName})

serviceList, err := service.GetServiceListByLabel(cname, namespace, key, addonName)
if err != nil {
exit.WithCodeT(exit.Unavailable, "Error getting service with namespace: {{.namespace}} and labels {{.labelName}}:{{.addonName}}: {{.error}}", out.V{"namespace": namespace, "labelName": key, "addonName": addonName, "error": err})
exit.Message(reason.SvcList, "Error getting service with namespace: {{.namespace}} and labels {{.labelName}}:{{.addonName}}: {{.error}}", out.V{"namespace": namespace, "labelName": key, "addonName": addonName, "error": err})
}
if len(serviceList.Items) == 0 {
exit.WithCodeT(exit.Config, `This addon does not have an endpoint defined for the 'addons open' command.
exit.Message(reason.SvcNotFound, `This addon does not have an endpoint defined for the 'addons open' command.
You can add one by annotating a service with the label {{.labelName}}:{{.addonName}}`, out.V{"labelName": key, "addonName": addonName})
}
for i := range serviceList.Items {
svc := serviceList.Items[i].ObjectMeta.Name
var urlString []string

if urlString, err = service.WaitForService(co.API, co.Config.Name, namespace, svc, addonsURLTemplate, addonsURLMode, https, wait, interval); err != nil {
exit.WithCodeT(exit.Unavailable, "Wait failed: {{.error}}", out.V{"error": err})
exit.Message(reason.SvcTimeout, "Wait failed: {{.error}}", out.V{"error": err})
}

if len(urlString) != 0 {
out.T(out.Celebrate, "Opening Kubernetes service {{.namespace_name}}/{{.service_name}} in default browser...", out.V{"namespace_name": namespace, "service_name": svc})
out.T(style.Celebrate, "Opening Kubernetes service {{.namespace_name}}/{{.service_name}} in default browser...", out.V{"namespace_name": namespace, "service_name": svc})
for _, url := range urlString {
if err := browser.OpenURL(url); err != nil {
exit.WithError(fmt.Sprintf("browser failed to open url %s", url), err)
exit.Error(reason.HostBrowser, fmt.Sprintf("browser failed to open url %s", url), err)
}
}
}
Expand Down
Loading

0 comments on commit 2c5e8fd

Please sign in to comment.