From b4f3eaa0c67716d320d2aa93001b845a136fc16f Mon Sep 17 00:00:00 2001 From: Lisa Guo Date: Thu, 1 Feb 2024 13:34:26 -0500 Subject: [PATCH] Add list of cluster names to clean_eks script (#1027) --- tool/clean/clean_eks/clean_eks.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tool/clean/clean_eks/clean_eks.go b/tool/clean/clean_eks/clean_eks.go index 2e02bf18e8..6685ce403b 100644 --- a/tool/clean/clean_eks/clean_eks.go +++ b/tool/clean/clean_eks/clean_eks.go @@ -16,6 +16,13 @@ import ( "github.com/aws/amazon-cloudwatch-agent/tool/clean" ) +var ( + ClustersToClean = []string{ + "cwagent-eks-integ-", + "cwagent-operator-helm-integ-", + } +) + // Clean eks clusters if they have been open longer than 7 day func main() { err := cleanCluster() @@ -37,6 +44,15 @@ func cleanCluster() error { return nil } +func clusterNameMatchesClustersToClean(clusterName string, clustersToClean []string) bool { + for _, clusterToClean := range clustersToClean { + if strings.HasPrefix(clusterName, clusterToClean) { + return true + } + } + return false +} + func terminateClusters(ctx context.Context, client *eks.Client) { listClusterInput := eks.ListClustersInput{} expirationDateCluster := time.Now().UTC().Add(clean.KeepDurationOneWeek) @@ -50,7 +66,7 @@ func terminateClusters(ctx context.Context, client *eks.Client) { if err != nil { return } - if expirationDateCluster.After(*describeClusterOutput.Cluster.CreatedAt) && strings.HasPrefix(*describeClusterOutput.Cluster.Name, "cwagent-eks-integ-") { + if expirationDateCluster.After(*describeClusterOutput.Cluster.CreatedAt) && clusterNameMatchesClustersToClean(*describeClusterOutput.Cluster.Name, ClustersToClean) { log.Printf("Try to delete cluster %s launch-date %s", cluster, *describeClusterOutput.Cluster.CreatedAt) describeNodegroupInput := eks.ListNodegroupsInput{ClusterName: aws.String(cluster)} nodeGroupOutput, err := client.ListNodegroups(ctx, &describeNodegroupInput)