Skip to content

Commit

Permalink
use client-go to port-forward to dashboard
Browse files Browse the repository at this point in the history
Now that we have updated to client-go v4.0.0, we can use the library to
port-forward to the ingress controller.
  • Loading branch information
Adnan Abdulhussein committed Dec 1, 2017
1 parent 6f9e757 commit 77529ce
Show file tree
Hide file tree
Showing 40 changed files with 6,098 additions and 237 deletions.
81 changes: 75 additions & 6 deletions cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,24 @@ package cmd
import (
"errors"
"fmt"
"net/url"
"os"
"os/exec"
"os/signal"
"runtime"
"syscall"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/tools/remotecommand"
)

const (
Expand Down Expand Up @@ -76,19 +83,81 @@ var dashboardCmd = &cobra.Command{
}

func runPortforward(podName string, localPort int) error {
cmd, err := exec.LookPath("kubectl")
stopChannel := make(chan struct{}, 1)
readyChannel := make(chan struct{})

signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
defer signal.Stop(signals)

go func() {
<-signals
if stopChannel != nil {
close(stopChannel)
}
}()

// Open the Dashboard in a browser when the port-forward is established
go func() {
<-readyChannel
openInBrowser(fmt.Sprintf("http://localhost:%d", localPort))
}()

fw, err := newPortforwarder(podName, localPort, stopChannel, readyChannel)
if err != nil {
return err
}
args := []string{"kubectl", "--namespace", ingressNamespace, "port-forward", podName, fmt.Sprintf("%d:80", localPort)}
return fw.ForwardPorts()
}

env := os.Environ()
func newPortforwarder(podName string, localPort int, stopChannel, readyChannel chan struct{}) (*portforward.PortForwarder, error) {
config, err := restClientConfig()
if err != nil {
return nil, err
}

url, err := portforwardReqURL(config, podName)
if err != nil {
return nil, err
}

dialer, err := remotecommand.NewExecutor(config, "POST", url)
if err != nil {
return nil, err
}

ports := []string{fmt.Sprintf("%d:80", localPort)}

return portforward.New(dialer, ports, stopChannel, readyChannel, os.Stdout, os.Stderr)
}

func restClientConfig() (*rest.Config, error) {
config, err := buildOutOfClusterConfig()
if err != nil {
return nil, err
}
config.APIPath = "/api"
config.GroupVersion = &v1.SchemeGroupVersion
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
return config, nil
}

func portforwardReqURL(config *rest.Config, podName string) (*url.URL, error) {
restClient, err := rest.RESTClientFor(config)
if err != nil {
return nil, err
}

openInBrowser(fmt.Sprintf("http://localhost:%d", localPort))
return syscall.Exec(cmd, args, env)
req := restClient.Post().
Resource("pods").
Namespace(ingressNamespace).
Name(podName).
SubResource("portforward")
return req.URL(), nil
}

func openInBrowser(url string) error {
fmt.Printf("Opening %s in your default browser...\n", url)
args := []string{"xdg-open"}
switch runtime.GOOS {
case "darwin":
Expand Down
42 changes: 38 additions & 4 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import:
- pkg/api
- dynamic
- discovery
- rest
- tools/clientcmd
- tools/remotecommand
- tools/portforward
- plugin/pkg/client/auth/gcp
- package: k8s.io/apimachinery
version: 917740426ad66ff818da4809990480bcc0786a77
Expand All @@ -18,6 +21,10 @@ import:
- pkg/kubecfg
- package: github.com/emicklei/go-restful
version: ff4f55a206334ef123e4f79bbf348980da81ca46
- package: github.com/emicklei/go-restful-swagger12
version: dcef7f55730566d41eae5db10e7d6981829720f6
- package: github.com/go-openapi/spec
version: 6aced65f8501fe1217321abf0749d354824ba2ff
- package: github.com/spf13/cobra
version: f62e98d28ab7ad31d707ba837a966378465c7b57
- package: github.com/spf13/pflag
Expand All @@ -29,4 +36,4 @@ import:
subpackages:
- unix
- package: github.com/go-ini/ini
- package: github.com/gosuri/uitable
- package: github.com/gosuri/uitable
13 changes: 13 additions & 0 deletions vendor/github.com/docker/spdystream/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 77529ce

Please sign in to comment.