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

chore: add set docker host by current context #9094

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
16 changes: 16 additions & 0 deletions pkg/skaffold/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -107,6 +108,21 @@ func newEnvAPIClient() ([]string, client.CommonAPIClient, error) {
} else {
opts = append(opts, client.FromEnv)
}
} else {
log.Entry(context.TODO()).Infof("DOCKER_HOST env is not set, using the host from docker context.")

command := exec.Command("docker", "context", "inspect", "--format", "{{.Endpoints.docker.Host}}")
out, err := util.RunCmdOut(context.TODO(), command)
if err != nil {
// docker cli not installed.
log.Entry(context.TODO()).Warnf("Could not get docker context: %s, falling back to the default docker host", err)
} else {
s := strings.TrimSpace(string(out))
// output can be empty if user uses docker as alias for podman
if len(s) > 0 {
opts = append(opts, client.WithHost(s))
}
}
}

cli, err := client.NewClientWithOpts(opts...)
Expand Down
28 changes: 25 additions & 3 deletions pkg/skaffold/docker/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestNewEnvClient(t *testing.T) {
tests := []struct {
description string
envs map[string]string
command *testutil.FakeCmd
shouldErr bool
}{
{
Expand All @@ -55,10 +56,28 @@ func TestNewEnvClient(t *testing.T) {
},
shouldErr: false,
},
{
description: "DOCKER_HOST not set",
command: testutil.CmdRunOut("docker context inspect --format {{.Endpoints.docker.Host}}", ""),
shouldErr: false,
},
{
description: "DOCKER_HOST not set, output invalid host",
command: testutil.CmdRunOut("docker context inspect --format {{.Endpoints.docker.Host}}", "invalid host"),
shouldErr: true,
},
{
description: "DOCKER_HOST not set, ignore error",
command: testutil.CmdRunOutErr("docker context inspect --format {{.Endpoints.docker.Host}}", "", fmt.Errorf("cannot get context")),
shouldErr: false,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.SetEnvs(test.envs)
if test.command != nil {
t.Override(&util.DefaultExecCommand, test.command)
}

env, _, err := newEnvAPIClient()

Expand Down Expand Up @@ -145,15 +164,18 @@ DOCKER_HOST`),
},
{
description: "minikube exit code 64 (minikube < 1.13.0) - fallback to host docker",
command: testutil.CmdRunOutErr("minikube docker-env --shell none -p minikube", "", fmt.Errorf("fail: %w", &oldBadUsageErr{})),
command: testutil.CmdRunOutErr("minikube docker-env --shell none -p minikube", "", fmt.Errorf("fail: %w", &oldBadUsageErr{})).
AndRunOut("docker context inspect --format {{.Endpoints.docker.Host}}", ""),
},
{
description: "minikube exit code 51 (minikube >= 1.13.0) - fallback to host docker",
command: testutil.CmdRunOutErr("minikube docker-env --shell none -p minikube", "", fmt.Errorf("fail: %w", &driverConflictErr{})),
command: testutil.CmdRunOutErr("minikube docker-env --shell none -p minikube", "", fmt.Errorf("fail: %w", &driverConflictErr{})).
AndRunOut("docker context inspect --format {{.Endpoints.docker.Host}}", ""),
},
{
description: "minikube exit code 89 - fallback to host docker",
command: testutil.CmdRunOutErr("minikube docker-env --shell none -p minikube", "", fmt.Errorf("fail: %w", &exGuestUnavailable{})),
command: testutil.CmdRunOutErr("minikube docker-env --shell none -p minikube", "", fmt.Errorf("fail: %w", &exGuestUnavailable{})).
AndRunOut("docker context inspect --format {{.Endpoints.docker.Host}}", ""),
},
}
for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/test/structure/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestNewRunner(t *testing.T) {
return "", semver.Version{}, errors.New("not found")
})

t.Override(&util.DefaultExecCommand, testutil.CmdRun("container-structure-test test -v warn --image image:tag --config "+tmpDir.Path("test.yaml")))
t.Override(&util.DefaultExecCommand, testutil.CmdRunOut("docker context inspect --format {{.Endpoints.docker.Host}}", "").AndRun("container-structure-test test -v warn --image image:tag --config "+tmpDir.Path("test.yaml")))

testCase := &latest.TestCase{
ImageName: "image",
Expand Down
Loading