Skip to content

Commit

Permalink
Adjust region for metrics of resource counter
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam committed Dec 1, 2023
1 parent 4ea4808 commit 2a8f6b7
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 35 deletions.
23 changes: 13 additions & 10 deletions test/hack/resource/clean/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down
18 changes: 9 additions & 9 deletions test/hack/resource/count/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
9 changes: 7 additions & 2 deletions test/hack/resource/pkg/resourcetypes/eni.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions test/hack/resource/pkg/resourcetypes/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions test/hack/resource/pkg/resourcetypes/instanceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions test/hack/resource/pkg/resourcetypes/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions test/hack/resource/pkg/resourcetypes/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions test/hack/resource/pkg/resourcetypes/resourcetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions test/hack/resource/pkg/resourcetypes/securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions test/hack/resource/pkg/resourcetypes/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions test/hack/resource/pkg/resourcetypes/vpc_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2a8f6b7

Please sign in to comment.