Merge pull request #5844 from rajula96reddy/issue-5836

Prevent minikube from crashing if namespace or service doesn't exist
pull/7116/head^2
Thomas Strömberg 2020-03-21 10:26:45 -07:00 committed by GitHub
commit 69545496c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
package cmd package cmd
import ( import (
"errors"
"fmt" "fmt"
"net/url" "net/url"
"os" "os"
@ -104,6 +105,11 @@ var serviceCmd = &cobra.Command{
urls, err := service.WaitForService(api, namespace, svc, serviceURLTemplate, serviceURLMode, https, wait, interval) urls, err := service.WaitForService(api, namespace, svc, serviceURLTemplate, serviceURLMode, https, wait, interval)
if err != nil { if err != nil {
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 <namespace>'. Or list out all the services using 'minikube service list'`, out.V{"service": svc, "namespace": namespace})
}
exit.WithError("Error opening service", err) exit.WithError("Error opening service", err)
} }

View File

@ -264,19 +264,34 @@ func PrintServiceList(writer io.Writer, data [][]string) {
table.Render() 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 // 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, func WaitForService(api libmachine.API, namespace string, service string, urlTemplate *template.Template, urlMode bool, https bool,
wait int, interval int) ([]string, error) { wait int, interval int) ([]string, error) {
var urlList []string var urlList []string
// Convert "Amount of time to wait" and "interval of each check" to attempts // Convert "Amount of time to wait" and "interval of each check" to attempts
if interval == 0 { if interval == 0 {
interval = 1 interval = 1
} }
err := CheckService(namespace, service)
if err != nil {
return nil, &SVCNotFoundError{err}
}
chkSVC := func() error { return CheckService(namespace, service) } chkSVC := func() error { return CheckService(namespace, service) }
if err := retry.Expo(chkSVC, time.Duration(interval)*time.Second, time.Duration(wait)*time.Second); err != nil { 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 <namespace>", service, namespace, service) return nil, &SVCNotFoundError{err}
} }
serviceURL, err := GetServiceURLsForService(api, namespace, service, urlTemplate) serviceURL, err := GetServiceURLsForService(api, namespace, service, urlTemplate)