Add service command, copied & refactored from dashboard command
parent
4e7073a2e6
commit
44b2d9ce20
|
@ -135,6 +135,13 @@ To access the [Kubernetes Dashboard](http://kubernetes.io/docs/user-guide/ui/),
|
|||
minikube dashboard
|
||||
```
|
||||
|
||||
### Services
|
||||
|
||||
To access a service exposed via a node port, run this command in a shell after starting minikube to get the address:
|
||||
```shell
|
||||
minikube service [-n NAMESPACE] [--url] NAME
|
||||
```
|
||||
|
||||
## Networking
|
||||
|
||||
The minikube VM is exposed to the host system via a host-only IP address, that can be obtained with the `minikube ip` command.
|
||||
|
|
|
@ -29,7 +29,7 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
urlMode bool
|
||||
dashboardURLMode bool
|
||||
)
|
||||
|
||||
// dashboardCmd represents the dashboard command
|
||||
|
@ -40,12 +40,12 @@ var dashboardCmd = &cobra.Command{
|
|||
Run: func(cmd *cobra.Command, args []string) {
|
||||
api := libmachine.NewClient(constants.Minipath, constants.MakeMiniPath("certs"))
|
||||
defer api.Close()
|
||||
url, err := cluster.GetDashboardURL(api)
|
||||
url, err := cluster.GetServiceURL(api, "kube-system", "kubernetes-dashboard")
|
||||
if err != nil {
|
||||
glog.Errorln("Error accessing the kubernetes dashboard (is minikube running?): Error: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if urlMode {
|
||||
if dashboardURLMode {
|
||||
fmt.Fprintln(os.Stdout, url)
|
||||
} else {
|
||||
fmt.Fprintln(os.Stdout, "Opening kubernetes dashboard in default browser...")
|
||||
|
@ -55,6 +55,6 @@ var dashboardCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
func init() {
|
||||
dashboardCmd.Flags().BoolVarP(&urlMode, "url", "", false, "Display the kubernetes dashboard in the CLI instead of opening it in the default browser")
|
||||
dashboardCmd.Flags().BoolVar(&dashboardURLMode, "url", false, "Display the kubernetes dashboard in the CLI instead of opening it in the default browser")
|
||||
RootCmd.AddCommand(dashboardCmd)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/docker/machine/libmachine"
|
||||
"github.com/pkg/browser"
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/minikube/pkg/minikube/cluster"
|
||||
"k8s.io/minikube/pkg/minikube/constants"
|
||||
)
|
||||
|
||||
var (
|
||||
namespace string
|
||||
serviceURLMode bool
|
||||
)
|
||||
|
||||
// serviceCmd represents the service command
|
||||
var serviceCmd = &cobra.Command{
|
||||
Use: "service [flags] SERVICE",
|
||||
Short: "Gets the kubernetes URL for the specified service in your local cluster",
|
||||
Long: `Gets the kubernetes URL for the specified service in your local cluster`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 || len(args) > 1 {
|
||||
fmt.Fprintln(os.Stderr, "Please specify a service name.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
service := args[0]
|
||||
|
||||
api := libmachine.NewClient(constants.Minipath, constants.MakeMiniPath("certs"))
|
||||
defer api.Close()
|
||||
url, err := cluster.GetServiceURL(api, namespace, service)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
fmt.Fprintln(os.Stderr, "Check that minikube is running and that you have specified the correct namespace (-n flag).")
|
||||
os.Exit(1)
|
||||
}
|
||||
if serviceURLMode {
|
||||
fmt.Fprintln(os.Stdout, url)
|
||||
} else {
|
||||
fmt.Fprintln(os.Stdout, "Opening kubernetes service "+namespace+"/"+service+" in default browser...")
|
||||
browser.OpenURL(url)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
serviceCmd.Flags().StringVarP(&namespace, "namespace", "n", "default", "The service namespace")
|
||||
serviceCmd.Flags().BoolVar(&serviceURLMode, "url", false, "Display the kubernetes service URL in the CLI instead of opening it in the default browser")
|
||||
RootCmd.AddCommand(serviceCmd)
|
||||
}
|
|
@ -27,6 +27,7 @@ Minikube is a CLI tool that provisions and manages single-node Kubernetes cluste
|
|||
* [minikube docker-env](minikube_docker-env.md) - sets up docker env variables; similar to '$(docker-machine env)'
|
||||
* [minikube ip](minikube_ip.md) - Retrieve the IP address of the running cluster.
|
||||
* [minikube logs](minikube_logs.md) - Gets the logs of the running localkube instance, used for debugging minikube, not user code.
|
||||
* [minikube service](minikube_service.md) - Gets the kubernetes URL for the specified service in your local cluster
|
||||
* [minikube ssh](minikube_ssh.md) - Log into or run a command on a machine with SSH; similar to 'docker-machine ssh'
|
||||
* [minikube start](minikube_start.md) - Starts a local kubernetes cluster.
|
||||
* [minikube status](minikube_status.md) - Gets the status of a local kubernetes cluster.
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
## minikube service
|
||||
|
||||
Gets the kubernetes URL for the specified service in your local cluster
|
||||
|
||||
### Synopsis
|
||||
|
||||
|
||||
Gets the kubernetes URL for the specified service in your local cluster
|
||||
|
||||
```
|
||||
minikube service [flags] SERVICE
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
-n, --namespace="default": The service namespace
|
||||
--url[=false]: Display the kubernetes service URL in the CLI instead of opening it in the default browser
|
||||
```
|
||||
|
||||
### Options inherited from parent commands
|
||||
|
||||
```
|
||||
--alsologtostderr[=false]: log to standard error as well as files
|
||||
--log-flush-frequency=5s: Maximum number of seconds between log flushes
|
||||
--log_backtrace_at=:0: when logging hits line file:N, emit a stack trace
|
||||
--log_dir="": If non-empty, write log files in this directory
|
||||
--logtostderr[=false]: log to standard error instead of files
|
||||
--show-libmachine-logs[=false]: Whether or not to show logs from libmachine.
|
||||
--stderrthreshold=2: logs at or above this threshold go to stderr
|
||||
--v=0: log level for V logs
|
||||
--vmodule=: comma-separated list of pattern=N settings for file-filtered logging
|
||||
```
|
||||
|
||||
### SEE ALSO
|
||||
* [minikube](minikube.md) - Minikube is a tool for managing local Kubernetes clusters.
|
||||
|
|
@ -385,7 +385,7 @@ func CreateSSHShell(api libmachine.API, args []string) error {
|
|||
return client.Shell(strings.Join(args, " "))
|
||||
}
|
||||
|
||||
func GetDashboardURL(api libmachine.API) (string, error) {
|
||||
func GetServiceURL(api libmachine.API, namespace, service string) (string, error) {
|
||||
host, err := checkIfApiExistsAndLoad(api)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -396,7 +396,7 @@ func GetDashboardURL(api libmachine.API) (string, error) {
|
|||
return "", err
|
||||
}
|
||||
|
||||
port, err := getDashboardPort()
|
||||
port, err := getServicePort(namespace, service)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -408,20 +408,20 @@ type serviceGetter interface {
|
|||
Get(name string) (*kubeApi.Service, error)
|
||||
}
|
||||
|
||||
func getDashboardPort() (int, error) {
|
||||
services, err := getKubernetesServicesWithNamespace("kube-system")
|
||||
func getServicePort(namespace, service string) (int, error) {
|
||||
services, err := getKubernetesServicesWithNamespace(namespace)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return getDashboardPortFromServiceGetter(services)
|
||||
return getServicePortFromServiceGetter(services, service)
|
||||
}
|
||||
|
||||
func getDashboardPortFromServiceGetter(services serviceGetter) (int, error) {
|
||||
dashboardService, err := services.Get("kubernetes-dashboard")
|
||||
func getServicePortFromServiceGetter(services serviceGetter, service string) (int, error) {
|
||||
svc, err := services.Get(service)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("Error getting kubernetes-dashboard service: %s", err)
|
||||
return 0, fmt.Errorf("Error getting %s service: %s", service, err)
|
||||
}
|
||||
return int(dashboardService.Spec.Ports[0].NodePort), nil
|
||||
return int(svc.Spec.Ports[0].NodePort), nil
|
||||
}
|
||||
|
||||
func getKubernetesServicesWithNamespace(namespace string) (serviceGetter, error) {
|
||||
|
|
|
@ -453,7 +453,7 @@ func TestGetDashboardURL(t *testing.T) {
|
|||
}
|
||||
mockServiceGetter.services["kubernetes-dashboard"] = mockDashboardService
|
||||
|
||||
port, err := getDashboardPortFromServiceGetter(mockServiceGetter)
|
||||
port, err := getServicePortFromServiceGetter(mockServiceGetter, "kubernetes-dashboard")
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting dashboard port from api: Error: ", err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue