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

Cleanup InstanceProfile only that have ownership tags in delete cluster #11568

Merged
merged 1 commit into from
May 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions pkg/resources/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2021,23 +2021,35 @@ func DeleteIAMInstanceProfile(cloud fi.Cloud, r *resources.Resource) error {
func ListIAMInstanceProfiles(cloud fi.Cloud, clusterName string) ([]*resources.Resource, error) {
c := cloud.(awsup.AWSCloud)

remove := make(map[string]bool)
remove["masters."+clusterName] = true
remove["nodes."+clusterName] = true
remove["bastions."+clusterName] = true

var getProfileErr error
var profiles []*iam.InstanceProfile
ownershipTag := "kubernetes.io/cluster/" + clusterName

request := &iam.ListInstanceProfilesInput{}
err := c.IAM().ListInstanceProfilesPages(request, func(p *iam.ListInstanceProfilesOutput, lastPage bool) bool {
for _, p := range p.InstanceProfiles {
name := aws.StringValue(p.InstanceProfileName)
if remove[name] {
profiles = append(profiles, p)
if !strings.HasSuffix(name, "."+clusterName) {
continue
}

getRequest := &iam.GetInstanceProfileInput{InstanceProfileName: p.InstanceProfileName}
profileOutput, err := c.IAM().GetInstanceProfile(getRequest)
if err != nil {
getProfileErr = fmt.Errorf("calling IAM GetInstanceProfile on %s: %w", name, err)
return false
}
for _, tag := range profileOutput.InstanceProfile.Tags {
if fi.StringValue(tag.Key) == ownershipTag && fi.StringValue(tag.Value) == "owned" {
profiles = append(profiles, p)
}
}
}
return true
})
if getProfileErr != nil {
return nil, getProfileErr
}
if err != nil {
return nil, fmt.Errorf("error listing IAM instance profiles: %v", err)
}
Expand Down