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

Support non-default docker endpoints #9510

Merged
merged 19 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
52 changes: 46 additions & 6 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 @@ -228,7 +262,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 +280,6 @@ func dockerUnsetScript(ec DockerEnvConfig, w io.Writer) error {
vars = append(vars, k)
}
}

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

Expand All @@ -257,14 +290,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
26 changes: 17 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,29 @@ 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.IsExternalRuntimeHost(drv) {
klog.Infof("Listening 0.0.0.0 on external docker host %v", oci.RuntimeHost(drv))
medyagh marked this conversation as resolved.
Show resolved Hide resolved
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 +232,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.RuntimeHost(d.DriverName()), nil
}

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

// GetSSHPort returns port for use with ssh
Expand Down
2 changes: 2 additions & 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 Expand Up @@ -128,6 +129,7 @@ func runCmd(cmd *exec.Cmd, warnSlow ...bool) (*RunResult, error) {
cmd.Stderr = errb

start := time.Now()
klog.Infof("Running %v", cmd.Args)
err := cmd.Run()
elapsed := time.Since(start)
if warn {
Expand Down
47 changes: 47 additions & 0 deletions pkg/drivers/kic/oci/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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
}
1 change: 0 additions & 1 deletion pkg/drivers/kic/oci/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,5 @@ func dockerContainerIP(name string) (string, string, error) {
if len(ips) != 2 {
return "", "", errors.Errorf("container addresses should have 2 values, got %d values: %+v", len(ips), ips)
}

return ips[0], ips[1], nil
}
78 changes: 61 additions & 17 deletions pkg/drivers/kic/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ limitations under the License.
package oci

import (
"bufio"
"bytes"
"context"
"fmt"
"net/url"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"

"bufio"
"bytes"

"github.com/docker/machine/libmachine/state"
"github.com/pkg/errors"

"k8s.io/klog/v2"

"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/util/retry"

"fmt"
"os/exec"
"runtime"
"strconv"
"strings"
)

// DeleteContainersByLabel deletes all containers that have a specific label
Expand Down Expand Up @@ -521,18 +521,29 @@ func ListContainersByLabel(ociBin string, label string, warnSlow ...bool) ([]str
// PointToHostDockerDaemon will unset env variables that point to docker inside minikube
// to make sure it points to the docker daemon installed by user.
func PointToHostDockerDaemon() error {
p := os.Getenv(constants.MinikubeActiveDockerdEnv)
if p != "" {

if p := os.Getenv(constants.MinikubeActiveDockerdEnv); p != "" {
klog.Infof("shell is pointing to dockerd inside minikube. will unset to use host")
for _, e := range constants.DockerDaemonEnvs {
if err := resetEnv(e); err != nil {
return err
}
}
}

for i := range constants.DockerDaemonEnvs {
e := constants.DockerDaemonEnvs[i]
err := os.Setenv(e, "")
if err != nil {
return errors.Wrapf(err, "resetting %s env", e)
}
return nil
}

func resetEnv(key string) error {
v := os.Getenv(constants.MinikubeExistingPrefix + key)
if v == "" {
if err := os.Unsetenv(key); err != nil {
return errors.Wrapf(err, "resetting %s env", key)
}
return nil
}
if err := os.Setenv(key, v); err != nil {
return errors.Wrapf(err, "resetting %s env", key)
}
return nil
}
Expand Down Expand Up @@ -628,3 +639,36 @@ func iptablesFileExists(ociBin string, nameOrID string) bool {
}
return true
}

// RuntimeHost returns the ip/hostname where OCI daemon service for driver is running
// For Podman it's always DefaultBindIPV4
// For Docker return the host part of DOCKER_HOST environment variable if set
// or DefaultBindIPV4 otherwise
func RuntimeHost(driver string) string {
medyagh marked this conversation as resolved.
Show resolved Hide resolved
if driver != Docker {
return DefaultBindIPV4
}
if dh := os.Getenv(constants.DockerHostEnv); dh != "" {
if u, err := url.Parse(dh); err == nil {
if u.Host != "" {
return u.Hostname()
}
}
}
return DefaultBindIPV4
}

// IsExternalRuntimeHost returns whether or not the OCI runtime is running on an external/virtual host
// For Podman driver it's always false for now
Copy link
Collaborator

Choose a reason for hiding this comment

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

the rules for podman should be the same as for docker, when it comes to external vs. local
only the default transport differs, if it is tcp: or ssh: (as opposed to unix: for the local socket)

eventually it is going to end up looking very similar, once we get rid of the legacy protocols
then it will always be the same unix socket, possibly tunneled over a ssh connection first...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I just want to reduce the scope of this PR and implement support of external docker endpoints here, and later do the same for Podman.

Copy link
Member

@medyagh medyagh Nov 6, 2020

Choose a reason for hiding this comment

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

I am okay with adding that in a following up PR, once we add support for non default podman

// For Docker driver return true if DOCKER_HOST is set to a URI, and the URI contains a host item
func IsExternalRuntimeHost(driver string) bool {
medyagh marked this conversation as resolved.
Show resolved Hide resolved
if driver != Docker {
return false
}
if dh := os.Getenv(constants.DockerHostEnv); dh != "" {
if u, err := url.Parse(dh); err == nil {
return u.Host != ""
}
}
return false
}
Loading