From b92b39c88956c2a8a6deb06794df969da36153f6 Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Thu, 6 Jul 2017 14:19:06 -0700 Subject: [PATCH] embed: connect json gateway with user-provided listen address net.Listener says its address is [::] when given 0.0.0.0, breaking hosts that have ipv6 disabled. Fixes #8151 Fixes #7961 --- embed/etcd.go | 3 +++ embed/serve.go | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/embed/etcd.go b/embed/etcd.go index f77dee0e0656..a3e93412ba11 100644 --- a/embed/etcd.go +++ b/embed/etcd.go @@ -326,6 +326,9 @@ func startClientListeners(cfg *Config) (sctxs map[string]*serveCtx, err error) { if sctx.l, err = net.Listen(proto, addr); err != nil { return nil, err } + // net.Listener will rewrite ipv4 0.0.0.0 to ipv6 [::], breaking + // hosts that disable ipv6. So, use the address given by the user. + sctx.addr = addr if fdLimit, fderr := runtimeutil.FDLimit(); fderr == nil { if fdLimit <= reservedInternalFDNum { diff --git a/embed/serve.go b/embed/serve.go index e43611f77a37..c7429e2c4562 100644 --- a/embed/serve.go +++ b/embed/serve.go @@ -44,6 +44,7 @@ import ( type serveCtx struct { l net.Listener + addr string secure bool insecure bool @@ -164,7 +165,10 @@ type registerHandlerFunc func(context.Context, *gw.ServeMux, string, []grpc.Dial func (sctx *serveCtx) registerGateway(opts []grpc.DialOption) (*gw.ServeMux, error) { ctx := sctx.ctx - addr := sctx.l.Addr().String() + conn, err := grpc.DialContext(ctx, sctx.addr, opts...) + if err != nil { + return nil, err + } gwmux := gw.NewServeMux() handlers := []registerHandlerFunc{ @@ -178,7 +182,7 @@ func (sctx *serveCtx) registerGateway(opts []grpc.DialOption) (*gw.ServeMux, err v3electiongw.RegisterElectionHandlerFromEndpoint, } for _, h := range handlers { - if err := h(ctx, gwmux, addr, opts); err != nil { + if err := h(ctx, gwmux, sctx.addr, opts); err != nil { return nil, err } }