@@ -2,10 +2,13 @@ package etcdutil
22
33import (
44 "context"
5+ "crypto/tls"
56 "encoding/json"
67 "errors"
78 "fmt"
89 "io/ioutil"
10+ "os"
11+ "path/filepath"
912 "time"
1013
1114 "github.com/kubernetes-incubator/bootkube/pkg/asset"
3235 pollTimeout = 300 * time .Second
3336)
3437
35- func Migrate (kubeConfig clientcmd.ClientConfig , svcPath , tprPath string ) error {
38+ func Migrate (kubeConfig clientcmd.ClientConfig , assetDir , svcPath , tprPath string ) error {
39+ useEtcdTLS , err := detectEtcdTLS (assetDir )
40+ if err != nil {
41+ return err
42+ }
43+ var etcdTLS * tls.Config
44+ if useEtcdTLS {
45+ etcdTLS , err = makeTLSConfig (assetDir )
46+ if err != nil {
47+ return err
48+ }
49+ }
50+
3651 config , err := kubeConfig .ClientConfig ()
3752 if err != nil {
3853 return fmt .Errorf ("failed to create kube client config: %v" , err )
@@ -49,7 +64,7 @@ func Migrate(kubeConfig clientcmd.ClientConfig, svcPath, tprPath string) error {
4964 }
5065 glog .Infof ("created etcd cluster TPR" )
5166
52- if err := createBootstrapEtcdService (kubecli , svcPath ); err != nil {
67+ if err := createBootstrapEtcdService (kubecli , etcdTLS , svcPath ); err != nil {
5368 return fmt .Errorf ("failed to create bootstrap-etcd-service: %v" , err )
5469 }
5570 defer cleanupBootstrapEtcdService (kubecli )
@@ -70,7 +85,7 @@ func Migrate(kubeConfig clientcmd.ClientConfig, svcPath, tprPath string) error {
7085 }
7186 glog .Info ("etcd cluster for migration is now running" )
7287
73- if err := waitBootEtcdRemoved (etcdServiceIP ); err != nil {
88+ if err := waitBootEtcdRemoved (etcdServiceIP , etcdTLS ); err != nil {
7489 return fmt .Errorf ("failed to wait for boot-etcd to be removed: %v" , err )
7590 }
7691 glog .Info ("removed boot-etcd from the etcd cluster" )
@@ -99,7 +114,7 @@ func waitEtcdTPRReady(restClient restclient.Interface, ns string) error {
99114 return nil
100115}
101116
102- func createBootstrapEtcdService (kubecli kubernetes.Interface , svcPath string ) error {
117+ func createBootstrapEtcdService (kubecli kubernetes.Interface , etcdTLS * tls. Config , svcPath string ) error {
103118 // Create the service.
104119 svcb , err := ioutil .ReadFile (svcPath )
105120 if err != nil {
@@ -115,8 +130,12 @@ func createBootstrapEtcdService(kubecli kubernetes.Interface, svcPath string) er
115130 return err
116131 }
117132
133+ scheme := "http://"
134+ if etcdTLS != nil {
135+ scheme = "https://"
136+ }
118137 // Wait for the service to be reachable (sometimes this takes a little while).
119- if err := WaitClusterReady ("http://" + svc .Spec .ClusterIP + ":12379" ); err != nil {
138+ if err := WaitClusterReady (scheme + svc .Spec .ClusterIP + ":12379" , etcdTLS ); err != nil {
120139 return fmt .Errorf ("timed out waiting for bootstrap etcd service: %s" , err )
121140 }
122141 return nil
@@ -165,12 +184,18 @@ func getServiceIP(kubecli kubernetes.Interface, ns, svcName string) (string, err
165184 return svc .Spec .ClusterIP , nil
166185}
167186
168- func waitBootEtcdRemoved (etcdServiceIP string ) error {
187+ func waitBootEtcdRemoved (etcdServiceIP string , etcdTLS * tls.Config ) error {
188+ scheme := "http"
189+ if etcdTLS != nil {
190+ scheme = "https"
191+ }
192+ cfg := clientv3.Config {
193+ Endpoints : []string {fmt .Sprintf ("%s://%s:2379" , scheme , etcdServiceIP )},
194+ TLS : etcdTLS ,
195+ DialTimeout : 5 * time .Second ,
196+ }
197+
169198 err := wait .Poll (pollInterval , pollTimeout , func () (bool , error ) {
170- cfg := clientv3.Config {
171- Endpoints : []string {fmt .Sprintf ("http://%s:2379" , etcdServiceIP )},
172- DialTimeout : 5 * time .Second ,
173- }
174199 etcdcli , err := clientv3 .New (cfg )
175200 if err != nil {
176201 glog .Errorf ("failed to create etcd client, will retry: %v" , err )
@@ -210,3 +235,15 @@ func cleanupBootstrapEtcdService(kubecli kubernetes.Interface) {
210235 glog .Errorf ("timed out removing bootstrap-etcd-service: %v" , err )
211236 }
212237}
238+
239+ func detectEtcdTLS (assetDir string ) (bool , error ) {
240+ etcdCAAssetPath := filepath .Join (assetDir , asset .AssetPathEtcdCA )
241+ _ , err := os .Stat (etcdCAAssetPath )
242+ if err == nil {
243+ return true , nil
244+ }
245+ if os .IsNotExist (err ) {
246+ return false , nil
247+ }
248+ return false , err
249+ }
0 commit comments