浏览代码

Merge pull request #44233 from thaJeztah/libnetwork_linting

libnetwork: fix some (linting) issues
Sebastiaan van Stijn 2 年之前
父节点
当前提交
b14d203884

+ 13 - 15
libnetwork/drivers/overlay/ov_network.go

@@ -539,25 +539,23 @@ func checkOverlap(nw *net.IPNet) error {
 }
 }
 
 
 func (n *network) restoreSubnetSandbox(s *subnet, brName, vxlanName string) error {
 func (n *network) restoreSubnetSandbox(s *subnet, brName, vxlanName string) error {
-	sbox := n.sbox
-
 	// restore overlay osl sandbox
 	// restore overlay osl sandbox
-	Ifaces := make(map[string][]osl.IfaceOption)
-	brIfaceOption := make([]osl.IfaceOption, 2)
-	brIfaceOption = append(brIfaceOption, sbox.InterfaceOptions().Address(s.gwIP))
-	brIfaceOption = append(brIfaceOption, sbox.InterfaceOptions().Bridge(true))
-	Ifaces[brName+"+br"] = brIfaceOption
-
-	err := sbox.Restore(Ifaces, nil, nil, nil)
-	if err != nil {
+	ifaces := map[string][]osl.IfaceOption{
+		brName + "+br": {
+			n.sbox.InterfaceOptions().Address(s.gwIP),
+			n.sbox.InterfaceOptions().Bridge(true),
+		},
+	}
+	if err := n.sbox.Restore(ifaces, nil, nil, nil); err != nil {
 		return err
 		return err
 	}
 	}
 
 
-	Ifaces = make(map[string][]osl.IfaceOption)
-	vxlanIfaceOption := make([]osl.IfaceOption, 1)
-	vxlanIfaceOption = append(vxlanIfaceOption, sbox.InterfaceOptions().Master(brName))
-	Ifaces[vxlanName+"+vxlan"] = vxlanIfaceOption
-	return sbox.Restore(Ifaces, nil, nil, nil)
+	ifaces = map[string][]osl.IfaceOption{
+		vxlanName + "+vxlan": {
+			n.sbox.InterfaceOptions().Master(brName),
+		},
+	}
+	return n.sbox.Restore(ifaces, nil, nil, nil)
 }
 }
 
 
 func (n *network) setupSubnetSandbox(s *subnet, brName, vxlanName string) error {
 func (n *network) setupSubnetSandbox(s *subnet, brName, vxlanName string) error {

+ 6 - 6
libnetwork/libnetwork_linux_test.go

@@ -618,12 +618,12 @@ func TestEnableIPv6(t *testing.T) {
 
 
 	tmpResolvConf := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\nnameserver 2001:4860:4860::8888\n")
 	tmpResolvConf := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\nnameserver 2001:4860:4860::8888\n")
 	expectedResolvConf := []byte("search pommesfrites.fr\nnameserver 127.0.0.11\nnameserver 2001:4860:4860::8888\noptions ndots:0\n")
 	expectedResolvConf := []byte("search pommesfrites.fr\nnameserver 127.0.0.11\nnameserver 2001:4860:4860::8888\noptions ndots:0\n")
-	//take a copy of resolv.conf for restoring after test completes
+	// take a copy of resolv.conf for restoring after test completes
 	resolvConfSystem, err := os.ReadFile("/etc/resolv.conf")
 	resolvConfSystem, err := os.ReadFile("/etc/resolv.conf")
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
-	//cleanup
+	// cleanup
 	defer func() {
 	defer func() {
 		if err := os.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
 		if err := os.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
 			t.Fatal(err)
 			t.Fatal(err)
@@ -696,12 +696,12 @@ func TestResolvConfHost(t *testing.T) {
 
 
 	tmpResolvConf := []byte("search localhost.net\nnameserver 127.0.0.1\nnameserver 2001:4860:4860::8888\n")
 	tmpResolvConf := []byte("search localhost.net\nnameserver 127.0.0.1\nnameserver 2001:4860:4860::8888\n")
 
 
-	//take a copy of resolv.conf for restoring after test completes
+	// take a copy of resolv.conf for restoring after test completes
 	resolvConfSystem, err := os.ReadFile("/etc/resolv.conf")
 	resolvConfSystem, err := os.ReadFile("/etc/resolv.conf")
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
-	//cleanup
+	// cleanup
 	defer func() {
 	defer func() {
 		if err := os.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
 		if err := os.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
 			t.Fatal(err)
 			t.Fatal(err)
@@ -779,12 +779,12 @@ func TestResolvConf(t *testing.T) {
 	expectedResolvConf1 := []byte("search pommesfrites.fr\nnameserver 127.0.0.11\noptions ndots:0\n")
 	expectedResolvConf1 := []byte("search pommesfrites.fr\nnameserver 127.0.0.11\noptions ndots:0\n")
 	tmpResolvConf3 := []byte("search pommesfrites.fr\nnameserver 113.34.56.78\n")
 	tmpResolvConf3 := []byte("search pommesfrites.fr\nnameserver 113.34.56.78\n")
 
 
-	//take a copy of resolv.conf for restoring after test completes
+	// take a copy of resolv.conf for restoring after test completes
 	resolvConfSystem, err := os.ReadFile("/etc/resolv.conf")
 	resolvConfSystem, err := os.ReadFile("/etc/resolv.conf")
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
-	//cleanup
+	// cleanup
 	defer func() {
 	defer func() {
 		if err := os.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
 		if err := os.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
 			t.Fatal(err)
 			t.Fatal(err)

+ 34 - 32
libnetwork/osl/sandbox.go

@@ -20,26 +20,26 @@ const (
 // Sandbox represents a network sandbox, identified by a specific key.  It
 // Sandbox represents a network sandbox, identified by a specific key.  It
 // holds a list of Interfaces, routes etc, and more can be added dynamically.
 // holds a list of Interfaces, routes etc, and more can be added dynamically.
 type Sandbox interface {
 type Sandbox interface {
-	// The path where the network namespace is mounted.
+	// Key returns the path where the network namespace is mounted.
 	Key() string
 	Key() string
 
 
-	// Add an existing Interface to this sandbox. The operation will rename
+	// AddInterface adds an existing Interface to this sandbox. The operation will rename
 	// from the Interface SrcName to DstName as it moves, and reconfigure the
 	// from the Interface SrcName to DstName as it moves, and reconfigure the
 	// interface according to the specified settings. The caller is expected
 	// interface according to the specified settings. The caller is expected
 	// to only provide a prefix for DstName. The AddInterface api will auto-generate
 	// to only provide a prefix for DstName. The AddInterface api will auto-generate
 	// an appropriate suffix for the DstName to disambiguate.
 	// an appropriate suffix for the DstName to disambiguate.
 	AddInterface(SrcName string, DstPrefix string, options ...IfaceOption) error
 	AddInterface(SrcName string, DstPrefix string, options ...IfaceOption) error
 
 
-	// Set default IPv4 gateway for the sandbox
+	// SetGateway sets the default IPv4 gateway for the sandbox.
 	SetGateway(gw net.IP) error
 	SetGateway(gw net.IP) error
 
 
-	// Set default IPv6 gateway for the sandbox
+	// SetGatewayIPv6 sets the default IPv6 gateway for the sandbox.
 	SetGatewayIPv6(gw net.IP) error
 	SetGatewayIPv6(gw net.IP) error
 
 
-	// Unset the previously set default IPv4 gateway in the sandbox
+	// UnsetGateway the previously set default IPv4 gateway in the sandbox.
 	UnsetGateway() error
 	UnsetGateway() error
 
 
-	// Unset the previously set default IPv6 gateway in the sandbox
+	// UnsetGatewayIPv6 unsets the previously set default IPv6 gateway in the sandbox.
 	UnsetGatewayIPv6() error
 	UnsetGatewayIPv6() error
 
 
 	// GetLoopbackIfaceName returns the name of the loopback interface
 	// GetLoopbackIfaceName returns the name of the loopback interface
@@ -52,13 +52,13 @@ type Sandbox interface {
 	RemoveAliasIP(ifName string, ip *net.IPNet) error
 	RemoveAliasIP(ifName string, ip *net.IPNet) error
 
 
 	// DisableARPForVIP disables ARP replies and requests for VIP addresses
 	// DisableARPForVIP disables ARP replies and requests for VIP addresses
-	// on a particular interface
+	// on a particular interface.
 	DisableARPForVIP(ifName string) error
 	DisableARPForVIP(ifName string) error
 
 
-	// Add a static route to the sandbox.
+	// AddStaticRoute adds a static route to the sandbox.
 	AddStaticRoute(*types.StaticRoute) error
 	AddStaticRoute(*types.StaticRoute) error
 
 
-	// Remove a static route from the sandbox.
+	// RemoveStaticRoute removes a static route from the sandbox.
 	RemoveStaticRoute(*types.StaticRoute) error
 	RemoveStaticRoute(*types.StaticRoute) error
 
 
 	// AddNeighbor adds a neighbor entry into the sandbox.
 	// AddNeighbor adds a neighbor entry into the sandbox.
@@ -67,25 +67,25 @@ type Sandbox interface {
 	// DeleteNeighbor deletes neighbor entry from the sandbox.
 	// DeleteNeighbor deletes neighbor entry from the sandbox.
 	DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr, osDelete bool) error
 	DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr, osDelete bool) error
 
 
-	// Returns an interface with methods to set neighbor options.
+	// NeighborOptions returns an interface with methods to set neighbor options.
 	NeighborOptions() NeighborOptionSetter
 	NeighborOptions() NeighborOptionSetter
 
 
-	// Returns an interface with methods to set interface options.
+	// InterfaceOptions an interface with methods to set interface options.
 	InterfaceOptions() IfaceOptionSetter
 	InterfaceOptions() IfaceOptionSetter
 
 
-	//Invoke
+	// InvokeFunc invoke a function in the network namespace.
 	InvokeFunc(func()) error
 	InvokeFunc(func()) error
 
 
-	// Returns an interface with methods to get sandbox state.
+	// Info returns an interface with methods to get sandbox state.
 	Info() Info
 	Info() Info
 
 
-	// Destroy the sandbox
+	// Destroy destroys the sandbox.
 	Destroy() error
 	Destroy() error
 
 
-	// restore sandbox
+	// Restore restores the sandbox.
 	Restore(ifsopt map[string][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error
 	Restore(ifsopt map[string][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error
 
 
-	// ApplyOSTweaks applies operating system specific knobs on the sandbox
+	// ApplyOSTweaks applies operating system specific knobs on the sandbox.
 	ApplyOSTweaks([]SandboxType)
 	ApplyOSTweaks([]SandboxType)
 }
 }
 
 
@@ -111,7 +111,7 @@ type IfaceOptionSetter interface {
 	// Address returns an option setter to set IPv4 address.
 	// Address returns an option setter to set IPv4 address.
 	Address(*net.IPNet) IfaceOption
 	Address(*net.IPNet) IfaceOption
 
 
-	// Address returns an option setter to set IPv6 address.
+	// AddressIPv6 returns an option setter to set IPv6 address.
 	AddressIPv6(*net.IPNet) IfaceOption
 	AddressIPv6(*net.IPNet) IfaceOption
 
 
 	// LinkLocalAddresses returns an option setter to set the link-local IP addresses.
 	// LinkLocalAddresses returns an option setter to set the link-local IP addresses.
@@ -122,7 +122,7 @@ type IfaceOptionSetter interface {
 	// previously added interface of type bridge.
 	// previously added interface of type bridge.
 	Master(string) IfaceOption
 	Master(string) IfaceOption
 
 
-	// Address returns an option setter to set interface routes.
+	// Routes returns an option setter to set interface routes.
 	Routes([]*net.IPNet) IfaceOption
 	Routes([]*net.IPNet) IfaceOption
 }
 }
 
 
@@ -130,20 +130,21 @@ type IfaceOptionSetter interface {
 // the driver wants to place in the sandbox which includes
 // the driver wants to place in the sandbox which includes
 // interfaces, routes and gateway
 // interfaces, routes and gateway
 type Info interface {
 type Info interface {
-	// The collection of Interface previously added with the AddInterface
+	// Interfaces returns the collection of Interface previously added with the AddInterface
 	// method. Note that this doesn't include network interfaces added in any
 	// method. Note that this doesn't include network interfaces added in any
 	// other way (such as the default loopback interface which is automatically
 	// other way (such as the default loopback interface which is automatically
 	// created on creation of a sandbox).
 	// created on creation of a sandbox).
 	Interfaces() []Interface
 	Interfaces() []Interface
 
 
-	// IPv4 gateway for the sandbox.
+	// Gateway returns the IPv4 gateway for the sandbox.
 	Gateway() net.IP
 	Gateway() net.IP
 
 
-	// IPv6 gateway for the sandbox.
+	// GatewayIPv6 returns the IPv6 gateway for the sandbox.
 	GatewayIPv6() net.IP
 	GatewayIPv6() net.IP
 
 
-	// Additional static routes for the sandbox.  (Note that directly
-	// connected routes are stored on the particular interface they refer to.)
+	// StaticRoutes returns additional static routes for the sandbox. Note that
+	// directly connected routes are stored on the particular interface they
+	// refer to.
 	StaticRoutes() []*types.StaticRoute
 	StaticRoutes() []*types.StaticRoute
 
 
 	// TODO: Add ip tables etc.
 	// TODO: Add ip tables etc.
@@ -155,28 +156,29 @@ type Info interface {
 // namespace to DstName in a different net namespace with the appropriate
 // namespace to DstName in a different net namespace with the appropriate
 // network settings.
 // network settings.
 type Interface interface {
 type Interface interface {
-	// The name of the interface in the origin network namespace.
+	// SrcName returns the name of the interface in the origin network namespace.
 	SrcName() string
 	SrcName() string
 
 
-	// The name that will be assigned to the interface once moves inside a
-	// network namespace. When the caller passes in a DstName, it is only
-	// expected to pass a prefix. The name will modified with an appropriately
+	// DstName returns the name that will be assigned to the interface once
+	// moved inside a network namespace. When the caller passes in a DstName,
+	// it is only expected to pass a prefix. The name will be modified with an
 	// auto-generated suffix.
 	// auto-generated suffix.
 	DstName() string
 	DstName() string
 
 
-	// IPv4 address for the interface.
+	// Address returns the IPv4 address for the interface.
 	Address() *net.IPNet
 	Address() *net.IPNet
 
 
-	// IPv6 address for the interface.
+	// AddressIPv6 returns the IPv6 address for the interface.
 	AddressIPv6() *net.IPNet
 	AddressIPv6() *net.IPNet
 
 
-	// LinkLocalAddresses returns the link-local IP addresses assigned to the interface.
+	// LinkLocalAddresses returns the link-local IP addresses assigned to the
+	// interface.
 	LinkLocalAddresses() []*net.IPNet
 	LinkLocalAddresses() []*net.IPNet
 
 
-	// IP routes for the interface.
+	// Routes returns IP routes for the interface.
 	Routes() []*net.IPNet
 	Routes() []*net.IPNet
 
 
-	// Bridge returns true if the interface is a bridge
+	// Bridge returns true if the interface is a bridge.
 	Bridge() bool
 	Bridge() bool
 
 
 	// Master returns the srcname of the master interface for this interface.
 	// Master returns the srcname of the master interface for this interface.

+ 6 - 5
libnetwork/sandbox.go

@@ -795,7 +795,6 @@ func (sb *sandbox) restoreOslSandbox() error {
 	// restore osl sandbox
 	// restore osl sandbox
 	Ifaces := make(map[string][]osl.IfaceOption)
 	Ifaces := make(map[string][]osl.IfaceOption)
 	for _, ep := range sb.endpoints {
 	for _, ep := range sb.endpoints {
-		var ifaceOptions []osl.IfaceOption
 		ep.Lock()
 		ep.Lock()
 		joinInfo := ep.joinInfo
 		joinInfo := ep.joinInfo
 		i := ep.iface
 		i := ep.iface
@@ -806,7 +805,10 @@ func (sb *sandbox) restoreOslSandbox() error {
 			continue
 			continue
 		}
 		}
 
 
-		ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().Address(i.addr), sb.osSbox.InterfaceOptions().Routes(i.routes))
+		ifaceOptions := []osl.IfaceOption{
+			sb.osSbox.InterfaceOptions().Address(i.addr),
+			sb.osSbox.InterfaceOptions().Routes(i.routes),
+		}
 		if i.addrv6 != nil && i.addrv6.IP.To16() != nil {
 		if i.addrv6 != nil && i.addrv6.IP.To16() != nil {
 			ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().AddressIPv6(i.addrv6))
 			ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().AddressIPv6(i.addrv6))
 		}
 		}
@@ -816,7 +818,7 @@ func (sb *sandbox) restoreOslSandbox() error {
 		if len(i.llAddrs) != 0 {
 		if len(i.llAddrs) != 0 {
 			ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().LinkLocalAddresses(i.llAddrs))
 			ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().LinkLocalAddresses(i.llAddrs))
 		}
 		}
-		Ifaces[fmt.Sprintf("%s+%s", i.srcName, i.dstPrefix)] = ifaceOptions
+		Ifaces[i.srcName+i.dstPrefix] = ifaceOptions
 		if joinInfo != nil {
 		if joinInfo != nil {
 			routes = append(routes, joinInfo.StaticRoutes...)
 			routes = append(routes, joinInfo.StaticRoutes...)
 		}
 		}
@@ -831,8 +833,7 @@ func (sb *sandbox) restoreOslSandbox() error {
 	}
 	}
 
 
 	// restore osl sandbox
 	// restore osl sandbox
-	err := sb.osSbox.Restore(Ifaces, routes, gwep.joinInfo.gw, gwep.joinInfo.gw6)
-	return err
+	return sb.osSbox.Restore(Ifaces, routes, gwep.joinInfo.gw, gwep.joinInfo.gw6)
 }
 }
 
 
 func (sb *sandbox) populateNetworkResources(ep *endpoint) error {
 func (sb *sandbox) populateNetworkResources(ep *endpoint) error {

+ 3 - 3
libnetwork/sandbox_dns_unix.go

@@ -20,8 +20,8 @@ import (
 
 
 const (
 const (
 	defaultPrefix = "/var/lib/docker/network/files"
 	defaultPrefix = "/var/lib/docker/network/files"
-	dirPerm       = 0755
-	filePerm      = 0644
+	dirPerm       = 0o755
+	filePerm      = 0o644
 )
 )
 
 
 func (sb *sandbox) startResolver(restore bool) {
 func (sb *sandbox) startResolver(restore bool) {
@@ -332,7 +332,7 @@ func (sb *sandbox) updateDNS(ipv6Enabled bool) error {
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	err = os.WriteFile(sb.config.resolvConfPath, newRC.Content, 0644) //nolint:gosec // gosec complains about perms here, which must be 0644 in this case
+	err = os.WriteFile(sb.config.resolvConfPath, newRC.Content, filePerm)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}

+ 4 - 10
libnetwork/sandbox_dns_windows.go

@@ -9,27 +9,21 @@ import (
 
 
 // Stub implementations for DNS related functions
 // Stub implementations for DNS related functions
 
 
-func (sb *sandbox) startResolver(bool) {
-}
+func (sb *sandbox) startResolver(bool) {}
 
 
 func (sb *sandbox) setupResolutionFiles() error {
 func (sb *sandbox) setupResolutionFiles() error {
 	return nil
 	return nil
 }
 }
 
 
-func (sb *sandbox) restorePath() {
-}
+func (sb *sandbox) restorePath() {}
 
 
 func (sb *sandbox) updateHostsFile(ifaceIP []string) error {
 func (sb *sandbox) updateHostsFile(ifaceIP []string) error {
 	return nil
 	return nil
 }
 }
 
 
-func (sb *sandbox) addHostsEntries(recs []etchosts.Record) {
-
-}
-
-func (sb *sandbox) deleteHostsEntries(recs []etchosts.Record) {
+func (sb *sandbox) addHostsEntries(recs []etchosts.Record) {}
 
 
-}
+func (sb *sandbox) deleteHostsEntries(recs []etchosts.Record) {}
 
 
 func (sb *sandbox) updateDNS(ipv6Enabled bool) error {
 func (sb *sandbox) updateDNS(ipv6Enabled bool) error {
 	return nil
 	return nil