From 2a8f6b7d8e55a98bafedc5843621dca67cabf320 Mon Sep 17 00:00:00 2001 From: Amanuel Engeda Date: Fri, 1 Dec 2023 01:58:34 -0800 Subject: [PATCH] Adjust region for metrics of resource counter --- test/hack/resource/clean/main.go | 23 +++++++++++-------- test/hack/resource/count/main.go | 18 +++++++-------- test/hack/resource/pkg/resourcetypes/eni.go | 9 ++++++-- .../resource/pkg/resourcetypes/instance.go | 9 ++++++-- .../pkg/resourcetypes/instanceprofile.go | 9 ++++++-- .../pkg/resourcetypes/launchtemplate.go | 9 ++++++-- test/hack/resource/pkg/resourcetypes/oidc.go | 9 ++++++-- .../pkg/resourcetypes/resourcetypes.go | 2 ++ .../pkg/resourcetypes/securitygroup.go | 9 ++++++-- test/hack/resource/pkg/resourcetypes/stack.go | 9 ++++++-- .../pkg/resourcetypes/vpc_endpoint.go | 9 ++++++-- 11 files changed, 80 insertions(+), 35 deletions(-) diff --git a/test/hack/resource/clean/main.go b/test/hack/resource/clean/main.go index c5c6b32854c0..a459c785ede8 100644 --- a/test/hack/resource/clean/main.go +++ b/test/hack/resource/clean/main.go @@ -59,14 +59,14 @@ func main() { // 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.NewInstance(ec2Client, cfg.Region), + resourcetypes.NewVPCEndpoint(ec2Client, cfg.Region), + resourcetypes.NewENI(ec2Client, cfg.Region), + resourcetypes.NewSecurityGroup(ec2Client, cfg.Region), + resourcetypes.NewLaunchTemplate(ec2Client, cfg.Region), + resourcetypes.NewOIDC(iamClient, "global"), + resourcetypes.NewInstanceProfile(iamClient, "global"), + resourcetypes.NewStack(cloudFormationClient, cfg.Region), } for i := range resourceTypes { @@ -87,8 +87,11 @@ func main() { if err != nil { resourceLogger.Errorf("%v", err) } - if err = metricsClient.FireMetric(ctx, sweeperCleanedResourcesTableName, fmt.Sprintf("%sDeleted", resourceTypes[i].String()), float64(len(cleaned)), cfg.Region); err != nil { - resourceLogger.Errorf("%v", err) + // Should only fire metrics if the resource have expired + if clusterName == "" { + if err = metricsClient.FireMetric(ctx, sweeperCleanedResourcesTableName, fmt.Sprintf("%sDeleted", resourceTypes[i].String()), float64(len(cleaned)), resourceTypes[i].Region()); err != nil { + resourceLogger.Errorf("%v", err) + } } resourceLogger.With("ids", cleaned, "count", len(cleaned)).Infof("deleted resourceTypes") } diff --git a/test/hack/resource/count/main.go b/test/hack/resource/count/main.go index 1213a81c8b64..264cce74d300 100644 --- a/test/hack/resource/count/main.go +++ b/test/hack/resource/count/main.go @@ -42,14 +42,14 @@ func main() { metricsClient := metrics.Client(metrics.NewTimeStream(cfg)) 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.NewInstance(ec2Client, cfg.Region), + resourcetypes.NewVPCEndpoint(ec2Client, cfg.Region), + resourcetypes.NewENI(ec2Client, cfg.Region), + resourcetypes.NewSecurityGroup(ec2Client, cfg.Region), + resourcetypes.NewLaunchTemplate(ec2Client, cfg.Region), + resourcetypes.NewOIDC(iamClient, "global"), + resourcetypes.NewInstanceProfile(iamClient, "global"), + resourcetypes.NewStack(cloudFormationClient, cfg.Region), } for i := range resourceTypes { @@ -59,7 +59,7 @@ func main() { resourceLogger.Errorf("%v", err) } - if err = metricsClient.FireMetric(ctx, resourceCountTableName, resourceTypes[i].String(), float64(resourceCount), cfg.Region); err != nil { + if err = metricsClient.FireMetric(ctx, resourceCountTableName, resourceTypes[i].String(), float64(resourceCount), resourceTypes[i].Region()); err != nil { resourceLogger.Errorf("%v", err) } resourceLogger.With("count", resourceCount).Infof("counted resourceTypes") diff --git a/test/hack/resource/pkg/resourcetypes/eni.go b/test/hack/resource/pkg/resourcetypes/eni.go index a81450b0facf..2ad21e81519e 100644 --- a/test/hack/resource/pkg/resourcetypes/eni.go +++ b/test/hack/resource/pkg/resourcetypes/eni.go @@ -26,17 +26,22 @@ import ( ) type ENI struct { + region string ec2Client *ec2.Client } -func NewENI(ec2Client *ec2.Client) *ENI { - return &ENI{ec2Client: ec2Client} +func NewENI(ec2Client *ec2.Client, region string) *ENI { + return &ENI{ec2Client: ec2Client, region: region} } func (e *ENI) String() string { return "ElasticNetworkInterface" } +func (e *ENI) Region() string { + return e.region +} + func (e *ENI) GetExpired(ctx context.Context, expirationTime time.Time) (ids []string, err error) { var nextToken *string for { diff --git a/test/hack/resource/pkg/resourcetypes/instance.go b/test/hack/resource/pkg/resourcetypes/instance.go index d869d71c3dd6..baaed49618d5 100644 --- a/test/hack/resource/pkg/resourcetypes/instance.go +++ b/test/hack/resource/pkg/resourcetypes/instance.go @@ -24,17 +24,22 @@ import ( ) type Instance struct { + region string ec2Client *ec2.Client } -func NewInstance(ec2Client *ec2.Client) *Instance { - return &Instance{ec2Client: ec2Client} +func NewInstance(ec2Client *ec2.Client, region string) *Instance { + return &Instance{ec2Client: ec2Client, region: region} } func (i *Instance) String() string { return "Instances" } +func (i *Instance) Region() string { + return i.region +} + func (i *Instance) GetExpired(ctx context.Context, expirationTime time.Time) (ids []string, err error) { var nextToken *string for { diff --git a/test/hack/resource/pkg/resourcetypes/instanceprofile.go b/test/hack/resource/pkg/resourcetypes/instanceprofile.go index 23fff1a5a4f3..a20555dffe92 100644 --- a/test/hack/resource/pkg/resourcetypes/instanceprofile.go +++ b/test/hack/resource/pkg/resourcetypes/instanceprofile.go @@ -24,17 +24,22 @@ import ( ) type InstanceProfile struct { + region string iamClient *iam.Client } -func NewInstanceProfile(iamClient *iam.Client) *InstanceProfile { - return &InstanceProfile{iamClient: iamClient} +func NewInstanceProfile(iamClient *iam.Client, region string) *InstanceProfile { + return &InstanceProfile{iamClient: iamClient, region: region} } func (ip *InstanceProfile) String() string { return "InstanceProfile" } +func (ip *InstanceProfile) Region() string { + return ip.region +} + func (ip *InstanceProfile) GetExpired(ctx context.Context, expirationTime time.Time) (names []string, err error) { out, err := ip.iamClient.ListInstanceProfiles(ctx, &iam.ListInstanceProfilesInput{}) if err != nil { diff --git a/test/hack/resource/pkg/resourcetypes/launchtemplate.go b/test/hack/resource/pkg/resourcetypes/launchtemplate.go index 38dc681eef4f..589fc07c7363 100644 --- a/test/hack/resource/pkg/resourcetypes/launchtemplate.go +++ b/test/hack/resource/pkg/resourcetypes/launchtemplate.go @@ -25,17 +25,22 @@ import ( ) type LaunchTemplate struct { + region string ec2Client *ec2.Client } -func NewLaunchTemplate(ec2Client *ec2.Client) *LaunchTemplate { - return &LaunchTemplate{ec2Client: ec2Client} +func NewLaunchTemplate(ec2Client *ec2.Client, region string) *LaunchTemplate { + return &LaunchTemplate{ec2Client: ec2Client, region: region} } func (lt *LaunchTemplate) String() string { return "LaunchTemplates" } +func (lt *LaunchTemplate) Region() string { + return lt.region +} + func (lt *LaunchTemplate) GetExpired(ctx context.Context, expirationTime time.Time) (names []string, err error) { var nextToken *string for { diff --git a/test/hack/resource/pkg/resourcetypes/oidc.go b/test/hack/resource/pkg/resourcetypes/oidc.go index def8e163db84..4b449c9f3666 100644 --- a/test/hack/resource/pkg/resourcetypes/oidc.go +++ b/test/hack/resource/pkg/resourcetypes/oidc.go @@ -24,17 +24,22 @@ import ( ) type OIDC struct { + region string iamClient *iam.Client } -func NewOIDC(iamClient *iam.Client) *OIDC { - return &OIDC{iamClient: iamClient} +func NewOIDC(iamClient *iam.Client, region string) *OIDC { + return &OIDC{iamClient: iamClient, region: region} } func (o *OIDC) String() string { return "OpenIDConnectProvider" } +func (o *OIDC) Region() string { + return o.region +} + func (o *OIDC) GetExpired(ctx context.Context, expirationTime time.Time) (names []string, err error) { out, err := o.iamClient.ListOpenIDConnectProviders(ctx, &iam.ListOpenIDConnectProvidersInput{}) if err != nil { diff --git a/test/hack/resource/pkg/resourcetypes/resourcetypes.go b/test/hack/resource/pkg/resourcetypes/resourcetypes.go index 58b47e3c61dc..21dcdf47e3ab 100644 --- a/test/hack/resource/pkg/resourcetypes/resourcetypes.go +++ b/test/hack/resource/pkg/resourcetypes/resourcetypes.go @@ -34,6 +34,8 @@ const ( type Type interface { // String is the string representation of the type String() string + // Region returns the region the resource is located + Region() string // Get returns all resources of the type associated with the clusterName Get(ctx context.Context, clusterName string) (ids []string, err error) // GetExpired returns all resources of the type that were provisioned before the expirationTime diff --git a/test/hack/resource/pkg/resourcetypes/securitygroup.go b/test/hack/resource/pkg/resourcetypes/securitygroup.go index eac9c902b88a..cd908f26c2b2 100644 --- a/test/hack/resource/pkg/resourcetypes/securitygroup.go +++ b/test/hack/resource/pkg/resourcetypes/securitygroup.go @@ -26,17 +26,22 @@ import ( ) type SecurityGroup struct { + region string ec2Client *ec2.Client } -func NewSecurityGroup(ec2Client *ec2.Client) *SecurityGroup { - return &SecurityGroup{ec2Client: ec2Client} +func NewSecurityGroup(ec2Client *ec2.Client, region string) *SecurityGroup { + return &SecurityGroup{ec2Client: ec2Client, region: region} } func (sg *SecurityGroup) String() string { return "SecurityGroup" } +func (sg *SecurityGroup) Region() string { + return sg.region +} + func (sg *SecurityGroup) GetExpired(ctx context.Context, expirationTime time.Time) (ids []string, err error) { var nextToken *string for { diff --git a/test/hack/resource/pkg/resourcetypes/stack.go b/test/hack/resource/pkg/resourcetypes/stack.go index 2aad555913a4..6615391b61dd 100644 --- a/test/hack/resource/pkg/resourcetypes/stack.go +++ b/test/hack/resource/pkg/resourcetypes/stack.go @@ -25,17 +25,22 @@ import ( ) type Stack struct { + region string cloudFormationClient *cloudformation.Client } -func NewStack(cloudFormationClient *cloudformation.Client) *Stack { - return &Stack{cloudFormationClient: cloudFormationClient} +func NewStack(cloudFormationClient *cloudformation.Client, region string) *Stack { + return &Stack{cloudFormationClient: cloudFormationClient, region: region} } func (s *Stack) String() string { return "CloudformationStacks" } +func (s *Stack) Region() string { + return s.region +} + func (s *Stack) GetExpired(ctx context.Context, expirationTime time.Time) (names []string, err error) { var nextToken *string for { diff --git a/test/hack/resource/pkg/resourcetypes/vpc_endpoint.go b/test/hack/resource/pkg/resourcetypes/vpc_endpoint.go index 380eba791996..1e11c0922a7e 100644 --- a/test/hack/resource/pkg/resourcetypes/vpc_endpoint.go +++ b/test/hack/resource/pkg/resourcetypes/vpc_endpoint.go @@ -24,17 +24,22 @@ import ( ) type VPCEndpoint struct { + region string ec2Client *ec2.Client } -func NewVPCEndpoint(ec2Client *ec2.Client) *VPCEndpoint { - return &VPCEndpoint{ec2Client: ec2Client} +func NewVPCEndpoint(ec2Client *ec2.Client, region string) *VPCEndpoint { + return &VPCEndpoint{ec2Client: ec2Client, region: region} } func (v *VPCEndpoint) String() string { return "VPCEndpoints" } +func (v *VPCEndpoint) Region() string { + return v.region +} + func (v *VPCEndpoint) Get(ctx context.Context, clusterName string) (ids []string, err error) { var nextToken *string for {