Skip to content

Commit

Permalink
feat(debug): add port-forward
Browse files Browse the repository at this point in the history
add port-forward

Signed-off-by: ysicing <i@ysicing.me>
  • Loading branch information
ysicing committed Jul 26, 2023
1 parent f3283c1 commit d1b9eb5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ func newCmdDebug(f factory.Factory) *cobra.Command {
debugCmd.AddCommand(debug.IngressNoHostCommand(f))
debugCmd.AddCommand(debug.GOpsCommand(f))
debugCmd.AddCommand(debug.NetcheckCommand(f))
debugCmd.AddCommand(debug.PortForwardCommand(f))
return debugCmd
}
32 changes: 32 additions & 0 deletions cmd/debug/portforward.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2021-2023 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved.
// Use of this source code is covered by the following dual licenses:
// (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2)
// (2) Affero General Public License 3.0 (AGPL 3.0)
// license that can be found in the LICENSE file.

package debug

import (
"context"

"github.com/easysoft/qcadmin/internal/pkg/k8s"
"github.com/easysoft/qcadmin/internal/pkg/util/factory"
"github.com/spf13/cobra"
)

func PortForwardCommand(f factory.Factory) *cobra.Command {
var ns, svc string
var port int
pf := &cobra.Command{
Use: "port-forward",
Aliases: []string{"pf"},
Short: "forward local port to kube pod or svc",
RunE: func(cmd *cobra.Command, args []string) error {
return k8s.PortForwardCommand(context.Background(), ns, svc, port)
},
}
pf.Flags().StringVarP(&ns, "ns", "n", "default", "namespace")
pf.Flags().StringVar(&svc, "svc", "kubernetes", "svc name")
pf.Flags().IntVarP(&port, "port", "p", 443, "port")
return pf
}
19 changes: 13 additions & 6 deletions internal/pkg/k8s/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ import (
"os"
"time"

"github.com/easysoft/qcadmin/common"
qcexec "github.com/easysoft/qcadmin/internal/pkg/util/exec"
"github.com/easysoft/qcadmin/internal/pkg/util/log"
"github.com/ergoapi/util/exnet"
"github.com/ergoapi/util/zos"

"github.com/pkg/browser"
)

func PortForwardCommand(ctx context.Context, ns, svc string, sport, dport int) error {
func PortForwardCommand(ctx context.Context, ns, svc string, sport int) error {
log := log.GetInstance()
dport := exnet.GetFreePort()
args := []string{
"experimental",
"kubectl",
Expand All @@ -29,18 +33,21 @@ func PortForwardCommand(ctx context.Context, ns, svc string, sport, dport int) e
fmt.Sprintf("svc/%s", svc),
"--address", "0.0.0.0",
"--address", "::",
"--kubeconfig", common.GetKubeConfig(),
fmt.Sprintf("%d:%d", dport, sport)}

url := fmt.Sprintf("%s:%d", exnet.LocalIPs()[0], dport)
log.Infof("listen to: %s", url)

go func() {
time.Sleep(5 * time.Second)
url := fmt.Sprintf("http://localhost:%d", dport)

// avoid cluttering stdout/stderr when opening the browser
browser.Stdout = io.Discard
browser.Stderr = io.Discard
log.Infof("Opening %q in your browser...", url)
browser.OpenURL(url)
if zos.IsMacOS() {
browser.OpenURL(url)
}
}()
_, err := qcexec.CommandRespByte(os.Args[0], args...)
return err
return qcexec.CommandRun(os.Args[0], args...)
}

0 comments on commit d1b9eb5

Please sign in to comment.