Repository: moby/moby
File: daemon/network.go (Go)

Code before:
	if !containertypes.NetworkMode(nwName).IsUserDefined() {
		createOptions = append(createOptions, libnetwork.CreateOptionDisableResolution())
	}

	epOpts, err := buildPortsRelatedCreateEndpointOptions(c, n, sb)
	if err != nil {
		return nil, err
	}
	createOptions = append(createOptions, epOpts...)

	// On Windows, DNS config is a per-adapter config option whereas on Linux, it's a sandbox-wide parameter; hence why
	// we're dealing with DNS config both here and in buildSandboxOptions. Following DNS options are only honored by
	// Windows netdrivers, whereas DNS options in buildSandboxOptions are only honored by Linux netdrivers.
	if !n.Internal() {
		if len(c.HostConfig.DNS) > 0 {
			createOptions = append(createOptions, libnetwork.CreateOptionDNS(c.HostConfig.DNS))
		} else if len(daemonDNS) > 0 {
			createOptions = append(createOptions, libnetwork.CreateOptionDNS(daemonDNS))
		}
	}

	createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(genericOptions))

	// IPv6 may be enabled on the network, but disabled in the container.
	if n.IPv6Enabled() {
		if sbIPv6, ok := sb.IPv6Enabled(); ok && !sbIPv6 {
			createOptions = append(createOptions, libnetwork.CreateOptionDisableIPv6())
		}
	}

	if path, ok := sb.NetnsPath(); ok {
		createOptions = append(createOptions, libnetwork.WithNetnsPath(path))
	}

	return createOptions, nil
}

// buildPortsRelatedCreateEndpointOptions returns the appropriate endpoint options to apply config related to port
// mapping and exposed ports.
func buildPortsRelatedCreateEndpointOptions(c *container.Container, n *libnetwork.Network, sb *libnetwork.Sandbox) ([]libnetwork.EndpointOption, error) {
	// 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.Internal() || len(getPortMapInfo(sb)) > 0 {
		return nil, nil
	}

	bindings := make(nat.PortMap)
	if c.HostConfig.PortBindings != nil {
		for p, b := range c.HostConfig.PortBindings {
			bindings[p] = []nat.PortBinding{}

Review comment (suggestion):
I got confused for a second because the `InterfaceInfo` interface change didn't match this signature, but they're separate  things, just with the same name 😂 

Code after:
	if err != nil {
		return nil, err
	}
	createOptions = append(createOptions, epOpts...)

	// On Windows, DNS config is a per-adapter config option whereas on Linux, it's a sandbox-wide parameter; hence why
	// we're dealing with DNS config both here and in buildSandboxOptions. Following DNS options are only honored by
	// Windows netdrivers, whereas DNS options in buildSandboxOptions are only honored by Linux netdrivers.
	if !n.Internal() {
		if len(c.HostConfig.DNS) > 0 {
			createOptions = append(createOptions, libnetwork.CreateOptionDNS(c.HostConfig.DNS))
		} else if len(daemonDNS) > 0 {
			createOptions = append(createOptions, libnetwork.CreateOptionDNS(daemonDNS))
		}
	}

	createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(genericOptions))

	// IPv6 may be enabled on the network, but disabled in the container.
	if n.IPv6Enabled() {
		if sbIPv6, ok := sb.IPv6Enabled(); ok && !sbIPv6 {
			createOptions = append(createOptions, libnetwork.CreateOptionDisableIPv6())
		}
	}

	if path, ok := sb.NetnsPath(); ok {
		createOptions = append(createOptions, libnetwork.WithNetnsPath(path))
	}

	return createOptions, nil
}

// buildPortsRelatedCreateEndpointOptions returns the appropriate endpoint options to apply config related to port
// mapping and exposed ports.
func buildPortsRelatedCreateEndpointOptions(c *container.Container, n *libnetwork.Network, sb *libnetwork.Sandbox) ([]libnetwork.EndpointOption, error) {
	// 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.Internal() || len(getPortMapInfo(sb)) > 0 {
		return nil, nil
	}

	bindings := make(nat.PortMap)
	if c.HostConfig.PortBindings != nil {
		for p, b := range c.HostConfig.PortBindings {
			bindings[p] = []nat.PortBinding{}
			for _, bb := range b {
				bindings[p] = append(bindings[p], nat.PortBinding{
					HostIP:   bb.HostIP,
					HostPort: bb.HostPort,
				})