func GetObservedNetworkConfig(source network.ConfigSource) ([]params.NetworkConfig, error) {
	logger.Tracef("discovering observed machine network config...")

	interfaces, err := source.Interfaces()
	if err != nil {
		return nil, errors.Annotate(err, "detecting network interfaces")
	}
	if len(interfaces) == 0 {
		logger.Tracef("no network interfaces")
		return nil, nil
	}

	knownOVSBridges, err := source.OvsManagedBridges()
	if err != nil {
		// NOTE(achilleasa): we will only get an error here if we do
		// locate the OVS cli tools and get an error executing them.
		return nil, errors.Annotate(err, "querying OVS bridges")
	}

	defaultRoute, defaultRouteDevice, err := source.DefaultRoute()
	if err != nil {
		return nil, errors.Annotate(err, "retrieving default route")
	}

	var configs []params.NetworkConfig
	var bridgeNames []string

	for _, nic := range interfaces {
		virtualPortType := network.NonVirtualPort
		if knownOVSBridges.Contains(nic.Name()) {
			virtualPortType = network.OvsPort
		}

		nicConfig := interfaceToNetworkConfig(nic, virtualPortType)

		if nicConfig.InterfaceName == defaultRouteDevice {
			nicConfig.IsDefaultGateway = true
			nicConfig.GatewayAddress = defaultRoute.String()
		}

		// Collect all the bridge device names. We will use these to update all
		// the parent device names for the bridge's port devices at the end.
		if nic.Type() == network.BridgeDevice {
			bridgeNames = append(bridgeNames, nic.Name())
		}

		nicAddrs, err := nic.Addresses()
		if err != nil {
			return nil, errors.Annotatef(err, "detecting addresses for %q", nic.Name())
		}

		if len(nicAddrs) > 0 {
			// TODO (manadart 2021-05-07): This preserves prior behaviour,
			// but is incorrect for DHCP configured devices.
			// At present we do not store a config type against the device,
			// only the addresses (which incorrectly default to static too).
			// This could be corrected by interrogating the DHCP leases for
			// the device, should we ever need that detail.
			// At present we do not - we only use it to determine if an address
			// has a configuration method of "loopback".
			if nic.Type() != network.LoopbackDevice {
				nicConfig.ConfigType = string(network.ConfigStatic)
			}

			nicConfig.Addresses, err = addressesToConfig(nicConfig, nicAddrs)
			if err != nil {
				return nil, errors.Trace(err)
			}
		} else {
			logger.Infof("no addresses observed on interface %q", nic.Name())
		}

		configs = append(configs, nicConfig)
	}

	updateParentsForBridgePorts(configs, bridgeNames, source)
	return configs, nil
}
// GetObservedNetworkConfig uses the given source to find all available network
// interfaces and their assigned addresses, and returns the result as
// []params.NetworkConfig. In addition to what the source returns, a few
// additional transformations are done:
//
// * On any OS, the state (UP/DOWN) of each interface and the DeviceIndex field,
//   will be correctly populated. Loopback interfaces are also properly detected
//   and will have InterfaceType set LoopbackInterface.
// * On Linux only, the InterfaceType field will be reliably detected for a few
//   types: BondInterface, BridgeInterface, VLAN_8021QInterface.
// * Also on Linux, for interfaces that are discovered to be ports on a bridge,
//   the ParentInterfaceName will be populated with the name of the bridge.
// * ConfigType fields will be set to ConfigManual when no address is detected,
//   or ConfigStatic when it is.
// * NICs that correspond to the internal port of an OVS-managed switch will
//   have their type forced to bridge and their virtual port type set to
//   OvsPort.
// * TODO: IPv6 link-local addresses will be ignored and treated as empty ATM.
GetObservedNetworkConfig uses the given source to find all available network
interfaces and their assigned addresses, and returns the result as
[]params.NetworkConfig. In addition to what the source returns, a few
additional transformations are done.

