-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add port-forwarding example #51
Comments
You can take a look at how kubectl use the equivalent packages in the main repo: https://github.com/kubernetes/kubernetes/blob/v1.6.0-alpha.0/pkg/kubectl/cmd/portforward.go#L107 |
This is still unclear. Did you mean we shouldn't use client-go to execute a port-forward ? @caesarxuchao |
@gtaylor Please try this to obtain the import(
"flag"
"fmt"
"os"
"path/filepath"
"net/http"
"net/url"
"strings"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"k8s.io/client-go/util/homedir"
)
...
var kubeconfig *string
if home := homedir.HomeDir(); 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()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
roundTripper, upgrader, err := spdy.RoundTripperFor(config)
if err != nil {
panic(err)
}
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: roundTripper}, http.MethodPost, &serverURL) What about import "bytes"
stopChan, readyChan := make(chan struct{}, 1), make(chan struct{}, 1)
out, errOut := new(bytes.Buffer), new(bytes.Buffer) The usage is following (with error handling simplified, you would probably want to add more context to the error messages, and/or use log messages instead of panics): forwarder, err := portforward.New(dialer, ports, stopChan, readyChan, out, errOut)
if err != nil {
panic(err)
}
go func() {
for range readyChan { // Kubernetes will close this channel when it has something to tell us.
}
if len(errOut.String()) != 0 {
panic(errOut.String())
} else if len(out.String()) != 0 {
fmt.Println(out.String())
}
}()
if err = forwarder.ForwardPorts(); err != nil { // Locks until stopChan is closed.
panic(err)
} Just close the |
I am getting an error while trying the above code as |
An article on the topic: |
recommended read: #796 if you find any dataraces. I used the safe buffer implementation for the following gist: |
how to forward for service? |
Hi, I made a library with example forwarding service or pods! You can see the repo: 😬 https://github.com/anthhub/forwarder |
Code example given above contains a bug. // All hosts starting with any of the letters 'h', 'p', 's', or 't', will be erroneously truncated.
hostIP := strings.TrimLeft(config.Host, "htps:/")
// Ex: "http://stamp-high.com"
// -> "amp-high.com" Solutions I found: // Solution 1
hostIP := strings.TrimPrefix(strings.TrimPrefix(config.Host, "http://"), "https://")
// Solution 2
var regexURLScheme = regexp.MustCompile(`(?i)^https?://`)
hostIP := regexURLScheme.ReplaceAllString(config.Host, "")
// Solution 3
hostURL, err := url.Parse(config.Host)
if err != nil {
panic(err)
}
hostIP := hostURL.Host I am posting here since I suspect others might end up here while searching for a solution. Searching github for 'TrimLeft "htps:/"' gives me over 100+ results, so the bug seems somewhat widely spread. |
This would be really helpful, as I'm coming from another language. In particular, how to come about the
dialer
param here:client-go/tools/portforward/portforward.go
Line 111 in 75399f6
The text was updated successfully, but these errors were encountered: