Skip to content

Commit d59b605

Browse files
committed
cli: "no such host" error when starting CRDB on Mac
When CockroachDB is started without `--advertise-addr`, it fallbacks to `--listen-addr`. If the latter is also unspecified, it defaults to the `hostname`. Subsequently, `validateAdvertiseAddr` attempts to resolve the hostname. If the resolution fails, the server aborts the startup sequence. This is a safe default because node discovery is compromised without a functioning DNS. On Mac, `hostname` is resolved via mDNS / Bonjour. Unless it's suffixed with `.local`, it doesn't resolve. An obvious workaround is to add it to /etc/hosts, or to change it via `sudo hostname $USER.local`. To make the (DEV) experience on Mac more seamless, this PR replaces DNS resolution error with a warning. Since we can't unambiguously determine if the user is running in DEV-mode, we assume that `--insecure` on Mac implies exactly that. We also update `roachprod` to explicitly use `--advertised-addr` with `127.0.0.1`. This also fixes `roachtest` in local mode. Fixes: #66426 Fixes: #149469 Release note: None
1 parent 63545b4 commit d59b605

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

pkg/base/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ go_library(
3030
"//pkg/util",
3131
"//pkg/util/envutil",
3232
"//pkg/util/humanizeutil",
33+
"//pkg/util/log",
34+
"//pkg/util/log/severity",
3335
"//pkg/util/metric",
3436
"//pkg/util/mon",
3537
"//pkg/util/netutil/addr",

pkg/base/addr_validation.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import (
1010
"fmt"
1111
"net"
1212
"os"
13+
"runtime"
1314
"strconv"
1415

1516
"github.com/cockroachdb/cockroach/pkg/cli/cliflags"
17+
"github.com/cockroachdb/cockroach/pkg/util/log"
18+
"github.com/cockroachdb/cockroach/pkg/util/log/severity"
1619
"github.com/cockroachdb/errors"
1720
)
1821

@@ -32,7 +35,7 @@ import (
3235
func (cfg *Config) ValidateAddrs(ctx context.Context) error {
3336
// Validate the advertise address.
3437
advHost, advPort, err := validateAdvertiseAddr(ctx,
35-
cfg.AdvertiseAddr, cfg.Addr, "", cliflags.ListenAddr)
38+
cfg.AdvertiseAddr, cfg.Addr, "", cliflags.ListenAddr, cfg.Insecure)
3639
if err != nil {
3740
return invalidFlagErr(err, cliflags.AdvertiseAddr)
3841
}
@@ -48,7 +51,7 @@ func (cfg *Config) ValidateAddrs(ctx context.Context) error {
4851
// Validate the SQL advertise address. Use the provided advertise
4952
// addr as default.
5053
advSQLHost, advSQLPort, err := validateAdvertiseAddr(ctx,
51-
cfg.SQLAdvertiseAddr, cfg.SQLAddr, advHost, cliflags.ListenSQLAddr)
54+
cfg.SQLAdvertiseAddr, cfg.SQLAddr, advHost, cliflags.ListenSQLAddr, cfg.Insecure)
5255
if err != nil {
5356
return invalidFlagErr(err, cliflags.SQLAdvertiseAddr)
5457
}
@@ -64,7 +67,7 @@ func (cfg *Config) ValidateAddrs(ctx context.Context) error {
6467
// Validate the HTTP advertise address. Use the provided advertise
6568
// addr as default.
6669
advHTTPHost, advHTTPPort, err := validateAdvertiseAddr(ctx,
67-
cfg.HTTPAdvertiseAddr, cfg.HTTPAddr, advHost, cliflags.ListenHTTPAddr)
70+
cfg.HTTPAdvertiseAddr, cfg.HTTPAddr, advHost, cliflags.ListenHTTPAddr, cfg.Insecure)
6871
if err != nil {
6972
return errors.Wrap(err, "cannot compute public HTTP address")
7073
}
@@ -86,7 +89,10 @@ func (cfg *Config) ValidateAddrs(ctx context.Context) error {
8689
// that if the "host" part is empty, it gets filled in with
8790
// the configured listen address if any, or the canonical host name.
8891
func validateAdvertiseAddr(
89-
ctx context.Context, advAddr, listenAddr, defaultHost string, listenFlag cliflags.FlagInfo,
92+
ctx context.Context,
93+
advAddr, listenAddr, defaultHost string,
94+
listenFlag cliflags.FlagInfo,
95+
insecure bool,
9096
) (string, string, error) {
9197
listenHost, listenPort, err := getListenAddr(listenAddr, defaultHost)
9298
if err != nil {
@@ -132,7 +138,12 @@ func validateAdvertiseAddr(
132138
// locally but not elsewhere) but at least it prevents typos.
133139
_, err = net.DefaultResolver.LookupIPAddr(ctx, advHost)
134140
if err != nil {
135-
return "", "", err
141+
// Host resolution failed. Don't error out when running on Mac with `--insecure`.
142+
if runtime.GOOS == "darwin" && insecure {
143+
log.Ops.Shoutf(ctx, severity.WARNING, "Unable to resolve `hostname` due to %v\n", err)
144+
} else {
145+
return "", "", err
146+
}
136147
}
137148
}
138149
}

pkg/roachprod/install/cockroach.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ func (c *SyncedCluster) generateStartArgs(
11691169
}
11701170

11711171
listenHost := ""
1172-
if c.IsLocal() && runtime.GOOS == "darwin " {
1172+
if c.IsLocal() && runtime.GOOS == "darwin" {
11731173
// This avoids annoying firewall prompts on Mac OS X.
11741174
listenHost = "127.0.0.1"
11751175
}
@@ -1201,17 +1201,20 @@ func (c *SyncedCluster) generateStartArgs(
12011201
}
12021202
args = append(args, fmt.Sprintf("--http-addr=%s:%d", listenHost, desc.Port))
12031203

1204+
advertiseHost := ""
12041205
if !c.IsLocal() {
1205-
advertiseHost := ""
12061206
if c.shouldAdvertisePublicIP() {
12071207
advertiseHost = c.Host(node)
12081208
} else {
12091209
advertiseHost = c.VMs[node-1].PrivateIP
12101210
}
1211-
args = append(args,
1212-
fmt.Sprintf("--advertise-addr=%s:%d", advertiseHost, sqlPort),
1213-
)
1211+
} else {
1212+
// N.B. in local mode, fallback to listenHost; per above, it defaults to 127.0.0.1 on macOS.
1213+
advertiseHost = listenHost
12141214
}
1215+
args = append(args,
1216+
fmt.Sprintf("--advertise-addr=%s:%d", advertiseHost, sqlPort),
1217+
)
12151218

12161219
// --join flags are unsupported/unnecessary in `cockroach start-single-node`.
12171220
if startOpts.Target == StartDefault && !c.useStartSingleNode() {

0 commit comments

Comments
 (0)