func()

in pkg/monitor/sqsevent/sqs-monitor.go [355:439]


func (m SQSMonitor) getNodeInfo(instanceID string) (*NodeInfo, error) {
	result, err := m.EC2.DescribeInstances(&ec2.DescribeInstancesInput{
		InstanceIds: []*string{
			aws.String(instanceID),
		},
	})
	if err != nil {
		// handle all kinds of InvalidInstanceID error events
		// - https://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html
		if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "InvalidInstanceID" {
			msg := fmt.Sprintf("Invalid instance id %s provided", instanceID)
			log.Warn().Msg(msg)
			return nil, skip{fmt.Errorf("%s", msg)}
		}
		if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "InvalidInstanceID.NotFound" {
			msg := fmt.Sprintf("No instance found with instance-id %s", instanceID)
			log.Warn().Msg(msg)
			return nil, skip{fmt.Errorf("%s", msg)}
		}
		if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "InvalidInstanceID.Malformed" {
			msg := fmt.Sprintf("Malformed instance-id %s", instanceID)
			log.Warn().Msg(msg)
			return nil, skip{fmt.Errorf("%s", msg)}
		}
		if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "InvalidInstanceID.NotLinkable" {
			msg := fmt.Sprintf("Instance-id %s not linkable", instanceID)
			log.Warn().Msg(msg)
			return nil, skip{fmt.Errorf("%s", msg)}
		}
		return nil, err
	}
	if len(result.Reservations) == 0 || len(result.Reservations[0].Instances) == 0 {
		msg := fmt.Sprintf("No reservation with instance-id %s", instanceID)
		log.Warn().Msg(msg)
		return nil, skip{fmt.Errorf("%s", msg)}
	}

	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, skip{fmt.Errorf("node: '%s' in state '%s'", instanceID, state)}
		}
		return nil, fmt.Errorf("unable to retrieve PrivateDnsName name for '%s' in state '%s'", instanceID, state)
	}

	providerID := ""
	if *instance.Placement.AvailabilityZone != "" {
		providerID = fmt.Sprintf("aws:///%s/%s", *instance.Placement.AvailabilityZone, instanceID)
	}

	nodeInfo := &NodeInfo{
		Name:         *instance.PrivateDnsName,
		InstanceID:   instanceID,
		ProviderID:   providerID,
		InstanceType: *instance.InstanceType,
		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 m.CheckIfManaged {
		if _, ok := nodeInfo.Tags[m.ManagedTag]; !ok {
			nodeInfo.IsManaged = false
		}
	}

	infoJSON, _ := json.MarshalIndent(nodeInfo, " ", "    ")
	log.Debug().Msgf("Got node info from AWS: %s", infoJSON)

	return nodeInfo, nil
}