From 5a32eb7c4ca0cee17327c0ba573988413f335496 Mon Sep 17 00:00:00 2001 From: rajula96reddy Date: Wed, 6 Nov 2019 16:52:23 +0530 Subject: [PATCH 1/6] Updated error handling when service or namespace doesn't exist --- cmd/minikube/cmd/service.go | 2 +- pkg/minikube/service/service.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/minikube/cmd/service.go b/cmd/minikube/cmd/service.go index 19827913d9..612d45be4e 100644 --- a/cmd/minikube/cmd/service.go +++ b/cmd/minikube/cmd/service.go @@ -104,7 +104,7 @@ var serviceCmd = &cobra.Command{ urls, err := service.WaitForService(api, namespace, svc, serviceURLTemplate, serviceURLMode, https, wait, interval) if err != nil { - exit.WithError("Error opening service", err) + exit.WithCodeT(exit.Data, fmt.Sprintf("Error opening service: %s", err)) } openURLs(svc, urls) diff --git a/pkg/minikube/service/service.go b/pkg/minikube/service/service.go index 2d349d7a3b..b13157d538 100644 --- a/pkg/minikube/service/service.go +++ b/pkg/minikube/service/service.go @@ -276,7 +276,7 @@ func WaitForService(api libmachine.API, namespace string, service string, urlTem chkSVC := func() error { return CheckService(namespace, service) } if err := retry.Expo(chkSVC, time.Duration(interval)*time.Second, time.Duration(wait)*time.Second); err != nil { - return urlList, errors.Wrapf(err, "Service %s was not found in %q namespace. You may select another namespace by using 'minikube service %s -n ", service, namespace, service) + return urlList, errors.Wrapf(err, "Service %s was not found in %q namespace. You may select another namespace by using 'minikube service %s -n '. Or list out all the services using 'minikube service list'", service, namespace, service) } serviceURL, err := GetServiceURLsForService(api, namespace, service, urlTemplate) From 061b15999193f0898cdcf97317502e86713f7c90 Mon Sep 17 00:00:00 2001 From: rajula96reddy Date: Thu, 7 Nov 2019 12:08:25 +0530 Subject: [PATCH 2/6] Formatted the error output --- cmd/minikube/cmd/service.go | 2 +- pkg/minikube/service/service.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/minikube/cmd/service.go b/cmd/minikube/cmd/service.go index 612d45be4e..321b7ccbc9 100644 --- a/cmd/minikube/cmd/service.go +++ b/cmd/minikube/cmd/service.go @@ -104,7 +104,7 @@ var serviceCmd = &cobra.Command{ urls, err := service.WaitForService(api, namespace, svc, serviceURLTemplate, serviceURLMode, https, wait, interval) if err != nil { - exit.WithCodeT(exit.Data, fmt.Sprintf("Error opening service: %s", err)) + exit.WithCodeT(exit.Data, `Error opening service: {{.error}}`, out.V{"error": err}) } openURLs(svc, urls) diff --git a/pkg/minikube/service/service.go b/pkg/minikube/service/service.go index b13157d538..6e9b6e97cf 100644 --- a/pkg/minikube/service/service.go +++ b/pkg/minikube/service/service.go @@ -276,7 +276,8 @@ func WaitForService(api libmachine.API, namespace string, service string, urlTem chkSVC := func() error { return CheckService(namespace, service) } if err := retry.Expo(chkSVC, time.Duration(interval)*time.Second, time.Duration(wait)*time.Second); err != nil { - return urlList, errors.Wrapf(err, "Service %s was not found in %q namespace. You may select another namespace by using 'minikube service %s -n '. Or list out all the services using 'minikube service list'", service, namespace, service) + return urlList, errors.Errorf(`Service %s was not found in %q namespace. +You may select another namespace by using 'minikube service %s -n '. Or list out all the services using 'minikube service list'`, service, namespace, service) } serviceURL, err := GetServiceURLsForService(api, namespace, service, urlTemplate) From 8b2289836a20972a6c8a9d86e030763483c61222 Mon Sep 17 00:00:00 2001 From: rajula96reddy Date: Wed, 18 Dec 2019 16:11:46 +0530 Subject: [PATCH 3/6] Modified the previous commit using Erros.New() function --- cmd/minikube/cmd/service.go | 6 +++++- pkg/minikube/service/service.go | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/minikube/cmd/service.go b/cmd/minikube/cmd/service.go index 321b7ccbc9..d138c73af3 100644 --- a/cmd/minikube/cmd/service.go +++ b/cmd/minikube/cmd/service.go @@ -104,7 +104,11 @@ var serviceCmd = &cobra.Command{ urls, err := service.WaitForService(api, namespace, svc, serviceURLTemplate, serviceURLMode, https, wait, interval) if err != nil { - exit.WithCodeT(exit.Data, `Error opening service: {{.error}}`, out.V{"error": err}) + if err.Error() == "Service not found in namespace" { + exit.WithCodeT(exit.Data, `Service '{{.service}}' was not found in '{{.namespace}}' namespace. +You may select another namespace by using 'minikube service {{.service}} -n '. Or list out all the services using 'minikube service list'`, out.V{"service": svc, "namespace": namespace}) + } + exit.WithError("Error opening service", err) } openURLs(svc, urls) diff --git a/pkg/minikube/service/service.go b/pkg/minikube/service/service.go index 6e9b6e97cf..d7ff77a37b 100644 --- a/pkg/minikube/service/service.go +++ b/pkg/minikube/service/service.go @@ -276,8 +276,7 @@ func WaitForService(api libmachine.API, namespace string, service string, urlTem chkSVC := func() error { return CheckService(namespace, service) } if err := retry.Expo(chkSVC, time.Duration(interval)*time.Second, time.Duration(wait)*time.Second); err != nil { - return urlList, errors.Errorf(`Service %s was not found in %q namespace. -You may select another namespace by using 'minikube service %s -n '. Or list out all the services using 'minikube service list'`, service, namespace, service) + return nil, errors.New("Service not found in namespace") } serviceURL, err := GetServiceURLsForService(api, namespace, service, urlTemplate) From 40c1c80b42713118f441c0185983bffe3da576bf Mon Sep 17 00:00:00 2001 From: rajula96reddy Date: Tue, 14 Jan 2020 16:16:36 +0530 Subject: [PATCH 4/6] Created new error type & made corresponding changes --- cmd/minikube/cmd/service.go | 12 +++++++++++- pkg/minikube/service/service.go | 23 +++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cmd/minikube/cmd/service.go b/cmd/minikube/cmd/service.go index d138c73af3..7a8f37cdc5 100644 --- a/cmd/minikube/cmd/service.go +++ b/cmd/minikube/cmd/service.go @@ -17,6 +17,7 @@ limitations under the License. package cmd import ( + "errors" "fmt" "net/url" "os" @@ -57,6 +58,14 @@ var ( interval int ) +// type serviceNotFoundError struct { +// Err error +// } + +// func (t serviceNotFoundError) Error() string { +// return "Service not found: " + t.Err.Error() +// } + // serviceCmd represents the service command var serviceCmd = &cobra.Command{ Use: "service [flags] SERVICE", @@ -104,7 +113,8 @@ var serviceCmd = &cobra.Command{ urls, err := service.WaitForService(api, namespace, svc, serviceURLTemplate, serviceURLMode, https, wait, interval) if err != nil { - if err.Error() == "Service not found in namespace" { + var s *service.SVCNotFoundError + if errors.As(err, &s) { exit.WithCodeT(exit.Data, `Service '{{.service}}' was not found in '{{.namespace}}' namespace. You may select another namespace by using 'minikube service {{.service}} -n '. Or list out all the services using 'minikube service list'`, out.V{"service": svc, "namespace": namespace}) } diff --git a/pkg/minikube/service/service.go b/pkg/minikube/service/service.go index d7ff77a37b..e077f7f7be 100644 --- a/pkg/minikube/service/service.go +++ b/pkg/minikube/service/service.go @@ -22,6 +22,7 @@ import ( "io" "net/url" "os" + "sort" "strings" "text/template" "time" @@ -264,19 +265,37 @@ func PrintServiceList(writer io.Writer, data [][]string) { table.Render() } +// SVCNotFoundError error type handles 'service not found' scenarios +type SVCNotFoundError struct { + Err error +} + +// Error method for SVCNotFoundError type +func (t SVCNotFoundError) Error() string { + return "Service not found" +} + // WaitForService waits for a service, and return the urls when available func WaitForService(api libmachine.API, namespace string, service string, urlTemplate *template.Template, urlMode bool, https bool, wait int, interval int) ([]string, error) { - var urlList []string // Convert "Amount of time to wait" and "interval of each check" to attempts if interval == 0 { interval = 1 } + services, err := GetServiceURLs(api, namespace, urlTemplate) + if err != nil { + return nil, err + } + searchServices := sort.Search(len(services), func(i int) bool { return services[i].Name == service }) + if searchServices == len(services) { + return nil, &SVCNotFoundError{err} + } + chkSVC := func() error { return CheckService(namespace, service) } if err := retry.Expo(chkSVC, time.Duration(interval)*time.Second, time.Duration(wait)*time.Second); err != nil { - return nil, errors.New("Service not found in namespace") + return nil, &SVCNotFoundError{err} } serviceURL, err := GetServiceURLsForService(api, namespace, service, urlTemplate) From 5d8d3f01b5e1b13226e07c3cff1226e25ace438f Mon Sep 17 00:00:00 2001 From: rajula96reddy Date: Wed, 5 Feb 2020 00:48:00 +0530 Subject: [PATCH 5/6] Modified 'checking if service exists before retry' logic --- pkg/minikube/service/service.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pkg/minikube/service/service.go b/pkg/minikube/service/service.go index e077f7f7be..26be52d267 100644 --- a/pkg/minikube/service/service.go +++ b/pkg/minikube/service/service.go @@ -22,7 +22,6 @@ import ( "io" "net/url" "os" - "sort" "strings" "text/template" "time" @@ -283,12 +282,9 @@ func WaitForService(api libmachine.API, namespace string, service string, urlTem if interval == 0 { interval = 1 } - services, err := GetServiceURLs(api, namespace, urlTemplate) + + err := CheckService(namespace, service) if err != nil { - return nil, err - } - searchServices := sort.Search(len(services), func(i int) bool { return services[i].Name == service }) - if searchServices == len(services) { return nil, &SVCNotFoundError{err} } From 3ffdaf3c1c6c674e439f3fc5ecebcc196da93d41 Mon Sep 17 00:00:00 2001 From: rajula96reddy Date: Fri, 7 Feb 2020 09:14:09 +0530 Subject: [PATCH 6/6] Deleted unnecessary comments --- cmd/minikube/cmd/service.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/cmd/minikube/cmd/service.go b/cmd/minikube/cmd/service.go index 7a8f37cdc5..736434dc0d 100644 --- a/cmd/minikube/cmd/service.go +++ b/cmd/minikube/cmd/service.go @@ -58,14 +58,6 @@ var ( interval int ) -// type serviceNotFoundError struct { -// Err error -// } - -// func (t serviceNotFoundError) Error() string { -// return "Service not found: " + t.Err.Error() -// } - // serviceCmd represents the service command var serviceCmd = &cobra.Command{ Use: "service [flags] SERVICE",