Upgrading connection error in Port forwarding via client-go

11/25/2019

This is an easy to run version of the code I wrote to do port-forwarding via client-go. There are hardcoded pod name, namespace, and port. You can change them with the one you have running.

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)
    }
}

Version 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

The error I get is

error upgrading connection:

but there is nothing after the :. Do you have any experience with this topic? Thanks

-- GianArb
client-go
go
kubernetes

2 Answers

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

works for me and gives url with .../api/v1/namespaces...

-- hsin
Source: StackOverflow

11/27/2019

I got the problem partially solved. At least I got it working and I am happy with that.

The URL generated by the following code is https://192.168.99.125:8443/namespaces/default/pods/influxdb-65c9fdf9cb-nzvpf/portforward?timeout=32s

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

It is wrong, the right one is:

https://192.168.99.125:8443/api/v1/namespaces/default/pods/influxdb-65c9fdf9cb-nzvpf/portforward

This is what I do to generate that URL right now:

    path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", "default", podName)

    hostIP := strings.TrimLeft(config.Host, "htps:/")
    serverURL := url.URL{Scheme: "https", Path: path, Host: hostIP}
    dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, &serverURL)
-- GianArb
Source: StackOverflow