Skip to content

Commit

Permalink
Merge pull request #1035 from mat007/kubernetes-check-hosts-match
Browse files Browse the repository at this point in the history
For docker stack ls make an error if Swarm and Kubernetes hosts do not match
  • Loading branch information
Vincent Demeester authored May 22, 2018
2 parents 56fc890 + bfe9aed commit cc6ff56
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cli/command/stack/kubernetes/cli.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit cc6ff56

Please sign in to comment.