func discoverIPs()

in pkg/cloudprovider/vsphere/nodemanager.go [365:429]


func discoverIPs(ipAddrNetworkNames []*ipAddrNetworkName, ipFamily string,
	internalNetworkSubnets, externalNetworkSubnets,
	excludeInternalNetworkSubnets, excludeExternalNetworkSubnets []*net.IPNet,
	internalVMNetworkName, externalVMNetworkName string) (internal *ipAddrNetworkName, external *ipAddrNetworkName) {

	ipFamilyMatches := collectMatchesForIPFamily(ipAddrNetworkNames, ipFamily)

	var discoveredInternal *ipAddrNetworkName
	var discoveredExternal *ipAddrNetworkName

	filteredInternalMatches := filterSubnetExclusions(ipFamilyMatches, excludeInternalNetworkSubnets)
	filteredExternalMatches := filterSubnetExclusions(ipFamilyMatches, excludeExternalNetworkSubnets)

	if len(filteredInternalMatches) > 0 || len(filteredExternalMatches) > 0 {
		discoveredInternal = findSubnetMatch(filteredInternalMatches, internalNetworkSubnets)
		if discoveredInternal != nil {
			klog.V(2).Infof("Adding Internal IP by AddressMatching: %s", discoveredInternal.ipAddr)
		}
		discoveredExternal = findSubnetMatch(filteredExternalMatches, externalNetworkSubnets)
		if discoveredExternal != nil {
			klog.V(2).Infof("Adding External IP by AddressMatching: %s", discoveredExternal.ipAddr)
		}

		if discoveredInternal == nil && internalVMNetworkName != "" {
			discoveredInternal = findNetworkNameMatch(filteredInternalMatches, internalVMNetworkName)
			if discoveredInternal != nil {
				klog.V(2).Infof("Adding Internal IP by NetworkName: %s", discoveredInternal.ipAddr)
			}
		}

		if discoveredExternal == nil && externalVMNetworkName != "" {
			discoveredExternal = findNetworkNameMatch(filteredExternalMatches, externalVMNetworkName)
			if discoveredExternal != nil {
				klog.V(2).Infof("Adding External IP by NetworkName: %s", discoveredExternal.ipAddr)
			}
		}

		// Neither internal or external addresses were found. This defaults to the legacy
		// address selection behavior which is to only support a single address and
		// return the first one found
		if discoveredInternal == nil && discoveredExternal == nil {
			klog.V(5).Info("Default address selection.")
			if len(filteredInternalMatches) > 0 {
				klog.V(2).Infof("Adding Internal IP: %s", filteredInternalMatches[0].ipAddr)
				discoveredInternal = filteredInternalMatches[0]
			}

			if len(filteredExternalMatches) > 0 {
				klog.V(2).Infof("Adding External IP: %s", filteredExternalMatches[0].ipAddr)
				discoveredExternal = filteredExternalMatches[0]
			}
		} else {
			// At least one of the Internal or External addresses has been found.
			// Minimally the Internal needs to exist for the node to function correctly.
			// If only one was discovered, will log the warning and continue which will
			// ultimately be visible to the end user
			if discoveredInternal != nil && discoveredExternal == nil {
				klog.Warning("Internal address found, but external address not found. Returning what addresses were discovered.")
			} else if discoveredInternal == nil && discoveredExternal != nil {
				klog.Warning("External address found, but internal address not found. Returning what addresses were discovered.")
			}
		}
	}
	return discoveredInternal, discoveredExternal
}