From fb86ecbc5b32bd5708848834500328acfc9f9c60 Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Thu, 29 Jul 2021 10:48:58 -0700 Subject: [PATCH] :bug: Envtest should try to allocate a port for etcd listenPeerURL Signed-off-by: Vince Prignano --- pkg/internal/testing/controlplane/etcd.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/internal/testing/controlplane/etcd.go b/pkg/internal/testing/controlplane/etcd.go index 00d8254777..5d6174c77b 100644 --- a/pkg/internal/testing/controlplane/etcd.go +++ b/pkg/internal/testing/controlplane/etcd.go @@ -84,6 +84,10 @@ type Etcd struct { // args contains the structured arguments to use for running etcd. // Lazily initialized by .Configure(), Defaulted eventually with .defaultArgs() args *process.Arguments + + // listenPeerURL is the address the Etcd should listen on for peer connections. + // It's automatically generated and a random port is picked during execution. + listenPeerURL *url.URL } // Start starts the etcd, waits for it to come up, and returns an error, if one @@ -111,6 +115,7 @@ func (e *Etcd) setProcessState() error { return err } + // Set the listen url. if e.URL == nil { port, host, err := addr.Suggest("") if err != nil { @@ -122,6 +127,18 @@ func (e *Etcd) setProcessState() error { } } + // Set the listen peer URL. + { + port, host, err := addr.Suggest("") + if err != nil { + return err + } + e.listenPeerURL = &url.URL{ + Scheme: "http", + Host: net.JoinHostPort(host, strconv.Itoa(port)), + } + } + // can use /health as of etcd 3.3.0 e.processState.HealthCheck.URL = *e.URL e.processState.HealthCheck.Path = "/health" @@ -150,7 +167,7 @@ func (e *Etcd) Stop() error { func (e *Etcd) defaultArgs() map[string][]string { args := map[string][]string{ - "listen-peer-urls": {"http://localhost:0"}, + "listen-peer-urls": {e.listenPeerURL.String()}, "data-dir": {e.DataDir}, } if e.URL != nil {