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 4, 2024
1 parent 6e36c24 commit 3b6bc84
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions test/hack/resource/pkg/resourcetypes/instanceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,40 @@ 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
continue
}

clusterName, _ := lo.Find(profiles.Tags, func(tag iamtypes.Tag) bool {
clusterName, foundClusterName := lo.Find(profiles.Tags, func(tag iamtypes.Tag) bool {
return lo.FromPtr(tag.Key) == karpenterTestingTag
})
// 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
region, foundRegion := lo.Find(profiles.Tags, func(tag iamtypes.Tag) bool {
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 (foundClusterName && slices.Contains(excludedClusters, lo.FromPtr(clusterName.Value))) || (foundRegion && 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))
if lo.FromPtr(t.Key) == karpenterTestingTag && instanceProfiles[i].CreateDate.Add(time.Hour*24).Before(expirationTime) {
names = append(names, lo.FromPtr(instanceProfiles[i].InstanceProfileName))
break
}
}
}
Expand All @@ -85,24 +86,23 @@ func (ip *InstanceProfile) GetExpired(ctx context.Context, expirationTime time.T
}

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 +111,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 +142,24 @@ func (ip *InstanceProfile) Cleanup(ctx context.Context, names []string) ([]strin
}
return deleted, errs
}

func (ip *InstanceProfile) getAllInstanceProfiles(ctx context.Context) (instanceprofile []iamtypes.InstanceProfile, err error) {
var nextToken *string
for {
out, err := ip.iamClient.ListInstanceProfiles(ctx, &iam.ListInstanceProfilesInput{
Marker: nextToken,
})
if err != nil {
return instanceprofile, err
}

instanceprofile = append(instanceprofile, out.InstanceProfiles...)

nextToken = out.Marker
if nextToken == nil {
break
}
}

return instanceprofile, nil
}

0 comments on commit 3b6bc84

Please sign in to comment.