in internal/deployers/eksapi/infra.go [280:326]
func (m *InfrastructureManager) deleteLeakedInstanceProfiles(infra *Infrastructure) error {
if infra.nodeRoleName == "" {
// if the infra stack failed to create, it could end up in a weird state with no node role
// we know there aren't any instance profiles in that case, so all good
return nil
}
out, err := m.clients.IAM().ListInstanceProfilesForRole(context.TODO(), &iam.ListInstanceProfilesForRoleInput{
RoleName: aws.String(infra.nodeRoleName),
})
if err != nil {
var notFound *iamtypes.NoSuchEntityException
if errors.As(err, ¬Found) {
return nil
}
return fmt.Errorf("failed to list instance profiles for role name: '%s': %v", infra.nodeRoleName, err)
} else if len(out.InstanceProfiles) > 0 {
var deletedInstanceProfiles []string
for _, instanceProfile := range out.InstanceProfiles {
_, err := m.clients.IAM().RemoveRoleFromInstanceProfile(context.TODO(), &iam.RemoveRoleFromInstanceProfileInput{
RoleName: aws.String(infra.nodeRoleName),
InstanceProfileName: instanceProfile.InstanceProfileName,
})
if err != nil {
var notFound *iamtypes.NoSuchEntityException
if errors.As(err, ¬Found) {
klog.Infof("instance profile does not exist: %s", aws.ToString(instanceProfile.InstanceProfileName))
continue
}
return fmt.Errorf("failed to remove node role %s from instance profile: %s: %v", infra.nodeRoleName, aws.ToString(instanceProfile.InstanceProfileName), err)
}
_, err = m.clients.IAM().DeleteInstanceProfile(context.TODO(), &iam.DeleteInstanceProfileInput{
InstanceProfileName: instanceProfile.InstanceProfileName,
})
if err != nil {
var notFound *iamtypes.NoSuchEntityException
if errors.As(err, ¬Found) {
klog.Infof("instance profile does not exist: %s", aws.ToString(instanceProfile.InstanceProfileName))
continue
}
return fmt.Errorf("failed to delete instance profile: %s: %v", aws.ToString(instanceProfile.InstanceProfileName), err)
}
deletedInstanceProfiles = append(deletedInstanceProfiles, aws.ToString(instanceProfile.InstanceProfileName))
}
klog.Infof("deleted %d leaked instance profile(s): %v", len(deletedInstanceProfiles), deletedInstanceProfiles)
}
return nil
}