Skip to content

Commit

Permalink
Merge pull request #9510 from ilya-zuyev/gh_9463_non_dev_dockerhost
Browse files Browse the repository at this point in the history
Support non-default docker endpoints
  • Loading branch information
medyagh authored Nov 17, 2020
2 parents f2e88b4 + 76c62af commit 8b39f56
Show file tree
Hide file tree
Showing 17 changed files with 416 additions and 79 deletions.
71 changes: 56 additions & 15 deletions cmd/minikube/cmd/docker-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/spf13/cobra"
"k8s.io/klog/v2"

"k8s.io/minikube/pkg/drivers/kic/oci"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/constants"
Expand All @@ -43,7 +44,31 @@ import (
"k8s.io/minikube/pkg/minikube/sysinit"
)

var dockerEnvTmpl = fmt.Sprintf("{{ .Prefix }}%s{{ .Delimiter }}{{ .DockerTLSVerify }}{{ .Suffix }}{{ .Prefix }}%s{{ .Delimiter }}{{ .DockerHost }}{{ .Suffix }}{{ .Prefix }}%s{{ .Delimiter }}{{ .DockerCertPath }}{{ .Suffix }}{{ .Prefix }}%s{{ .Delimiter }}{{ .MinikubeDockerdProfile }}{{ .Suffix }}{{ if .NoProxyVar }}{{ .Prefix }}{{ .NoProxyVar }}{{ .Delimiter }}{{ .NoProxyValue }}{{ .Suffix }}{{end}}{{ .UsageHint }}", constants.DockerTLSVerifyEnv, constants.DockerHostEnv, constants.DockerCertPathEnv, constants.MinikubeActiveDockerdEnv)
var dockerSetEnvTmpl = fmt.Sprintf(
"{{ .Prefix }}%s{{ .Delimiter }}{{ .DockerTLSVerify }}{{ .Suffix }}"+
"{{ .Prefix }}%s{{ .Delimiter }}{{ .DockerHost }}{{ .Suffix }}"+
"{{ .Prefix }}%s{{ .Delimiter }}{{ .DockerCertPath }}{{ .Suffix }}"+
"{{ if .ExistingDockerTLSVerify }}"+
"{{ .Prefix }}%s{{ .Delimiter }}{{ .ExistingDockerTLSVerify }}{{ .Suffix }}"+
"{{ end }}"+
"{{ if .ExistingDockerHost }}"+
"{{ .Prefix }}%s{{ .Delimiter }}{{ .ExistingDockerHost }}{{ .Suffix }}"+
"{{ end }}"+
"{{ if .ExistingDockerCertPath }}"+
"{{ .Prefix }}%s{{ .Delimiter }}{{ .ExistingDockerCertPath }}{{ .Suffix }}"+
"{{ end }}"+
"{{ .Prefix }}%s{{ .Delimiter }}{{ .MinikubeDockerdProfile }}{{ .Suffix }}"+
"{{ if .NoProxyVar }}"+
"{{ .Prefix }}{{ .NoProxyVar }}{{ .Delimiter }}{{ .NoProxyValue }}{{ .Suffix }}"+
"{{ end }}"+
"{{ .UsageHint }}",
constants.DockerTLSVerifyEnv,
constants.DockerHostEnv,
constants.DockerCertPathEnv,
constants.ExistingDockerTLSVerifyEnv,
constants.ExistingDockerHostEnv,
constants.ExistingDockerCertPathEnv,
constants.MinikubeActiveDockerdEnv)

