From bfe9aed63a194586b8fbb3a71c64712ee1c7e80d Mon Sep 17 00:00:00 2001 From: Mathieu Champlon Date: Wed, 2 May 2018 18:52:31 +0200 Subject: [PATCH] For docker stack ls make an error if Swarm and Kubernetes hosts do no match Signed-off-by: Mathieu Champlon --- cli/command/stack/kubernetes/cli.go | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/cli/command/stack/kubernetes/cli.go b/cli/command/stack/kubernetes/cli.go index 018dd05e21..27bc819fa4 100644 --- a/cli/command/stack/kubernetes/cli.go +++ b/cli/command/stack/kubernetes/cli.go @@ -1,6 +1,10 @@ package kubernetes import ( + "fmt" + "net" + "net/url" + "github.com/docker/cli/cli/command" "github.com/docker/cli/kubernetes" flag "github.com/spf13/pflag" @@ -69,9 +73,43 @@ func WrapCli(dockerCli command.Cli, opts Options) (*KubeCli, error) { } cli.clientSet = clientSet + if dockerCli.ClientInfo().HasAll() { + if err := cli.checkHostsMatch(); err != nil { + return nil, err + } + } return cli, nil } func (c *KubeCli) composeClient() (*Factory, error) { return NewFactory(c.kubeNamespace, c.kubeConfig, c.clientSet) } + +func (c *KubeCli) checkHostsMatch() error { + daemonEndpoint, err := url.Parse(c.Client().DaemonHost()) + if err != nil { + return err + } + kubeEndpoint, err := url.Parse(c.kubeConfig.Host) + if err != nil { + return err + } + if daemonEndpoint.Hostname() == kubeEndpoint.Hostname() { + return nil + } + // The daemon can be local in Docker for Desktop, e.g. "npipe", "unix", ... + if daemonEndpoint.Scheme != "tcp" { + ips, err := net.LookupIP(kubeEndpoint.Hostname()) + if err != nil { + return err + } + for _, ip := range ips { + if ip.IsLoopback() { + return nil + } + } + } + fmt.Fprintf(c.Err(), "WARNING: Swarm and Kubernetes hosts do not match (docker host=%s, kubernetes host=%s).\n"+ + " Update $DOCKER_HOST (or pass -H), or use 'kubectl config use-context' to match.\n", daemonEndpoint.Hostname(), kubeEndpoint.Hostname()) + return nil +}