diff --git a/test/hack/resource/pkg/resourcetypes/instanceprofile.go b/test/hack/resource/pkg/resourcetypes/instanceprofile.go index 306eb2390b9b..6f722e614b7d 100644 --- a/test/hack/resource/pkg/resourcetypes/instanceprofile.go +++ b/test/hack/resource/pkg/resourcetypes/instanceprofile.go @@ -16,7 +16,6 @@ package resourcetypes import ( "context" - "strings" "time" "github.com/aws/aws-sdk-go-v2/config" @@ -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, }) @@ -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 } @@ -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 } diff --git a/test/hack/resource/pkg/resourcetypes/resourcetypes.go b/test/hack/resource/pkg/resourcetypes/resourcetypes.go index ce8a7bfd9441..b784ab425d32 100644 --- a/test/hack/resource/pkg/resourcetypes/resourcetypes.go +++ b/test/hack/resource/pkg/resourcetypes/resourcetypes.go @@ -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