This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 295
/
cluster.go
232 lines (193 loc) · 5.71 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
package cluster
import (
"bytes"
"fmt"
"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/s3"
"github.com/coreos/kube-aws/cfnstack"
"github.com/coreos/kube-aws/core/nodepool/config"
"text/tabwriter"
)
const STACK_TEMPLATE_FILENAME = "stack.json"
type ClusterRef struct {
config.ProvidedConfig
session *session.Session
}
type Cluster struct {
*ClusterRef
*config.CompressedStackConfig
}
type Info struct {
Name string
}
type ec2DescribeKeyPairsService interface {
DescribeKeyPairs(*ec2.DescribeKeyPairsInput) (*ec2.DescribeKeyPairsOutput, error)
}
type ec2CreateVolumeService interface {
CreateVolume(*ec2.CreateVolumeInput) (*ec2.Volume, error)
}
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)
w.Flush()
return buf.String()
}
func NewClusterRef(cfg *config.ProvidedConfig, awsDebug bool) *ClusterRef {
awsConfig := aws.NewConfig().
WithRegion(cfg.Region.String()).
WithCredentialsChainVerboseErrors(true)
if awsDebug {
awsConfig = awsConfig.WithLogLevel(aws.LogDebug)
}
return &ClusterRef{
ProvidedConfig: *cfg,
session: session.New(awsConfig),
}
}
func NewCluster(provided *config.ProvidedConfig, opts config.StackTemplateOptions, awsDebug bool) (*Cluster, error) {
computed, err := provided.Config()
if err != nil {
return nil, err
}
stackConfig, err := computed.StackConfig(opts)
if err != nil {
return nil, err
}
compressed, err := stackConfig.Compress()
if err != nil {
return nil, err
}
ref := NewClusterRef(provided, awsDebug)
return &Cluster{
CompressedStackConfig: compressed,
ClusterRef: ref,
}, nil
}
func (c *Cluster) Assets() (cfnstack.Assets, error) {
stackTemplate, err := c.RenderStackTemplateAsString()
if err != nil {
return nil, fmt.Errorf("Error while rendering template : %v", err)
}
return cfnstack.NewAssetsBuilder(c.StackName(), c.StackConfig.S3URI, c.StackConfig.Region).
Add(c.UserDataWorkerFileName(), c.UserDataWorker).
Add(STACK_TEMPLATE_FILENAME, stackTemplate).
Build(), nil
}
func (c *Cluster) TemplateURL() (string, error) {
assets, err := c.Assets()
if err != nil {
return "", fmt.Errorf("failed to get template url: %v", err)
}
asset, err := assets.FindAssetByStackAndFileName(c.StackName(), STACK_TEMPLATE_FILENAME)
if err != nil {
return "", fmt.Errorf("failed to get template url: %v", err)
}
return asset.URL(), nil
}
func (c *Cluster) stackProvisioner() *cfnstack.Provisioner {
stackPolicyBody := `{
"Statement" : [
{
"Effect" : "Allow",
"Principal" : "*",
"Action" : "Update:*",
"Resource" : "*"
}
]
}`
return cfnstack.NewProvisioner(c.StackName(), c.WorkerDeploymentSettings().StackTags(), c.S3URI, c.Region, stackPolicyBody, c.session())
}
func (c *Cluster) session() *session.Session {
return c.ClusterRef.session
}
func (c *Cluster) Create() error {
cfSvc := cloudformation.New(c.session())
s3Svc := s3.New(c.session())
stackTemplate, err := c.RenderStackTemplateAsString()
if err != nil {
return err
}
cloudConfigs := map[string]string{
"userdata-worker": c.UserDataWorker,
}
return c.stackProvisioner().CreateStackAndWait(cfSvc, s3Svc, stackTemplate, cloudConfigs)
}
func (c *Cluster) Update() (string, error) {
cfSvc := cloudformation.New(c.session())
s3Svc := s3.New(c.session())
stackTemplate, err := c.RenderStackTemplateAsString()
if err != nil {
return "", err
}
cloudConfigs := map[string]string{
"userdata-worker": c.UserDataWorker,
}
updateOutput, err := c.stackProvisioner().UpdateStackAndWait(cfSvc, s3Svc, stackTemplate, cloudConfigs)
return updateOutput, err
}
func (c *Cluster) ValidateStack() (string, error) {
if err := c.ValidateUserData(); err != nil {
return "", fmt.Errorf("failed to validate userdata : %v", err)
}
ec2Svc := ec2.New(c.session())
if err := c.validateWorkerRootVolume(ec2Svc); err != nil {
return "", err
}
if c.KeyName != "" {
if err := c.validateKeyPair(ec2Svc); err != nil {
return "", err
}
}
stackTemplate, err := c.RenderStackTemplateAsString()
if err != nil {
return "", fmt.Errorf("failed to validate template : %v", err)
}
return c.stackProvisioner().Validate(stackTemplate)
}
func (c *ClusterRef) validateKeyPair(ec2Svc ec2DescribeKeyPairsService) 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
}
func (c *ClusterRef) validateWorkerRootVolume(ec2Svc ec2CreateVolumeService) error {
//Send a dry-run request to validate the worker root volume parameters
workerRootVolume := &ec2.CreateVolumeInput{
DryRun: aws.Bool(true),
AvailabilityZone: aws.String(c.Subnets[0].AvailabilityZone),
Iops: aws.Int64(int64(c.RootVolumeIOPS)),
Size: aws.Int64(int64(c.RootVolumeSize)),
VolumeType: aws.String(c.RootVolumeType),
}
if _, err := ec2Svc.CreateVolume(workerRootVolume); err != nil {
operr, ok := err.(awserr.Error)
if !ok || (ok && operr.Code() != "DryRunOperation") {
return fmt.Errorf("create volume dry-run request failed: %v", err)
}
}
return nil
}
func (c *ClusterRef) Info() (*Info, error) {
var info Info
{
info.Name = c.NodePoolName
}
return &info, nil
}
func (c *ClusterRef) Destroy() error {
return cfnstack.NewDestroyer(c.StackName(), c.session).Destroy()
}