Skip to content

Commit

Permalink
use-paginated-call
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam committed May 24, 2024
1 parent c6dec4a commit 5460005
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions test/hack/resource/pkg/resourcetypes/instanceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package resourcetypes

import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go-v2/config"
Expand Down Expand Up @@ -44,15 +45,15 @@ func (ip *InstanceProfile) Global() bool {
}

func (ip *InstanceProfile) GetExpired(ctx context.Context, expirationTime time.Time, excludedClusters []string) (names []string, err error) {
out, err := ip.iamClient.ListInstanceProfiles(ctx, &iam.ListInstanceProfilesInput{})
instanceProfiles, err := ip.getAllInstanceProfiles(ctx)
if err != nil {
return names, err
}

errs := make([]error, len(out.InstanceProfiles))
for i := range out.InstanceProfiles {
errs := make([]error, len(instanceProfiles))
for i := range instanceProfiles {
profiles, err := ip.iamClient.ListInstanceProfileTags(ctx, &iam.ListInstanceProfileTagsInput{
InstanceProfileName: out.InstanceProfiles[i].InstanceProfileName,
InstanceProfileName: instanceProfiles[i].InstanceProfileName,
})
if err != nil {
errs[i] = err
Expand All @@ -64,45 +65,40 @@ func (ip *InstanceProfile) GetExpired(ctx context.Context, expirationTime time.T
})
// Checking to make sure we are only list resources in the given region
region, _ := lo.Find(profiles.Tags, func(tag iamtypes.Tag) bool {
return lo.FromPtr(tag.Key) == v1.LabelTopologyZone
return lo.FromPtr(tag.Key) == v1.LabelTopologyRegion
})

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

for _, t := range profiles.Tags {
// Since we can only get the date of the instance profile (not the exact time the instance profile was created)
// we add a day to the time that it was created to account for the worst-case of the instance profile being created
// at 23:59:59 and being marked with a time of 00:00:00 due to only capturing the date and not the time
if lo.FromPtr(t.Key) == karpenterTestingTag && out.InstanceProfiles[i].CreateDate.Add(time.Hour*24).Before(expirationTime) {
names = append(names, lo.FromPtr(out.InstanceProfiles[i].InstanceProfileName))
}
// Since we can only get the date of the instance profile (not the exact time the instance profile was created)
// we add a day to the time that it was created to account for the worst-case of the instance profile being created
// at 23:59:59 and being marked with a time of 00:00:00 due to only capturing the date and not the time
if lo.FromPtr(clusterName.Value) != "" && instanceProfiles[i].CreateDate.Add(time.Hour * 24).Before(expirationTime) {
names = append(names, lo.FromPtr(instanceProfiles[i].InstanceProfileName))
}
}

return names, multierr.Combine(errs...)
}

func (ip *InstanceProfile) CountAll(ctx context.Context) (count int, err error) {
out, err := ip.iamClient.ListInstanceProfiles(ctx, &iam.ListInstanceProfilesInput{})
instanceProfiles, err := ip.getAllInstanceProfiles(ctx)
if err != nil {
return count, err
}

return len(out.InstanceProfiles), nil
return len(instanceProfiles), err
}

func (ip *InstanceProfile) Get(ctx context.Context, clusterName string) (names []string, err error) {
out, err := ip.iamClient.ListInstanceProfiles(ctx, &iam.ListInstanceProfilesInput{})
instanceProfiles, err := ip.getAllInstanceProfiles(ctx)
if err != nil {
return names, err
}

errs := make([]error, len(out.InstanceProfiles))
for i := range out.InstanceProfiles {
errs := make([]error, len(instanceProfiles))
for i := range instanceProfiles {
profiles, err := ip.iamClient.ListInstanceProfileTags(ctx, &iam.ListInstanceProfileTagsInput{
InstanceProfileName: out.InstanceProfiles[i].InstanceProfileName,
InstanceProfileName: instanceProfiles[i].InstanceProfileName,
})
if err != nil {
errs[i] = err
Expand All @@ -111,7 +107,7 @@ func (ip *InstanceProfile) Get(ctx context.Context, clusterName string) (names [

for _, t := range profiles.Tags {
if lo.FromPtr(t.Key) == karpenterTestingTag && lo.FromPtr(t.Value) == clusterName {
names = append(names, lo.FromPtr(out.InstanceProfiles[i].InstanceProfileName))
names = append(names, lo.FromPtr(instanceProfiles[i].InstanceProfileName))
}
}
}
Expand Down Expand Up @@ -142,3 +138,17 @@ func (ip *InstanceProfile) Cleanup(ctx context.Context, names []string) ([]strin
}
return deleted, errs
}

func (ip *InstanceProfile) getAllInstanceProfiles(ctx context.Context) (instanceprofiles []iamtypes.InstanceProfile, err error) {
paginator := iam.NewListInstanceProfilesPaginator(ip.iamClient, &iam.ListInstanceProfilesInput{})

for paginator.HasMorePages() {
out, err := paginator.NextPage(ctx)
if err != nil {
return instanceprofiles, err
}
instanceprofiles = append(instanceprofiles, out.InstanceProfiles...)
}

return instanceprofiles, nil
}

0 comments on commit 5460005

Please sign in to comment.