@@ -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
2934type 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
6670type 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