Merge pull request #61210 from hzxuzhonghu/etcd-random-check

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

check etcd servers by a random order

**What this PR does / why we need it**:

Every time a health check is called on the APIServer via the /healthz endpoint, an etcd healthcheck is performed. Here makes servers check with a random order.

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #61180

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2018-04-13 12:12:47 -07:00 committed by GitHub
commit a0a742c38b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,7 @@ package preflight
import (
"fmt"
"math/rand"
"net"
"net/url"
"time"
@ -25,12 +26,6 @@ import (
const connectionTimeout = 1 * time.Second
type connection interface {
serverReachable(address string) bool
parseServerList(serverList []string) error
CheckEtcdServers() (bool, error)
}
// EtcdConnection holds the Etcd server list
type EtcdConnection struct {
ServerList []string
@ -59,9 +54,11 @@ func parseServerURI(serverURI string) (*url.URL, error) {
// CheckEtcdServers will attempt to reach all etcd servers once. If any
// can be reached, return true.
func (con EtcdConnection) CheckEtcdServers() (done bool, err error) {
// Attempt to reach every Etcd server in order
for _, serverURI := range con.ServerList {
host, err := parseServerURI(serverURI)
// Attempt to reach every Etcd server randomly.
serverNumber := len(con.ServerList)
serverPerms := rand.Perm(serverNumber)
for _, index := range serverPerms {
host, err := parseServerURI(con.ServerList[index])
if err != nil {
return false, err
}