From d1b9eb53dc69b813290e5fd575915a9aca53653c Mon Sep 17 00:00:00 2001 From: ysicing Date: Wed, 26 Jul 2023 15:25:08 +0800 Subject: [PATCH] feat(debug): add port-forward add port-forward Signed-off-by: ysicing --- cmd/debug.go | 1 + cmd/debug/portforward.go | 32 ++++++++++++++++++++++++++++++++ internal/pkg/k8s/portforward.go | 19 +++++++++++++------ 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 cmd/debug/portforward.go diff --git a/cmd/debug.go b/cmd/debug.go index 66330bbe..8d94e324 100644 --- a/cmd/debug.go +++ b/cmd/debug.go @@ -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 } diff --git a/cmd/debug/portforward.go b/cmd/debug/portforward.go new file mode 100644 index 00000000..4d4759cd --- /dev/null +++ b/cmd/debug/portforward.go @@ -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 +} diff --git a/internal/pkg/k8s/portforward.go b/internal/pkg/k8s/portforward.go index 01ed11cf..5ba8b770 100644 --- a/internal/pkg/k8s/portforward.go +++ b/internal/pkg/k8s/portforward.go @@ -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", @@ -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...) }