/
cleanup.go
423 lines (355 loc) · 13.8 KB
/
cleanup.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
package eks
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/terraform-modules-krish/go-commons/errors"
"github.com/terraform-modules-krish/go-commons/retry"
"github.com/terraform-modules-krish/kubergrunt/eksawshelper"
"github.com/terraform-modules-krish/kubergrunt/logging"
)
// Set wait variables for NetworkInterface detaching and deleting
const (
waitSleepBetweenRetries time.Duration = 10 * time.Second
waitMaxRetries int = 30
)
// CleanupSecurityGroup deletes the AWS EKS managed security group, which otherwise doesn't get cleaned up when
// destroying the EKS cluster. It also attempts to delete the security group left by ALB ingress controller, if applicable.
func CleanupSecurityGroup(
clusterArn string,
securityGroupID string,
vpcID string,
) error {
logger := logging.GetProjectLogger()
region, err := eksawshelper.GetRegionFromArn(clusterArn)
if err != nil {
return errors.WithStackTrace(err)
}
clusterID, err := eksawshelper.GetClusterNameFromArn(clusterArn)
if err != nil {
return errors.WithStackTrace(err)
}
sess, err := eksawshelper.NewAuthenticatedSession(region)
if err != nil {
return errors.WithStackTrace(err)
}
ec2Svc := ec2.New(sess)
logger.Infof("Successfully authenticated with AWS")
// 1. Delete provided EKS security group
err = deleteDependencies(ec2Svc, securityGroupID)
if err != nil {
return errors.WithStackTrace(err)
}
logger.Infof("Deleting security group %s", securityGroupID)
delSGInput := &ec2.DeleteSecurityGroupInput{
GroupId: aws.String(securityGroupID),
}
_, err = ec2Svc.DeleteSecurityGroup(delSGInput)
if err != nil {
if awsErr, isAwsErr := err.(awserr.Error); isAwsErr && awsErr.Code() == "InvalidGroup.NotFound" {
logger.Infof("Security group %s already deleted.", securityGroupID)
return nil
}
return errors.WithStackTrace(err)
}
logger.Infof("Successfully deleted security group with name=%s", securityGroupID)
// 2, Delete Load Balancer Controller's security group, if it exists
sgResult, err := lookupSecurityGroup(ec2Svc, vpcID, clusterID)
if err != nil {
return errors.WithStackTrace(err)
}
for _, result := range sgResult.SecurityGroups {
groupID := aws.StringValue(result.GroupId)
groupName := aws.StringValue(result.GroupName)
err = deleteDependencies(ec2Svc, groupID)
if err != nil {
return errors.WithStackTrace(err)
}
input := &ec2.DeleteSecurityGroupInput{GroupId: aws.String(groupID)}
_, err := ec2Svc.DeleteSecurityGroup(input)
if err != nil {
if awsErr, isAwsErr := err.(awserr.Error); isAwsErr && awsErr.Code() == "InvalidGroup.NotFound" {
logger.Infof("Security group %s already deleted.", groupID)
return nil
}
return errors.WithStackTrace(err)
}
logger.Infof("Successfully deleted security group with name=%s, id=%s", groupID, groupName)
}
return nil
}
// Detach and delete elastic network interfaces used by the security group
// so that the security group can be deleted.
func deleteDependencies(ec2Svc *ec2.EC2, securityGroupID string) error {
networkInterfacesResult, err := findNetworkInterfaces(ec2Svc, securityGroupID)
if err != nil {
return err
}
err = detachNetworkInterfaces(ec2Svc, networkInterfacesResult, securityGroupID)
if err != nil {
return err
}
if len(networkInterfacesResult.NetworkInterfaces) > 0 {
err = waitForNetworkInterfacesToBeDetached(ec2Svc, networkInterfacesResult.NetworkInterfaces, waitMaxRetries, waitSleepBetweenRetries)
if err != nil {
return err
}
}
err = deleteNetworkInterfaces(ec2Svc, networkInterfacesResult, securityGroupID, waitMaxRetries, waitSleepBetweenRetries)
if err != nil {
return err
}
err = waitForNetworkInterfacesToBeDeleted(ec2Svc, networkInterfacesResult.NetworkInterfaces, waitMaxRetries, waitSleepBetweenRetries)
if err != nil {
return err
}
return nil
}
func findNetworkInterfaces(
ec2Svc *ec2.EC2,
securityGroupID string,
) (*ec2.DescribeNetworkInterfacesOutput, error) {
logger := logging.GetProjectLogger()
describeNetworkInterfacesInput := &ec2.DescribeNetworkInterfacesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("group-id"),
Values: []*string{aws.String(securityGroupID)},
},
},
}
networkInterfacesResult, err := ec2Svc.DescribeNetworkInterfaces(describeNetworkInterfacesInput)
if err != nil {
return nil, errors.WithStackTrace(err)
}
for _, ni := range networkInterfacesResult.NetworkInterfaces {
logger.Infof("Found network interface %s", aws.StringValue(ni.NetworkInterfaceId))
}
return networkInterfacesResult, nil
}
func detachNetworkInterfaces(
ec2Svc *ec2.EC2,
networkInterfaces *ec2.DescribeNetworkInterfacesOutput,
securityGroupID string,
) error {
logger := logging.GetProjectLogger()
for _, ni := range networkInterfaces.NetworkInterfaces {
// First check the network interface has an attachment. It might have gotten detached before we can even process it.
// If it doesn't have an attachment, process the next network interface.
if ni.Attachment == nil || aws.StringValue(ni.Attachment.Status) == "detached" {
logger.Infof("Network interface %s is detached.", aws.StringValue(ni.NetworkInterfaceId))
continue
}
err := requestDetach(ec2Svc, ni)
switch {
// Base case: no error means we can process the next interface.
case err == nil:
logger.Infof("Requested to detach network interface %s for security group %s", aws.StringValue(ni.NetworkInterfaceId), securityGroupID)
continue
// The attachment is already gone, so process the next network interface.
case isNIAttachmentNotFoundErr(err):
logger.Infof("Network interface %s is detached.", aws.StringValue(ni.NetworkInterfaceId))
continue
// Any other kind of error means we failed this cleanup.
default:
return errors.WithStackTrace(err)
}
}
return nil
}
func deleteNetworkInterfaces(
ec2Svc *ec2.EC2,
networkInterfaces *ec2.DescribeNetworkInterfacesOutput,
securityGroupID string,
maxRetries int,
sleepBetweenRetries time.Duration,
) error {
logger := logging.GetProjectLogger()
for _, ni := range networkInterfaces.NetworkInterfaces {
logger.Infof("Attempting to delete network interface %s", aws.StringValue(ni.NetworkInterfaceId))
deleteNetworkInterfacesInput := &ec2.DeleteNetworkInterfaceInput{
NetworkInterfaceId: ni.NetworkInterfaceId,
}
err := retry.DoWithRetry(
logger.Logger,
"Request Delete Network Interface",
maxRetries, sleepBetweenRetries,
func() error {
_, err := ec2Svc.DeleteNetworkInterface(deleteNetworkInterfacesInput)
if err == nil {
logger.Infof("Requested to delete network interface %s for security group %s", aws.StringValue(ni.NetworkInterfaceId), securityGroupID)
return nil
}
awsErr, isAwsErr := err.(awserr.Error)
switch {
// Note: Handle InvalidNetworkInterfaceID.NotFound error. We have a process, terraformVpcCniAwareDestroy, that
// automatically cleans up detached ENIs. When that process runs, this loop will not be able to find those ENIs
// anymore. But we're also thinking about removing that process in the future, because it might be obsolete now.
// The process lives in terraform-aws-eks. If we handle the cleanup well in kubergrunt, we don't need that.
// AWS might now be set to automatically delete detached ENIs, so it's doubly not needed, and we may even remove
// the steps here to delete network interfaces and wait for their deletion.
case isAwsErr && awsErr.Code() == "InvalidNetworkInterfaceID.NotFound":
logger.Infof("Network interface %s is deleted.", aws.StringValue(ni.NetworkInterfaceId))
return nil // exit retry loop with success
// Note: Handle InvalidParameterValue: Network interface [eni-id] is currently in use.
// We suspect this is an issue with eventual consistency around AWS's resource state.
case isAwsErr && awsErr.Code() == "InvalidParameterValue":
logger.Infof("Waiting for network interface %s to not be in-use (eventual consistency issue).", aws.StringValue(ni.NetworkInterfaceId))
return errors.WithStackTrace(err) // continue retrying
default:
logger.Errorf("Error requesting deleting network interface %s", aws.StringValue(ni.NetworkInterfaceId))
return retry.FatalError{Underlying: err} // halt retries with error
}
})
// All the retries failed or we hit a fatal error.
if err != nil {
return err
}
}
return nil
}
func waitForNetworkInterfacesToBeDetached(
ec2Svc *ec2.EC2,
networkInterfaces []*ec2.NetworkInterface,
maxRetries int,
sleepBetweenRetries time.Duration,
) error {
logger := logging.GetProjectLogger()
for _, ni := range networkInterfaces {
logger.Infof("Waiting for network interface %s to reach detached state.", aws.StringValue(ni.NetworkInterfaceId))
// Poll for the new status
describeNetworkInterfacesInput := &ec2.DescribeNetworkInterfaceAttributeInput{
Attribute: aws.String("attachment"),
NetworkInterfaceId: ni.NetworkInterfaceId,
}
err := retry.DoWithRetry(
logger.Logger,
"Wait for Network Interface to be Detached",
maxRetries, sleepBetweenRetries,
func() error {
niResult, err := ec2Svc.DescribeNetworkInterfaceAttribute(describeNetworkInterfacesInput)
switch {
// Yay, we're detached, process the next network interface.
case err == nil && isNIDetached(niResult):
logger.Infof("Network interface %s is detached.", aws.StringValue(ni.NetworkInterfaceId))
return nil // exit retry loop with success
// Since we checked whether the NI was detached in the first case, no error in this switch means the NI is
// not detached, so we need to retry.
case err == nil:
if niResult.Attachment != nil {
logger.Warnf("Network interface %s attachment status: %s", aws.StringValue(ni.NetworkInterfaceId), aws.StringValue(niResult.Attachment.Status))
}
return errors.WithStackTrace(fmt.Errorf("Network Interface %s not detached.", aws.StringValue(ni.NetworkInterfaceId))) // continue retrying
// If the NI cannot be found, then it is already deleted so halt the loop and move on to the next NI.
case isNINotFoundErr(err):
logger.Infof("Network interface %s is already deleted.", aws.StringValue(ni.NetworkInterfaceId))
return nil // exit retry loop with success
// All other errors are unretryable errors.
default:
logger.Errorf("Error polling attachment for network interface %s", aws.StringValue(ni.NetworkInterfaceId))
return retry.FatalError{Underlying: err} // halt retries with error
}
})
// All the retries failed or we hit a fatal error.
if err != nil {
if isMaxRetriesExceededErr(err) {
return errors.WithStackTrace(NetworkInterfaceDetachedTimeoutError{aws.StringValue(ni.NetworkInterfaceId)})
}
return err
}
}
return nil
}
func waitForNetworkInterfacesToBeDeleted(
ec2Svc *ec2.EC2,
networkInterfaces []*ec2.NetworkInterface,
maxRetries int,
sleepBetweenRetries time.Duration,
) error {
logger := logging.GetProjectLogger()
for _, ni := range networkInterfaces {
logger.Infof("Waiting for network interface %s to be deleted.", aws.StringValue(ni.NetworkInterfaceId))
// Poll for the new status
describeNetworkInterfacesInput := &ec2.DescribeNetworkInterfacesInput{
NetworkInterfaceIds: []*string{ni.NetworkInterfaceId},
}
err := retry.DoWithRetry(
logger.Logger,
"Wait for Network Interface to be Deleted",
maxRetries, sleepBetweenRetries,
func() error {
_, err := ec2Svc.DescribeNetworkInterfaces(describeNetworkInterfacesInput)
switch {
// If there's no error, we have to keep trying.
case err == nil:
return errors.WithStackTrace(fmt.Errorf("Network Interface %s not deleted.", aws.StringValue(ni.NetworkInterfaceId))) // continue retrying
// Yay, it's deleted, process the next network interface.
case isNINotFoundErr(err):
logger.Infof("Network interface %s is deleted.", aws.StringValue(ni.NetworkInterfaceId))
return nil // exit retry loop with success
default:
return retry.FatalError{Underlying: err} // halt retries with error
}
})
// All the retries failed or we hit a fatal error.
if err != nil {
if isMaxRetriesExceededErr(err) {
return errors.WithStackTrace(NetworkInterfaceDeletedTimeoutError{aws.StringValue(ni.NetworkInterfaceId)})
}
return err
}
}
return nil
}
// Used to look up the security group for the ALB ingress controller
func lookupSecurityGroup(
ec2Svc *ec2.EC2,
vpcID string,
clusterID string,
) (*ec2.DescribeSecurityGroupsOutput, error) {
logger := logging.GetProjectLogger()
logger.Infof("Looking up security group containing tag for EKS cluster %s", clusterID)
sgInput := &ec2.DescribeSecurityGroupsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(vpcID)},
},
{
Name: aws.String("tag:elbv2.k8s.aws/cluster"),
Values: []*string{aws.String(clusterID)},
},
}}
sgResult, err := ec2Svc.DescribeSecurityGroups(sgInput)
if err != nil {
return nil, errors.WithStackTrace(err)
}
return sgResult, nil
}
func isNIAttachmentNotFoundErr(err error) bool {
awsErr, isAwsErr := err.(awserr.Error)
return isAwsErr && awsErr.Code() == "InvalidAttachmentID.NotFound"
}
func isNIDetached(niResult *ec2.DescribeNetworkInterfaceAttributeOutput) bool {
return niResult.Attachment == nil || aws.StringValue(niResult.Attachment.Status) == "detached"
}
func isNINotFoundErr(err error) bool {
awsErr, isAwsErr := err.(awserr.Error)
return isAwsErr && awsErr.Code() == "InvalidNetworkInterfaceID.NotFound"
}
func isMaxRetriesExceededErr(err error) bool {
_, isRetryErr := err.(retry.MaxRetriesExceeded)
return isRetryErr
}
func requestDetach(
ec2Svc *ec2.EC2,
ni *ec2.NetworkInterface,
) error {
detachInput := &ec2.DetachNetworkInterfaceInput{
AttachmentId: aws.String(aws.StringValue(ni.Attachment.AttachmentId)),
}
_, err := ec2Svc.DetachNetworkInterface(detachInput)
return err
}