Skip to content

Commit b7bc118

Browse files
authored
feat(eks): add DisableDeletionProtection setting to EKS cluster type (#753)
1 parent 525b559 commit b7bc118

File tree

4 files changed

+92
-34
lines changed

4 files changed

+92
-34
lines changed

docs/resources/eks-cluster.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,38 @@ generated: true
1111
EKSCluster
1212
```
1313

14+
## Properties
1415

1516

17+
- `CreatedAt`: No Description
18+
- `Name`: No Description
19+
- `tag:<key>:`: This resource has tags with property `Tags`. These are key/value pairs that are
20+
added as their own property with the prefix of `tag:` (e.g. [tag:example: "value"])
21+
22+
!!! note - Using Properties
23+
Properties are what [Filters](../config-filtering.md) are written against in your configuration. You use the property
24+
names to write filters for what you want to **keep** and omit from the nuke process.
25+
26+
### String Property
27+
28+
The string representation of a resource is generally the value of the Name, ID or ARN field of the resource. Not all
29+
resources support properties. To write a filter against the string representation, simply omit the `property` field in
30+
the filter.
31+
32+
The string value is always what is used in the output of the log format when a resource is identified.
33+
34+
## Settings
35+
36+
- `DisableDeletionProtection`
37+
38+
39+
### DisableDeletionProtection
40+
41+
!!! note
42+
There is currently no description for this setting. Often times settings are fairly self-explanatory. However, we
43+
are working on adding descriptions for all settings.
44+
45+
```text
46+
DisableDeletionProtection
47+
```
48+

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ toolchain go1.24.5
66

77
require (
88
github.com/aws/aws-sdk-go v1.55.7
9-
github.com/aws/aws-sdk-go-v2 v1.39.0
9+
github.com/aws/aws-sdk-go-v2 v1.39.2
1010
github.com/aws/aws-sdk-go-v2/config v1.28.11
1111
github.com/aws/aws-sdk-go-v2/credentials v1.17.68
1212
github.com/aws/aws-sdk-go-v2/service/amp v1.36.0
@@ -49,10 +49,11 @@ require (
4949
require (
5050
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.7 // indirect
5151
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 // indirect
52-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.7 // indirect
53-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.7 // indirect
52+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.9 // indirect
53+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.9 // indirect
5454
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
5555
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.27 // indirect
56+
github.com/aws/aws-sdk-go-v2/service/eks v1.74.2 // indirect
5657
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.0 // indirect
5758
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.4.8 // indirect
5859
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.1 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/aws/aws-sdk-go v1.55.7 h1:UJrkFq7es5CShfBwlWAC8DA077vp8PyVbQd3lqLiztE
22
github.com/aws/aws-sdk-go v1.55.7/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
33
github.com/aws/aws-sdk-go-v2 v1.39.0 h1:xm5WV/2L4emMRmMjHFykqiA4M/ra0DJVSWUkDyBjbg4=
44
github.com/aws/aws-sdk-go-v2 v1.39.0/go.mod h1:sDioUELIUO9Znk23YVmIk86/9DOpkbyyVb1i/gUNFXY=
5+
github.com/aws/aws-sdk-go-v2 v1.39.2 h1:EJLg8IdbzgeD7xgvZ+I8M1e0fL0ptn/M47lianzth0I=
6+
github.com/aws/aws-sdk-go-v2 v1.39.2/go.mod h1:sDioUELIUO9Znk23YVmIk86/9DOpkbyyVb1i/gUNFXY=
57
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.7 h1:lL7IfaFzngfx0ZwUGOZdsFFnQ5uLvR0hWqqhyE7Q9M8=
68
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.7/go.mod h1:QraP0UcVlQJsmHfioCrveWOC1nbiWUl3ej08h4mXWoc=
79
github.com/aws/aws-sdk-go-v2/config v1.28.11 h1:7Ekru0IkRHRnSRWGQLnLN6i0o1Jncd0rHo2T130+tEQ=
@@ -12,8 +14,12 @@ github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 h1:x793wxmUWVDhshP8WW2mln
1214
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30/go.mod h1:Jpne2tDnYiFascUEs2AWHJL9Yp7A5ZVy3TNyxaAjD6M=
1315
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.7 h1:UCxq0X9O3xrlENdKf1r9eRJoKz/b0AfGkpp3a7FPlhg=
1416
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.7/go.mod h1:rHRoJUNUASj5Z/0eqI4w32vKvC7atoWR0jC+IkmVH8k=
17+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.9 h1:se2vOWGD3dWQUtfn4wEjRQJb1HK1XsNIt825gskZ970=
18+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.9/go.mod h1:hijCGH2VfbZQxqCDN7bwz/4dzxV+hkyhjawAtdPWKZA=
1519
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.7 h1:Y6DTZUn7ZUC4th9FMBbo8LVE+1fyq3ofw+tRwkUd3PY=
1620
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.7/go.mod h1:x3XE6vMnU9QvHN/Wrx2s44kwzV2o2g5x/siw4ZUJ9g8=
21+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.9 h1:6RBnKZLkJM4hQ+kN6E7yWFveOTg8NLPHAkqrs4ZPlTU=
22+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.9/go.mod h1:V9rQKRmK7AWuEsOMnHzKj8WyrIir1yUJbZxDuZLFvXI=
1723
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 h1:VaRN3TlFdd6KxX1x3ILT5ynH6HvKgqdiXoTxAF4HQcQ=
1824
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1/go.mod h1:FbtygfRFze9usAadmnGJNc8KsP346kEe+y2/oyhGAGc=
1925
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.27 h1:AmB5QxnD+fBFrg9LcqzkgF/CaYvMyU/BTlejG4t1S7Q=
@@ -38,6 +44,8 @@ github.com/aws/aws-sdk-go-v2/service/ecs v1.54.6 h1:TE4XBXeHvTTnD4rISqqMET4TwE7S
3844
github.com/aws/aws-sdk-go-v2/service/ecs v1.54.6/go.mod h1:wAtdeFanDuF9Re/ge4DRDaYe3Wy1OGrU7jG042UcuI4=
3945
github.com/aws/aws-sdk-go-v2/service/efs v1.35.4 h1:QJHwC9X5TxJJGdesIJP65gAsu0gXCGs0nF2/wBe4aAA=
4046
github.com/aws/aws-sdk-go-v2/service/efs v1.35.4/go.mod h1:XT6hcgC1HV33EBGPWdXnbgyeqND4k43qX3argLyEZM8=
47+
github.com/aws/aws-sdk-go-v2/service/eks v1.74.2 h1:GKqBur7gp6rnYbMZXh2+89f8g+/bu26ZKwpXfXrno80=
48+
github.com/aws/aws-sdk-go-v2/service/eks v1.74.2/go.mod h1:f1/1x766rRjLVUk94exobjhggT1MR3vO4wxglqOvpY4=
4149
github.com/aws/aws-sdk-go-v2/service/iam v1.38.10 h1:u/MwkFwRkKRDvy7D76/khJTk8HMp4mC5sZKErU53jos=
4250
github.com/aws/aws-sdk-go-v2/service/iam v1.38.10/go.mod h1:Gid0WEVky3EWbkeXiS67kHhbiK+q3/wO/hvPh7plR0c=
4351
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.0 h1:6+lZi2JeGKtCraAj1rpoZfKqnQ9SptseRZioejfUOLM=

resources/eks-clusters.go

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55

66
"time"
77

8-
"github.com/aws/aws-sdk-go/aws"
9-
"github.com/aws/aws-sdk-go/service/eks"
8+
"github.com/aws/aws-sdk-go-v2/aws"
9+
"github.com/aws/aws-sdk-go-v2/service/eks"
10+
"github.com/gotidy/ptr"
1011

1112
"github.com/ekristen/libnuke/pkg/registry"
1213
"github.com/ekristen/libnuke/pkg/resource"
14+
libsettings "github.com/ekristen/libnuke/pkg/settings"
1315
"github.com/ekristen/libnuke/pkg/types"
1416

1517
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
@@ -23,69 +25,83 @@ func init() {
2325
Scope: nuke.Account,
2426
Resource: &EKSCluster{},
2527
Lister: &EKSClusterLister{},
28+
Settings: []string{
29+
"DisableDeletionProtection",
30+
},
2631
})
2732
}
2833

2934
type EKSClusterLister struct{}
3035

31-
func (l *EKSClusterLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
36+
func (l *EKSClusterLister) List(ctx context.Context, o interface{}) ([]resource.Resource, error) {
3237
opts := o.(*nuke.ListerOpts)
33-
svc := eks.New(opts.Session)
38+
svc := eks.NewFromConfig(*opts.Config)
3439
var resources []resource.Resource
3540

3641
params := &eks.ListClustersInput{
37-
MaxResults: aws.Int64(100),
42+
MaxResults: aws.Int32(100),
3843
}
3944

40-
for {
41-
resp, err := svc.ListClusters(params)
45+
paginator := eks.NewListClustersPaginator(svc, params)
46+
47+
for paginator.HasMorePages() {
48+
resp, err := paginator.NextPage(ctx)
4249
if err != nil {
4350
return nil, err
4451
}
4552

4653
for _, cluster := range resp.Clusters {
47-
dcResp, err := svc.DescribeCluster(&eks.DescribeClusterInput{Name: cluster})
54+
dcResp, err := svc.DescribeCluster(ctx, &eks.DescribeClusterInput{Name: aws.String(cluster)})
4855
if err != nil {
4956
return nil, err
5057
}
5158
resources = append(resources, &EKSCluster{
52-
svc: svc,
53-
name: cluster,
54-
cluster: dcResp.Cluster,
59+
svc: svc,
60+
Name: aws.String(cluster),
61+
CreatedAt: dcResp.Cluster.CreatedAt,
62+
protection: dcResp.Cluster.DeletionProtection,
63+
Tags: dcResp.Cluster.Tags,
5564
})
5665
}
57-
if resp.NextToken == nil {
58-
break
59-
}
60-
61-
params.NextToken = resp.NextToken
6266
}
6367
return resources, nil
6468
}
6569

6670
type EKSCluster struct {
67-
svc *eks.EKS
68-
name *string
69-
cluster *eks.Cluster
71+
Name *string
72+
CreatedAt *time.Time
73+
Tags map[string]string
74+
75+
svc *eks.Client
76+
settings *libsettings.Setting
77+
protection *bool
7078
}
7179

72-
func (f *EKSCluster) Remove(_ context.Context) error {
73-
_, err := f.svc.DeleteCluster(&eks.DeleteClusterInput{
74-
Name: f.name,
80+
func (r *EKSCluster) Remove(ctx context.Context) error {
81+
if ptr.ToBool(r.protection) && r.settings.GetBool("DisableDeletionProtection") {
82+
updateClusterConfigInput := &eks.UpdateClusterConfigInput{
83+
Name: r.Name,
84+
DeletionProtection: aws.Bool(false),
85+
}
86+
if _, err := r.svc.UpdateClusterConfig(ctx, updateClusterConfigInput); err != nil {
87+
return err
88+
}
89+
}
90+
_, err := r.svc.DeleteCluster(ctx, &eks.DeleteClusterInput{
91+
Name: r.Name,
7592
})
7693

7794
return err
7895
}
7996

80-
func (f *EKSCluster) Properties() types.Properties {
81-
properties := types.NewProperties()
82-
properties.Set("CreatedAt", f.cluster.CreatedAt.Format(time.RFC3339))
83-
for key, value := range f.cluster.Tags {
84-
properties.SetTag(&key, value)
85-
}
86-
return properties
97+
func (r *EKSCluster) Properties() types.Properties {
98+
return types.NewPropertiesFromStruct(r)
99+
}
100+
101+
func (r *EKSCluster) String() string {
102+
return *r.Name
87103
}
88104

89-
func (f *EKSCluster) String() string {
90-
return *f.name
105+
func (r *EKSCluster) Settings(setting *libsettings.Setting) {
106+
r.settings = setting
91107
}

0 commit comments

Comments
 (0)