Skip to content
Closed
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
29 changes: 29 additions & 0 deletions pkg/connectors/container_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"github.com/moby/term"
)

var execCommand = exec.Command

type ContainerClient interface {
CreateContainer(opts ContainerOpts) (string, error)
StartContainer(containerId string) error
Expand Down Expand Up @@ -75,6 +77,10 @@ func NewContainerClient(driver string) (ContainerClient, error) {
}

func NewDockerClient() (*containerClient, error) {
if err := ConfigureDockerHost(); err != nil {
return nil, err
}

Comment on lines +80 to +83

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SafeeImran Bro this solution is already implemented in #538 by @ish-g09. I intentionally put that on halt as we want to discover that wether this is possible or not without making any manual effort. So I cant approve these changes.

cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())

if err != nil {
Expand Down Expand Up @@ -102,6 +108,29 @@ func PingDockerHost() error {
return err
}

func ConfigureDockerHost() error {
if os.Getenv("DOCKER_HOST") != "" {
return nil
}

cmd := execCommand("docker", "context", "inspect", "--format", "{{.Endpoints.docker.Host}}")
out, err := cmd.Output()
if err != nil {
exitError, ok := err.(*exec.ExitError)
if ok && exitError.ExitCode() == 1 {
return nil
}
return fmt.Errorf("resolving docker context host: %w", err)
}

dockerHost := strings.TrimSpace(string(out))
if dockerHost != "" {
return os.Setenv("DOCKER_HOST", dockerHost)
}

return nil
}

func ConfigurePodmanHost() error {
switch runtime.GOOS {
case "windows":
Expand Down
112 changes: 112 additions & 0 deletions pkg/connectors/container_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package connectors

import (
"context"
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConfigureDockerHost(t *testing.T) {
oldDockerHost := os.Getenv("DOCKER_HOST")
defer func() {
if oldDockerHost == "" {
os.Unsetenv("DOCKER_HOST")
} else {
os.Setenv("DOCKER_HOST", oldDockerHost)
}
}()

oldExecCommand := execCommand
defer func() {
execCommand = oldExecCommand
}()

t.Run("sets DOCKER_HOST from docker context", func(t *testing.T) {
os.Unsetenv("DOCKER_HOST")

execCommand = func(name string, args ...string) *exec.Cmd {
cmd := exec.Command(
os.Args[0],
"-test.run=TestHelperProcess",
"--",
"npipe:////./pipe/dockerDesktopLinuxEngine",
)
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
return cmd
}

require.NoError(t, ConfigureDockerHost())
assert.Equal(t, "npipe:////./pipe/dockerDesktopLinuxEngine", os.Getenv("DOCKER_HOST"))
})

t.Run("does not overwrite existing DOCKER_HOST", func(t *testing.T) {
require.NoError(t, os.Setenv("DOCKER_HOST", "existing-endpoint"))

require.NoError(t, ConfigureDockerHost())
assert.Equal(t, "existing-endpoint", os.Getenv("DOCKER_HOST"))
})

t.Run("returns error when docker context command fails", func(t *testing.T) {
os.Unsetenv("DOCKER_HOST")

execCommand = func(name string, args ...string) *exec.Cmd {
cmd := exec.Command(
os.Args[0],
"-test.run=TestHelperProcessFailure",
)
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
return cmd
}

err := ConfigureDockerHost()
require.Error(t, err)
assert.Contains(t, err.Error(), "resolving docker context host")
})
}
func TestNewDockerClient(t *testing.T) {
oldDockerHost := os.Getenv("DOCKER_HOST")
defer func() {
if oldDockerHost == "" {
os.Unsetenv("DOCKER_HOST")
} else {
os.Setenv("DOCKER_HOST", oldDockerHost)
}
}()

os.Unsetenv("DOCKER_HOST")

dockerClient, err := NewDockerClient()
require.NoError(t, err)
require.NotNil(t, dockerClient)

defer dockerClient.cli.Close()

ctx := context.Background()
_, err = dockerClient.cli.Ping(ctx)
require.NoError(t, err)
}
func TestHelperProcess(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}

for i, arg := range os.Args {
if arg == "--" && i+1 < len(os.Args) {
os.Stdout.WriteString(os.Args[i+1])
os.Exit(0)
}
}

os.Exit(1)
}
func TestHelperProcessFailure(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}

os.Exit(2)
}