-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cluster: improve local docker host detection (#142)
Fixes #132
- Loading branch information
Showing
8 changed files
with
127 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package docker | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
func GetHostEnv() string { | ||
return os.Getenv("DOCKER_HOST") | ||
} | ||
|
||
func IsLocalHost(dockerHost string) bool { | ||
return dockerHost == "" || | ||
|
||
// Check all the "standard" docker localhosts. | ||
// https://github.com/docker/cli/blob/a32cd16160f1b41c1c4ae7bee4dac929d1484e59/opts/hosts.go#L22 | ||
strings.HasPrefix(dockerHost, "tcp://localhost:") || | ||
strings.HasPrefix(dockerHost, "tcp://127.0.0.1:") || | ||
|
||
// https://github.com/moby/moby/blob/master/client/client_windows.go#L4 | ||
strings.HasPrefix(dockerHost, "npipe:") || | ||
|
||
// https://github.com/moby/moby/blob/master/client/client_unix.go#L6 | ||
strings.HasPrefix(dockerHost, "unix:") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package docker | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type dockerHostTestCase struct { | ||
host string | ||
expected bool | ||
} | ||
|
||
func TestIsLocalDockerHost(t *testing.T) { | ||
cases := []dockerHostTestCase{ | ||
dockerHostTestCase{"", true}, | ||
dockerHostTestCase{"tcp://localhost:2375", true}, | ||
dockerHostTestCase{"tcp://127.0.0.1:2375", true}, | ||
dockerHostTestCase{"npipe:////./pipe/docker_engine", true}, | ||
dockerHostTestCase{"unix:///var/run/docker.sock", true}, | ||
dockerHostTestCase{"tcp://cluster:2375", false}, | ||
dockerHostTestCase{"http://cluster:2375", false}, | ||
} | ||
for i, c := range cases { | ||
c := c | ||
t.Run(fmt.Sprintf("%s-%d", t.Name(), i), func(t *testing.T) { | ||
assert.Equal(t, c.expected, IsLocalHost(c.host)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters