mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
* feat: mode sync modify kubeconfig apiserver to in cluster apiserver * feat: add ut for sync mode and run mode * fix: bugs
58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/url"
|
|
|
|
"k8s.io/apimachinery/pkg/util/httpstream"
|
|
restclient "k8s.io/client-go/rest"
|
|
"k8s.io/client-go/tools/remotecommand"
|
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
|
)
|
|
|
|
// just same as bellow but add ctx for cancel
|
|
// pkg/mod/k8s.io/kubectl@v0.32.3/pkg/cmd/exec/exec.go:121
|
|
|
|
// DefaultRemoteExecutor is the standard implementation of remote command execution
|
|
type DefaultRemoteExecutor struct {
|
|
Ctx context.Context
|
|
}
|
|
|
|
func (r *DefaultRemoteExecutor) Execute(url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool, terminalSizeQueue remotecommand.TerminalSizeQueue) error {
|
|
exec, err := createExecutor(url, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return exec.StreamWithContext(r.Ctx, remotecommand.StreamOptions{
|
|
Stdin: stdin,
|
|
Stdout: stdout,
|
|
Stderr: stderr,
|
|
Tty: tty,
|
|
TerminalSizeQueue: terminalSizeQueue,
|
|
})
|
|
}
|
|
|
|
// createExecutor returns the Executor or an error if one occurred.
|
|
func createExecutor(url *url.URL, config *restclient.Config) (remotecommand.Executor, error) {
|
|
exec, err := remotecommand.NewSPDYExecutor(config, "POST", url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Fallback executor is default, unless feature flag is explicitly disabled.
|
|
if !cmdutil.RemoteCommandWebsockets.IsDisabled() {
|
|
// WebSocketExecutor must be "GET" method as described in RFC 6455 Sec. 4.1 (page 17).
|
|
websocketExec, err := remotecommand.NewWebSocketExecutor(config, "GET", url.String())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
exec, err = remotecommand.NewFallbackExecutor(websocketExec, exec, func(err error) bool {
|
|
return httpstream.IsUpgradeFailure(err) || httpstream.IsHTTPSProxyError(err)
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return exec, nil
|
|
}
|