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

Fix intention of support for fallback protocol and add tests for probe/cri/registry #3392

Merged
merged 3 commits into from
Nov 12, 2018
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
45 changes: 28 additions & 17 deletions probe/cri/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,59 @@ import (
client "github.com/weaveworks/scope/cri/runtime"
)

const unixProtocol = "unix"
const (
unixProtocol = "unix"
tcpProtocol = "tcp"
)

func dial(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout(unixProtocol, addr, timeout)
}

func getAddressAndDialer(endpoint string) (string, func(addr string, timeout time.Duration) (net.Conn, error), error) {
protocol, addr, err := parseEndpointWithFallbackProtocol(endpoint, unixProtocol)
addr, err := parseEndpointWithFallbackProtocol(endpoint, unixProtocol)
if err != nil {
return "", nil, err
}
if protocol != unixProtocol {
return "", nil, fmt.Errorf("endpoint was not unix socket %v", protocol)
}

return addr, dial, nil
}

func parseEndpointWithFallbackProtocol(endpoint string, fallbackProtocol string) (protocol string, addr string, err error) {
if protocol, addr, err = parseEndpoint(endpoint); err != nil && protocol == "" {
func parseEndpointWithFallbackProtocol(endpoint string, fallbackProtocol string) (addr string, err error) {
var protocol string

protocol, addr, err = parseEndpoint(endpoint)

if err != nil {
return "", err
}

if protocol == "" {
fallbackEndpoint := fallbackProtocol + "://" + endpoint
protocol, addr, err = parseEndpoint(fallbackEndpoint)
_, addr, err = parseEndpoint(fallbackEndpoint)

if err != nil {
return "", "", err
return "", err
}
}
return
return addr, err
}

func parseEndpoint(endpoint string) (string, string, error) {
u, err := url.Parse(endpoint)

if err != nil {
return "", "", err
}

if u.Scheme == "tcp" {
return "tcp", u.Host, nil
} else if u.Scheme == "unix" {
return "unix", u.Path, nil
} else if u.Scheme == "" {
return "", "", fmt.Errorf("Using %q as endpoint is deprecated, please consider using full url format", endpoint)
} else {
switch u.Scheme {
case tcpProtocol:
return tcpProtocol, u.Host, fmt.Errorf("endpoint was not unix socket %v", u.Scheme)
case unixProtocol:
return unixProtocol, u.Path, nil
case "":
return "", "", nil
default:
return u.Scheme, "", fmt.Errorf("protocol %q not supported", u.Scheme)
}
}
Expand Down
35 changes: 28 additions & 7 deletions probe/cri/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,36 @@ import (
"github.com/weaveworks/scope/probe/cri"
)

func TestParseHttpEndpointUrl(t *testing.T) {
_, err := cri.NewCRIClient("http://xyz.com")
var nonUnixSocketsTest = []struct {
endpoint string
errorMessage string
}{
{"http://xyz.com", "protocol \"http\" not supported"},
{"tcp://var/unix.sock", "endpoint was not unix socket tcp"},
{"http://[fe80::%31]/", "parse http://[fe80::%31]/: invalid URL escape \"%31\""},
}

func TestParseNonUnixEndpointUrl(t *testing.T) {
for _, tt := range nonUnixSocketsTest {
_, err := cri.NewCRIClient(tt.endpoint)

assert.Equal(t, "protocol \"http\" not supported", err.Error())
assert.Equal(t, tt.errorMessage, err.Error())
}
}

func TestParseTcpEndpointUrl(t *testing.T) {
client, err := cri.NewCRIClient("127.0.0.1")
var unixSocketsTest = []string{
"127.0.0.1", // tests the fallback endpoint
"unix://127.0.0.1",
"unix///var/run/dockershim.sock",
"var/run/dockershim.sock",
}

func TestParseUnixEndpointUrl(t *testing.T) {
for _, tt := range unixSocketsTest {
client, err := cri.NewCRIClient(tt)

assert.Equal(t, nil, err)
assert.NotEqual(t, nil, client)
}

assert.Equal(t, nil, err)
assert.NotEqual(t, nil, client)
}