Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent minikube from crashing if namespace or service doesn't exist #5844

Merged
merged 6 commits into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/minikube/cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"errors"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -104,6 +105,11 @@ var serviceCmd = &cobra.Command{

urls, err := service.WaitForService(api, namespace, svc, serviceURLTemplate, serviceURLMode, https, wait, interval)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because getting list of ALL services is a very quick call,
First, get a list of services and then check if the service is in that list , if there isnt return quick,
then Seconly if the service exist, then wait for the service to be up !

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)
}

Expand Down
19 changes: 17 additions & 2 deletions pkg/minikube/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,34 @@ 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
}

err := CheckService(namespace, service)
if err != nil {
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 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)
Expand Down