Skip to content

Commit

Permalink
修改dump2hosts参数,新增dump2hostsNS参数,向下兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
丁祖巍 committed Apr 2, 2020
1 parent 8becc1a commit 8420380
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
36 changes: 23 additions & 13 deletions pkg/kt/command/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ func newConnectCommand(options *options.DaemonOptions, action ActionInterface) c
Usage: "Custom CIDR eq '172.2.0.0/16'",
Destination: &options.ConnectOptions.CIDR,
},
cli.StringFlag{
cli.BoolFlag{
Name: "dump2hosts",
Usage: "Auto write service to local hosts file",
Destination: &options.ConnectOptions.Dump2Hosts,
},
cli.StringSliceFlag{
Name: "dump2hostsNS",
Usage: "Which namespaces service to local hosts file, support multiple namespaces.",
Value: &options.ConnectOptions.Dump2HostsNamespaces,
},
},
Action: func(c *cli.Context) error {
if options.Debug {
Expand Down Expand Up @@ -100,23 +105,28 @@ func (action *Action) Connect(options *options.DaemonOptions) (err error) {

func connectToCluster(shadow connect.ShadowInterface, kubernetes cluster.KubernetesInterface, options *options.DaemonOptions) (err error) {

if options.ConnectOptions.Dump2Hosts != "" {
if options.ConnectOptions.Dump2Hosts {
log.Debug().Msgf("Serach service in %s namespace...", options.Namespace)
hosts := kubernetes.ServiceHosts(options.Namespace)

namespaces := strings.Split(options.ConnectOptions.Dump2Hosts, ",")
for _, namespace := range namespaces {
if namespace == options.Namespace {
continue
}
singleHosts := kubernetes.ServiceHosts(namespace)
for k, v := range singleHosts {
if v == "" || v == "None" {
for k, v := range hosts {
log.Debug().Msgf("Service found: %s %s", k, v)
}
if options.ConnectOptions.Dump2HostsNamespaces != nil {
for _, namespace := range options.ConnectOptions.Dump2HostsNamespaces {
if namespace == options.Namespace {
continue
}
hosts[k+"."+namespace] = v
log.Debug().Msgf("Serach service in %s namespace...", namespace)
singleHosts := kubernetes.ServiceHosts(namespace)
for k, v := range singleHosts {
if v == "" || v == "None" {
continue
}
log.Debug().Msgf("Service found: %s.%s %s", k, namespace, v)
hosts[k+"."+namespace] = v
}
}
}

util.DumpHosts(hosts)
options.ConnectOptions.Hosts = hosts
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/kt/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/alibaba/kt-connect/pkg/kt/util"
"github.com/urfave/cli"
)

type runOptions struct {
Expand All @@ -12,13 +13,14 @@ type runOptions struct {
}

type connectOptions struct {
DisableDNS bool
SSHPort int
Socke5Proxy int
CIDR string
Method string
Dump2Hosts string
Hosts map[string]string
DisableDNS bool
SSHPort int
Socke5Proxy int
CIDR string
Method string
Dump2Hosts bool
Dump2HostsNamespaces cli.StringSlice
Hosts map[string]string
}

type exchangeOptions struct {
Expand Down

0 comments on commit 8420380

Please # to comment.