func BuildMapWithInterfaceInfo()

in agent/envoy_bootstrap/netinfo/network_info_collector.go [153:202]


func BuildMapWithInterfaceInfo() (*map[string]interface{}, error) {
	ifaces, err := net.Interfaces()
	if err != nil {
		log.Error("Unable to get network interfaces on host")
		return nil, err
	}

	var addressMap addressTypeMap

	for index := range ifaces {
		iface := ifaces[index]
		log.WithFields(log.Fields{
			"interface": iface.Name,
		}).Debug("Loading addresses for interface")

		err := getInterfaceAddressInfo(iface, &addressMap)
		if err != nil {
			log.WithFields(log.Fields{
				"interface": iface.Name,
			}).Error("Unable to get address information for interface")
			continue
		}
	}

	log.WithFields(log.Fields{
		"addressMap": addressMap,
	}).Debug("Generated addressMap")

	// TODO: Should either use the json encoding library to iteratively build a map of JSON values
	//   or iteratively construct the map[string]interface{}. These loops just convert
	//   the strongly typed interface mapping data into just maps and lists of interface{}
	//   which is what protobuf needs to make a struct.
	mapping := make(map[string]interface{})
	for ipver, iface := range addressMap.interfaceMapping {
		mapping[ipver] = make(map[string]interface{})
		for name, ips := range iface {
			// convert the ip list to []interface{}
			iplist := make([]interface{}, len(ips))
			for i, ip := range ips {
				iplist[i] = ip
			}
			mapping[ipver].(map[string]interface{})[name] = iplist
		}
	}

	metadata := map[string]interface{}{
		namespace: mapping,
	}
	return &metadata, nil
}