From 29087066d0f6bc0e06d466e8c3b6e5f055f0f4e7 Mon Sep 17 00:00:00 2001 From: Steve Kriss Date: Tue, 24 Jan 2017 15:56:53 -0800 Subject: [PATCH] fixed bug #36988 -- kubeadm crashes when using multiple API endpoints --- cmd/kubeadm/app/node/bootstrap.go | 3 +- cmd/kubeadm/app/node/bootstrap_test.go | 92 ++++++++++++++++---------- 2 files changed, 60 insertions(+), 35 deletions(-) diff --git a/cmd/kubeadm/app/node/bootstrap.go b/cmd/kubeadm/app/node/bootstrap.go index 9207e4d4a70..94a315b3687 100644 --- a/cmd/kubeadm/app/node/bootstrap.go +++ b/cmd/kubeadm/app/node/bootstrap.go @@ -80,9 +80,10 @@ func EstablishMasterConnection(c *kubeadmapi.TokenDiscovery, clusterInfo *kubead return } fmt.Printf("[bootstrap] Successfully established connection with endpoint %q\n", apiEndpoint) + // connection established, stop all wait threads - close(stopChan) once.Do(func() { + close(stopChan) clientConfig = ac.clientConfig }) }, retryTimeout*time.Second, stopChan) diff --git a/cmd/kubeadm/app/node/bootstrap_test.go b/cmd/kubeadm/app/node/bootstrap_test.go index e206c376a94..1fe6ba1fc66 100644 --- a/cmd/kubeadm/app/node/bootstrap_test.go +++ b/cmd/kubeadm/app/node/bootstrap_test.go @@ -31,6 +31,63 @@ import ( ) func TestEstablishMasterConnection(t *testing.T) { + srv := stubServer(t) + defer srv.Close() + + tests := []struct { + c string + e string + expect bool + }{ + { + c: "", + e: "", + expect: false, + }, + { + c: "", + e: srv.URL, + expect: true, + }, + { + c: "foo", + e: srv.URL, + expect: true, + }, + } + for _, rt := range tests { + s := &kubeadmapi.TokenDiscovery{} + c := &kubeadmapi.ClusterInfo{Endpoints: []string{rt.e}, CertificateAuthorities: []string{rt.c}} + _, actual := EstablishMasterConnection(s, c) + if (actual == nil) != rt.expect { + t.Errorf( + "failed EstablishMasterConnection:\n\texpected: %t\n\t actual: %t", + rt.expect, + (actual == nil), + ) + } + } +} + +func TestEstablishMasterConnectionWithMultipleEndpoints(t *testing.T) { + // ref. https://github.com/kubernetes/kubernetes/issues/36988 + + srv := stubServer(t) + defer srv.Close() + + s := &kubeadmapi.TokenDiscovery{} + c := &kubeadmapi.ClusterInfo{Endpoints: []string{srv.URL, srv.URL}, CertificateAuthorities: []string{"foo"}} + + defer func() { + if r := recover(); r != nil { + t.Errorf("failed EstablishMasterConnectionWithMultipleEndpoints; got a panic.") + } + }() + + EstablishMasterConnection(s, c) +} + +func stubServer(t *testing.T) *httptest.Server { expect := version.Info{ Major: "foo", Minor: "bar", @@ -83,41 +140,8 @@ func TestEstablishMasterConnection(t *testing.T) { w.Write(output) } })) - defer srv.Close() - tests := []struct { - c string - e string - expect bool - }{ - { - c: "", - e: "", - expect: false, - }, - { - c: "", - e: srv.URL, - expect: true, - }, - { - c: "foo", - e: srv.URL, - expect: true, - }, - } - for _, rt := range tests { - s := &kubeadmapi.TokenDiscovery{} - c := &kubeadmapi.ClusterInfo{Endpoints: []string{rt.e}, CertificateAuthorities: []string{rt.c}} - _, actual := EstablishMasterConnection(s, c) - if (actual == nil) != rt.expect { - t.Errorf( - "failed EstablishMasterConnection:\n\texpected: %t\n\t actual: %t", - rt.expect, - (actual == nil), - ) - } - } + return srv } func TestCreateClients(t *testing.T) {