Added dashboard cmd to minikube. Also added dependencies via godep.

In the future will add feature to wait for dashboard to start and then
display if it is not running yet.
pull/170/head
aprindle 2016-06-04 15:02:05 -07:00
parent da18f11bad
commit adf5fc48fa
11 changed files with 337 additions and 1 deletions

6
Godeps/Godeps.json generated
View File

@ -1,7 +1,7 @@
{ {
"ImportPath": "k8s.io/minikube", "ImportPath": "k8s.io/minikube",
"GoVersion": "go1.6", "GoVersion": "go1.6",
"GodepVersion": "v63", "GodepVersion": "v74",
"Packages": [ "Packages": [
"./..." "./..."
], ],
@ -1340,6 +1340,10 @@
"ImportPath": "github.com/pborman/uuid", "ImportPath": "github.com/pborman/uuid",
"Rev": "ca53cad383cad2479bbba7f7a1a05797ec1386e4" "Rev": "ca53cad383cad2479bbba7f7a1a05797ec1386e4"
}, },
{
"ImportPath": "github.com/pkg/browser",
"Rev": "9302be274faad99162b9d48ec97b24306872ebb0"
},
{ {
"ImportPath": "github.com/pmezard/go-difflib/difflib", "ImportPath": "github.com/pmezard/go-difflib/difflib",
"Rev": "d8ed2627bdf02c080bf22230dbb337003b7aba2d" "Rev": "d8ed2627bdf02c080bf22230dbb337003b7aba2d"

View File

@ -0,0 +1,60 @@
/*
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/golang/glog"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/constants"
)
var (
urlMode bool
)
// dashboardCmd represents the dashboard command
var dashboardCmd = &cobra.Command{
Use: "dashboard",
Short: "Opens/displays the kubernetes dashboard URL for your local cluster",
Long: `Opens/displays the kubernetes dashboard URL for your local cluster`,
Run: func(cmd *cobra.Command, args []string) {
api := libmachine.NewClient(constants.Minipath, constants.MakeMiniPath("certs"))
defer api.Close()
url, err := cluster.GetDashboardURL(api)
if err != nil {
glog.Errorln("Error accessing the kubernetes dashboard (is minikube running?): Error: ", err)
os.Exit(1)
}
if urlMode {
fmt.Fprintln(os.Stdout, url)
} else {
fmt.Fprintln(os.Stdout, "Opening kubernetes dashboard in default browser...")
browser.OpenURL(url)
}
},
}
func init() {
dashboardCmd.Flags().BoolVarP(&urlMode, "url", "", false, "Display the kubernetes dashboard in the CLI instead of opening it in the default browser")
RootCmd.AddCommand(dashboardCmd)
}

View File

@ -31,6 +31,9 @@ import (
"github.com/docker/machine/libmachine/host" "github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/state" "github.com/docker/machine/libmachine/state"
"github.com/golang/glog" "github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
"k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/sshutil" "k8s.io/minikube/pkg/minikube/sshutil"
"k8s.io/minikube/pkg/util" "k8s.io/minikube/pkg/util"
@ -315,3 +318,58 @@ func CreateSSHShell(api libmachine.API, args []string) error {
} }
return client.Shell(strings.Join(args, " ")) return client.Shell(strings.Join(args, " "))
} }
func GetDashboardURL(api libmachine.API) (string, error) {
host, err := checkIfApiExistsAndLoad(api)
if err != nil {
return "", err
}
ip, err := host.Driver.GetIP()
if err != nil {
return "", err
}
port, err := getDashboardPort()
if err != nil {
return "", err
}
return fmt.Sprintf("http://%s:%d", ip, port), nil
}
type serviceGetter interface {
Get(name string) (*api.Service, error)
}
func getDashboardPort() (int, error) {
services, err := getKubernetesServicesWithNamespace("kube-system")
if err != nil {
return 0, err
}
return getDashboardPortFromServiceGetter(services)
}
func getDashboardPortFromServiceGetter(services serviceGetter) (int, error) {
dashboardService, err := services.Get("kubernetes-dashboard")
if err != nil {
return 0, fmt.Errorf("Error getting kubernetes-dashboard service: %s", err)
}
return int(dashboardService.Spec.Ports[0].NodePort), nil
}
func getKubernetesServicesWithNamespace(namespace string) (serviceGetter, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
config, err := kubeConfig.ClientConfig()
if err != nil {
return nil, fmt.Errorf("Error creating kubeConfig: %s", err)
}
client, err := unversioned.New(config)
if err != nil {
return nil, err
}
services := client.Services(namespace)
return services, nil
}

View File

@ -27,6 +27,7 @@ import (
"github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/host" "github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/state" "github.com/docker/machine/libmachine/state"
"k8s.io/kubernetes/pkg/api"
"k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/tests" "k8s.io/minikube/pkg/minikube/tests"
) )
@ -417,3 +418,44 @@ func TestCreateSSHShell(t *testing.T) {
t.Fatalf("Expected ssh session to be run") t.Fatalf("Expected ssh session to be run")
} }
} }
type MockServiceGetter struct {
services map[string]api.Service
}
func NewMockServiceGetter() *MockServiceGetter {
return &MockServiceGetter{
services: make(map[string]api.Service),
}
}
func (mockServiceGetter *MockServiceGetter) Get(name string) (*api.Service, error) {
service, ok := mockServiceGetter.services[name]
if !ok {
return nil, fmt.Errorf("Error getting %s service from mockServiceGetter", name)
}
return &service, nil
}
func TestGetDashboardURL(t *testing.T) {
mockServiceGetter := NewMockServiceGetter()
nodeport := api.ServicePort{
NodePort: 1234,
}
mockDashboardService := api.Service{
Spec: api.ServiceSpec{
Ports: []api.ServicePort{nodeport},
},
}
mockServiceGetter.services["kubernetes-dashboard"] = mockDashboardService
port, err := getDashboardPortFromServiceGetter(mockServiceGetter)
if err != nil {
t.Fatalf("Error getting dashboard port from api: Error: ", err)
}
expected := 1234
if port != expected {
t.Fatalf("Error getting dashboard port from api: Expected: %s, Got: %s", port, expected)
}
}

23
vendor/github.com/pkg/browser/LICENSE generated vendored Normal file
View File

@ -0,0 +1,23 @@
Copyright (c) 2014, Dave Cheney <dave@cheney.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

55
vendor/github.com/pkg/browser/README.md generated vendored Normal file
View File

@ -0,0 +1,55 @@
# browser
import "github.com/pkg/browser"
Package browser provides helpers to open files, readers, and urls in a browser window.
The choice of which browser is started is entirely client dependant.
## Variables
``` go
var Stderr io.Writer = os.Stderr
```
Stderr is the io.Writer to which executed commands write standard error.
``` go
var Stdout io.Writer = os.Stdout
```
Stdout is the io.Writer to which executed commands write standard output.
## func OpenFile
``` go
func OpenFile(path string) error
```
OpenFile opens new browser window for the file path.
## func OpenReader
``` go
func OpenReader(r io.Reader) error
```
OpenReader consumes the contents of r and presents the
results in a new browser window.
## func OpenURL
``` go
func OpenURL(url string) error
```
OpenURL opens a new browser window pointing to url.
- - -
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)

62
vendor/github.com/pkg/browser/browser.go generated vendored Normal file
View File

@ -0,0 +1,62 @@
// Package browser provides helpers to open files, readers, and urls in a browser window.
//
// The choice of which browser is started is entirely client dependant.
package browser
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)
// Stdout is the io.Writer to which executed commands write standard output.
var Stdout io.Writer = os.Stdout
// Stderr is the io.Writer to which executed commands write standard error.
var Stderr io.Writer = os.Stderr
// OpenFile opens new browser window for the file path.
func OpenFile(path string) error {
path, err := filepath.Abs(path)
if err != nil {
return err
}
return OpenURL("file://" + path)
}
// OpenReader consumes the contents of r and presents the
// results in a new browser window.
func OpenReader(r io.Reader) error {
f, err := ioutil.TempFile("", "browser")
if err != nil {
return fmt.Errorf("browser: could not create temporary file: %v", err)
}
if _, err := io.Copy(f, r); err != nil {
f.Close()
return fmt.Errorf("browser: caching temporary file failed: %v", err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("browser: caching temporary file failed: %v", err)
}
oldname := f.Name()
newname := oldname + ".html"
if err := os.Rename(oldname, newname); err != nil {
return fmt.Errorf("browser: renaming temporary file failed: %v", err)
}
return OpenFile(newname)
}
// OpenURL opens a new browser window pointing to url.
func OpenURL(url string) error {
return openBrowser(url)
}
func runCmd(prog string, args ...string) error {
cmd := exec.Command(prog, args...)
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}

5
vendor/github.com/pkg/browser/browser_darwin.go generated vendored Normal file
View File

@ -0,0 +1,5 @@
package browser
func openBrowser(url string) error {
return runCmd("open", url)
}

5
vendor/github.com/pkg/browser/browser_linux.go generated vendored Normal file
View File

@ -0,0 +1,5 @@
package browser
func openBrowser(url string) error {
return runCmd("xdg-open", url)
}

12
vendor/github.com/pkg/browser/browser_unsupported.go generated vendored Normal file
View File

@ -0,0 +1,12 @@
// +build !linux,!windows,!darwin
package browser
import (
"fmt"
"runtime"
)
func openBrowser(url string) error {
return fmt.Errorf("openBrowser: unsupported operating system: %v", runtime.GOOS)
}

10
vendor/github.com/pkg/browser/browser_windows.go generated vendored Normal file
View File

@ -0,0 +1,10 @@
package browser
import (
"strings"
)
func openBrowser(url string) error {
r := strings.NewReplacer("&", "^&")
return runCmd("cmd", "/c", "start", r.Replace(url))
}