in pkg/monitor/sqsevent/sqs-monitor.go [242:314]
func (m SQSMonitor) getNodeInfo(instanceID string) (*NodeInfo, error) {
result, err := m.EC2.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: []*string{
aws.String(instanceID),
},
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "InvalidInstanceID.NotFound" {
log.Warn().Msgf("No instance found with instance-id %s", instanceID)
return nil, ErrNodeStateNotRunning
}
return nil, err
}
if len(result.Reservations) == 0 || len(result.Reservations[0].Instances) == 0 {
log.Warn().Msgf("No instance found with instance-id %s", instanceID)
return nil, ErrNodeStateNotRunning
}
instance := result.Reservations[0].Instances[0]
instanceJSON, _ := json.MarshalIndent(*instance, " ", " ")
log.Debug().Msgf("Got instance data from ec2 describe call: %s", instanceJSON)
if *instance.PrivateDnsName == "" {
state := "unknown"
// safe access instance.State potentially being nil
if instance.State != nil {
state = *instance.State.Name
}
// anything except running might not contain PrivateDnsName
if state != ec2.InstanceStateNameRunning {
return nil, fmt.Errorf("node: '%s' in state '%s': %w", instanceID, state, ErrNodeStateNotRunning)
}
return nil, fmt.Errorf("unable to retrieve PrivateDnsName name for '%s' in state '%s'", instanceID, state)
}
nodeInfo := &NodeInfo{
Name: *instance.PrivateDnsName,
InstanceID: instanceID,
Tags: make(map[string]string),
IsManaged: true,
}
for _, t := range (*instance).Tags {
nodeInfo.Tags[*t.Key] = *t.Value
if *t.Key == ASGTagName {
nodeInfo.AsgName = *t.Value
}
}
if nodeInfo.AsgName == "" && !m.AssumeAsgTagPropagation {
// If ASG tags are not propagated we might need to use the API
// to retrieve the ASG name
nodeInfo.AsgName, err = m.retrieveAutoScalingGroupName(nodeInfo.InstanceID)
if err != nil {
return nil, fmt.Errorf("unable to retrieve AutoScaling group: %w", err)
}
}
if m.CheckIfManaged && nodeInfo.Tags[m.ManagedAsgTag] == "" {
if m.AssumeAsgTagPropagation {
nodeInfo.IsManaged = false
} else {
// if ASG tags are not propagated we might have to check the ASG directly
nodeInfo.IsManaged, err = m.isASGManaged(nodeInfo.AsgName, nodeInfo.InstanceID)
if err != nil {
return nil, err
}
}
}
infoJSON, _ := json.MarshalIndent(nodeInfo, " ", " ")
log.Debug().Msgf("Got node info from AWS: %s", infoJSON)
return nodeInfo, nil
}