On any OS, the state (UP/DOWN) of each interface and the DeviceIndex field,
will be correctly populated. Loopback interfaces are also properly detected
and will have InterfaceType set LoopbackInterface.
On Linux only, the InterfaceType field will be reliably detected for a few
types: BondInterface, BridgeInterface, VLAN_8021QInterface.
Also on Linux, for interfaces that are discovered to be ports on a bridge,
the ParentInterfaceName will be populated with the name of the bridge.
ConfigType fields will be set to ConfigManual when no address is detected,
or ConfigStatic when it is.
NICs that correspond to the internal port of an OVS-managed switch will
have their type forced to bridge and their virtual port type set to
OvsPort.
TODO: IPv6 link-local addresses will be ignored and treated as empty ATM.
// GetObservedNetworkConfig uses the given source to find all available network // interfaces and their assigned addresses and returns the result as // [] params . NetworkConfig . In addition to what the source returns a few // additional transformations are done : // // * On any OS the state ( UP / DOWN ) of each interface and the DeviceIndex field // will be correctly populated . Loopback interfaces are also properly detected // and will have InterfaceType set LoopbackInterface . // * On Linux only the InterfaceType field will be reliably detected for a few // types : BondInterface BridgeInterface VLAN_8021QInterface . // * Also on Linux for interfaces that are discovered to be ports on a bridge // the ParentInterfaceName will be populated with the name of the bridge . // * ConfigType fields will be set to ConfigManual when no address is detected // or ConfigStatic when it is . // * NICs that correspond to the internal port of an OVS - managed switch will // have their type forced to bridge and their virtual port type set to // OvsPort . // * TODO : IPv6 link - local addresses will be ignored and treated as empty ATM .
func GetObservedNetworkConfig(source network.ConfigSource) ([]params.NetworkConfig, error) {
	logger.Tracef("discovering observed machine network config...")
	interfaces, err := source.Interfaces()
	if err != nil {
		return nil, errors.Annotate(err, "detecting network interfaces")
	}
	if len(interfaces) == 0 {
		logger.Tracef("no network interfaces")
		return nil, nil
	}
	knownOVSBridges, err := source.OvsManagedBridges()
	if err != nil {
		return nil, errors.Annotate(err, "querying OVS bridges")
	}
	defaultRoute, defaultRouteDevice, err := source.DefaultRoute()
	if err != nil {
		return nil, errors.Annotate(err, "retrieving default route")
	}
	var configs []params.NetworkConfig
	var bridgeNames []string
	for _, nic := range interfaces {
		virtualPortType := network.NonVirtualPort
		if knownOVSBridges.Contains(nic.Name()) {
			virtualPortType = network.OvsPort
		}
		nicConfig := interfaceToNetworkConfig(nic, virtualPortType)
		if nicConfig.InterfaceName == defaultRouteDevice {
			nicConfig.IsDefaultGateway = true
			nicConfig.GatewayAddress = defaultRoute.String()
		}
		if nic.Type() == network.BridgeDevice {
			bridgeNames = append(bridgeNames, nic.Name())
		}
		nicAddrs, err := nic.Addresses()
		if err != nil {
			return nil, errors.Annotatef(err, "detecting addresses for %q", nic.Name())
		}
		if len(nicAddrs) > 0 {
			if nic.Type() != network.LoopbackDevice {
				nicConfig.ConfigType = string(network.ConfigStatic)
			}
			nicConfig.Addresses, err = addressesToConfig(nicConfig, nicAddrs)
			if err != nil {
				return nil, errors.Trace(err)
			}
		} else {
			logger.Infof("no addresses observed on interface %q", nic.Name())
		}
		configs = append(configs, nicConfig)
	}
	updateParentsForBridgePorts(configs, bridgeNames, source)
	return configs, nil
}
func GetObservedNetworkConfig ( source network . ConfigSource ) ( [ ] params . NetworkConfig , error ) { logger . Tracef ( " " ) interfaces , err := source . Interfaces ( ) if err != nil { return nil , errors . Annotate ( err , " " ) } if len ( interfaces ) == 0 { logger . Tracef ( " " ) return nil , nil } knownOVSBridges , err := source . OvsManagedBridges ( ) if err != nil { return nil , errors . Annotate ( err , " " ) } defaultRoute , defaultRouteDevice , err := source . DefaultRoute ( ) if err != nil { return nil , errors . Annotate ( err , " " ) } var configs [ ] params . NetworkConfig var bridgeNames [ ] string for _ , nic := range interfaces { virtualPortType := network . NonVirtualPort if knownOVSBridges . Contains ( nic . Name ( ) ) { virtualPortType = network . OvsPort } nicConfig := interfaceToNetworkConfig ( nic , virtualPortType ) if nicConfig . InterfaceName == defaultRouteDevice { nicConfig . IsDefaultGateway = true nicConfig . GatewayAddress = defaultRoute . String ( ) } if nic . Type ( ) == network . BridgeDevice { bridgeNames = append ( bridgeNames , nic . Name ( ) ) } nicAddrs , err := nic . Addresses ( ) if err != nil { return nil , errors . Annotatef ( err , " " , nic . Name ( ) ) } if len ( nicAddrs ) > 0 { if nic . Type ( ) != network . LoopbackDevice { nicConfig . ConfigType = string ( network . ConfigStatic ) } nicConfig . Addresses , err = addressesToConfig ( nicConfig , nicAddrs ) if err != nil { return nil , errors . Trace ( err ) } } else { logger . Infof ( " " , nic . Name ( ) ) } configs = append ( configs , nicConfig ) } updateParentsForBridgePorts ( configs , bridgeNames , source ) return configs , nil }
GetObservedNetworkConfig uses the given source to find all available network
interfaces and their assigned addresses, and returns the result as
[]params.NetworkConfig.
GetObservedNetworkConfig uses the given source to find all available network interfaces and their assigned addresses and returns the result as [] params . NetworkConfig .
// NOTE(achilleasa): we will only get an error here if we do // locate the OVS cli tools and get an error executing them. // Collect all the bridge device names. We will use these to update all // the parent device names for the bridge's port devices at the end. // TODO (manadart 2021-05-07): This preserves prior behaviour, // but is incorrect for DHCP configured devices. // At present we do not store a config type against the device, // only the addresses (which incorrectly default to static too). // This could be corrected by interrogating the DHCP leases for // the device, should we ever need that detail. // At present we do not - we only use it to determine if an address // has a configuration method of "loopback".