Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/dashboard supports random port #874

Merged
merged 20 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ dapr dashboard -k -p 9999 -a 0.0.0.0

# Port forward to dashboard in Kubernetes using a port
dapr dashboard -k -p 9999

# Port forward to dashboard in Kubernetes using a random port which is free.
dapr dashboard -k -p 0
`,
Run: func(cmd *cobra.Command, args []string) {
if dashboardVersionCmd {
Expand All @@ -84,7 +87,7 @@ dapr dashboard -k -p 9999
os.Exit(1)
}

if dashboardLocalPort <= 0 {
if dashboardLocalPort < 0 {
print.FailureStatusEvent(os.Stderr, "Invalid port: %v", dashboardLocalPort)
os.Exit(1)
}
Expand Down Expand Up @@ -161,7 +164,7 @@ dapr dashboard -k -p 9999
}()

// url for dashboard after port forwarding
var webURL string = fmt.Sprintf("http://%s:%d", dashboardHost, dashboardLocalPort)
var webURL string = fmt.Sprintf("http://%s:%d", dashboardHost, portForward.LocalPort)

print.InfoStatusEvent(os.Stdout, fmt.Sprintf("Dapr dashboard found in namespace:\t%s", foundNamespace))
print.InfoStatusEvent(os.Stdout, fmt.Sprintf("Dapr dashboard available at:\t%s\n", webURL))
Expand Down
33 changes: 18 additions & 15 deletions pkg/kubernetes/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewPortForward(
) (*PortForward, error) {
client, err := k8s.NewForConfig(config)
if err != nil {
return nil, err
return nil, fmt.Errorf("can't creat Clientset: %s", deployName)
}

podList, err := ListPods(client, namespace, nil)
Expand Down Expand Up @@ -94,12 +94,13 @@ func NewPortForward(
}, nil
}

// run creates port-forward connection and blocks
// until Stop() is called.
func (pf *PortForward) run() error {
// Init creates and runs a port-forward connection.
// This function blocks until connection is established.
// Note: Caller should call Stop() to finish the connection.
func (pf *PortForward) Init() error {
transport, upgrader, err := spdy.RoundTripperFor(pf.Config)
if err != nil {
return err
return fmt.Errorf("can't connect to a Kubernetes cluster: %v", err)
}

out := ioutil.Discard
Expand All @@ -114,27 +115,29 @@ func (pf *PortForward) run() error {

fw, err := portforward.NewOnAddresses(dialer, []string{pf.Host}, ports, pf.StopCh, pf.ReadyCh, out, errOut)
if err != nil {
return err
return fmt.Errorf("can't creat PortForwarder: %w", err)
}

return fw.ForwardPorts()
}

// Init creates and runs a port-forward connection.
// This function blocks until connection is established.
// Note: Caller should call Stop() to finish the connection.
func (pf *PortForward) Init() error {
failure := make(chan error)

go func() {
if err := pf.run(); err != nil {
if err := fw.ForwardPorts(); err != nil {
failure <- err
}
}()

select {
// if `pf.run()` succeeds, block until terminated
case <-pf.ReadyCh:
ports, err := fw.GetPorts()
if err != nil {
return fmt.Errorf("can't get the ports that were forwarded: %w", err)
}
if len(ports) == 0 {
return fmt.Errorf("error ports length")
}

pf.LocalPort = int(ports[0].Local)
pf.RemotePort = int(ports[0].Remote)

// if failure, causing a receive `<-failure` and returns the error
case err := <-failure:
Expand Down