-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
233 lines (200 loc) · 5.82 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
kutil "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
const (
ns = "default"
apiVersion = "v1beta3"
etcdClientPort = 2379
etcdPeerPort = 2380
)
func main() {
fs := flag.NewFlagSet("etcd-deploy", flag.ExitOnError)
k8sEndpoint := fs.String("kubernetes", "http://127.0.0.1:8080", "Kubernetes API endpoint")
clusterID := fs.String("cluster", "", "identifier of cluster")
clusterSize := fs.Int("size", 3, "expected size of cluster")
if err := fs.Parse(os.Args[1:]); err != nil {
log.Fatalf("Failed parsing flags: %v", err)
}
if *clusterID == "" {
log.Fatalf("Flag required: --cluster")
}
kfg := &kclient.Config{
Host: *k8sEndpoint,
Version: apiVersion,
}
kc, err := kclient.New(kfg)
if err != nil {
log.Fatalf("Failed initializing kubernetes client: %v", err)
}
d := &deployer{
kc: kc,
cID: *clusterID,
cSize: *clusterSize,
}
d.Reconcile()
log.Printf("All necessary services and pods exist")
}
type deployer struct {
kc *kclient.Client
cID string
cSize int
}
func (d *deployer) Reconcile() error {
d.ensureClientServiceExists()
svcs := d.ensurePeerServicesExist()
d.ensurePodsExist(svcs)
return nil
}
func (d *deployer) ensureClientServiceExists() {
manifest := buildClientServiceManifest(d.cID)
d.ensureServiceExists(manifest)
}
func (d *deployer) ensurePeerServicesExist() []kapi.Service {
ss := make([]kapi.Service, 0, d.cSize)
for i := 1; i <= d.cSize; i++ {
manifest := buildPeerServiceManifest(d.cID, strconv.Itoa(i))
ss = append(ss, d.ensureServiceExists(manifest))
}
return ss
}
func (d *deployer) ensureServiceExists(manifest kapi.Service) kapi.Service {
svc, err := d.kc.Services(ns).Get(manifest.ObjectMeta.Name)
if err == nil {
return *svc
}
if err := d.createService(manifest); err != nil {
log.Fatalf("Failed creating service: %v", err)
}
log.Printf("Created service %s", manifest.ObjectMeta.Name)
return d.waitForServiceIP(manifest.ObjectMeta.Name)
}
func (d *deployer) createService(svc kapi.Service) error {
_, err := d.kc.Services(ns).Create(&svc)
return err
}
func (d *deployer) waitForServiceIP(name string) kapi.Service {
var s *kapi.Service
var err error
for {
s, err = d.kc.Services(ns).Get(name)
if err != nil {
log.Printf("Failed getting service: %v", err)
time.Sleep(time.Second)
continue
} else if s.Spec.PortalIP == "" {
log.Printf("Waiting on service to get PortalIP")
time.Sleep(time.Second)
continue
}
break
}
return *s
}
func (d *deployer) ensurePodsExist(ss []kapi.Service) []kapi.Pod {
peers := make(map[string]string, len(ss))
for _, svc := range ss {
peers[nodeName(d.cID, svc.Spec.Selector["etcd-instance"])] = svc.Spec.PortalIP
}
pods := make([]kapi.Pod, len(ss))
for i, svc := range ss {
pods[i] = buildPodManifest(d.cID, svc.Spec.Selector["etcd-instance"], svc.Spec.PortalIP, peers)
_, err := d.kc.Pods(ns).Get(pods[i].ObjectMeta.Name)
if err == nil {
continue
}
if err := d.createPod(pods[i]); err != nil {
log.Fatalf("Failed creating pod: %v", err)
}
log.Printf("Created pod %s", pods[i].ObjectMeta.Name)
}
return pods
}
func (d *deployer) createPod(pod kapi.Pod) error {
_, err := d.kc.Pods(ns).Create(&pod)
return err
}
func nodeName(clusterID, instance string) string {
return fmt.Sprintf("etcd-clust-%s-peer-%s", clusterID, instance)
}
func buildPeerServiceManifest(clusterID, instance string) kapi.Service {
return kapi.Service{
TypeMeta: kapi.TypeMeta{
Kind: "Service",
APIVersion: apiVersion,
},
ObjectMeta: kapi.ObjectMeta{
Name: nodeName(clusterID, instance),
},
Spec: kapi.ServiceSpec{
Port: etcdPeerPort,
Selector: map[string]string{"etcd-cluster": clusterID, "etcd-instance": instance},
ContainerPort: kutil.NewIntOrStringFromString("peer"),
},
}
}
func buildClientServiceManifest(clusterID string) kapi.Service {
return kapi.Service{
TypeMeta: kapi.TypeMeta{
Kind: "Service",
APIVersion: apiVersion,
},
ObjectMeta: kapi.ObjectMeta{
Name: fmt.Sprintf("etcd-clust-%s-clients", clusterID),
},
Spec: kapi.ServiceSpec{
Port: etcdClientPort,
Selector: map[string]string{"etcd-cluster": clusterID},
ContainerPort: kutil.NewIntOrStringFromString("client"),
},
}
}
func buildPodManifest(clusterID, instance, advertiseIP string, peers map[string]string) kapi.Pod {
return kapi.Pod{
TypeMeta: kapi.TypeMeta{
Kind: "Pod",
APIVersion: apiVersion,
},
ObjectMeta: kapi.ObjectMeta{
Name: nodeName(clusterID, instance),
Labels: map[string]string{"etcd-cluster": clusterID, "etcd-instance": instance},
},
Spec: kapi.PodSpec{
Containers: []kapi.Container{
kapi.Container{
Name: "etcd",
Image: "quay.io/coreos/etcd:v2.0.5",
Command: []string{
fmt.Sprintf("--name=%s", nodeName(clusterID, instance)),
fmt.Sprintf("--listen-client-urls=http://0.0.0.0:%d", etcdClientPort),
fmt.Sprintf("--advertise-client-urls=http://%s:%d", advertiseIP, etcdClientPort),
fmt.Sprintf("--listen-peer-urls=http://0.0.0.0:%d", etcdPeerPort),
fmt.Sprintf("--initial-advertise-peer-urls=http://%s:%d", advertiseIP, etcdPeerPort),
fmt.Sprintf("--initial-cluster=%s", initialClusterPeers(peers)),
"--initial-cluster-state=new",
},
Ports: []kapi.Port{
kapi.Port{Name: "client", ContainerPort: etcdClientPort},
kapi.Port{Name: "peer", ContainerPort: etcdPeerPort},
},
},
},
},
}
}
func initialClusterPeers(peers map[string]string) string {
tmp := make([]string, 0, len(peers))
for name, ip := range peers {
tmp = append(tmp, fmt.Sprintf("%s=http://%s:%d", name, ip, etcdPeerPort))
}
return strings.Join(tmp, ",")
}