Skip to content
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

feat: attemp to do port-forwarding using websockets #60

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"strings"
Expand All @@ -22,7 +21,6 @@ import (
"k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/transport/spdy"

"github.com/knight42/krelay/pkg/constants"
"github.com/knight42/krelay/pkg/ports"
Expand Down Expand Up @@ -246,11 +244,6 @@ func (o *Options) Run(ctx context.Context, args []string) error {
}
slog.Info("krelay-server is running", slog.String("pod", svrPodName), slog.String("namespace", svrPod.Namespace))

transport, upgrader, err := spdy.RoundTripperFor(restCfg)
if err != nil {
return err
}

restClient, err := rest.RESTClientFor(restCfg)
if err != nil {
return err
Expand All @@ -260,7 +253,12 @@ func (o *Options) Run(ctx context.Context, args []string) error {
Resource("pods").
Namespace(svrPod.Namespace).Name(svrPodName).
SubResource("portforward")
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, req.URL())

dialer, err := createDialer(restCfg, req.URL())
if err != nil {
return fmt.Errorf("create dialer: %w", err)
}

streamConn, _, err := dialer.Dial(constants.PortForwardProtocolV1Name)
if err != nil {
return fmt.Errorf("dial: %w", err)
Expand Down
32 changes: 32 additions & 0 deletions cmd/client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"log/slog"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
Expand All @@ -27,6 +29,9 @@ import (
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"

"github.com/knight42/krelay/pkg/constants"
"github.com/knight42/krelay/pkg/remoteaddr"
Expand Down Expand Up @@ -282,3 +287,30 @@ func parseTargetsFile(r io.Reader, defaultNamespace string) ([]target, error) {
}
return ret, nil
}

func createDialer(restCfg *rest.Config, dstURL *url.URL) (httpstream.Dialer, error) {
// Excerpt from https://github.com/kubernetes/kubernetes/blob/f5c538418189e119a8dbb60e2a2b22394548e326/staging/src/k8s.io/kubectl/pkg/cmd/portforward/portforward.go#L139
transport, upgrader, err := spdy.RoundTripperFor(restCfg)
if err != nil {
return nil, err
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, dstURL)

if strings.ToLower(os.Getenv("KUBECTL_PORT_FORWARD_WEBSOCKETS")) != "false" {
slog.Debug("Trying to forward ports using websocket")

tunnelDialer, err := portforward.NewSPDYOverWebsocketDialer(dstURL, restCfg)
if err != nil {
return nil, fmt.Errorf("create tunneling dialer: %w", err)
}
dialer = portforward.NewFallbackDialer(tunnelDialer, dialer, func(err error) bool {
shouldFallback := httpstream.IsUpgradeFailure(err) || httpstream.IsHTTPSProxyError(err)
if shouldFallback {
slog.Debug("Websocket upgrade failed, falling back to SPDY")
}
return shouldFallback
})
}

return dialer, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaU
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA=
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
Expand Down