diff --git a/pkg/cmd/apply/apply.go b/pkg/cmd/apply/apply.go index 863cfd06..457d9c02 100644 --- a/pkg/cmd/apply/apply.go +++ b/pkg/cmd/apply/apply.go @@ -131,7 +131,7 @@ func (f *ApplyFlags) AddFlags(cmd *cobra.Command) { cmd.Flags().BoolVarP(&f.Yes, "yes", "y", false, i18n.T("Automatically approve and perform the update after previewing it")) cmd.Flags().BoolVarP(&f.DryRun, "dry-run", "", false, i18n.T("Preview the execution effect (always successful) without actually applying the changes")) cmd.Flags().BoolVarP(&f.Watch, "watch", "", false, i18n.T("After creating/updating/deleting the requested object, watch for changes")) - cmd.Flags().IntVarP(&f.PortForward, "port-forward", "", 0, i18n.T("Forward an available local port to the specified service port")) + cmd.Flags().IntVarP(&f.PortForward, "port-forward", "", 0, i18n.T("Forward the specified port from local to service")) } // ToOptions converts from CLI inputs to runtime inputs. @@ -451,8 +451,7 @@ func Watch( return nil } -// PortForward function will forward an available local port to the specified port -// of the project Kubernetes Service. +// PortForward function will forward the specified port from local to the project Kubernetes Service. // // Example: // diff --git a/pkg/engine/operation/port_forward.go b/pkg/engine/operation/port_forward.go index 35ac2fa8..f71889a9 100644 --- a/pkg/engine/operation/port_forward.go +++ b/pkg/engine/operation/port_forward.go @@ -4,8 +4,6 @@ import ( "context" "errors" "fmt" - "math/rand" - "net" "net/http" "os" "strings" @@ -105,7 +103,6 @@ func (bpo *PortForwardOperation) PortForward(req *PortForwardRequest) error { } // Port-forward the Service with client-go. - var localPort int failed := make(chan error) for res, svc := range services { namespace := svc.GetNamespace() @@ -129,25 +126,8 @@ func (bpo *PortForwardOperation) PortForward(req *PortForwardRequest) error { return err } - // Get an available local port for forwarding. - minPort, maxPort := 1024, 65535 - - for { - localPort = rand.Intn(maxPort-minPort) + minPort - - // Try to listen to the port. - addr := fmt.Sprintf(":%d", localPort) - listener, err := net.Listen("tcp", addr) - if err != nil { - continue - } - _ = listener.Close() - - break - } - go func() { - err := ForwardPort(ctx, cfg, clientset, namespace, serviceName, servicePort, localPort) + err := ForwardPort(ctx, cfg, clientset, namespace, serviceName, servicePort, servicePort) failed <- err }() }