Skip to content

Commit

Permalink
cluster: change the docker-desktop socket path (#177)
Browse files Browse the repository at this point in the history
Fixes #176
  • Loading branch information
nicks authored Jan 13, 2022
1 parent c2cff0b commit a2a1cd9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
24 changes: 17 additions & 7 deletions pkg/cluster/docker_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,35 @@ type HTTPClient interface {
// for this, so we do the best we can.
type DockerDesktopClient struct {
httpClient HTTPClient
socketPath string
}

func NewDockerDesktopClient() (DockerDesktopClient, error) {
socketPath, err := dockerDesktopSocketPath()
socketPaths, err := dockerDesktopSocketPaths()
if err != nil {
return DockerDesktopClient{}, err
}

httpClient := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
return dialDockerDesktop(socketPath)
var lastErr error

// Different versions of docker use different socket paths,
// so return all of them and connect to the first one that
// accepts a TCP dial.
for _, socketPath := range socketPaths {
conn, err := dialDockerDesktop(socketPath)
if err == nil {
return conn, nil
}
lastErr = err
}
return nil, lastErr
},
},
}
return DockerDesktopClient{
httpClient: httpClient,
socketPath: socketPath,
}, nil
}

Expand Down Expand Up @@ -84,7 +94,7 @@ func (c DockerDesktopClient) Quit(ctx context.Context) error {
}

func (c DockerDesktopClient) ResetCluster(ctx context.Context) error {
klog.V(7).Infof("POST %s /kubernetes/reset\n", c.socketPath)
klog.V(7).Infof("POST /kubernetes/reset\n")
req, err := http.NewRequest("POST", "http://localhost/kubernetes/reset", nil)
if err != nil {
return errors.Wrap(err, "reset docker-desktop kubernetes")
Expand Down Expand Up @@ -222,7 +232,7 @@ func (c DockerDesktopClient) applySet(settings map[string]interface{}, key, newV
}

func (c DockerDesktopClient) writeSettings(ctx context.Context, settings map[string]interface{}) error {
klog.V(7).Infof("POST %s /settings\n", c.socketPath)
klog.V(7).Infof("POST /settings\n")
buf := bytes.NewBuffer(nil)
err := json.NewEncoder(buf).Encode(c.settingsForWrite(settings))
if err != nil {
Expand All @@ -249,7 +259,7 @@ func (c DockerDesktopClient) writeSettings(ctx context.Context, settings map[str
}

func (c DockerDesktopClient) settings(ctx context.Context) (map[string]interface{}, error) {
klog.V(7).Infof("GET %s /settings\n", c.socketPath)
klog.V(7).Infof("GET /settings\n")
req, err := http.NewRequest("GET", "http://localhost/settings", nil)
if err != nil {
return nil, errors.Wrap(err, "reading docker-desktop settings")
Expand Down
12 changes: 9 additions & 3 deletions pkg/cluster/docker_desktop_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import (
"github.com/mitchellh/go-homedir"
)

func dockerDesktopSocketPath() (string, error) {
func dockerDesktopSocketPaths() ([]string, error) {
homedir, err := homedir.Dir()
if err != nil {
return "", err
return nil, err
}

return filepath.Join(homedir, "Library/Containers/com.docker.docker/Data/gui-api.sock"), nil
return []string{
// Older versions of docker desktop use this socket.
filepath.Join(homedir, "Library/Containers/com.docker.docker/Data/gui-api.sock"),

// Newer versions of docker desktop use this socket.
filepath.Join(homedir, "Library/Containers/com.docker.docker/Data/backend.sock"),
}, nil
}

func dialDockerDesktop(socketPath string) (net.Conn, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/docker_desktop_dial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"gopkg.in/natefinch/npipe.v2"
)

func dockerDesktopSocketPath() (string, error) {
return `\\.\pipe\dockerWebApiServer`, nil
func dockerDesktopSocketPaths() ([]string, error) {
return []string{`\\.\pipe\dockerWebApiServer`}, nil
}

func dialDockerDesktop(socketPath string) (net.Conn, error) {
Expand Down

0 comments on commit a2a1cd9

Please sign in to comment.