1 回答
TA贡献1802条经验 获得超4个赞
我设法用 Kubernetes go-client库做到了:
package main
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"os"
"time"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
)
func main(){
/*req.Header.Add("X-Stream-Protocol-Version", "v4.channel.k8s.io")
req.Header.Add("X-Stream-Protocol-Version", "v3.channel.k8s.io")
req.Header.Add("X-Stream-Protocol-Version", "v2.channel.k8s.io")
req.Header.Add("X-Stream-Protocol-Version", "channel.k8s.io")
req.Header.Add("Connection", "upgrade")
req.Header.Add("Upgrade", "SPDY/3.1")*/
//url2 := "https://123.123.123.123:10250/exec/default/my-pod/nginx?command=ls&command=/&input=1&output=1&tty=1"
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
config := &restclient.Config{
Host: "https://123.123.123.123:10250",
APIPath: "/exec/default/my-pod/nginx",
TLSClientConfig: restclient.TLSClientConfig{
Insecure: true,
},
Transport: tr,
}
url3 := &url.URL{
Scheme: "https",
Opaque: "",
User: nil,
Host: "123.123.123.123:10250",
Path: "/exec/default/my-pod/nginx",
RawPath: "",
RawQuery: "command=ls&command=/&input=1&output=1&tty=1",
}
exec, err := remotecommand.NewSPDYExecutor(config, "POST", url3)
if err != nil {
fmt.Println(err)
}
// Thanks for this blog post https://www.henryxieblogs.com/2019/05/
err = exec.Stream(remotecommand.StreamOptions{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty:true,
})
fmt.Println(err)
}
- 1 回答
- 0 关注
- 116 浏览
添加回答
举报