Skip to content

Commit

Permalink
Merge pull request #73114 from neolit123/automated-cherry-pick-of-#72…
Browse files Browse the repository at this point in the history
…984-origin-release-1.13

Automated cherry pick of #72984: kubeadm: wait for the etcd cluster to be available when
  • Loading branch information
k8s-ci-robot committed Jan 21, 2019
2 parents 328d336 + 485d0cb commit a0ad376
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
22 changes: 16 additions & 6 deletions cmd/kubeadm/app/phases/etcd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"path/filepath"
"strings"
"time"

"github.com/pkg/errors"
"k8s.io/klog"
Expand All @@ -35,8 +36,10 @@ import (
)

const (
etcdVolumeName = "etcd-data"
certsVolumeName = "etcd-certs"
etcdVolumeName = "etcd-data"
certsVolumeName = "etcd-certs"
etcdHealthyCheckInterval = 5 * time.Second
etcdHealthyCheckRetries = 8
)

// CreateLocalEtcdStaticPodManifestFile will write local etcd static pod manifest file.
Expand All @@ -54,13 +57,13 @@ func CreateLocalEtcdStaticPodManifestFile(manifestDir string, cfg *kubeadmapi.In
return err
}

klog.V(1).Infof("[etcd] wrote Static Pod manifest for a local etcd instance to %q\n", kubeadmconstants.GetStaticPodFilepath(kubeadmconstants.Etcd, manifestDir))
klog.V(1).Infof("[etcd] wrote Static Pod manifest for a local etcd member to %q\n", kubeadmconstants.GetStaticPodFilepath(kubeadmconstants.Etcd, manifestDir))
return nil
}

// CheckLocalEtcdClusterStatus verifies health state of local/stacked etcd cluster before installing a new etcd member
func CheckLocalEtcdClusterStatus(client clientset.Interface, cfg *kubeadmapi.InitConfiguration) error {
fmt.Println("[etcd] Checking Etcd cluster health")
fmt.Println("[etcd] Checking etcd cluster health")

// creates an etcd client that connects to all the local/stacked etcd members
klog.V(1).Info("creating etcd client that connects to etcd pods")
Expand Down Expand Up @@ -108,7 +111,14 @@ func CreateStackedEtcdStaticPodManifestFile(client clientset.Interface, manifest
return err
}

fmt.Printf("[etcd] Wrote Static Pod manifest for a local etcd instance to %q\n", kubeadmconstants.GetStaticPodFilepath(kubeadmconstants.Etcd, manifestDir))
fmt.Printf("[etcd] Wrote Static Pod manifest for a local etcd member to %q\n", kubeadmconstants.GetStaticPodFilepath(kubeadmconstants.Etcd, manifestDir))

fmt.Printf("[etcd] Waiting for the new etcd member to join the cluster. This can take up to %v\n", etcdHealthyCheckInterval*etcdHealthyCheckRetries)
noDelay := 0 * time.Second
if _, err := etcdClient.WaitForClusterAvailable(noDelay, etcdHealthyCheckRetries, etcdHealthyCheckInterval); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -160,7 +170,7 @@ func getEtcdCommand(cfg *kubeadmapi.InitConfiguration, initialCluster []etcdutil
if len(initialCluster) == 0 {
defaultArguments["initial-cluster"] = fmt.Sprintf("%s=%s", cfg.GetNodeName(), etcdutil.GetPeerURL(cfg))
} else {
// NB. the joining etcd instance should be part of the initialCluster list
// NB. the joining etcd member should be part of the initialCluster list
endpoints := []string{}
for _, member := range initialCluster {
endpoints = append(endpoints, fmt.Sprintf("%s=%s", member.Name, member.PeerURL))
Expand Down
18 changes: 15 additions & 3 deletions cmd/kubeadm/app/util/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/tls"
"fmt"
"net"
"net/url"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -115,7 +116,7 @@ func New(endpoints []string, ca, cert, key string) (*Client, error) {
return &client, nil
}

// NewFromCluster creates an etcd client for the the etcd endpoints defined in the ClusterStatus value stored in
// NewFromCluster creates an etcd client for the etcd endpoints defined in the ClusterStatus value stored in
// the kubeadm-config ConfigMap in kube-system namespace.
// Once created, the client synchronizes client's endpoints with the known endpoints from the etcd membership API (reality check).
func NewFromCluster(client clientset.Interface, certificatesDir string) (*Client, error) {
Expand Down Expand Up @@ -233,7 +234,15 @@ type Member struct {
}

// AddMember notifies an existing etcd cluster that a new member is joining
func (c Client) AddMember(name string, peerAddrs string) ([]Member, error) {
func (c *Client) AddMember(name string, peerAddrs string) ([]Member, error) {
// Parse the peer address, required to add the client URL later to the list
// of endpoints for this client. Parsing as a first operation to make sure that
// if this fails no member addition is performed on the etcd cluster.
parsedPeerAddrs, err := url.Parse(peerAddrs)
if err != nil {
return nil, errors.Wrapf(err, "error parsing peer address %s", peerAddrs)
}

cli, err := clientv3.New(clientv3.Config{
Endpoints: c.Endpoints,
DialTimeout: 20 * time.Second,
Expand Down Expand Up @@ -263,6 +272,9 @@ func (c Client) AddMember(name string, peerAddrs string) ([]Member, error) {
}
}

// Add the new member client address to the list of endpoints
c.Endpoints = append(c.Endpoints, GetClientURLByIP(parsedPeerAddrs.Hostname()))

return ret, nil
}

Expand Down Expand Up @@ -344,7 +356,7 @@ func (c Client) WaitForClusterAvailable(delay time.Duration, retries int, retryI
fmt.Printf("[util/etcd] Waiting %v until next retry\n", retryInterval)
time.Sleep(retryInterval)
}
fmt.Printf("[util/etcd] Attempting to see if all cluster endpoints are available %d/%d\n", i+1, retries)
klog.V(2).Infof("attempting to see if all cluster endpoints (%s) are available %d/%d", c.Endpoints, i+1, retries)
resp, err := c.ClusterAvailable()
if err != nil {
switch err {
Expand Down

0 comments on commit a0ad376

Please sign in to comment.