Skip to content

Commit

Permalink
main: propagate the current Docker CLI context to $DOCKER_HOST
Browse files Browse the repository at this point in the history
So that google/go-containerregistry can pick it up

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Dec 2, 2022
1 parent e2dabd7 commit 6fcb8d8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
58 changes: 58 additions & 0 deletions cmd/minikube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"crypto/sha1"
"encoding/hex"
"errors"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -51,6 +52,10 @@ import (
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/out"
_ "k8s.io/minikube/pkg/provision"

dconfig "github.com/docker/cli/cli/config"
ddocker "github.com/docker/cli/cli/context/docker"
dstore "github.com/docker/cli/cli/context/store"
)

const minikubeEnableProfile = "MINIKUBE_ENABLE_PROFILING"
Expand All @@ -68,6 +73,8 @@ func main() {
bridgeLogMessages()
defer klog.Flush()

propagateDockerContextToEnv()

// Don't parse flags when running as kubectl
_, callingCmd := filepath.Split(os.Args[0])
callingCmd = strings.TrimSuffix(callingCmd, ".exe")
Expand Down Expand Up @@ -247,3 +254,54 @@ func setLastStartFlags() {
klog.Warningf("Unable to set default flag value for log_file: %v", err)
}
}

// propagateDockerContextToEnv propagates the current context in ~/.docker/config.json to $DOCKER_HOST,
// so that google/go-containerregistry can pick it up.
func propagateDockerContextToEnv() {
if os.Getenv("DOCKER_HOST") != "" {
// Already explicitly set
return
}
currentContext := os.Getenv("DOCKER_CONTEXT")
if currentContext == "" {
dockerConfigDir := dconfig.Dir()
if _, err := os.Stat(dockerConfigDir); err != nil {
if !errors.Is(err, os.ErrNotExist) {
klog.Warning(err)
}
return
}
cf, err := dconfig.Load(dockerConfigDir)
if err != nil {
klog.Warningf("Unable to load the current Docker config from %q", dockerConfigDir)
return
}
currentContext = cf.CurrentContext
}
if currentContext == "" {
return
}
storeConfig := dstore.NewConfig(
func() interface{} { return &ddocker.EndpointMeta{} },
dstore.EndpointTypeGetter(ddocker.DockerEndpoint, func() interface{} { return &ddocker.EndpointMeta{} }),
)
st := dstore.New(dconfig.ContextStoreDir(), storeConfig)
md, err := st.GetMetadata(currentContext)
if err != nil {
klog.Warningf("Unable to resolve the current Docker CLI context %q: %v", currentContext, err)
return
}
dockerEP, ok := md.Endpoints[ddocker.DockerEndpoint]
if !ok {
// No warning (the context is not for Docker)
return
}
dockerEPMeta, ok := dockerEP.(ddocker.EndpointMeta)
if !ok {
klog.Warningf("expected docker.EndpointMeta, got %T", dockerEP)
return
}
if dockerEPMeta.Host != "" {
os.Setenv("DOCKER_HOST", dockerEPMeta.Host)
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ require (
require (
github.com/Xuanwo/go-locale v1.1.0
github.com/blang/semver v3.5.1+incompatible
github.com/docker/cli v20.10.20+incompatible
github.com/docker/go-connections v0.4.0
github.com/google/go-github/v43 v43.0.0
github.com/opencontainers/runc v1.1.4
Expand Down Expand Up @@ -132,13 +133,13 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.20+incompatible // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fvbommel/sortorder v1.0.1 // indirect
github.com/go-fonts/liberation v0.2.0 // indirect
github.com/go-latex/latex v0.0.0-20210823091927-c0d11ff05a81 // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa/go.mod h1:KnogPXtdwXqoenmZCw6S+25EAm2MkxbG0deNDu4cbSA=
github.com/fvbommel/sortorder v1.0.1 h1:dSnXLt4mJYH25uDDGa3biZNQsozaUWDSWeKJ0qqFfzE=
github.com/fvbommel/sortorder v1.0.1/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down

0 comments on commit 6fcb8d8

Please sign in to comment.