From f428b779fef4658002469530cec5010c461a507d Mon Sep 17 00:00:00 2001 From: t00416110 Date: Thu, 11 Oct 2018 21:25:45 +0800 Subject: [PATCH] fix error way of parsing URL for cmd exec and attach Signed-off-by: t00416110 --- cmd/crictl/attach.go | 12 ++++++++---- cmd/crictl/exec.go | 16 ++++++++++------ cmd/crictl/portforward.go | 13 +++++++++---- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/cmd/crictl/attach.go b/cmd/crictl/attach.go index eef5fd57e8..2610da6a23 100644 --- a/cmd/crictl/attach.go +++ b/cmd/crictl/attach.go @@ -19,7 +19,6 @@ package main import ( "fmt" "net/url" - "strings" "github.com/sirupsen/logrus" "github.com/urfave/cli" @@ -89,14 +88,19 @@ func Attach(client pb.RuntimeServiceClient, opts attachOptions) error { return err } attachURL := r.Url - if !strings.HasPrefix(attachURL, "http") { - attachURL = kubeletURLPrefix + attachURL - } URL, err := url.Parse(attachURL) if err != nil { return err } + + if URL.Host == "" { + URL.Host = kubeletURLHost + } + if URL.Scheme == "" { + URL.Scheme = kubeletURLSchema + } + logrus.Debugf("Attach URL: %v", URL) return stream(opts.stdin, opts.tty, URL) } diff --git a/cmd/crictl/exec.go b/cmd/crictl/exec.go index 008dd58dd2..49f60d0531 100644 --- a/cmd/crictl/exec.go +++ b/cmd/crictl/exec.go @@ -19,7 +19,6 @@ package main import ( "fmt" "net/url" - "strings" dockerterm "github.com/docker/docker/pkg/term" "github.com/sirupsen/logrus" @@ -33,7 +32,8 @@ import ( const ( // TODO: make this configurable in kubelet. - kubeletURLPrefix = "http://127.0.0.1:10250" + kubeletURLSchema = "http" + kubeletURLHost = "http://127.0.0.1:10250" ) var runtimeExecCommand = cli.Command{ @@ -134,16 +134,20 @@ func Exec(client pb.RuntimeServiceClient, opts execOptions) error { return err } execURL := r.Url - if !strings.HasPrefix(execURL, "http") { - execURL = kubeletURLPrefix + execURL - - } URL, err := url.Parse(execURL) if err != nil { return err } + if URL.Host == "" { + URL.Host = kubeletURLHost + } + + if URL.Scheme == "" { + URL.Scheme = kubeletURLSchema + } + logrus.Debugf("Exec URL: %v", URL) return stream(opts.stdin, opts.tty, URL) } diff --git a/cmd/crictl/portforward.go b/cmd/crictl/portforward.go index 885fb5e5e3..b02cad5664 100644 --- a/cmd/crictl/portforward.go +++ b/cmd/crictl/portforward.go @@ -22,7 +22,6 @@ import ( "net/url" "os" "os/signal" - "strings" "github.com/sirupsen/logrus" "github.com/urfave/cli" @@ -78,14 +77,20 @@ func PortForward(client pb.RuntimeServiceClient, opts portforwardOptions) error return err } portforwardURL := r.Url - if !strings.HasPrefix(portforwardURL, "http") { - portforwardURL = kubeletURLPrefix + portforwardURL - } URL, err := url.Parse(portforwardURL) if err != nil { return err } + + if URL.Host == "" { + URL.Host = kubeletURLHost + } + + if URL.Scheme == "" { + URL.Scheme = kubeletURLSchema + } + logrus.Debugf("PortForward URL: %v", URL) transport, upgrader, err := spdy.RoundTripperFor(&restclient.Config{}) if err != nil {