浏览代码

Merge pull request #46075 from thaJeztah/libnetwork_remove_NetworkInfo

libnetwork: remove Network.Info() and remove NetworkInfo interface
Sebastiaan van Stijn 1 年之前
父节点
当前提交
2cccb1f02c

+ 1 - 1
daemon/cluster/networks.go

@@ -321,7 +321,7 @@ func (c *Cluster) populateNetworkID(ctx context.Context, client swarmapi.Control
 				}
 				goto setid
 			}
-			if ln != nil && !ln.Info().Dynamic() {
+			if ln != nil && !ln.Dynamic() {
 				errMsg := fmt.Sprintf("The network %s cannot be used with services. Only networks scoped to the swarm can be used, such as those created with the overlay driver.", ln.Name())
 				return errors.WithStack(notAllowedError(errMsg))
 			}

+ 4 - 4
daemon/container_operations.go

@@ -232,7 +232,7 @@ func (daemon *Daemon) updateNetworkSettings(container *container.Container, n *l
 			// is an attachable network, which may not
 			// be locally available previously.
 			// So always update.
-			if n.Info().Scope() == scope.Swarm {
+			if n.Scope() == scope.Swarm {
 				continue
 			}
 			// Avoid duplicate config
@@ -327,7 +327,7 @@ func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrN
 	// If we found a network and if it is not dynamically created
 	// we should never attempt to attach to that network here.
 	if n != nil {
-		if container.Managed || !n.Info().Dynamic() {
+		if container.Managed || !n.Dynamic() {
 			return n, nil, nil
 		}
 		// Throw an error if the container is already attached to the network
@@ -591,7 +591,7 @@ func validateNetworkingConfig(n *libnetwork.Network, epConfig *networktypes.Endp
 		return nil
 	}
 
-	_, _, nwIPv4Configs, nwIPv6Configs := n.Info().IpamConfig()
+	_, _, nwIPv4Configs, nwIPv6Configs := n.IpamConfig()
 	for _, s := range []struct {
 		ipConfigured  bool
 		subnetConfigs []*libnetwork.IpamConf
@@ -879,7 +879,7 @@ func (daemon *Daemon) disconnectFromNetwork(container *container.Container, n *l
 }
 
 func (daemon *Daemon) tryDetachContainerFromClusterNetwork(network *libnetwork.Network, container *container.Container) {
-	if !container.Managed && daemon.clusterProvider != nil && network.Info().Dynamic() {
+	if !container.Managed && daemon.clusterProvider != nil && network.Dynamic() {
 		if err := daemon.clusterProvider.DetachNetwork(network.Name(), container.ID); err != nil {
 			log.G(context.TODO()).WithError(err).Warn("error detaching from network")
 			if err := daemon.clusterProvider.DetachNetwork(network.ID(), container.ID); err != nil {

+ 5 - 9
daemon/daemon.go

@@ -192,11 +192,9 @@ func (daemon *Daemon) UsesSnapshotter() bool {
 // RegistryHosts returns the registry hosts configuration for the host component
 // of a distribution image reference.
 func (daemon *Daemon) RegistryHosts(host string) ([]docker.RegistryHost, error) {
-	m := map[string]resolverconfig.RegistryConfig{}
-
-	mirrors := daemon.registryService.ServiceConfig().Mirrors
-	m["docker.io"] = resolverconfig.RegistryConfig{Mirrors: mirrors}
-
+	m := map[string]resolverconfig.RegistryConfig{
+		"docker.io": {Mirrors: daemon.registryService.ServiceConfig().Mirrors},
+	}
 	conf := daemon.registryService.ServiceConfig().IndexConfigs
 	for k, v := range conf {
 		c := resolverconfig.RegistryConfig{}
@@ -1323,10 +1321,8 @@ func (daemon *Daemon) Subnets() ([]net.IPNet, []net.IPNet) {
 	var v4Subnets []net.IPNet
 	var v6Subnets []net.IPNet
 
-	managedNetworks := daemon.netController.Networks()
-
-	for _, managedNetwork := range managedNetworks {
-		v4infos, v6infos := managedNetwork.Info().IpamInfo()
+	for _, managedNetwork := range daemon.netController.Networks() {
+		v4infos, v6infos := managedNetwork.IpamInfo()
 		for _, info := range v4infos {
 			if info.IPAMData.Pool != nil {
 				v4Subnets = append(v4Subnets, *info.IPAMData.Pool)

+ 3 - 5
daemon/daemon_unix.go

@@ -899,14 +899,12 @@ func setHostGatewayIP(controller *libnetwork.Controller, config *config.Config)
 		return
 	}
 	if n, err := controller.NetworkByName("bridge"); err == nil {
-		v4Info, v6Info := n.Info().IpamInfo()
-		var gateway net.IP
+		v4Info, v6Info := n.IpamInfo()
 		if len(v4Info) > 0 {
-			gateway = v4Info[0].Gateway.IP
+			config.HostGatewayIP = v4Info[0].Gateway.IP
 		} else if len(v6Info) > 0 {
-			gateway = v6Info[0].Gateway.IP
+			config.HostGatewayIP = v6Info[0].Gateway.IP
 		}
-		config.HostGatewayIP = gateway
 	}
 }
 

+ 8 - 8
daemon/daemon_windows.go

@@ -249,7 +249,7 @@ func (daemon *Daemon) initNetworkController(daemonCfg *config.Config, activeSand
 
 	// Remove networks not present in HNS
 	for _, v := range daemon.netController.Networks() {
-		hnsid := v.Info().DriverOptions()[winlibnetwork.HNSID]
+		hnsid := v.DriverOptions()[winlibnetwork.HNSID]
 		found := false
 
 		for _, v := range hnsresponse {
@@ -262,9 +262,9 @@ func (daemon *Daemon) initNetworkController(daemonCfg *config.Config, activeSand
 		if !found {
 			// non-default nat networks should be re-created if missing from HNS
 			if v.Type() == "nat" && v.Name() != "nat" {
-				_, _, v4Conf, v6Conf := v.Info().IpamConfig()
+				_, _, v4Conf, v6Conf := v.IpamConfig()
 				netOption := map[string]string{}
-				for k, v := range v.Info().DriverOptions() {
+				for k, v := range v.DriverOptions() {
 					if k != winlibnetwork.NetworkName && k != winlibnetwork.HNSID {
 						netOption[k] = v
 					}
@@ -290,7 +290,7 @@ func (daemon *Daemon) initNetworkController(daemonCfg *config.Config, activeSand
 			}
 
 			// global networks should not be deleted by local HNS
-			if v.Info().Scope() != scope.Global {
+			if v.Scope() != scope.Global {
 				err = v.Delete()
 				if err != nil {
 					log.G(context.TODO()).Errorf("Error occurred when removing network %v", err)
@@ -307,7 +307,7 @@ func (daemon *Daemon) initNetworkController(daemonCfg *config.Config, activeSand
 	defaultNetworkExists := false
 
 	if network, err := daemon.netController.NetworkByName(runconfig.DefaultDaemonNetworkMode().NetworkName()); err == nil {
-		hnsid := network.Info().DriverOptions()[winlibnetwork.HNSID]
+		hnsid := network.DriverOptions()[winlibnetwork.HNSID]
 		for _, v := range hnsresponse {
 			if hnsid == v.Id {
 				defaultNetworkExists = true
@@ -325,7 +325,7 @@ func (daemon *Daemon) initNetworkController(daemonCfg *config.Config, activeSand
 		}
 		var n *libnetwork.Network
 		s := func(current *libnetwork.Network) bool {
-			hnsid := current.Info().DriverOptions()[winlibnetwork.HNSID]
+			hnsid := current.DriverOptions()[winlibnetwork.HNSID]
 			if hnsid == v.Id {
 				n = current
 				return true
@@ -341,7 +341,7 @@ func (daemon *Daemon) initNetworkController(daemonCfg *config.Config, activeSand
 			nid = n.ID()
 
 			// global networks should not be deleted by local HNS
-			if n.Info().Scope() == scope.Global {
+			if n.Scope() == scope.Global {
 				continue
 			}
 			v.Name = n.Name()
@@ -349,7 +349,7 @@ func (daemon *Daemon) initNetworkController(daemonCfg *config.Config, activeSand
 			// is not yet populated in the libnetwork windows driver
 
 			// restore option if it existed before
-			drvOptions = n.Info().DriverOptions()
+			drvOptions = n.DriverOptions()
 			n.Delete()
 		}
 		netOption := map[string]string{

+ 20 - 21
daemon/network.go

@@ -313,7 +313,7 @@ func (daemon *Daemon) createNetwork(cfg *config.Config, create types.NetworkCrea
 		// check if user defined CheckDuplicate, if set true, return err
 		// otherwise prepare a warning message
 		if create.CheckDuplicate {
-			if !agent || nw.Info().Dynamic() {
+			if !agent || nw.Dynamic() {
 				return nil, libnetwork.NetworkNameError(create.Name)
 			}
 		}
@@ -532,7 +532,7 @@ func (daemon *Daemon) deleteNetwork(nw *libnetwork.Network, dynamic bool) error
 		return errdefs.Forbidden(err)
 	}
 
-	if dynamic && !nw.Info().Dynamic() {
+	if dynamic && !nw.Dynamic() {
 		if runconfig.IsPreDefinedNetwork(nw.Name()) {
 			// Predefined networks now support swarm services. Make this
 			// a no-op when cluster requests to remove the predefined network.
@@ -548,9 +548,9 @@ func (daemon *Daemon) deleteNetwork(nw *libnetwork.Network, dynamic bool) error
 
 	// If this is not a configuration only network, we need to
 	// update the corresponding remote drivers' reference counts
-	if !nw.Info().ConfigOnly() {
+	if !nw.ConfigOnly() {
 		daemon.pluginRefCount(nw.Type(), driverapi.NetworkPluginEndpointType, plugingetter.Release)
-		ipamType, _, _, _ := nw.Info().IpamConfig()
+		ipamType, _, _, _ := nw.IpamConfig()
 		daemon.pluginRefCount(ipamType, ipamapi.PluginEndpointType, plugingetter.Release)
 		daemon.LogNetworkEvent(nw, "destroy")
 	}
@@ -599,24 +599,23 @@ func buildNetworkResource(nw *libnetwork.Network) types.NetworkResource {
 		return types.NetworkResource{}
 	}
 
-	info := nw.Info()
 	return types.NetworkResource{
 		Name:       nw.Name(),
 		ID:         nw.ID(),
-		Created:    info.Created(),
-		Scope:      info.Scope(),
+		Created:    nw.Created(),
+		Scope:      nw.Scope(),
 		Driver:     nw.Type(),
-		EnableIPv6: info.IPv6Enabled(),
-		IPAM:       buildIPAMResources(info),
-		Internal:   info.Internal(),
-		Attachable: info.Attachable(),
-		Ingress:    info.Ingress(),
-		ConfigFrom: network.ConfigReference{Network: info.ConfigFrom()},
-		ConfigOnly: info.ConfigOnly(),
+		EnableIPv6: nw.IPv6Enabled(),
+		IPAM:       buildIPAMResources(nw),
+		Internal:   nw.Internal(),
+		Attachable: nw.Attachable(),
+		Ingress:    nw.Ingress(),
+		ConfigFrom: network.ConfigReference{Network: nw.ConfigFrom()},
+		ConfigOnly: nw.ConfigOnly(),
 		Containers: map[string]types.EndpointResource{},
-		Options:    info.DriverOptions(),
-		Labels:     info.Labels(),
-		Peers:      buildPeerInfoResources(info.Peers()),
+		Options:    nw.DriverOptions(),
+		Labels:     nw.Labels(),
+		Peers:      buildPeerInfoResources(nw.Peers()),
 	}
 }
 
@@ -643,7 +642,7 @@ func buildContainerAttachments(nw *libnetwork.Network) map[string]types.Endpoint
 // attached to the network. It is used when listing networks in "verbose" mode.
 func buildServiceAttachments(nw *libnetwork.Network) map[string]network.ServiceInfo {
 	services := make(map[string]network.ServiceInfo)
-	for name, service := range nw.Info().Services() {
+	for name, service := range nw.Services() {
 		tasks := make([]network.Task, 0, len(service.Tasks))
 		for _, t := range service.Tasks {
 			tasks = append(tasks, network.Task{
@@ -679,7 +678,7 @@ func buildPeerInfoResources(peers []networkdb.PeerInfo) []network.PeerInfo {
 
 // buildIPAMResources constructs a [network.IPAM] from the network's
 // IPAM information for inclusion in API responses.
-func buildIPAMResources(nw libnetwork.NetworkInfo) network.IPAM {
+func buildIPAMResources(nw *libnetwork.Network) network.IPAM {
 	var ipamConfig []network.IPAMConfig
 
 	ipamDriver, ipamOptions, ipv4Conf, ipv6Conf := nw.IpamConfig()
@@ -772,7 +771,7 @@ func buildEndpointResource(ep *libnetwork.Endpoint, info libnetwork.EndpointInfo
 // after disconnecting any connected container
 func (daemon *Daemon) clearAttachableNetworks() {
 	for _, n := range daemon.getAllNetworks() {
-		if !n.Info().Attachable() {
+		if !n.Attachable() {
 			continue
 		}
 		for _, ep := range n.Endpoints() {
@@ -882,7 +881,7 @@ func buildCreateEndpointOptions(c *container.Container, n *libnetwork.Network, e
 	// Port-mapping rules belong to the container & applicable only to non-internal networks.
 	//
 	// TODO(thaJeztah): Look if we can provide a more minimal function for getPortMapInfo, as it does a lot, and we only need the "length".
-	if n.Info().Internal() || len(getPortMapInfo(sb)) > 0 {
+	if n.Internal() || len(getPortMapInfo(sb)) > 0 {
 		return createOptions, nil
 	}
 

+ 3 - 3
daemon/prune.go

@@ -109,13 +109,13 @@ func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filte
 			return true
 		default:
 		}
-		if nw.Info().ConfigOnly() {
+		if nw.ConfigOnly() {
 			return false
 		}
-		if !until.IsZero() && nw.Info().Created().After(until) {
+		if !until.IsZero() && nw.Created().After(until) {
 			return false
 		}
-		if !matchLabels(pruneFilters, nw.Info().Labels()) {
+		if !matchLabels(pruneFilters, nw.Labels()) {
 			return false
 		}
 		nwName := nw.Name()

+ 3 - 2
libnetwork/agent.go

@@ -442,9 +442,9 @@ type epRecord struct {
 	lbIndex int
 }
 
+// Services returns a map of services keyed by the service name with the details
+// of all the tasks that belong to the service. Applicable only in swarm mode.
 func (n *Network) Services() map[string]ServiceInfo {
-	eps := make(map[string]epRecord)
-
 	if !n.isClusterEligible() {
 		return nil
 	}
@@ -455,6 +455,7 @@ func (n *Network) Services() map[string]ServiceInfo {
 
 	// Walk through libnetworkEPTable and fetch the driver agnostic endpoint info
 	entries := agent.networkDB.GetTableByNetwork(libnetworkEPTable, n.id)
+	eps := make(map[string]epRecord)
 	for eid, value := range entries {
 		var epRec EndpointRecord
 		nid := n.ID()

+ 5 - 31
libnetwork/network.go

@@ -25,31 +25,6 @@ import (
 	"github.com/docker/docker/pkg/stringid"
 )
 
-// NetworkInfo returns some configuration and operational information about the network
-type NetworkInfo interface {
-	IpamConfig() (string, map[string]string, []*IpamConf, []*IpamConf)
-	IpamInfo() ([]*IpamInfo, []*IpamInfo)
-	DriverOptions() map[string]string
-	Scope() string
-	IPv6Enabled() bool
-	Internal() bool
-	Attachable() bool
-	Ingress() bool
-	ConfigFrom() string
-	ConfigOnly() bool
-	Labels() map[string]string
-	Dynamic() bool
-	Created() time.Time
-	// Peers returns a slice of PeerInfo structures which has the information about the peer
-	// nodes participating in the same overlay network. This is currently the per-network
-	// gossip cluster. For non-dynamic overlay networks and bridge networks it returns an
-	// empty slice
-	Peers() []networkdb.PeerInfo
-	// Services returns a map of services keyed by the service name with the details
-	// of all the tasks that belong to the service. Applicable only in swarm mode.
-	Services() map[string]ServiceInfo
-}
-
 // EndpointWalker is a client provided function which will be used to walk the Endpoints.
 // When the function returns true, the walk will stop.
 type EndpointWalker func(ep *Endpoint) bool
@@ -1772,11 +1747,10 @@ func (n *Network) deriveAddressSpace() (string, error) {
 	return local, nil
 }
 
-// Info returns certain operational data belonging to this network.
-func (n *Network) Info() NetworkInfo {
-	return n
-}
-
+// Peers returns a slice of PeerInfo structures which has the information about the peer
+// nodes participating in the same overlay network. This is currently the per-network
+// gossip cluster. For non-dynamic overlay networks and bridge networks it returns an
+// empty slice
 func (n *Network) Peers() []networkdb.PeerInfo {
 	if !n.Dynamic() {
 		return []networkdb.PeerInfo{}
@@ -2118,7 +2092,7 @@ func (n *Network) NdotsSet() bool {
 func (c *Controller) getConfigNetwork(name string) (*Network, error) {
 	var n *Network
 	c.WalkNetworks(func(current *Network) bool {
-		if current.Info().ConfigOnly() && current.Name() == name {
+		if current.ConfigOnly() && current.Name() == name {
 			n = current
 			return true
 		}

+ 1 - 3
libnetwork/network_windows.go

@@ -34,9 +34,7 @@ func (n *Network) startResolver() {
 	}
 	n.resolverOnce.Do(func() {
 		log.G(context.TODO()).Debugf("Launching DNS server for network %q", n.Name())
-		options := n.Info().DriverOptions()
-		hnsid := options[windows.HNSID]
-
+		hnsid := n.DriverOptions()[windows.HNSID]
 		if hnsid == "" {
 			return
 		}