通过client-go升级端口转发时出现连接错误

这是我编写的通过 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

我得到的错误是


升级连接时出错:


但之后就什么也没有了:。您对这个话题有什么经验吗?谢谢


凤凰求蛊
浏览 112回答 3
3回答

喵喵时光机

clientset.CoreV1().RESTClient().Post().&nbsp; &nbsp; &nbsp; &nbsp; Resource("pods").&nbsp; &nbsp; &nbsp; &nbsp; Namespace("default").&nbsp; &nbsp; &nbsp; &nbsp; Name("test").&nbsp; &nbsp; &nbsp; &nbsp; SubResource("portforward").URL()对我有用,并给出带有 .../api/v1/namespaces... 的 url

Helenr

有*rest.Request一个Prefix(string)方法可以用来插入丢失的子路径:reqURL := clientset.RESTClient().Post().&nbsp; &nbsp; &nbsp; &nbsp; Prefix("api/v1").&nbsp; &nbsp; &nbsp; &nbsp; Resource("pods").&nbsp; &nbsp; &nbsp; &nbsp; Namespace("default").&nbsp; &nbsp; &nbsp; &nbsp; Name("test").&nbsp; &nbsp; &nbsp; &nbsp; SubResource("portforward").URL()

烙印99

我部分解决了问题。至少我让它工作了并且我对此感到满意。下面的代码生成的URL是https://192.168.99.125:8443/namespaces/default/pods/influxdb-65c9fdf9cb-nzvpf/portforward?timeout=32sreqURL := clientset.RESTClient().Post().&nbsp; &nbsp; &nbsp; &nbsp; Resource("pods").&nbsp; &nbsp; &nbsp; &nbsp; Namespace("default").&nbsp; &nbsp; &nbsp; &nbsp; Name("test").&nbsp; &nbsp; &nbsp; &nbsp; SubResource("portforward").URL()这是错误的,正确的是:https://192.168.99.125:8443/api/v1/namespaces/default/pods/influxdb-65c9fdf9cb-nzvpf/portforward这就是我现在生成该 URL 的方法:&nbsp; &nbsp; path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", "default", podName)&nbsp; &nbsp; hostIP := strings.TrimLeft(config.Host, "htps:/")&nbsp; &nbsp; serverURL := url.URL{Scheme: "https", Path: path, Host: hostIP}&nbsp; &nbsp; dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, &serverURL)
打开App,查看更多内容
随时随地看视频慕课网APP