gpt4 book ai didi

go - 通过client-go在端口转发中升级连接错误

转载 作者:行者123 更新时间:2023-12-01 22:27:32 24 4
gpt4 key购买 nike

这是我编写的代码的一个易于运行的版本,用于通过 client-go 进行端口转发。有硬编码的 pod 名称、命名空间和端口。你可以用你正在运行的那个来改变它们。

package main

import (
"flag"
"net/http"
"os"
"path/filepath"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
)

func main() {

stopCh := make(<-chan struct{})
readyCh := make(chan struct{})
var kubeconfig *string
if home := "/home/gianarb"; home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()

// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}

// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}

reqURL := clientset.RESTClient().Post().
Resource("pods").
Namespace("default").
Name("test").
SubResource("portforward").URL()

transport, upgrader, err := spdy.RoundTripperFor(config)
if err != nil {
panic(err)
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, reqURL)
fw, err := portforward.New(dialer, []string{"9999:9999"}, stopCh, readyCh, os.Stdout, os.Stdout)
if err != nil {
panic(err)
}
if err := fw.ForwardPorts(); err != nil {
panic(err)
}
}

版本 golang 1.13:
    k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
k8s.io/cli-runtime v0.0.0-20190409023024-d644b00f3b79
k8s.io/client-go v11.0.0+incompatible

我得到的错误是

error upgrading connection:



但是 : 之后什么都没有.
你对这个话题有什么经验吗?
谢谢

最佳答案

clientset.CoreV1().RESTClient().Post().
Resource("pods").
Namespace("default").
Name("test").
SubResource("portforward").URL()

为我工作,并提供带有 .../api/v1/namespaces... 的 url

关于go - 通过client-go在端口转发中升级连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59027739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com