Skip to content

Commit

Permalink
Merge pull request moby#46050 from thaJeztah/libnetwork_remove_interface
Browse files Browse the repository at this point in the history
libnetwork: remove Network interface
  • Loading branch information
thaJeztah authored Jul 25, 2023
2 parents b3ed92d + 64c6f72 commit 7e4ffa3
Show file tree
Hide file tree
Showing 31 changed files with 266 additions and 295 deletions.
2 changes: 1 addition & 1 deletion api/server/router/network/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Backend is all the methods that need to be implemented
// to provide network specific functionality.
type Backend interface {
FindNetwork(idName string) (libnetwork.Network, error)
FindNetwork(idName string) (*libnetwork.Network, error)
GetNetworks(filters.Args, types.NetworkListConfig) ([]types.NetworkResource, error)
CreateNetwork(nc types.NetworkCreateRequest) (*types.NetworkCreateResponse, error)
ConnectContainerToNetwork(containerName, networkName string, endpointConfig *network.EndpointSettings) error
Expand Down
2 changes: 1 addition & 1 deletion builder/builder-next/executor_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type lnInterface struct {
provider *bridgeProvider
}

func (iface *lnInterface) init(c *libnetwork.Controller, n libnetwork.Network) {
func (iface *lnInterface) init(c *libnetwork.Controller, n *libnetwork.Network) {
defer close(iface.ready)
id := identity.NewID()

Expand Down
2 changes: 1 addition & 1 deletion daemon/cluster/executor/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
type Backend interface {
CreateManagedNetwork(clustertypes.NetworkCreateRequest) error
DeleteManagedNetwork(networkID string) error
FindNetwork(idName string) (libnetwork.Network, error)
FindNetwork(idName string) (*libnetwork.Network, error)
SetupIngress(clustertypes.NetworkCreateRequest, string) (<-chan struct{}, error)
ReleaseIngress() (<-chan struct{}, error)
CreateManagedContainer(ctx context.Context, config types.ContainerCreateConfig) (container.CreateResponse, error)
Expand Down
22 changes: 11 additions & 11 deletions daemon/container_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, container *contain
return sboxOptions, nil
}

func (daemon *Daemon) updateNetworkSettings(container *container.Container, n libnetwork.Network, endpointConfig *networktypes.EndpointSettings) error {
func (daemon *Daemon) updateNetworkSettings(container *container.Container, n *libnetwork.Network, endpointConfig *networktypes.EndpointSettings) error {
if container.NetworkSettings == nil {
container.NetworkSettings = &network.Settings{}
}
Expand Down Expand Up @@ -293,7 +293,7 @@ func (daemon *Daemon) updateNetworkSettings(container *container.Container, n li
return nil
}

func (daemon *Daemon) updateEndpointNetworkSettings(cfg *config.Config, container *container.Container, n libnetwork.Network, ep *libnetwork.Endpoint) error {
func (daemon *Daemon) updateEndpointNetworkSettings(cfg *config.Config, container *container.Container, n *libnetwork.Network, ep *libnetwork.Endpoint) error {
if err := buildEndpointInfo(container.NetworkSettings, n, ep); err != nil {
return err
}
Expand All @@ -320,7 +320,7 @@ func (daemon *Daemon) updateNetwork(cfg *config.Config, container *container.Con
}

// Find if container is connected to the default bridge network
var n libnetwork.Network
var n *libnetwork.Network
for name, v := range container.NetworkSettings.Networks {
sn, err := daemon.FindNetwork(getNetworkID(name, v.EndpointSettings))
if err != nil {
Expand Down Expand Up @@ -351,7 +351,7 @@ func (daemon *Daemon) updateNetwork(cfg *config.Config, container *container.Con
return nil
}

func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrName string, epConfig *networktypes.EndpointSettings) (libnetwork.Network, *networktypes.NetworkingConfig, error) {
func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrName string, epConfig *networktypes.EndpointSettings) (*libnetwork.Network, *networktypes.NetworkingConfig, error) {
id := getNetworkID(idOrName, epConfig)

n, err := daemon.FindNetwork(id)
Expand Down Expand Up @@ -451,7 +451,7 @@ func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrN

// updateContainerNetworkSettings updates the network settings
func (daemon *Daemon) updateContainerNetworkSettings(container *container.Container, endpointsConfig map[string]*networktypes.EndpointSettings) {
var n libnetwork.Network
var n *libnetwork.Network

mode := container.HostConfig.NetworkMode
if container.Config.NetworkDisabled || mode.IsContainer() {
Expand Down Expand Up @@ -622,7 +622,7 @@ func hasUserDefinedIPAddress(ipamConfig *networktypes.EndpointIPAMConfig) bool {
}

// User specified ip address is acceptable only for networks with user specified subnets.
func validateNetworkingConfig(n libnetwork.Network, epConfig *networktypes.EndpointSettings) error {
func validateNetworkingConfig(n *libnetwork.Network, epConfig *networktypes.EndpointSettings) error {
if n == nil || epConfig == nil {
return nil
}
Expand Down Expand Up @@ -684,7 +684,7 @@ func cleanOperationalData(es *network.EndpointSettings) {
}
}

func (daemon *Daemon) updateNetworkConfig(container *container.Container, n libnetwork.Network, endpointConfig *networktypes.EndpointSettings, updateSettings bool) error {
func (daemon *Daemon) updateNetworkConfig(container *container.Container, n *libnetwork.Network, endpointConfig *networktypes.EndpointSettings, updateSettings bool) error {
if containertypes.NetworkMode(n.Name()).IsUserDefined() {
addShortID := true
shortID := stringid.TruncateID(container.ID)
Expand Down Expand Up @@ -835,7 +835,7 @@ func (daemon *Daemon) connectToNetwork(cfg *config.Config, container *container.
return nil
}

func updateJoinInfo(networkSettings *network.Settings, n libnetwork.Network, ep *libnetwork.Endpoint) error {
func updateJoinInfo(networkSettings *network.Settings, n *libnetwork.Network, ep *libnetwork.Endpoint) error {
if ep == nil {
return errors.New("invalid enppoint whhile building portmap info")
}
Expand Down Expand Up @@ -880,7 +880,7 @@ func (daemon *Daemon) ForceEndpointDelete(name string, networkName string) error
return ep.Delete(true)
}

func (daemon *Daemon) disconnectFromNetwork(container *container.Container, n libnetwork.Network, force bool) error {
func (daemon *Daemon) disconnectFromNetwork(container *container.Container, n *libnetwork.Network, force bool) error {
var (
ep *libnetwork.Endpoint
sbox *libnetwork.Sandbox
Expand Down Expand Up @@ -932,7 +932,7 @@ func (daemon *Daemon) disconnectFromNetwork(container *container.Container, n li
return nil
}

func (daemon *Daemon) tryDetachContainerFromClusterNetwork(network libnetwork.Network, container *container.Container) {
func (daemon *Daemon) tryDetachContainerFromClusterNetwork(network *libnetwork.Network, container *container.Container) {
if daemon.clusterProvider != nil && network.Info().Dynamic() && !container.Managed {
if err := daemon.clusterProvider.DetachNetwork(network.Name(), container.ID); err != nil {
log.G(context.TODO()).Warnf("error detaching from network %s: %v", network.Name(), err)
Expand Down Expand Up @@ -1018,7 +1018,7 @@ func (daemon *Daemon) releaseNetwork(container *container.Container) {
return
}

var networks []libnetwork.Network
var networks []*libnetwork.Network
for n, epSettings := range settings {
if nw, err := daemon.FindNetwork(getNetworkID(n, epSettings.EndpointSettings)); err == nil {
networks = append(networks, nw)
Expand Down
4 changes: 2 additions & 2 deletions daemon/daemon_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ func (daemon *Daemon) initNetworkController(daemonCfg *config.Config, activeSand
if networkTypeNorm == "private" || networkTypeNorm == "internal" {
continue // workaround for HNS reporting unsupported networks
}
var n libnetwork.Network
s := func(current libnetwork.Network) bool {
var n *libnetwork.Network
s := func(current *libnetwork.Network) bool {
hnsid := current.Info().DriverOptions()[winlibnetwork.HNSID]
if hnsid == v.Id {
n = current
Expand Down
4 changes: 2 additions & 2 deletions daemon/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ func (daemon *Daemon) LogVolumeEvent(volumeID, action string, attributes map[str
}

// LogNetworkEvent generates an event related to a network with only the default attributes.
func (daemon *Daemon) LogNetworkEvent(nw libnetwork.Network, action string) {
func (daemon *Daemon) LogNetworkEvent(nw *libnetwork.Network, action string) {
daemon.LogNetworkEventWithAttributes(nw, action, map[string]string{})
}

// LogNetworkEventWithAttributes generates an event related to a network with specific given attributes.
func (daemon *Daemon) LogNetworkEventWithAttributes(nw libnetwork.Network, action string, attributes map[string]string) {
func (daemon *Daemon) LogNetworkEventWithAttributes(nw *libnetwork.Network, action string, attributes map[string]string) {
attributes["name"] = nw.Name()
attributes["type"] = nw.Type()
actor := events.Actor{
Expand Down
32 changes: 16 additions & 16 deletions daemon/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func (daemon *Daemon) NetworkController() *libnetwork.Controller {
// 2. Full Name
// 3. Partial ID
// as long as there is no ambiguity
func (daemon *Daemon) FindNetwork(term string) (libnetwork.Network, error) {
listByFullName := []libnetwork.Network{}
listByPartialID := []libnetwork.Network{}
func (daemon *Daemon) FindNetwork(term string) (*libnetwork.Network, error) {
listByFullName := []*libnetwork.Network{}
listByPartialID := []*libnetwork.Network{}
for _, nw := range daemon.getAllNetworks() {
if nw.ID() == term {
return nw, nil
Expand Down Expand Up @@ -94,7 +94,7 @@ func (daemon *Daemon) FindNetwork(term string) (libnetwork.Network, error) {

// GetNetworkByID function returns a network whose ID matches the given ID.
// It fails with an error if no matching network is found.
func (daemon *Daemon) GetNetworkByID(id string) (libnetwork.Network, error) {
func (daemon *Daemon) GetNetworkByID(id string) (*libnetwork.Network, error) {
c := daemon.netController
if c == nil {
return nil, errors.Wrap(libnetwork.ErrNoSuchNetwork(id), "netcontroller is nil")
Expand All @@ -104,7 +104,7 @@ func (daemon *Daemon) GetNetworkByID(id string) (libnetwork.Network, error) {

// GetNetworkByName function returns a network for a given network name.
// If no network name is given, the default network is returned.
func (daemon *Daemon) GetNetworkByName(name string) (libnetwork.Network, error) {
func (daemon *Daemon) GetNetworkByName(name string) (*libnetwork.Network, error) {
c := daemon.netController
if c == nil {
return nil, libnetwork.ErrNoSuchNetwork(name)
Expand All @@ -116,13 +116,13 @@ func (daemon *Daemon) GetNetworkByName(name string) (libnetwork.Network, error)
}

// GetNetworksByIDPrefix returns a list of networks whose ID partially matches zero or more networks
func (daemon *Daemon) GetNetworksByIDPrefix(partialID string) []libnetwork.Network {
func (daemon *Daemon) GetNetworksByIDPrefix(partialID string) []*libnetwork.Network {
c := daemon.netController
if c == nil {
return nil
}
list := []libnetwork.Network{}
l := func(nw libnetwork.Network) bool {
list := []*libnetwork.Network{}
l := func(nw *libnetwork.Network) bool {
if strings.HasPrefix(nw.ID(), partialID) {
list = append(list, nw)
}
Expand All @@ -134,7 +134,7 @@ func (daemon *Daemon) GetNetworksByIDPrefix(partialID string) []libnetwork.Netwo
}

// getAllNetworks returns a list containing all networks
func (daemon *Daemon) getAllNetworks() []libnetwork.Network {
func (daemon *Daemon) getAllNetworks() []*libnetwork.Network {
c := daemon.netController
if c == nil {
return nil
Expand Down Expand Up @@ -527,7 +527,7 @@ func (daemon *Daemon) DeleteNetwork(networkID string) error {
return daemon.deleteNetwork(n, false)
}

func (daemon *Daemon) deleteNetwork(nw libnetwork.Network, dynamic bool) error {
func (daemon *Daemon) deleteNetwork(nw *libnetwork.Network, dynamic bool) error {
if runconfig.IsPreDefinedNetwork(nw.Name()) && !dynamic {
err := fmt.Errorf("%s is a pre-defined network and cannot be removed", nw.Name())
return errdefs.Forbidden(err)
Expand Down Expand Up @@ -564,9 +564,9 @@ func (daemon *Daemon) GetNetworks(filter filters.Args, config types.NetworkListC
networks := daemon.getAllNetworks()

list := make([]types.NetworkResource, 0, len(networks))
var idx map[string]libnetwork.Network
var idx map[string]*libnetwork.Network
if config.Detailed {
idx = make(map[string]libnetwork.Network)
idx = make(map[string]*libnetwork.Network)
}

for _, n := range networks {
Expand Down Expand Up @@ -594,7 +594,7 @@ func (daemon *Daemon) GetNetworks(filter filters.Args, config types.NetworkListC
return list, nil
}

func buildNetworkResource(nw libnetwork.Network) types.NetworkResource {
func buildNetworkResource(nw *libnetwork.Network) types.NetworkResource {
r := types.NetworkResource{}
if nw == nil {
return r
Expand Down Expand Up @@ -628,7 +628,7 @@ func buildNetworkResource(nw libnetwork.Network) types.NetworkResource {
return r
}

func buildDetailedNetworkResources(r *types.NetworkResource, nw libnetwork.Network, verbose bool) {
func buildDetailedNetworkResources(r *types.NetworkResource, nw *libnetwork.Network, verbose bool) {
if nw == nil {
return
}
Expand Down Expand Up @@ -797,7 +797,7 @@ func (daemon *Daemon) clearAttachableNetworks() {
}

// buildCreateEndpointOptions builds endpoint options from a given network.
func buildCreateEndpointOptions(c *container.Container, n libnetwork.Network, epConfig *network.EndpointSettings, sb *libnetwork.Sandbox, daemonDNS []string) ([]libnetwork.EndpointOption, error) {
func buildCreateEndpointOptions(c *container.Container, n *libnetwork.Network, epConfig *network.EndpointSettings, sb *libnetwork.Sandbox, daemonDNS []string) ([]libnetwork.EndpointOption, error) {
var (
bindings = make(nat.PortMap)
pbList []networktypes.PortBinding
Expand Down Expand Up @@ -1028,7 +1028,7 @@ func getEndpointPortMapInfo(ep *libnetwork.Endpoint) (nat.PortMap, error) {
}

// buildEndpointInfo sets endpoint-related fields on container.NetworkSettings based on the provided network and endpoint.
func buildEndpointInfo(networkSettings *internalnetwork.Settings, n libnetwork.Network, ep *libnetwork.Endpoint) error {
func buildEndpointInfo(networkSettings *internalnetwork.Settings, n *libnetwork.Network, ep *libnetwork.Endpoint) error {
if ep == nil {
return errors.New("endpoint cannot be nil")
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/network_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// getEndpointInNetwork returns the container's endpoint to the provided network.
func getEndpointInNetwork(name string, n libnetwork.Network) (*libnetwork.Endpoint, error) {
func getEndpointInNetwork(name string, n *libnetwork.Network) (*libnetwork.Endpoint, error) {
endpointName := strings.TrimPrefix(name, "/")
return n.EndpointByName(endpointName)
}
5 changes: 2 additions & 3 deletions daemon/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filte
until, _ := getUntilFromPruneFilters(pruneFilters)

// When the function returns true, the walk will stop.
l := func(nw libnetwork.Network) bool {
daemon.netController.WalkNetworks(func(nw *libnetwork.Network) bool {
select {
case <-ctx.Done():
// context cancelled
Expand Down Expand Up @@ -131,8 +131,7 @@ func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filte
}
rep.NetworksDeleted = append(rep.NetworksDeleted, nwName)
return false
}
daemon.netController.WalkNetworks(l)
})
return rep
}

Expand Down
16 changes: 8 additions & 8 deletions libnetwork/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ type epRecord struct {
lbIndex int
}

func (n *network) Services() map[string]ServiceInfo {
func (n *Network) Services() map[string]ServiceInfo {
eps := make(map[string]epRecord)

if !n.isClusterEligible() {
Expand Down Expand Up @@ -519,14 +519,14 @@ func (n *network) Services() map[string]ServiceInfo {
return sinfo
}

func (n *network) isClusterEligible() bool {
func (n *Network) isClusterEligible() bool {
if n.scope != datastore.SwarmScope || !n.driverIsMultihost() {
return false
}
return n.getController().getAgent() != nil
}

func (n *network) joinCluster() error {
func (n *Network) joinCluster() error {
if !n.isClusterEligible() {
return nil
}
Expand All @@ -539,7 +539,7 @@ func (n *network) joinCluster() error {
return agent.networkDB.JoinNetwork(n.ID())
}

func (n *network) leaveCluster() error {
func (n *Network) leaveCluster() error {
if !n.isClusterEligible() {
return nil
}
Expand Down Expand Up @@ -743,7 +743,7 @@ func (ep *Endpoint) deleteServiceInfoFromCluster(sb *Sandbox, fullRemove bool, m
return nil
}

func disableServiceInNetworkDB(a *agent, n *network, ep *Endpoint) {
func disableServiceInNetworkDB(a *agent, n *Network, ep *Endpoint) {
var epRec EndpointRecord

log.G(context.TODO()).Debugf("disableServiceInNetworkDB for %s %s", ep.svcName, ep.ID())
Expand Down Expand Up @@ -772,7 +772,7 @@ func disableServiceInNetworkDB(a *agent, n *network, ep *Endpoint) {
}
}

func (n *network) addDriverWatches() {
func (n *Network) addDriverWatches() {
if !n.isClusterEligible() {
return
}
Expand Down Expand Up @@ -808,7 +808,7 @@ func (n *network) addDriverWatches() {
}
}

func (n *network) cancelDriverWatches() {
func (n *Network) cancelDriverWatches() {
if !n.isClusterEligible() {
return
}
Expand Down Expand Up @@ -839,7 +839,7 @@ func (c *Controller) handleTableEvents(ch *events.Channel, fn func(events.Event)
}
}

func (n *network) handleDriverTableEvent(ev events.Event) {
func (n *Network) handleDriverTableEvent(ev events.Event) {
d, err := n.driver(false)
if err != nil {
log.G(context.TODO()).Errorf("Could not resolve driver %s while handling driver table event: %v", n.networkType, err)
Expand Down
Loading

0 comments on commit 7e4ffa3

Please sign in to comment.