main: propagate the current Docker CLI context to $DOCKER_HOST
So that google/go-containerregistry can pick it up Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>pull/15450/head
parent
98d5467d9f
commit
ba11072162
|
@ -20,6 +20,7 @@ import (
|
|||
"bytes"
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
|
@ -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"
|
||||
|
@ -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")
|
||||
|
@ -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
go.mod
3
go.mod
|
@ -96,6 +96,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
|
||||
|
@ -128,13 +129,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.9.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
|
||||
|
|
2
go.sum
2
go.sum
|
@ -411,6 +411,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=
|
||||
|
|
Loading…
Reference in New Issue