Skip to content

Commit

Permalink
Ensure DNS is looked up on boostrap
Browse files Browse the repository at this point in the history
PR etcd-io#13224 introduced an early exit when the names match. This change
restores the previous behaviour by doing an explicit DNS resolution
before checking the URLs for equality.

Signed-off-by: Thomas Jungblut <tjungblu@redhat.com>
  • Loading branch information
tjungblu committed Jan 5, 2023
1 parent 9e3966f commit fc3dbba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
8 changes: 4 additions & 4 deletions pkg/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func resolveTCPAddrDefault(ctx context.Context, addr string) (*net.TCPAddr, erro
return &net.TCPAddr{IP: ip.IP, Port: portnum, Zone: ip.Zone}, nil
}

// resolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
// resolveTCPAddrs return a new set of url.URLs, in which all DNS hostnames
// ResolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
// ResolveTCPAddrs return a new set of url.URLs, in which all DNS hostnames
// are resolved.
func resolveTCPAddrs(ctx context.Context, lg *zap.Logger, urls [][]url.URL) ([][]url.URL, error) {
func ResolveTCPAddrs(ctx context.Context, lg *zap.Logger, urls [][]url.URL) ([][]url.URL, error) {
newurls := make([][]url.URL, 0)
for _, us := range urls {
nus := make([]url.URL, len(us))
Expand Down Expand Up @@ -163,7 +163,7 @@ func urlsEqual(ctx context.Context, lg *zap.Logger, a []url.URL, b []url.URL) (b
}

// If URLs are not equal, try to resolve it and compare again.
urls, err := resolveTCPAddrs(ctx, lg, [][]url.URL{a, b})
urls, err := ResolveTCPAddrs(ctx, lg, [][]url.URL{a, b})
if err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/netutil/netutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestResolveTCPAddrs(t *testing.T) {
return &net.TCPAddr{IP: net.ParseIP(tt.hostMap[host]), Port: i, Zone: ""}, nil
}
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
urls, err := resolveTCPAddrs(ctx, zap.NewExample(), tt.urls)
urls, err := ResolveTCPAddrs(ctx, zap.NewExample(), tt.urls)
cancel()
if tt.hasError {
if err == nil {
Expand Down
14 changes: 10 additions & 4 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package config
import (
"context"
"fmt"
"net/url"
"path/filepath"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -240,11 +240,17 @@ func (c *ServerConfig) hasLocalMember() error {

// advertiseMatchesCluster confirms peer URLs match those in the cluster peer list.
func (c *ServerConfig) advertiseMatchesCluster() error {
urls, apurls := c.InitialPeerURLsMap[c.Name], c.PeerURLs.StringSlice()
urls.Sort()
sort.Strings(apurls)
urls := c.InitialPeerURLsMap[c.Name]
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
defer cancel()

// try to resolve the urls that were passed to fail early on bootstrap
_, err := netutil.ResolveTCPAddrs(ctx, c.Logger, [][]url.URL{urls, c.PeerURLs})
if err != nil {
return fmt.Errorf("failed to resolve addresses: --initial-cluster=%s --initial-advertise-peer-urls=%s (%v)", urls, c.PeerURLs, err)
}

apurls := c.PeerURLs.StringSlice()
ok, err := netutil.URLStringsEqual(ctx, c.Logger, apurls, urls.StringSlice())
if ok {
return nil
Expand Down

0 comments on commit fc3dbba

Please sign in to comment.