Validate the service for the minikube service cmd.
parent
5fb2b35698
commit
8b5d027fd3
|
|
@ -67,6 +67,11 @@ var serviceCmd = &cobra.Command{
|
|||
defer api.Close()
|
||||
|
||||
cluster.EnsureMinikubeRunningOrExit(api, 1)
|
||||
if err := validateService(namespace, service); err != nil {
|
||||
fmt.Fprintln(os.Stderr, fmt.Sprintf("service '%s' could not be found running in namespace '%s' within kubernetes",
|
||||
service, namespace))
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := util.RetryAfter(20, func() error { return CheckService(namespace, service) }, 6*time.Second); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Could not find finalized endpoint being pointed to by %s: %s\n", service, err)
|
||||
cmdutil.MaybeReportErrorAndExit(err)
|
||||
|
|
@ -100,12 +105,28 @@ func init() {
|
|||
RootCmd.AddCommand(serviceCmd)
|
||||
}
|
||||
|
||||
func validateService(namespace string, service string) error {
|
||||
client, err := cluster.GetKubernetesClient()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error validating input service name")
|
||||
}
|
||||
services := client.Services(namespace)
|
||||
if _, err = services.Get(service); err != nil {
|
||||
return errors.Wrapf(err, "service '%s' could not be found running in namespace '%s' within kubernetes", service, namespace)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckService waits for the specified service to be ready by returning an error until the service is up
|
||||
// The check is done by polling the endpoint associated with the service and when the endpoint exists, returning no error->service-online
|
||||
func CheckService(namespace string, service string) error {
|
||||
endpoints, err := cluster.GetKubernetesEndpointsWithNamespace(namespace)
|
||||
client, err := cluster.GetKubernetesClient()
|
||||
if err != nil {
|
||||
return err
|
||||
return &util.RetriableError{Err: err}
|
||||
}
|
||||
endpoints := client.Endpoints(namespace)
|
||||
if err != nil {
|
||||
return &util.RetriableError{Err: err}
|
||||
}
|
||||
endpoint, err := endpoints.Get(service)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -563,7 +563,7 @@ func GetServiceURL(api libmachine.API, namespace, service string, t *template.Te
|
|||
return "", errors.Wrap(err, "Error getting ip from host")
|
||||
}
|
||||
|
||||
client, err := getKubernetesClient()
|
||||
client, err := GetKubernetesClient()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -604,7 +604,7 @@ type endpointGetter interface {
|
|||
}
|
||||
|
||||
func getServicePort(client *unversioned.Client, namespace, service string) (int, error) {
|
||||
services := getKubernetesServicesWithNamespace(client, namespace)
|
||||
services := client.Services(namespace)
|
||||
return getServicePortFromServiceGetter(services, service)
|
||||
}
|
||||
|
||||
|
|
@ -639,7 +639,7 @@ func getServicePortFromServiceGetter(services serviceGetter, service string) (in
|
|||
return nodePort, nil
|
||||
}
|
||||
|
||||
func getKubernetesClient() (*unversioned.Client, error) {
|
||||
func GetKubernetesClient() (*unversioned.Client, error) {
|
||||
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||
configOverrides := &clientcmd.ConfigOverrides{}
|
||||
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
|
||||
|
|
@ -654,19 +654,6 @@ func getKubernetesClient() (*unversioned.Client, error) {
|
|||
return client, nil
|
||||
}
|
||||
|
||||
func getKubernetesServicesWithNamespace(client *unversioned.Client, namespace string) serviceGetter {
|
||||
return client.Services(namespace)
|
||||
}
|
||||
|
||||
func GetKubernetesEndpointsWithNamespace(namespace string) (endpointGetter, error) {
|
||||
client, err := getKubernetesClient()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Error getting kubernetes client")
|
||||
}
|
||||
endpoints := client.Endpoints(namespace)
|
||||
return endpoints, nil
|
||||
}
|
||||
|
||||
// EnsureMinikubeRunningOrExit checks that minikube has a status available and that
|
||||
// that the status is `Running`, otherwise it will exit
|
||||
func EnsureMinikubeRunningOrExit(api libmachine.API, exitStatus int) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue