-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
cluster.go
296 lines (255 loc) · 8.79 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
package manager
import (
"context"
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/builder"
"github.com/weaveworks/eksctl/pkg/cfn/outputs"
)
// MakeChangeSetName builds a consistent name for a changeset.
func (c *StackCollection) MakeChangeSetName(action string) string {
return fmt.Sprintf("eksctl-%s-%d", action, time.Now().Unix())
}
func (c *StackCollection) MakeClusterStackName() string {
return c.MakeClusterStackNameFromName(c.spec.Metadata.Name)
}
func (c *StackCollection) MakeClusterStackNameFromName(name string) string {
return "eksctl-" + name + "-cluster"
}
// createClusterTask creates the cluster
func (c *StackCollection) createClusterTask(ctx context.Context, errs chan error, supportsManagedNodes bool) error {
name := c.MakeClusterStackName()
logger.Info("building cluster stack %q", name)
stack := builder.NewClusterResourceSet(c.ec2API, c.region, c.spec, nil, false)
if err := stack.AddAllResources(ctx); err != nil {
return err
}
return c.createClusterStack(ctx, name, stack, errs)
}
// DescribeClusterStackIfExists calls ListStacks and filters out cluster stack.
// If the stack does not exist, it returns nil.
func (c *StackCollection) DescribeClusterStackIfExists(ctx context.Context) (*Stack, error) {
stacks, err := c.ListStacks(ctx)
if err != nil {
return nil, err
}
if len(stacks) == 0 {
return nil, nil
}
for _, s := range stacks {
if s.StackStatus == types.StackStatusDeleteComplete {
continue
}
if getClusterName(s) != "" {
return s, nil
}
}
return nil, nil
}
// DescribeClusterStack returns the cluster stack. If the stack does not exist, it returns an error.
func (c *StackCollection) DescribeClusterStack(ctx context.Context) (*Stack, error) {
stack, err := c.DescribeClusterStackIfExists(ctx)
if err != nil {
return nil, err
}
if stack == nil {
return nil, &StackNotFoundErr{
ClusterName: c.spec.Metadata.Name,
}
}
return stack, nil
}
// RefreshFargatePodExecutionRoleARN reads the CloudFormation stacks and
// their output values, and sets the Fargate pod execution role ARN to
// the ClusterConfig. If there is no cluster stack found but a fargate stack
// exists, use the output from that stack.
func (c *StackCollection) RefreshFargatePodExecutionRoleARN(ctx context.Context) error {
fargateOutputs := map[string]outputs.Collector{
outputs.FargatePodExecutionRoleARN: func(v string) error {
c.spec.IAM.FargatePodExecutionRoleARN = &v
return nil
},
}
stack, err := c.DescribeClusterStackIfExists(ctx)
if err != nil {
return err
}
//check if fargate role is on the cluster stack
if stack != nil {
if err := outputs.Collect(*stack, nil, fargateOutputs); err != nil {
return err
}
if c.spec.IAM.FargatePodExecutionRoleARN != nil {
return nil
}
}
//check if fargate role is in separate stack
stack, err = c.GetFargateStack(ctx)
if err != nil {
return err
}
return outputs.Collect(*stack, fargateOutputs, nil)
}
// AppendNewClusterStackResource will update cluster
// stack with new resources in append-only way
func (c *StackCollection) AppendNewClusterStackResource(ctx context.Context, extendForOutposts, plan bool) (bool, error) {
name := c.MakeClusterStackName()
// NOTE: currently we can only append new resources to the stack,
// as there are a few limitations:
// - it must work with VPC that is imported as well as VPC that
// is managed as part of the stack;
// - CloudFormation cannot yet upgrade EKS control plane itself;
currentTemplate, err := c.GetStackTemplate(ctx, name)
if err != nil {
return false, errors.Wrapf(err, "error getting stack template %s", name)
}
currentResources := gjson.Get(currentTemplate, resourcesRootPath)
currentOutputs := gjson.Get(currentTemplate, outputsRootPath)
currentMappings := gjson.Get(currentTemplate, mappingsRootPath)
if !currentResources.IsObject() || !currentOutputs.IsObject() {
return false, fmt.Errorf("unexpected template format of the current stack ")
}
if err := c.importServiceRoleARN(ctx, currentResources); err != nil {
return false, err
}
logger.Info("re-building cluster stack %q", name)
newStack := builder.NewClusterResourceSet(c.ec2API, c.region, c.spec, ¤tResources, extendForOutposts)
if err := newStack.AddAllResources(ctx); err != nil {
return false, err
}
newTemplate, err := newStack.RenderJSON()
if err != nil {
return false, errors.Wrapf(err, "rendering template for %q stack", name)
}
logger.Debug("newTemplate = %s", newTemplate)
newResources := gjson.Get(string(newTemplate), resourcesRootPath)
newOutputs := gjson.Get(string(newTemplate), outputsRootPath)
newMappings := gjson.Get(string(newTemplate), mappingsRootPath)
if !newResources.IsObject() || !newOutputs.IsObject() || !newMappings.IsObject() {
return false, errors.New("unexpected template format of the new version of the stack")
}
logger.Debug("currentTemplate = %s", currentTemplate)
var iterErr error
iterFunc := func(list *[]string, root string, currentSet, key, value gjson.Result) bool {
k := key.String()
if currentSet.Get(k).Exists() {
return true
}
*list = append(*list, k)
path := root + "." + k
currentTemplate, iterErr = sjson.Set(currentTemplate, path, value.Value())
return iterErr == nil
}
var (
addResources []string
addOutputs []string
addMappings []string
)
newResources.ForEach(func(k, v gjson.Result) bool {
return iterFunc(&addResources, resourcesRootPath, currentResources, k, v)
})
if iterErr != nil {
return false, errors.Wrap(iterErr, "adding resources to current stack template")
}
newOutputs.ForEach(func(k, v gjson.Result) bool {
return iterFunc(&addOutputs, outputsRootPath, currentOutputs, k, v)
})
if iterErr != nil {
return false, errors.Wrap(iterErr, "adding outputs to current stack template")
}
newMappings.ForEach(func(k, v gjson.Result) bool {
return iterFunc(&addMappings, mappingsRootPath, currentMappings, k, v)
})
if iterErr != nil {
return false, errors.Wrap(iterErr, "adding mappings to current stack template")
}
if len(addResources) == 0 && len(addOutputs) == 0 && len(addMappings) == 0 {
logger.Success("all resources in cluster stack %q are up-to-date", name)
return false, nil
}
logger.Debug("currentTemplate = %s", currentTemplate)
describeUpdate := fmt.Sprintf("updating stack to add new resources %v and outputs %v", addResources, addOutputs)
if plan {
logger.Info("(plan) %s", describeUpdate)
return true, nil
}
err = c.UpdateStack(ctx, UpdateStackOptions{
StackName: name,
ChangeSetName: c.MakeChangeSetName("update-cluster"),
Description: describeUpdate,
TemplateData: TemplateBody(currentTemplate),
Wait: true,
})
if err != nil {
return false, err
}
stack, err := c.DescribeStack(ctx, &Stack{
StackName: aws.String(name),
})
if err != nil {
return false, fmt.Errorf("error describing cluster stack: %w", err)
}
if err := newStack.GetAllOutputs(*stack); err != nil {
return false, fmt.Errorf("error getting outputs for updated cluster stack: %w", err)
}
return true, nil
}
// ClusterHasDedicatedVPC returns true if the cluster was created with a dedicated VPC.
func (c *StackCollection) ClusterHasDedicatedVPC(ctx context.Context) (bool, error) {
stackName := c.MakeClusterStackName()
currentTemplate, err := c.GetStackTemplate(ctx, stackName)
if err != nil {
return false, fmt.Errorf("error getting stack template %q: %w", stackName, err)
}
resources := gjson.Get(currentTemplate, resourcesRootPath)
return resources.IsObject() && resources.Get("VPC").Exists(), nil
}
func (c *StackCollection) importServiceRoleARN(ctx context.Context, resources gjson.Result) error {
s, err := c.DescribeClusterStackIfExists(ctx)
if err != nil {
return err
}
usesEksctlCreatedServiceRole := false
resources.ForEach(func(key, value gjson.Result) bool {
if key.String() == "ServiceRole" {
usesEksctlCreatedServiceRole = true
}
return true
})
if usesEksctlCreatedServiceRole {
return nil
}
for _, o := range s.Outputs {
if *o.OutputKey == "ServiceRoleARN" {
c.spec.IAM.ServiceRoleARN = o.OutputValue
}
}
return nil
}
func getClusterName(s *Stack) string {
if strings.HasSuffix(*s.StackName, "-cluster") {
if v := getClusterNameTag(s); v != "" {
return v
}
}
if strings.HasPrefix(*s.StackName, "EKS-") && strings.HasSuffix(*s.StackName, "-ControlPlane") {
return strings.TrimPrefix("EKS-", strings.TrimSuffix(*s.StackName, "-ControlPlane"))
}
return ""
}
func getClusterNameTag(s *Stack) string {
for _, tag := range s.Tags {
if *tag.Key == api.ClusterNameTag || *tag.Key == api.OldClusterNameTag {
return *tag.Value
}
}
return ""
}