func()

in pkg/providers/v1/aws.go [1557:1641]


func (c *Cloud) ipv4AddressesFromMetadata() ([]v1.NodeAddress, error) {
	addresses := []v1.NodeAddress{}

	macs, err := c.metadata.GetMetadata("network/interfaces/macs/")
	if err != nil {
		return nil, fmt.Errorf("error querying AWS metadata for %q: %q", "network/interfaces/macs", err)
	}

	// We want the IPs to end up in order by interface (in particular, we want eth0's
	// IPs first), but macs isn't necessarily sorted in that order so we have to
	// explicitly order by device-number (device-number == the "0" in "eth0").

	var macIDs []string
	macDevNum := make(map[string]int)
	for _, macID := range strings.Split(macs, "\n") {
		if macID == "" {
			continue
		}
		numPath := path.Join("network/interfaces/macs/", macID, "device-number")
		numStr, err := c.metadata.GetMetadata(numPath)
		if err != nil {
			return nil, fmt.Errorf("error querying AWS metadata for %q: %q", numPath, err)
		}
		num, err := strconv.Atoi(strings.TrimSpace(numStr))
		if err != nil {
			klog.Warningf("Bad device-number %q for interface %s\n", numStr, macID)
			continue
		}
		macIDs = append(macIDs, macID)
		macDevNum[macID] = num
	}

	// Sort macIDs by interface device-number
	sort.Slice(macIDs, func(i, j int) bool {
		return macDevNum[macIDs[i]] < macDevNum[macIDs[j]]
	})

	for _, macID := range macIDs {
		ipPath := path.Join("network/interfaces/macs/", macID, "local-ipv4s")
		internalIPs, err := c.metadata.GetMetadata(ipPath)
		if err != nil {
			return nil, fmt.Errorf("error querying AWS metadata for %q: %q", ipPath, err)
		}

		for _, internalIP := range strings.Split(internalIPs, "\n") {
			if internalIP == "" {
				continue
			}
			addresses = append(addresses, v1.NodeAddress{Type: v1.NodeInternalIP, Address: internalIP})
		}
	}

	externalIP, err := c.metadata.GetMetadata("public-ipv4")
	if err != nil {
		//TODO: It would be nice to be able to determine the reason for the failure,
		// but the AWS client masks all failures with the same error description.
		klog.V(4).Info("Could not determine public IP from AWS metadata.")
	} else {
		addresses = append(addresses, v1.NodeAddress{Type: v1.NodeExternalIP, Address: externalIP})
	}

	localHostname, err := c.metadata.GetMetadata("local-hostname")
	if err != nil || len(localHostname) == 0 {
		//TODO: It would be nice to be able to determine the reason for the failure,
		// but the AWS client masks all failures with the same error description.
		klog.V(4).Info("Could not determine private DNS from AWS metadata.")
	} else {
		hostname, internalDNS := parseMetadataLocalHostname(localHostname)
		addresses = append(addresses, v1.NodeAddress{Type: v1.NodeHostName, Address: hostname})
		for _, d := range internalDNS {
			addresses = append(addresses, v1.NodeAddress{Type: v1.NodeInternalDNS, Address: d})
		}
	}

	externalDNS, err := c.metadata.GetMetadata("public-hostname")
	if err != nil || len(externalDNS) == 0 {
		//TODO: It would be nice to be able to determine the reason for the failure,
		// but the AWS client masks all failures with the same error description.
		klog.V(4).Info("Could not determine public DNS from AWS metadata.")
	} else {
		addresses = append(addresses, v1.NodeAddress{Type: v1.NodeExternalDNS, Address: externalDNS})
	}

	return addresses, nil
}