in internal/resources/providers/awslib/cluster_name_provider.go [106:147]
func (provider EKSClusterNameProvider) getClusterNameFromAutoscalingGroup(ctx context.Context, cfg aws.Config, instanceId string) (string, error) {
svc := autoscaling.NewFromConfig(cfg)
input := &autoscaling.DescribeAutoScalingGroupsInput{}
for {
r, err := svc.DescribeAutoScalingGroups(ctx, input)
if err != nil {
klog.Errorf("ec2 describe-autoscaling-group: %v", err)
return "", err
}
// ClusterName will be found in the autoscaling group tag with "owned" value.
// Find the autoscaling group that has this instance, then get the cluster name from the tag of that group
// We wish to limit the search and preform a best effort search, therefore we will limit the number of iterations
for scalingGroupNumber, autoscalingGroup := range r.AutoScalingGroups {
if scalingGroupNumber > numberOfAutoScalingGroups {
break
}
for numberOfInstance, instance := range autoscalingGroup.Instances {
if numberOfInstance > numberOfIterationsInEachAutoScalingGroup {
break
}
if *instance.InstanceId == instanceId {
for _, tag := range autoscalingGroup.Tags {
stringifyTag := *tag.Key
if *tag.Value == "owned" && asgCompiledRegex.MatchString(stringifyTag) {
groups := asgCompiledRegex.FindStringSubmatch(stringifyTag)
clusterName := groups[1]
return clusterName, nil
}
}
}
}
}
if r.NextToken == nil {
break
}
input.NextToken = r.NextToken
}
return "", errors.New("cluster name not found from autoscaling groups")
}