@@ -17,15 +17,20 @@ limitations under the License.
17
17
package bootstrap
18
18
19
19
import (
20
+ "context"
21
+ "errors"
20
22
"fmt"
21
23
"os"
22
24
"path/filepath"
23
25
"time"
24
26
25
27
"github.com/golang/glog"
26
28
29
+ "k8s.io/apimachinery/pkg/runtime/serializer"
27
30
"k8s.io/apimachinery/pkg/types"
28
31
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
32
+ "k8s.io/apimachinery/pkg/util/wait"
33
+ "k8s.io/client-go/kubernetes/scheme"
29
34
certificates "k8s.io/client-go/kubernetes/typed/certificates/v1beta1"
30
35
restclient "k8s.io/client-go/rest"
31
36
"k8s.io/client-go/tools/clientcmd"
@@ -59,6 +64,7 @@ func LoadClientCert(kubeconfigPath string, bootstrapPath string, certDir string,
59
64
if err != nil {
60
65
return fmt .Errorf ("unable to load bootstrap kubeconfig: %v" , err )
61
66
}
67
+
62
68
bootstrapClient , err := certificates .NewForConfig (bootstrapClientConfig )
63
69
if err != nil {
64
70
return fmt .Errorf ("unable to create certificates signing request client: %v" , err )
@@ -92,6 +98,10 @@ func LoadClientCert(kubeconfigPath string, bootstrapPath string, certDir string,
92
98
}
93
99
}
94
100
101
+ if err := waitForServer (* bootstrapClientConfig , 1 * time .Minute ); err != nil {
102
+ glog .Warningf ("Error waiting for apiserver to come up: %v" , err )
103
+ }
104
+
95
105
certData , err := csr .RequestNodeCertificate (bootstrapClient .CertificateSigningRequests (), keyData , nodeName )
96
106
if err != nil {
97
107
return err
@@ -207,3 +217,30 @@ func verifyKeyData(data []byte) bool {
207
217
_ , err := certutil .ParsePrivateKeyPEM (data )
208
218
return err == nil
209
219
}
220
+
221
+ func waitForServer (cfg restclient.Config , deadline time.Duration ) error {
222
+ cfg .NegotiatedSerializer = serializer.DirectCodecFactory {CodecFactory : scheme .Codecs }
223
+ cfg .Timeout = 1 * time .Second
224
+ cli , err := restclient .UnversionedRESTClientFor (& cfg )
225
+ if err != nil {
226
+ return fmt .Errorf ("couldn't create client: %v" , err )
227
+ }
228
+
229
+ ctx , cancel := context .WithTimeout (context .TODO (), deadline )
230
+ defer cancel ()
231
+
232
+ var connected bool
233
+ wait .JitterUntil (func () {
234
+ if _ , err := cli .Get ().AbsPath ("/healthz" ).Do ().Raw (); err != nil {
235
+ glog .Infof ("Failed to connect to apiserver: %v" , err )
236
+ return
237
+ }
238
+ cancel ()
239
+ connected = true
240
+ }, 2 * time .Second , 0.2 , true , ctx .Done ())
241
+
242
+ if ! connected {
243
+ return errors .New ("timed out waiting to connect to apiserver" )
244
+ }
245
+ return nil
246
+ }
0 commit comments