Skip to content

Commit

Permalink
Created new error type & made corresponding changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rajula96reddy committed Jan 14, 2020
1 parent 84932e6 commit 5ac578b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
13 changes: 11 additions & 2 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 @@ -45,6 +46,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",
Expand Down Expand Up @@ -74,10 +83,10 @@ var serviceCmd = &cobra.Command{
if !cluster.IsMinikubeRunning(api) {
os.Exit(1)
}

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 <namespace>'. Or list out all the services using 'minikube service list'`, out.V{"service": svc, "namespace": namespace})
}
Expand Down
27 changes: 23 additions & 4 deletions pkg/minikube/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"net/url"
"os"
"sort"
"strings"
"text/template"
"time"
Expand All @@ -48,9 +49,9 @@ import (
const (
defaultK8sClientTimeout = 60 * time.Second
// DefaultWait is the default wait time, in seconds
DefaultWait = 20
DefaultWait = 2
// DefaultInterval is the default interval, in seconds
DefaultInterval = 6
DefaultInterval = 1
)

// K8sClient represents a kubernetes client
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5ac578b

Please sign in to comment.