Skip to content

Commit

Permalink
fix clean-up for instance profile
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam committed Mar 11, 2024
1 parent cdde4e2 commit 6d60c2e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
18 changes: 10 additions & 8 deletions test/hack/resource/pkg/resourcetypes/instanceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package resourcetypes

import (
"context"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/config"
Expand Down Expand Up @@ -51,10 +50,6 @@ func (ip *InstanceProfile) GetExpired(ctx context.Context, expirationTime time.T

errs := make([]error, len(out.InstanceProfiles))
for i := range out.InstanceProfiles {
// Checking to make sure we are only list resources in the given region
if !strings.Contains(lo.FromPtr(out.InstanceProfiles[i].Arn), lo.Must(config.LoadDefaultConfig(ctx)).Region) {
continue
}
profiles, err := ip.iamClient.ListInstanceProfileTags(ctx, &iam.ListInstanceProfileTagsInput{
InstanceProfileName: out.InstanceProfiles[i].InstanceProfileName,
})
Expand All @@ -63,10 +58,16 @@ func (ip *InstanceProfile) GetExpired(ctx context.Context, expirationTime time.T
continue
}

clusterName, found := lo.Find(out.InstanceProfiles[i].Tags, func(tag iamtypes.Tag) bool {
return *tag.Key == k8sClusterTag
clusterName, foundClusterName := lo.Find(profiles.Tags, func(tag iamtypes.Tag) bool {
return lo.FromPtr(tag.Key) == karpenterTestingTag
})
if found && slices.Contains(excludedClusters, lo.FromPtr(clusterName.Value)) {
// Checking to make sure we are only list resources in the given region
region, foundRegion := lo.Find(profiles.Tags, func(tag iamtypes.Tag) bool {
return lo.FromPtr(tag.Key) == resourceRegionTag
})

if foundClusterName && slices.Contains(excludedClusters, lo.FromPtr(clusterName.Value)) ||
(!foundRegion || lo.FromPtr(region.Value) != lo.Must(config.LoadDefaultConfig(ctx)).Region) {
continue
}

Expand Down Expand Up @@ -137,6 +138,7 @@ func (ip *InstanceProfile) Cleanup(ctx context.Context, names []string) ([]strin
if err != nil {
errs = multierr.Append(errs, err)
}
deleted = append(deleted, names[i])
}
return deleted, errs
}
1 change: 1 addition & 0 deletions test/hack/resource/pkg/resourcetypes/resourcetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
karpenterTestingTag = "testing/cluster"
k8sClusterTag = "cluster.k8s.amazonaws.com/name"
githubRunURLTag = "github.com/run-url"
resourceRegionTag = "topology.kubernetes.io/region"
)

// Type is a resource type that can be cleaned through a cluster clean-up operation
Expand Down

0 comments on commit 6d60c2e

Please sign in to comment.