Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Envtest should try to allocate a port for etcd listenPeerURL #1612

Merged
merged 1 commit into from
Aug 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pkg/internal/testing/controlplane/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
Expand Down Expand Up @@ -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()},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What difference does this make? Shouldn't etcd get a free port from the kernel (and then our allocation not consider that one)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are multiple parallel packages running at the same time, process A could internally allocate a port (on file) which is then used for either etcd listen or api-server, while process B (etcd) is already listening on it.

There is always a race condition between the code that allocates the port and the actual program startup, because the port gets closed right after we have a file lock, another program not knowing about the file allocation can snatch it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks for the explanation.

/lgtm
/approve

"data-dir": {e.DataDir},
}
if e.URL != nil {
Expand Down