-
Notifications
You must be signed in to change notification settings - Fork 18
/
run.go
242 lines (203 loc) · 7.24 KB
/
run.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
package aws
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"sync"
"time"
)
func RunPlecoAWS(cmd *cobra.Command, regions []string, interval int64, dryRun bool, wg *sync.WaitGroup) {
tagName, _ := cmd.Flags().GetString("tag-name")
for _, region := range regions {
// AWS session
currentSession, err := CreateSession(region)
if err != nil {
logrus.Errorf("AWS session error: %s", err)
}
wg.Add(1)
go runPlecoInRegion(cmd, region, interval, dryRun, wg, currentSession, tagName)
}
// AWS session
currentSession, err := CreateSession(regions[0])
if err != nil {
logrus.Errorf("AWS session error: %s", err)
}
wg.Add(1)
go runPlecoInGlobal(cmd, interval, dryRun, wg, currentSession, tagName)
}
func runPlecoInRegion(cmd *cobra.Command, region string, interval int64, dryRun bool, wg *sync.WaitGroup, currentSession *session.Session, tagName string) {
defer wg.Done()
logrus.Infof("Starting to check expired resources in region %s.", *currentSession.Config.Region)
var currentS3Session *s3.S3
var currentRdsSession *rds.RDS
var currentElasticacheSession *elasticache.ElastiCache
var currentEKSSession *eks.EKS
var currentElbSession *elbv2.ELBV2
var currentEC2Session *ec2.EC2
var currentCloudwatchLogsSession *cloudwatchlogs.CloudWatchLogs
var currentKMSSession *kms.KMS
var currentECRSession *ecr.ECR
elbEnabled := false
ebsEnabled := false
// S3
s3Enabled, _ := cmd.Flags().GetBool("enable-s3")
if s3Enabled {
currentS3Session = s3.New(currentSession)
}
// RDS + DocumentDB connection
rdsEnabled, _ := cmd.Flags().GetBool("enable-rds")
documentdbEnabled, _ := cmd.Flags().GetBool("enable-documentdb")
if rdsEnabled || documentdbEnabled {
currentRdsSession = RdsSession(*currentSession, region)
}
// Elasticache connection
elasticacheEnabled, _ := cmd.Flags().GetBool("enable-elasticache")
if elasticacheEnabled {
currentElasticacheSession = ElasticacheSession(*currentSession, region)
}
// EKS connection
eksEnabled, _ := cmd.Flags().GetBool("enable-eks")
if eksEnabled {
currentEKSSession = eks.New(currentSession)
currentElbSession = elbv2.New(currentSession)
elbEnabled = true
currentEC2Session = ec2.New(currentSession)
ebsEnabled = true
currentCloudwatchLogsSession = cloudwatchlogs.New(currentSession)
currentRdsSession = rds.New(currentSession)
}
// ELB connection
elbEnabled, _ = cmd.Flags().GetBool("enable-elb")
if elbEnabled {
currentEKSSession = eks.New(currentSession)
currentElbSession = elbv2.New(currentSession)
elbEnabled = true
}
// EBS connection
ebsEnabled, _ = cmd.Flags().GetBool("enable-ebs")
if ebsEnabled {
currentEKSSession = eks.New(currentSession)
currentEC2Session = ec2.New(currentSession)
ebsEnabled = true
}
// VPC
vpcEnabled, _ := cmd.Flags().GetBool("enable-vpc")
if vpcEnabled {
currentEC2Session = ec2.New(currentSession)
currentRdsSession = rds.New(currentSession)
}
// Cloudwatch
cloudwatchLogsEnabled, _ := cmd.Flags().GetBool("enable-cloudwatch-logs")
if cloudwatchLogsEnabled {
currentEKSSession = eks.New(currentSession)
currentCloudwatchLogsSession = cloudwatchlogs.New(currentSession)
}
// KMS
kmsEnabled, _ := cmd.Flags().GetBool("enable-kms")
if kmsEnabled {
currentKMSSession = kms.New(currentSession)
}
// SSH
sshKeysEnabled, _ := cmd.Flags().GetBool("enable-ssh-keys")
if sshKeysEnabled {
currentEC2Session = ec2.New(currentSession)
}
// ECR
ecrEnabled, _ := cmd.Flags().GetBool("enable-ecr")
if ecrEnabled {
currentECRSession = ecr.New(currentSession)
}
for {
// check s3
if s3Enabled {
logrus.Debugf("Listing all S3 buckets in region %s.", *currentS3Session.Config.Region)
DeleteExpiredBuckets(*currentS3Session, tagName, dryRun)
}
// check RDS
if rdsEnabled {
logrus.Debugf("Listing all RDS databases in region %s.", *currentRdsSession.Config.Region)
DeleteExpiredRDSDatabases(*currentRdsSession, tagName, dryRun)
}
// check DocumentDB
if documentdbEnabled {
logrus.Debugf("Listing all DocumentDB databases in region %s.", *currentRdsSession.Config.Region)
DeleteExpiredDocumentDBClusters(*currentRdsSession, tagName, dryRun)
}
// check Elasticache
if elasticacheEnabled {
logrus.Debugf("Listing all Elasticache databases in region %s.", *currentElasticacheSession.Config.Region)
DeleteExpiredElasticacheDatabases(*currentElasticacheSession, tagName, dryRun)
}
// check EKS
if eksEnabled {
logrus.Debugf("Listing all EKS clusters in region %s.", *currentEKSSession.Config.Region)
DeleteExpiredEKSClusters(*currentEKSSession, *currentEC2Session, *currentElbSession, *currentCloudwatchLogsSession, *currentRdsSession, tagName, dryRun)
}
// check load balancers
if elbEnabled {
logrus.Debugf("Listing all ELB load balancers in region %s.", *currentElbSession.Config.Region)
DeleteExpiredLoadBalancers(*currentEKSSession, *currentElbSession, tagName, dryRun)
}
// check EBS volumes
if ebsEnabled {
logrus.Debugf("Listing all EBS volumes in region %s.", *currentEC2Session.Config.Region)
DeleteExpiredVolumes(*currentEKSSession, *currentEC2Session, tagName, dryRun)
}
// check VPC
if vpcEnabled {
logrus.Debugf("Listing all VPC resources in region %s.", *currentEC2Session.Config.Region)
DeleteExpiredElasticIps(currentEC2Session, tagName, dryRun)
DeleteExpiredVPC(*currentEC2Session, tagName, dryRun)
}
//check Cloudwatch
if cloudwatchLogsEnabled {
logrus.Debugf("Listing all Cloudwatch logs in region %s.", *currentCloudwatchLogsSession.Config.Region)
DeleteExpiredLogs(*currentCloudwatchLogsSession, tagName, dryRun)
DeleteUnlinkedLogs(*currentCloudwatchLogsSession, *currentEKSSession, dryRun)
}
// check KMS
if kmsEnabled {
logrus.Debugf("Listing all KMS keys in region %s.", *currentKMSSession.Config.Region)
DeleteExpiredKeys(*currentKMSSession, tagName, dryRun)
}
// check SSH
if sshKeysEnabled {
logrus.Debugf("Listing all EC2 key pairs in region %s.", *currentEC2Session.Config.Region)
DeleteExpiredKeyPairs(currentEC2Session, tagName, dryRun)
}
// check ECR
if ecrEnabled {
logrus.Debugf("Listing all ECR repositories in region %s.", *currentECRSession.Config.Region)
DeleteEmptyRepositories(currentECRSession, dryRun)
}
time.Sleep(time.Duration(interval) * time.Second)
}
}
func runPlecoInGlobal(cmd *cobra.Command, interval int64, dryRun bool, wg *sync.WaitGroup, currentSession *session.Session, tagName string) {
defer wg.Done()
logrus.Info("Starting to check global expired resources.")
var currentIAMSession *iam.IAM
// IAM
iamEnabled, _ := cmd.Flags().GetBool("enable-iam")
if iamEnabled {
currentIAMSession = iam.New(currentSession)
}
for {
// check IAM
if iamEnabled {
logrus.Debug("Listing all IAM access.")
DeleteExpiredIAM(currentIAMSession, tagName, dryRun)
}
time.Sleep(time.Duration(interval) * time.Second)
}
}