-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
delete_tasks.go
346 lines (296 loc) · 9.92 KB
/
delete_tasks.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
package manager
import (
"context"
"fmt"
"time"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
awseks "github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/awsapi"
"github.com/weaveworks/eksctl/pkg/cfn/waiter"
iamoidc "github.com/weaveworks/eksctl/pkg/iam/oidc"
"github.com/weaveworks/eksctl/pkg/kubernetes"
"github.com/weaveworks/eksctl/pkg/utils/apierrors"
"github.com/weaveworks/eksctl/pkg/utils/tasks"
)
// think Jake is deleting this soon
func deleteAll(_ string) bool { return true }
type NewOIDCManager func() (*iamoidc.OpenIDConnectManager, error)
// NewTasksToDeleteAddonIAM temporary type, to be removed after moving NewTasksToDeleteClusterWithNodeGroups to actions package
type NewTasksToDeleteAddonIAM func(ctx context.Context, wait bool) (*tasks.TaskTree, error)
// NewTasksToDeleteClusterWithNodeGroups defines tasks required to delete the given cluster along with all of its resources
func (c *StackCollection) NewTasksToDeleteClusterWithNodeGroups(
ctx context.Context,
clusterStack *Stack,
nodeGroupStacks []NodeGroupStack,
clusterOperable bool,
newOIDCManager NewOIDCManager,
newTasksToDeleteAddonIAM NewTasksToDeleteAddonIAM,
cluster *ekstypes.Cluster,
clientSetGetter kubernetes.ClientSetGetter,
wait, force bool,
cleanup func(chan error, string) error) (*tasks.TaskTree, error) {
taskTree := &tasks.TaskTree{Parallel: false}
nodeGroupTasks, err := c.NewTasksToDeleteNodeGroups(nodeGroupStacks, deleteAll, true, cleanup)
if err != nil {
return nil, err
}
if nodeGroupTasks.Len() > 0 {
nodeGroupTasks.IsSubTask = true
taskTree.Append(nodeGroupTasks)
}
if clusterOperable {
serviceAccountAndOIDCTasks, err := c.NewTasksToDeleteOIDCProviderWithIAMServiceAccounts(ctx, newOIDCManager, cluster, clientSetGetter, force)
if err != nil {
return nil, err
}
if serviceAccountAndOIDCTasks.Len() > 0 {
serviceAccountAndOIDCTasks.IsSubTask = true
taskTree.Append(serviceAccountAndOIDCTasks)
}
}
deleteAddonIAMTasks, err := newTasksToDeleteAddonIAM(ctx, wait)
if err != nil {
return nil, err
}
if deleteAddonIAMTasks.Len() > 0 {
deleteAddonIAMTasks.IsSubTask = true
taskTree.Append(deleteAddonIAMTasks)
}
if clusterStack == nil {
return nil, &StackNotFoundErr{ClusterName: c.spec.Metadata.Name}
}
info := fmt.Sprintf("delete cluster control plane %q", c.spec.Metadata.Name)
if wait {
taskTree.Append(&taskWithStackSpec{
info: info,
stack: clusterStack,
call: c.DeleteStackBySpecSync,
})
} else {
taskTree.Append(&asyncTaskWithStackSpec{
info: info,
stack: clusterStack,
call: c.DeleteStackBySpec,
})
}
return taskTree, nil
}
// NewTasksToDeleteNodeGroups defines tasks required to delete all of the nodegroups
func (c *StackCollection) NewTasksToDeleteNodeGroups(nodeGroupStacks []NodeGroupStack, shouldDelete func(string) bool, wait bool, cleanup func(chan error, string) error) (*tasks.TaskTree, error) {
taskTree := &tasks.TaskTree{Parallel: true}
for _, s := range nodeGroupStacks {
if !shouldDelete(s.NodeGroupName) {
continue
}
if s.Stack.StackStatus == types.StackStatusDeleteFailed && cleanup != nil {
taskTree.Append(&tasks.TaskWithNameParam{
Info: fmt.Sprintf("cleanup for nodegroup %q", s.NodeGroupName),
Call: cleanup,
})
}
info := fmt.Sprintf("delete nodegroup %q", s.NodeGroupName)
if wait {
taskTree.Append(&taskWithStackSpec{
info: info,
stack: s.Stack,
call: c.DeleteStackBySpecSync,
})
} else {
taskTree.Append(&asyncTaskWithStackSpec{
info: info,
stack: s.Stack,
call: c.DeleteStackBySpec,
})
}
}
return taskTree, nil
}
type DeleteWaitCondition struct {
Condition func() (bool, error)
Timeout time.Duration
Interval time.Duration
}
type DeleteUnownedNodegroupTask struct {
cluster string
nodegroup string
wait *DeleteWaitCondition
info string
eksAPI awsapi.EKS
ctx context.Context
}
func (d *DeleteUnownedNodegroupTask) Describe() string {
return d.info
}
func (d *DeleteUnownedNodegroupTask) Do() error {
out, err := d.eksAPI.DeleteNodegroup(d.ctx, &awseks.DeleteNodegroupInput{
ClusterName: &d.cluster,
NodegroupName: &d.nodegroup,
})
if err != nil {
return err
}
if d.wait != nil {
w := waiter.Waiter{
NextDelay: func(_ int) time.Duration {
return d.wait.Interval
},
Operation: d.wait.Condition,
}
if err := w.WaitWithTimeout(d.wait.Timeout); err != nil {
if err == context.DeadlineExceeded {
return errors.Errorf("timed out waiting for nodegroup deletion after %s", d.wait.Timeout)
}
return err
}
}
if out != nil {
logger.Debug("delete nodegroup %q output: %+v", d.nodegroup, out.Nodegroup)
}
return nil
}
func (c *StackCollection) NewTaskToDeleteUnownedNodeGroup(ctx context.Context, clusterName, nodegroup string, eksAPI awsapi.EKS, waitCondition *DeleteWaitCondition) tasks.Task {
return tasks.SynchronousTask{
SynchronousTaskIface: &DeleteUnownedNodegroupTask{
cluster: clusterName,
nodegroup: nodegroup,
eksAPI: eksAPI,
wait: waitCondition,
info: fmt.Sprintf("delete unowned nodegroup %s", nodegroup),
ctx: ctx,
}}
}
// NewTasksToDeleteOIDCProviderWithIAMServiceAccounts defines tasks required to delete all of the iamserviceaccounts
// along with associated IAM OIDC provider
func (c *StackCollection) NewTasksToDeleteOIDCProviderWithIAMServiceAccounts(ctx context.Context, newOIDCManager NewOIDCManager, cluster *ekstypes.Cluster, clientSetGetter kubernetes.ClientSetGetter, force bool) (*tasks.TaskTree, error) {
taskTree := &tasks.TaskTree{Parallel: false}
oidc, err := newOIDCManager()
if err != nil {
if _, ok := err.(*iamoidc.UnsupportedOIDCError); ok {
logger.Debug("OIDC is not supported for this cluster")
return taskTree, nil
}
return nil, fmt.Errorf("error creating OIDC manager: %w", err)
}
allServiceAccountsWithStacks, err := c.getAllServiceAccounts(ctx)
if err != nil {
return nil, err
}
if len(allServiceAccountsWithStacks) > 0 {
saTasks, err := c.NewTasksToDeleteIAMServiceAccounts(ctx, allServiceAccountsWithStacks, clientSetGetter, true)
if err != nil {
return nil, err
}
if saTasks.Len() > 0 {
saTasks.IsSubTask = true
taskTree.Append(saTasks)
}
}
providerExists, err := oidc.CheckProviderExists(ctx)
if err != nil {
if apierrors.IsAccessDeniedError(err) {
clusterHasOIDC, foundTag := clusterHasOIDCProvider(cluster)
errMsg := "IAM permissions are required to delete OIDC provider"
switch {
case foundTag:
if clusterHasOIDC {
return nil, fmt.Errorf("%s: %w", errMsg, err)
}
if len(allServiceAccountsWithStacks) > 0 {
logger.Warning("expected an OIDC provider to be associated with the cluster; found %d service account(s)", len(allServiceAccountsWithStacks))
}
case len(allServiceAccountsWithStacks) > 0:
if !force {
return nil, fmt.Errorf("found %d IAM service account(s); %s: %w", len(allServiceAccountsWithStacks), errMsg, err)
}
default:
logger.Info("could not determine if cluster has an OIDC provider because of missing IAM permissions; " +
"if an OIDC provider was associated with the cluster (either by setting `iam.withOIDC: true` or by using `eksctl utils associate-iam-oidc-provider`), " +
"run `aws iam delete-open-id-connect-provider` from an authorized IAM entity to delete it")
}
return taskTree, nil
}
return nil, err
}
if providerExists {
taskTree.Append(&asyncTaskWithoutParams{
info: "delete IAM OIDC provider",
call: func() error {
return oidc.DeleteProvider(ctx)
},
})
}
return taskTree, nil
}
func (c *StackCollection) getAllServiceAccounts(ctx context.Context) ([]string, error) {
serviceAccountStacks, err := c.DescribeIAMServiceAccountStacks(ctx)
if err != nil {
return nil, err
}
var serviceAccounts []string
for _, stack := range serviceAccountStacks {
serviceAccounts = append(serviceAccounts, GetIAMServiceAccountName(stack))
}
return serviceAccounts, nil
}
// NewTasksToDeleteIAMServiceAccounts defines tasks required to delete all of the iamserviceaccounts
func (c *StackCollection) NewTasksToDeleteIAMServiceAccounts(ctx context.Context, serviceAccounts []string, clientSetGetter kubernetes.ClientSetGetter, wait bool) (*tasks.TaskTree, error) {
serviceAccountStacks, err := c.DescribeIAMServiceAccountStacks(ctx)
if err != nil {
return nil, err
}
stacksMap := stacksToServiceAccountMap(serviceAccountStacks)
taskTree := &tasks.TaskTree{Parallel: true}
for _, serviceAccount := range serviceAccounts {
saTasks := &tasks.TaskTree{
Parallel: false,
IsSubTask: true,
}
if s, ok := stacksMap[serviceAccount]; ok {
info := fmt.Sprintf("delete IAM role for serviceaccount %q", serviceAccount)
if wait {
saTasks.Append(&taskWithStackSpec{
info: info,
stack: s,
call: c.DeleteStackBySpecSync,
})
} else {
saTasks.Append(&asyncTaskWithStackSpec{
info: info,
stack: s,
call: c.DeleteStackBySpec,
})
}
}
meta, err := api.ClusterIAMServiceAccountNameStringToClusterIAMMeta(serviceAccount)
if err != nil {
return nil, err
}
saTasks.Append(&kubernetesTask{
info: fmt.Sprintf("delete serviceaccount %q", serviceAccount),
kubernetes: clientSetGetter,
objectMeta: meta.AsObjectMeta(),
call: kubernetes.MaybeDeleteServiceAccount,
})
taskTree.Append(saTasks)
}
return taskTree, nil
}
func stacksToServiceAccountMap(stacks []*types.Stack) map[string]*types.Stack {
stackMap := make(map[string]*types.Stack)
for _, stack := range stacks {
stackMap[GetIAMServiceAccountName(stack)] = stack
}
return stackMap
}
func clusterHasOIDCProvider(cluster *ekstypes.Cluster) (hasOIDC bool, found bool) {
for k, v := range cluster.Tags {
if k == api.ClusterOIDCEnabledTag {
return v == "true", true
}
}
return false, false
}