Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Find all of instance-profiles in the CI account #6137

Merged
merged 4 commits into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 31 additions & 22 deletions test/hack/resource/pkg/resourcetypes/instanceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,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 +64,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 {
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 +106,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 +137,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
}