forked from coreos/coreos-kubernetes
/
cluster.go
434 lines (370 loc) · 12 KB
/
cluster.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package cluster
import (
"bytes"
"errors"
"fmt"
"strings"
"text/tabwriter"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/coreos/coreos-kubernetes/multi-node/aws/pkg/config"
)
// VERSION set by build script
var VERSION = "UNKNOWN"
type Info struct {
Name string
ControllerIP string
}
func (c *Info) String() string {
buf := new(bytes.Buffer)
w := new(tabwriter.Writer)
w.Init(buf, 0, 8, 0, '\t', 0)
fmt.Fprintf(w, "Cluster Name:\t%s\n", c.Name)
fmt.Fprintf(w, "Controller IP:\t%s\n", c.ControllerIP)
w.Flush()
return buf.String()
}
func New(cfg *config.Cluster, awsDebug bool) *Cluster {
awsConfig := aws.NewConfig().
WithRegion(cfg.Region).
WithCredentialsChainVerboseErrors(true)
if awsDebug {
awsConfig = awsConfig.WithLogLevel(aws.LogDebug)
}
return &Cluster{
Cluster: *cfg,
session: session.New(awsConfig),
}
}
type Cluster struct {
config.Cluster
session *session.Session
}
func (c *Cluster) ValidateStack(stackBody string) (string, error) {
validateInput := cloudformation.ValidateTemplateInput{
TemplateBody: &stackBody,
}
cfSvc := cloudformation.New(c.session)
validationReport, err := cfSvc.ValidateTemplate(&validateInput)
if err != nil {
return "", fmt.Errorf("invalid cloudformation stack: %v", err)
}
return validationReport.String(), nil
}
type ec2Service interface {
DescribeVpcs(*ec2.DescribeVpcsInput) (*ec2.DescribeVpcsOutput, error)
DescribeSubnets(*ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error)
DescribeKeyPairs(*ec2.DescribeKeyPairsInput) (*ec2.DescribeKeyPairsOutput, error)
}
func (c *Cluster) validateExistingVPCState(ec2Svc ec2Service) error {
if c.VPCID == "" {
//The VPC will be created. No existing state to validate
return nil
}
describeVpcsInput := ec2.DescribeVpcsInput{
VpcIds: []*string{aws.String(c.VPCID)},
}
vpcOutput, err := ec2Svc.DescribeVpcs(&describeVpcsInput)
if err != nil {
return fmt.Errorf("error describing existing vpc: %v", err)
}
if len(vpcOutput.Vpcs) == 0 {
return fmt.Errorf("could not find vpc %s in region %s", c.VPCID, c.Region)
}
if len(vpcOutput.Vpcs) > 1 {
//Theoretically this should never happen. If it does, we probably want to know.
return fmt.Errorf("found more than one vpc with id %s. this is NOT NORMAL", c.VPCID)
}
existingVPC := vpcOutput.Vpcs[0]
if *existingVPC.CidrBlock != c.VPCCIDR {
//If this is the case, our network config validation cannot be trusted and we must abort
return fmt.Errorf(
"configured vpcCidr (%s) does not match actual existing vpc cidr (%s)",
c.VPCCIDR,
*existingVPC.CidrBlock,
)
}
describeSubnetsInput := ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{existingVPC.VpcId},
},
},
}
subnetOutput, err := ec2Svc.DescribeSubnets(&describeSubnetsInput)
if err != nil {
return fmt.Errorf("error describing subnets for vpc: %v", err)
}
subnetCIDRS := make([]string, len(subnetOutput.Subnets))
for i, existingSubnet := range subnetOutput.Subnets {
subnetCIDRS[i] = *existingSubnet.CidrBlock
}
if err := c.ValidateExistingVPC(*existingVPC.CidrBlock, subnetCIDRS); err != nil {
return fmt.Errorf("error validating existing VPC: %v", err)
}
return nil
}
func (c *Cluster) Create(stackBody string) error {
r53Svc := route53.New(c.session)
if err := c.validateDNSConfig(r53Svc); err != nil {
return err
}
ec2Svc := ec2.New(c.session)
if err := c.validateKeyPair(ec2Svc); err != nil {
return err
}
if err := c.validateExistingVPCState(ec2Svc); err != nil {
return err
}
cfSvc := cloudformation.New(c.session)
resp, err := c.createStack(cfSvc, stackBody)
if err != nil {
return err
}
req := cloudformation.DescribeStacksInput{
StackName: resp.StackId,
}
for {
resp, err := cfSvc.DescribeStacks(&req)
if err != nil {
return err
}
if len(resp.Stacks) == 0 {
return fmt.Errorf("stack not found")
}
statusString := aws.StringValue(resp.Stacks[0].StackStatus)
switch statusString {
case cloudformation.ResourceStatusCreateComplete:
return nil
case cloudformation.ResourceStatusCreateFailed:
errMsg := fmt.Sprintf(
"Stack creation failed: %s : %s",
statusString,
aws.StringValue(resp.Stacks[0].StackStatusReason),
)
errMsg = errMsg + "\n\nPrinting the most recent failed stack events:\n"
stackEventsOutput, err := cfSvc.DescribeStackEvents(
&cloudformation.DescribeStackEventsInput{
StackName: resp.Stacks[0].StackName,
})
if err != nil {
return err
}
errMsg = errMsg + strings.Join(stackEventErrMsgs(stackEventsOutput.StackEvents), "\n")
return errors.New(errMsg)
case cloudformation.ResourceStatusCreateInProgress:
time.Sleep(3 * time.Second)
continue
default:
return fmt.Errorf("unexpected stack status: %s", statusString)
}
}
}
type cloudformationService interface {
CreateStack(*cloudformation.CreateStackInput) (*cloudformation.CreateStackOutput, error)
}
func (c *Cluster) createStack(cfSvc cloudformationService, stackBody string) (*cloudformation.CreateStackOutput, error) {
var tags []*cloudformation.Tag
for k, v := range c.StackTags {
key := k
value := v
tags = append(tags, &cloudformation.Tag{Key: &key, Value: &value})
}
creq := &cloudformation.CreateStackInput{
StackName: aws.String(c.ClusterName),
OnFailure: aws.String(cloudformation.OnFailureDoNothing),
Capabilities: []*string{aws.String(cloudformation.CapabilityCapabilityIam)},
TemplateBody: &stackBody,
Tags: tags,
}
return cfSvc.CreateStack(creq)
}
func (c *Cluster) Update(stackBody string) (string, error) {
cfSvc := cloudformation.New(c.session)
input := &cloudformation.UpdateStackInput{
Capabilities: []*string{aws.String(cloudformation.CapabilityCapabilityIam)},
StackName: aws.String(c.ClusterName),
TemplateBody: &stackBody,
}
updateOutput, err := cfSvc.UpdateStack(input)
if err != nil {
return "", fmt.Errorf("error updating cloudformation stack: %v", err)
}
req := cloudformation.DescribeStacksInput{
StackName: updateOutput.StackId,
}
for {
resp, err := cfSvc.DescribeStacks(&req)
if err != nil {
return "", err
}
if len(resp.Stacks) == 0 {
return "", fmt.Errorf("stack not found")
}
statusString := aws.StringValue(resp.Stacks[0].StackStatus)
switch statusString {
case cloudformation.ResourceStatusUpdateComplete:
return updateOutput.String(), nil
case cloudformation.ResourceStatusUpdateFailed, cloudformation.StackStatusUpdateRollbackComplete, cloudformation.StackStatusUpdateRollbackFailed:
errMsg := fmt.Sprintf("Stack status: %s : %s", statusString, aws.StringValue(resp.Stacks[0].StackStatusReason))
return "", errors.New(errMsg)
case cloudformation.ResourceStatusUpdateInProgress:
time.Sleep(3 * time.Second)
continue
default:
return "", fmt.Errorf("unexpected stack status: %s", statusString)
}
}
}
func (c *Cluster) Info() (*Info, error) {
cfSvc := cloudformation.New(c.session)
resp, err := cfSvc.DescribeStackResource(
&cloudformation.DescribeStackResourceInput{
LogicalResourceId: aws.String("EIPController"),
StackName: aws.String(c.ClusterName),
},
)
if err != nil {
errmsg := "unable to get public IP of controller instance:\n" + err.Error()
return nil, fmt.Errorf(errmsg)
}
var info Info
info.ControllerIP = *resp.StackResourceDetail.PhysicalResourceId
info.Name = c.ClusterName
return &info, nil
}
func (c *Cluster) Destroy() error {
cfSvc := cloudformation.New(c.session)
dreq := &cloudformation.DeleteStackInput{
StackName: aws.String(c.ClusterName),
}
_, err := cfSvc.DeleteStack(dreq)
return err
}
func (c *Cluster) validateKeyPair(ec2Svc ec2Service) error {
_, err := ec2Svc.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
KeyNames: []*string{aws.String(c.KeyName)},
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "InvalidKeyPair.NotFound" {
return fmt.Errorf("Key %s does not exist.", c.KeyName)
}
}
return err
}
return nil
}
type r53Service interface {
ListHostedZonesByName(*route53.ListHostedZonesByNameInput) (*route53.ListHostedZonesByNameOutput, error)
ListResourceRecordSets(*route53.ListResourceRecordSetsInput) (*route53.ListResourceRecordSetsOutput, error)
GetHostedZone(*route53.GetHostedZoneInput) (*route53.GetHostedZoneOutput, error)
}
func (c *Cluster) validateDNSConfig(r53 r53Service) error {
if !c.CreateRecordSet {
return nil
}
if c.HostedZoneID == "" {
//TODO(colhom): When HostedZone parameter is gone, this block can be removed
//Config will gaurantee that HostedZoneID is set from the get-go
listHostedZoneInput := route53.ListHostedZonesByNameInput{
DNSName: aws.String(c.HostedZone),
}
zonesResp, err := r53.ListHostedZonesByName(&listHostedZoneInput)
if err != nil {
return fmt.Errorf("Error validating HostedZone: %s", err)
}
zones := zonesResp.HostedZones
if len(zones) == 0 {
return fmt.Errorf("hosted zone %s does not exist", c.HostedZone)
}
var matchingZone *route53.HostedZone
for _, zone := range zones {
if aws.StringValue(zone.Name) == c.HostedZone {
if matchingZone != nil {
//This means we've found another match, and HostedZone is ambiguous
return fmt.Errorf("multiple hosted-zones found for DNS name \"%s\"", c.HostedZone)
}
matchingZone = zone
} else {
/* Weird API semantics: if we see a zone which doesn't match the name
we've exhausted all zones which match the name
http://docs.aws.amazon.com/cli/latest/reference/route53/list-hosted-zones-by-name.html#options */
break
}
}
if matchingZone == nil {
return fmt.Errorf("hosted zone %s does not exist", c.HostedZone)
}
c.HostedZoneID = aws.StringValue(matchingZone.Id)
}
hzOut, err := r53.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(c.HostedZoneID)})
if err != nil {
return fmt.Errorf("error getting hosted zone %s: %v", c.HostedZoneID, err)
}
if !isSubdomain(c.ExternalDNSName, aws.StringValue(hzOut.HostedZone.Name)) {
return fmt.Errorf("externalDNSName %s is not a sub-domain of hosted-zone %s", c.ExternalDNSName, aws.StringValue(hzOut.HostedZone.Name))
}
recordSetsResp, err := r53.ListResourceRecordSets(
&route53.ListResourceRecordSetsInput{
HostedZoneId: hzOut.HostedZone.Id,
},
)
if err != nil {
return fmt.Errorf("error listing record sets for hosted zone id = %s: %v", c.HostedZoneID, err)
}
if len(recordSetsResp.ResourceRecordSets) > 0 {
for _, recordSet := range recordSetsResp.ResourceRecordSets {
if *recordSet.Name == config.WithTrailingDot(c.ExternalDNSName) {
return fmt.Errorf(
"RecordSet for \"%s\" already exists in Hosted Zone \"%s.\"",
c.ExternalDNSName,
c.HostedZone,
)
}
}
}
return nil
}
func stackEventErrMsgs(events []*cloudformation.StackEvent) []string {
var errMsgs []string
for _, event := range events {
if aws.StringValue(event.ResourceStatus) == cloudformation.ResourceStatusCreateFailed {
// Only show actual failures, not cancelled dependent resources.
if aws.StringValue(event.ResourceStatusReason) != "Resource creation cancelled" {
errMsgs = append(errMsgs,
strings.TrimSpace(
strings.Join([]string{
aws.StringValue(event.ResourceStatus),
aws.StringValue(event.ResourceType),
aws.StringValue(event.LogicalResourceId),
aws.StringValue(event.ResourceStatusReason),
}, " ")))
}
}
}
return errMsgs
}
func isSubdomain(sub, parent string) bool {
sub, parent = config.WithTrailingDot(sub), config.WithTrailingDot(parent)
subParts, parentParts := strings.Split(sub, "."), strings.Split(parent, ".")
if len(parentParts) > len(subParts) {
return false
}
subSuffixes := subParts[len(subParts)-len(parentParts):]
if len(subSuffixes) != len(parentParts) {
return false
}
for i := range subSuffixes {
if subSuffixes[i] != parentParts[i] {
return false
}
}
return true
}