forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
106 lines (91 loc) · 3.28 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package etcdserver
import (
"net/url"
"strings"
"time"
"github.com/coreos/etcd/embed"
"github.com/coreos/etcd/pkg/osutil"
"github.com/coreos/etcd/pkg/types"
"github.com/coreos/go-semver/semver"
"github.com/golang/glog"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
)
const defaultName = "openshift.local"
// RunEtcd starts an etcd server and runs it forever
func RunEtcd(etcdServerConfig *configapi.EtcdConfig) {
cfg := embed.NewConfig()
cfg.Debug = true
cfg.Name = defaultName
cfg.Dir = etcdServerConfig.StorageDir
clientTLS := true
cfg.ClientTLSInfo.CAFile = etcdServerConfig.ServingInfo.ClientCA
cfg.ClientTLSInfo.CertFile = etcdServerConfig.ServingInfo.ServerCert.CertFile
cfg.ClientTLSInfo.KeyFile = etcdServerConfig.ServingInfo.ServerCert.KeyFile
cfg.ClientTLSInfo.ClientCertAuth = len(cfg.ClientTLSInfo.CAFile) > 0
u, err := types.NewURLs(addressToURLs(etcdServerConfig.ServingInfo.BindAddress, clientTLS))
if err != nil {
glog.Fatalf("Unable to build etcd peer URLs: %v", err)
}
cfg.LCUrls = []url.URL(u)
peerTLS := true
cfg.PeerTLSInfo.CAFile = etcdServerConfig.PeerServingInfo.ClientCA
cfg.PeerTLSInfo.CertFile = etcdServerConfig.PeerServingInfo.ServerCert.CertFile
cfg.PeerTLSInfo.KeyFile = etcdServerConfig.PeerServingInfo.ServerCert.KeyFile
cfg.PeerTLSInfo.ClientCertAuth = len(cfg.PeerTLSInfo.CAFile) > 0
u, err = types.NewURLs(addressToURLs(etcdServerConfig.PeerServingInfo.BindAddress, peerTLS))
if err != nil {
glog.Fatalf("Unable to build etcd peer URLs: %v", err)
}
cfg.LPUrls = []url.URL(u)
u, err = types.NewURLs(addressToURLs(etcdServerConfig.Address, clientTLS))
if err != nil {
glog.Fatalf("Unable to build etcd announce client URLs: %v", err)
}
cfg.ACUrls = []url.URL(u)
u, err = types.NewURLs(addressToURLs(etcdServerConfig.PeerAddress, peerTLS))
if err != nil {
glog.Fatalf("Unable to build etcd announce peer URLs: %v", err)
}
cfg.APUrls = []url.URL(u)
cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
osutil.HandleInterrupts()
e, err := embed.StartEtcd(cfg)
if err != nil {
glog.Fatalf("Unable to start etcd: %v", err)
}
ready := make(chan struct{})
go func() {
defer e.Close()
select {
case <-e.Server.ReadyNotify():
// embedded servers must negotiate to reach v3 mode, this ensures we loop until that happens
glog.V(4).Infof("Waiting for etcd to reach cluster version 3.0.0")
for min := semver.Must(semver.NewVersion("3.0.0")); e.Server.ClusterVersion() == nil || e.Server.ClusterVersion().LessThan(*min); {
time.Sleep(25 * time.Millisecond)
}
close(ready)
glog.Infof("Started etcd at %s", etcdServerConfig.Address)
case <-time.After(60 * time.Second):
glog.Warning("etcd took too long to start, stopped")
e.Server.Stop() // trigger a shutdown
}
glog.Fatalf("etcd has returned an error: %v", <-e.Err())
}()
<-ready
}
// addressToURLs turns a host:port comma delimited list into an array valid
// URL strings with the appropriate prefix for the TLS mode.
func addressToURLs(addr string, isTLS bool) []string {
addrs := strings.Split(addr, ",")
for i := range addrs {
if strings.HasPrefix(addrs[i], "unix://") || strings.HasPrefix(addrs[i], "unixs://") {
continue
}
if isTLS {
addrs[i] = "https://" + addrs[i]
} else {
addrs[i] = "http://" + addrs[i]
}
}
return addrs
}