Skip to content

Commit

Permalink
config, info: add config proxy.advertise-addr (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored Mar 27, 2024
1 parent d58674c commit b543b47
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions conf/proxy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[proxy]
# addr = "0.0.0.0:6000"
# advertise-addr = ""
# tcp-keep-alive = true

# possible values:
Expand Down
1 change: 1 addition & 0 deletions lib/config/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type ProxyServerOnline struct {

type ProxyServer struct {
Addr string `yaml:"addr,omitempty" toml:"addr,omitempty" json:"addr,omitempty"`
AdvertiseAddr string `yaml:"advertise-addr,omitempty" toml:"advertise-addr,omitempty" json:"advertise-addr,omitempty"`
PDAddrs string `yaml:"pd-addrs,omitempty" toml:"pd-addrs,omitempty" json:"pd-addrs,omitempty"`
ProxyServerOnline `yaml:",inline" toml:",inline" json:",inline"`
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/manager/infosync/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,16 @@ func (is *InfoSyncer) getTopologyInfo(cfg *config.Config) (*TopologyInfo, error)
if err != nil {
return nil, errors.WithStack(err)
}
// reporting a non unicast IP makes no sense, try to find one
// loopback/linklocal-unicast are not global unicast IP, but are valid local unicast IP
if pip := net.ParseIP(ip); ip == "" || pip.Equal(net.IPv4bcast) || pip.IsUnspecified() || pip.IsMulticast() {
if v := sys.GetGlobalUnicastIP(); v != "" {
ip = v
// AdvertiseAddr may be a DNS in k8s and certificate SAN typically contains DNS but not IP.
if len(cfg.Proxy.AdvertiseAddr) > 0 {
ip = cfg.Proxy.AdvertiseAddr
} else {
// reporting a non unicast IP makes no sense, try to find one
// loopback/linklocal-unicast are not global unicast IP, but are valid local unicast IP
if pip := net.ParseIP(ip); ip == "" || pip.Equal(net.IPv4bcast) || pip.IsUnspecified() || pip.IsMulticast() {
if v := sys.GetGlobalUnicastIP(); v != "" {
ip = v
}
}
}
return &TopologyInfo{
Expand Down
40 changes: 23 additions & 17 deletions pkg/manager/infosync/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,32 +182,38 @@ func TestGetTopology(t *testing.T) {
ts := newEtcdTestSuite(t)
t.Cleanup(ts.close)
for _, cas := range []struct {
ip string
port string
non_unicast bool
addr string
advertiseAddr string
port string
nonUnicast bool
}{
{":34", "34", true},
{"0.0.0.0:34", "34", true},
{"255.255.255.255:34", "34", true},
{"239.255.255.255:34", "34", true},
{"[FF02::1:FF47]:34", "34", true},
{"127.0.0.1:34", "34", false},
{"[F02::1:FF47]:34", "34", false},
{"192.0.0.1:6049", "6049", false},
{":34", "", "34", true},
{"0.0.0.0:34", "", "34", true},
{"255.255.255.255:34", "", "34", true},
{"239.255.255.255:34", "", "34", true},
{"[FF02::1:FF47]:34", "", "34", true},
{"127.0.0.1:34", "", "34", false},
{"[F02::1:FF47]:34", "", "34", false},
{"192.0.0.1:6049", "", "6049", false},
{"0.0.0.0:1000", "tc-tiproxy-0.tc-tiproxy-peer.ns.svc", "1000", false},
} {
is, err := ts.is.getTopologyInfo(&config.Config{
Proxy: config.ProxyServer{
Addr: cas.ip,
Addr: cas.addr,
AdvertiseAddr: cas.advertiseAddr,
},
API: config.API{
Addr: cas.ip,
Addr: cas.addr,
},
})
require.NoError(t, err)
ip, _, err := net.SplitHostPort(cas.ip)
require.NoError(t, err)
if cas.non_unicast {
ip = sys.GetGlobalUnicastIP()
ip := cas.advertiseAddr
if len(ip) == 0 {
ip, _, err = net.SplitHostPort(cas.addr)
require.NoError(t, err)
if cas.nonUnicast {
ip = sys.GetGlobalUnicastIP()
}
}
require.Equal(t, ip, is.IP)
require.Equal(t, cas.port, is.Port)
Expand Down

0 comments on commit b543b47

Please sign in to comment.