Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit ca27c9e

Browse files
authored
Merge pull request #540 from xiang90/out
etcdutil: factor out wait ready
2 parents d21ee80 + 9441b66 commit ca27c9e

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

pkg/util/etcdutil/migrate.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"io/ioutil"
9-
"net/http"
109
"time"
1110

1211
"github.com/kubernetes-incubator/bootkube/pkg/asset"
@@ -102,34 +101,22 @@ func waitEtcdTPRReady(restClient restclient.Interface, ns string) error {
102101

103102
func createBootstrapEtcdService(kubecli kubernetes.Interface, svcPath string) error {
104103
// Create the service.
105-
svc, err := ioutil.ReadFile(svcPath)
104+
svcb, err := ioutil.ReadFile(svcPath)
106105
if err != nil {
107106
return err
108107
}
109-
if err := kubecli.CoreV1().RESTClient().Post().RequestURI(fmt.Sprintf("/api/v1/namespaces/%s/services", api.NamespaceSystem)).SetHeader("Content-Type", "application/json").Body(svc).Do().Error(); err != nil {
108+
if err := kubecli.CoreV1().RESTClient().Post().RequestURI(fmt.Sprintf("/api/v1/namespaces/%s/services", api.NamespaceSystem)).SetHeader("Content-Type", "application/json").Body(svcb).Do().Error(); err != nil {
109+
return err
110+
}
111+
112+
svc, err := kubecli.CoreV1().Services(api.NamespaceSystem).Get(bootstrapEtcdServiceName, v1.GetOptions{})
113+
if err != nil {
114+
glog.Errorf("failed to get bootstrap etcd service: %v", err)
110115
return err
111116
}
112117

113118
// Wait for the service to be reachable (sometimes this takes a little while).
114-
if err := wait.Poll(pollInterval, pollTimeout, func() (bool, error) {
115-
svc, err := kubecli.CoreV1().Services(api.NamespaceSystem).Get(bootstrapEtcdServiceName, v1.GetOptions{})
116-
if err != nil {
117-
glog.Errorf("failed to get bootstrap etcd service: %v", err)
118-
return false, nil
119-
}
120-
resp, err := http.Get(fmt.Sprintf("http://%s:12379/version", svc.Spec.ClusterIP))
121-
if err != nil {
122-
glog.Infof("could not read bootstrap etcd version: %v", err)
123-
return false, nil
124-
}
125-
defer resp.Body.Close()
126-
body, err := ioutil.ReadAll(resp.Body)
127-
if len(body) == 0 || err != nil {
128-
glog.Infof("could not read boot-etcd version: %v", err)
129-
return false, nil
130-
}
131-
return true, nil
132-
}); err != nil {
119+
if err := WaitClusterReady(svc.Spec.ClusterIP + ":12379"); err != nil {
133120
return fmt.Errorf("timed out waiting for bootstrap etcd service: %s", err)
134121
}
135122
return nil

pkg/util/etcdutil/util.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package etcdutil
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"net/http"
7+
8+
"github.com/golang/glog"
9+
"k8s.io/apimachinery/pkg/util/wait"
10+
)
11+
12+
// WaitClusterReady waits the etcd server ready to serve client requests.
13+
func WaitClusterReady(endpoint string) error {
14+
err := wait.Poll(pollInterval, pollTimeout, func() (bool, error) {
15+
resp, err := http.Get(fmt.Sprintf("http://%s/version", endpoint))
16+
if err != nil {
17+
glog.Infof("could not read from etcd: %v", err)
18+
return false, nil
19+
}
20+
defer resp.Body.Close()
21+
body, err := ioutil.ReadAll(resp.Body)
22+
if len(body) == 0 || err != nil {
23+
glog.Infof("could not read from etcd: %v", err)
24+
return false, nil
25+
}
26+
return true, nil
27+
})
28+
29+
return err
30+
}

0 commit comments

Comments
 (0)