mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-13 11:25:19 +00:00
Update Exec and Portforward client to use pod subresource
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
"os/signal"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/portforward"
|
||||
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
|
||||
"github.com/golang/glog"
|
||||
@@ -49,7 +50,7 @@ func NewCmdPortForward(f *cmdutil.Factory) *cobra.Command {
|
||||
Long: "Forward one or more local ports to a pod.",
|
||||
Example: portforward_example,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := RunPortForward(f, cmd, args)
|
||||
err := RunPortForward(f, cmd, args, &defaultPortForwarder{})
|
||||
cmdutil.CheckErr(err)
|
||||
},
|
||||
}
|
||||
@@ -59,12 +60,25 @@ func NewCmdPortForward(f *cmdutil.Factory) *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
func RunPortForward(f *cmdutil.Factory, cmd *cobra.Command, args []string) error {
|
||||
type portForwarder interface {
|
||||
ForwardPorts(req *client.Request, config *client.Config, ports []string, stopChan <-chan struct{}) error
|
||||
}
|
||||
|
||||
type defaultPortForwarder struct{}
|
||||
|
||||
func (*defaultPortForwarder) ForwardPorts(req *client.Request, config *client.Config, ports []string, stopChan <-chan struct{}) error {
|
||||
fw, err := portforward.New(req, config, ports, stopChan)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fw.ForwardPorts()
|
||||
}
|
||||
|
||||
func RunPortForward(f *cmdutil.Factory, cmd *cobra.Command, args []string, fw portForwarder) error {
|
||||
podName := cmdutil.GetFlagString(cmd, "pod")
|
||||
if len(podName) == 0 {
|
||||
return cmdutil.UsageError(cmd, "POD is required for exec")
|
||||
}
|
||||
|
||||
if len(args) < 1 {
|
||||
return cmdutil.UsageError(cmd, "at least 1 PORT is required for port-forward")
|
||||
}
|
||||
@@ -104,15 +118,10 @@ func RunPortForward(f *cmdutil.Factory, cmd *cobra.Command, args []string) error
|
||||
}()
|
||||
|
||||
req := client.RESTClient.Get().
|
||||
Prefix("proxy").
|
||||
Resource("nodes").
|
||||
Name(pod.Spec.Host).
|
||||
Suffix("portForward", namespace, podName)
|
||||
Resource("pods").
|
||||
Namespace(namespace).
|
||||
Name(pod.Name).
|
||||
SubResource("portforward")
|
||||
|
||||
pf, err := portforward.New(req, config, args, stopCh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return pf.ForwardPorts()
|
||||
return fw.ForwardPorts(req, config, args, stopCh)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user