// DockerShellConfig represents the shell config for Docker
type DockerShellConfig struct {
Expand All @@ -54,6 +79,10 @@ type DockerShellConfig struct {
MinikubeDockerdProfile string
NoProxyVar string
NoProxyValue string

ExistingDockerCertPath string
ExistingDockerHost string
ExistingDockerTLSVerify string
}

var (
Expand Down Expand Up @@ -81,6 +110,11 @@ func dockerShellCfgSet(ec DockerEnvConfig, envMap map[string]string) *DockerShel
s.DockerCertPath = envMap[constants.DockerCertPathEnv]
s.DockerHost = envMap[constants.DockerHostEnv]
s.DockerTLSVerify = envMap[constants.DockerTLSVerifyEnv]

s.ExistingDockerCertPath = envMap[constants.ExistingDockerCertPathEnv]
s.ExistingDockerHost = envMap[constants.ExistingDockerHostEnv]
s.ExistingDockerTLSVerify = envMap[constants.ExistingDockerTLSVerifyEnv]

s.MinikubeDockerdProfile = envMap[constants.MinikubeActiveDockerdEnv]

if ec.noProxy {
Expand Down Expand Up @@ -134,8 +168,17 @@ var dockerEnvCmd = &cobra.Command{
Short: "Configure environment to use minikube's Docker daemon",
Long: `Sets up docker env variables; similar to '$(docker-machine env)'.`,
Run: func(cmd *cobra.Command, args []string) {
var err error

shl := shell.ForceShell
if shl == "" {
shl, err = shell.Detect()
if err != nil {
exit.Error(reason.InternalShellDetect, "Error detecting shell", err)
}
}
sh := shell.EnvConfig{
Shell: shell.ForceShell,
Shell: shl,
}

if dockerUnset {
Expand Down Expand Up @@ -167,7 +210,6 @@ var dockerEnvCmd = &cobra.Command{
mustRestartDocker(cname, co.CP.Runner)
}

var err error
port := constants.DockerDaemonPort
if driver.NeedsPortForward(driverName) {
port, err = oci.ForwardedPort(driverName, cname, port)
Expand All @@ -186,13 +228,6 @@ var dockerEnvCmd = &cobra.Command{
noProxy: noProxy,
}

if ec.Shell == "" {
ec.Shell, err = shell.Detect()
if err != nil {
exit.Error(reason.InternalShellDetect, "Error detecting shell", err)
}
}

dockerPath, err := exec.LookPath("docker")
if err != nil {
klog.Warningf("Unable to find docker in path - skipping connectivity check: %v", err)
Expand Down Expand Up @@ -228,7 +263,7 @@ type DockerEnvConfig struct {
// dockerSetScript writes out a shell-compatible 'docker-env' script
func dockerSetScript(ec DockerEnvConfig, w io.Writer) error {
envVars := dockerEnvVars(ec)
return shell.SetScript(ec.EnvConfig, w, dockerEnvTmpl, dockerShellCfgSet(ec, envVars))
return shell.SetScript(ec.EnvConfig, w, dockerSetEnvTmpl, dockerShellCfgSet(ec, envVars))
}

// dockerSetScript writes out a shell-compatible 'docker-env unset' script
Expand All @@ -246,7 +281,6 @@ func dockerUnsetScript(ec DockerEnvConfig, w io.Writer) error {
vars = append(vars, k)
}
}

return shell.UnsetScript(ec.EnvConfig, w, vars)
}

Expand All @@ -257,14 +291,21 @@ func dockerURL(ip string, port int) string {

// dockerEnvVars gets the necessary docker env variables to allow the use of minikube's docker daemon
func dockerEnvVars(ec DockerEnvConfig) map[string]string {
env := map[string]string{
rt := map[string]string{
constants.DockerTLSVerifyEnv: "1",
constants.DockerHostEnv: dockerURL(ec.hostIP, ec.port),
constants.DockerCertPathEnv: ec.certsDir,
constants.MinikubeActiveDockerdEnv: ec.profile,
}

return env
if os.Getenv(constants.MinikubeActiveDockerdEnv) == "" {
for _, env := range constants.DockerDaemonEnvs {
if v := oci.InitialEnv(env); v != "" {
key := constants.MinikubeExistingPrefix + env
rt[key] = v
}
}
}
return rt
}

// dockerEnvVarsList gets the necessary docker env variables to allow the use of minikube's docker daemon to be used in a exec.Command
Expand Down
49 changes: 40 additions & 9 deletions cmd/minikube/cmd/docker-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export MINIKUBE_ACTIVE_DOCKERD="dockerdriver"
# To point your shell to minikube's docker-daemon, run:
# eval $(minikube -p dockerdriver docker-env)
`,
`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD
`unset DOCKER_TLS_VERIFY;
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
`,
},
{
Expand All @@ -67,7 +70,10 @@ export MINIKUBE_ACTIVE_DOCKERD="bash"
# To point your shell to minikube's docker-daemon, run:
# eval $(minikube -p bash docker-env)
`,
`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD
`unset DOCKER_TLS_VERIFY;
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
`,
},
{
Expand All @@ -82,7 +88,10 @@ export MINIKUBE_ACTIVE_DOCKERD="ipv6"
# To point your shell to minikube's docker-daemon, run:
# eval $(minikube -p ipv6 docker-env)
`,
`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD
`unset DOCKER_TLS_VERIFY;
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
`,
},
{
Expand Down Expand Up @@ -115,7 +124,10 @@ $Env:MINIKUBE_ACTIVE_DOCKERD = "powershell"
# & minikube -p powershell docker-env | Invoke-Expression
`,

`Remove-Item Env:\\DOCKER_TLS_VERIFY Env:\\DOCKER_HOST Env:\\DOCKER_CERT_PATH Env:\\MINIKUBE_ACTIVE_DOCKERD
`Remove-Item Env:\\DOCKER_TLS_VERIFY
Remove-Item Env:\\DOCKER_HOST
Remove-Item Env:\\DOCKER_CERT_PATH
Remove-Item Env:\\MINIKUBE_ACTIVE_DOCKERD
`,
},
{
Expand Down Expand Up @@ -167,7 +179,11 @@ export NO_PROXY="127.0.0.1"
# eval $(minikube -p bash-no-proxy docker-env)
`,

`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD NO_PROXY
`unset DOCKER_TLS_VERIFY;
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
unset NO_PROXY;
`,
},
{
Expand All @@ -184,7 +200,11 @@ export no_proxy="127.0.0.1"
# eval $(minikube -p bash-no-proxy-lower docker-env)
`,

`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD no_proxy
`unset DOCKER_TLS_VERIFY;
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
unset no_proxy;
`,
},
{
Expand All @@ -200,7 +220,11 @@ $Env:no_proxy = "192.168.0.1"
# & minikube -p powershell-no-proxy-idempotent docker-env | Invoke-Expression
`,

`Remove-Item Env:\\DOCKER_TLS_VERIFY Env:\\DOCKER_HOST Env:\\DOCKER_CERT_PATH Env:\\MINIKUBE_ACTIVE_DOCKERD Env:\\no_proxy
`Remove-Item Env:\\DOCKER_TLS_VERIFY
Remove-Item Env:\\DOCKER_HOST
Remove-Item Env:\\DOCKER_CERT_PATH
Remove-Item Env:\\MINIKUBE_ACTIVE_DOCKERD
Remove-Item Env:\\no_proxy
`,
},
{
Expand All @@ -217,7 +241,11 @@ export NO_PROXY="192.168.0.1,10.0.0.4,127.0.0.1"
# eval $(minikube -p sh-no-proxy-add docker-env)
`,

`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD NO_PROXY
`unset DOCKER_TLS_VERIFY;
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
unset NO_PROXY;
`,
},
{
Expand All @@ -229,7 +257,10 @@ DOCKER_HOST=tcp://127.0.0.1:32842
DOCKER_CERT_PATH=/certs
MINIKUBE_ACTIVE_DOCKERD=noneshell
`,
`DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD
`DOCKER_TLS_VERIFY
DOCKER_HOST
DOCKER_CERT_PATH
MINIKUBE_ACTIVE_DOCKERD
`,
},
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/minikube/cmd/podman-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export MINIKUBE_ACTIVE_PODMAN="bash"
# To point your shell to minikube's podman service, run:
# eval $(minikube -p bash podman-env)
`,
`unset PODMAN_VARLINK_BRIDGE MINIKUBE_ACTIVE_PODMAN
`unset PODMAN_VARLINK_BRIDGE;
unset MINIKUBE_ACTIVE_PODMAN;
`,
},
{
Expand All @@ -62,7 +63,9 @@ export MINIKUBE_ACTIVE_PODMAN="bash"
# To point your shell to minikube's podman service, run:
# eval $(minikube -p bash podman-env)
`,
`unset CONTAINER_HOST CONTAINER_SSHKEY MINIKUBE_ACTIVE_PODMAN
`unset CONTAINER_HOST;
unset CONTAINER_SSHKEY;
unset MINIKUBE_ACTIVE_PODMAN;
`,
},
}
Expand Down
27 changes: 18 additions & 9 deletions pkg/drivers/kic/kic.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/docker/machine/libmachine/state"
"github.com/pkg/errors"
"k8s.io/klog/v2"

pkgdrivers "k8s.io/minikube/pkg/drivers"
"k8s.io/minikube/pkg/drivers/kic/oci"
"k8s.io/minikube/pkg/minikube/assets"
Expand Down Expand Up @@ -93,22 +94,30 @@ func (d *Driver) Create() error {
klog.Infof("calculated static IP %q for the %q container", ip.String(), d.NodeConfig.MachineName)
params.IP = ip.String()
}
drv := d.DriverName()
listAddr := oci.DefaultBindIPV4
if oci.IsExternalDaemonHost(drv) {
out.WarningT("Listening to 0.0.0.0 on external docker host {{.host}}. Please be advised",
out.V{"host": oci.DaemonHost(drv)})
listAddr = "0.0.0.0"
}

// control plane specific options
params.PortMappings = append(params.PortMappings, oci.PortMapping{
ListenAddress: oci.DefaultBindIPV4,
ContainerPort: int32(params.APIServerPort),
},
params.PortMappings = append(params.PortMappings,
oci.PortMapping{
ListenAddress: listAddr,
ContainerPort: int32(params.APIServerPort),
},
oci.PortMapping{
ListenAddress: oci.DefaultBindIPV4,
ListenAddress: listAddr,
ContainerPort: constants.SSHPort,
},
oci.PortMapping{
ListenAddress: oci.DefaultBindIPV4,
ListenAddress: listAddr,
ContainerPort: constants.DockerDaemonPort,
},
oci.PortMapping{
ListenAddress: oci.DefaultBindIPV4,
ListenAddress: listAddr,
ContainerPort: constants.RegistryAddonPort,
},
)
Expand Down Expand Up @@ -224,12 +233,12 @@ func (d *Driver) GetIP() (string, error) {

// GetExternalIP returns an IP which is accessible from outside
func (d *Driver) GetExternalIP() (string, error) {
return oci.DefaultBindIPV4, nil
return oci.DaemonHost(d.DriverName()), nil
}

// GetSSHHostname returns hostname for use with ssh
func (d *Driver) GetSSHHostname() (string, error) {
return oci.DefaultBindIPV4, nil
return oci.DaemonHost(d.DriverName()), nil
}

// GetSSHPort returns port for use with ssh
Expand Down
1 change: 1 addition & 0 deletions pkg/drivers/kic/oci/cli_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"k8s.io/klog/v2"

"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/style"
)
Expand Down
48 changes: 48 additions & 0 deletions pkg/drivers/kic/oci/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2020 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package oci

import (
"os"

"k8s.io/minikube/pkg/minikube/constants"
)

var initialEnvs = make(map[string]string)

func init() {
for _, env := range constants.DockerDaemonEnvs {
if v, set := os.LookupEnv(env); set {
initialEnvs[env] = v
}
exEnv := constants.MinikubeExistingPrefix + env
if v, set := os.LookupEnv(exEnv); set {
initialEnvs[exEnv] = v
}
}
}

// InitialEnv returns the value of the environment variable env before any environment changes made by minikube
func InitialEnv(env string) string {
return initialEnvs[env]
}

// LookupInitialEnv returns the value of the environment variable env before any environment changes made by minikube
func LookupInitialEnv(env string) (string, bool) {
v, set := initialEnvs[env]
return v, set
}
Loading

0 comments on commit 8b39f56

Please sign in to comment.