-
Notifications
You must be signed in to change notification settings - Fork 833
/
main.go
109 lines (92 loc) · 3.9 KB
/
main.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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/samber/lo"
"go.uber.org/zap"
"golang.org/x/exp/slices"
"github.com/aws/karpenter-provider-aws/test/hack/resource/pkg/metrics"
"github.com/aws/karpenter-provider-aws/test/hack/resource/pkg/resourcetypes"
)
const sweeperCleanedResourcesTableName = "sweeperCleanedResources"
var excludedClusters = []string{
"soak-periodic-2785632730",
}
func main() {
expiration := flag.String("expiration", "12h", "define the expirationTTL of the resources")
clusterName := flag.String("cluster-name", "", "define cluster name to cleanup")
flag.Parse()
ctx := context.Background()
cfg := lo.Must(config.LoadDefaultConfig(ctx))
logger := lo.Must(zap.NewProduction()).Sugar()
expirationTTL, err := time.ParseDuration(lo.FromPtr(expiration))
if err != nil {
logger.Fatalln("need a valid expiration duration", err)
}
expirationTime := time.Now().Add(-expirationTTL)
logger.With("expiration-time", expirationTime.String()).Infof("resolved expiration time for all resourceTypes")
ec2Client := ec2.NewFromConfig(cfg)
cloudFormationClient := cloudformation.NewFromConfig(cfg)
iamClient := iam.NewFromConfig(cfg)
metricsClient := metrics.Client(metrics.NewTimeStream(cfg))
// These resources are intentionally ordered so that instances that are using ENIs
// will be cleaned before ENIs are attempted to be cleaned up. Likewise, instances and ENIs
// are cleaned up before security groups are cleaned up to ensure that everything is detached and doesn't
// prevent deletion
resourceTypes := []resourcetypes.Type{
resourcetypes.NewInstance(ec2Client),
resourcetypes.NewVPCEndpoint(ec2Client),
resourcetypes.NewENI(ec2Client),
resourcetypes.NewSecurityGroup(ec2Client),
resourcetypes.NewLaunchTemplate(ec2Client),
resourcetypes.NewOIDC(iamClient),
resourcetypes.NewInstanceProfile(iamClient),
resourcetypes.NewStack(cloudFormationClient),
resourcetypes.NewVPCPeeringConnection(ec2Client),
}
for i := range resourceTypes {
resourceLogger := logger.With("type", resourceTypes[i].String())
var ids []string
var err error
// If there's no cluster defined, clean up all expired resources. otherwise, only cleanup the resources associated with the cluster
if lo.FromPtr(clusterName) == "" {
ids, err = resourceTypes[i].GetExpired(ctx, expirationTime, excludedClusters)
} else if !slices.Contains(excludedClusters, lo.FromPtr(clusterName)) {
ids, err = resourceTypes[i].Get(ctx, lo.FromPtr(clusterName))
}
if err != nil {
resourceLogger.Errorf("%v", err)
}
resourceLogger.With("ids", ids, "count", len(ids)).Infof("discovered resourceTypes")
if len(ids) > 0 {
cleaned, err := resourceTypes[i].Cleanup(ctx, ids)
if err != nil {
resourceLogger.Errorf("%v", err)
}
// Should only fire metrics if the resource have expired
if lo.FromPtr(clusterName) == "" {
if err = metricsClient.FireMetric(ctx, sweeperCleanedResourcesTableName, fmt.Sprintf("%sDeleted", resourceTypes[i].String()), float64(len(cleaned)), lo.Ternary(resourceTypes[i].Global(), "global", cfg.Region)); err != nil {
resourceLogger.Errorf("%v", err)
}
}
resourceLogger.With("ids", cleaned, "count", len(cleaned)).Infof("deleted resourceTypes")
}
}
}