Commit Graph

95 Commits

Author SHA1 Message Date
Rob Murray
468c2c814d Fix swarm network creation from a config-only network
Creating a swarm network from a config-only network failed
because the new EnableIPv4 wasn't validated/propagated
correctly.

So:
- Always initialise EnableIPv4 to true, including for a config
  only network, and on load from the store.
- Treat enableIPv4=true as the no-overridden state when checking
  params for a config-from network.
- Propagate settings from the config 'Network' objects attributes
  to its generic options, because the network driver only sees
  generic options.
  - This was happening already for Network.internal, after the
    config-only network options were processed. Move that to
    'applyConfigurationTo'.
  - Add enableIPv4/enableIpv6 - enableIPv6 will normaly be present
    anyway. But, for a network created pre-28.x and restored from
    the store, there was no entry for 'netlabel.EnableIpv4'.
- Extend TestSwarmScopedNetFromConfig to start a service and
  check it's ok.

Signed-off-by: Rob Murray <rob.murray@docker.com>
2025-02-24 17:48:23 +00:00
Sebastiaan van Stijn
a98ecc5f79 libnetwork: remove ErrInvalidID
It was only returned in a few places, and not used any different than
a "invalid parameter" error, so let's use a standard errdefs.ErrInvalidParameter

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-27 14:19:19 +01:00
Sebastiaan van Stijn
cef6fd2fa2 libnetwork: remove Network.EndpointByID as it must not be used
commit 80c44b4b2e removed uses of this
method and added a comment that it should never be used;

> EndpointByID should *never* be called as it's going to create a 2nd instance
> of an Endpoint. The first one lives in the Sandbox the endpoint is attached to.
> Instead, the endpoint should be retrieved by calling [Sandbox.Endpoints()].

Given that the only use of this method is in tests, we can remove if altogether.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-27 10:49:40 +01:00
Sebastiaan van Stijn
d34ca256b8 libnetwork: TestNetworkConfig: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:35:22 +01:00
Sebastiaan van Stijn
c6f0fe5fc0 libnetwork: parallelTester.Do: use errdefs for error assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:35:22 +01:00
Sebastiaan van Stijn
11a6cb5fac libnetwork: TestEndpointJoin: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:35:21 +01:00
Sebastiaan van Stijn
ea7d17376e libnetwork: TestInvalidRemoteDriver: use gotest.tools for error assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:35:21 +01:00
Sebastiaan van Stijn
01c87cc809 libnetwork: TestContainerInvalidLeave: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:35:21 +01:00
Sebastiaan van Stijn
9472c860de libnetwork: TestEndpointMultipleJoins: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:35:21 +01:00
Sebastiaan van Stijn
ab9d250876 libnetwork: TestEndpointDeleteWithActiveContainer: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:35:21 +01:00
Sebastiaan van Stijn
56debcfc1c libnetwork: TestNetworkQuery: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:35:20 +01:00
Sebastiaan van Stijn
874fb4d2f4 libnetwork: TestControllerQuery: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:35:13 +01:00
Sebastiaan van Stijn
53bf2e8960 libnetwork: TestUnknownEndpoint: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:17:43 +01:00
Sebastiaan van Stijn
895ee1214a libnetwork: TestDuplicateEndpoint: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:17:43 +01:00
Sebastiaan van Stijn
678a8a27f5 libnetwork: TestUnknownNetwork: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:17:42 +01:00
Sebastiaan van Stijn
9304d98757 libnetwork: TestDeleteNetworkWithActiveEndpoints: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:17:42 +01:00
Sebastiaan van Stijn
01b0fe1172 libnetwork: TestNetworkName: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:17:42 +01:00
Sebastiaan van Stijn
24df497674 libnetwork: TestNilRemoteDriver: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:17:42 +01:00
Sebastiaan van Stijn
532d36c251 libnetwork: TestUnknownDriver: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:17:42 +01:00
Sebastiaan van Stijn
400ad04c74 libnetwork: TestNull: use gotest.tools for errdefs assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-26 21:17:39 +01:00
Sebastiaan van Stijn
f2804e04b2 Merge pull request #49329 from thaJeztah/libnetwork_use_errdefs_step2
libnetwork: rewrite some tests to use gotest.tools
2025-01-23 11:30:03 +01:00
Sebastiaan van Stijn
c8a456a5a2 Merge pull request #49327 from thaJeztah/libnetwork_type_conversions
libnetwork: remove some redundant type-conversions
2025-01-23 11:28:18 +01:00
Sebastiaan van Stijn
203d6530d0 libnetwork: use gotest.tools assertions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-22 22:20:43 +01:00
Sebastiaan van Stijn
a39bee9b71 libnetwork: suppress some unhandled errors in tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-22 22:06:40 +01:00
Sebastiaan van Stijn
376ff6add6 libnetwork: use assert.Check in defers
Prefering Check here over NilError so that all defers
wil be executed, instead of potentially failing on the
first one.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-22 22:06:40 +01:00
Sebastiaan van Stijn
71c9a45672 libnetwork: use assert.NilError (step 2)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-22 22:06:38 +01:00
Sebastiaan van Stijn
231012996f libnetwork: use assert.NilError (step 1)
First step; this is just a find and replace

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-22 22:05:58 +01:00
Rob Murray
94444587d4 Merge pull request #48808 from robmry/v6only/gratuitous_arp
IPv6 only: use random MAC addresses
2025-01-22 18:52:21 +00:00
Rob Murray
bc130f367d bridge/macvlan endpoints always use a random MAC address
Now a gratuitous/unsolicted ARP is sent, there's no need to
use an IPv4-based MAC address to preserve arp-cache mappings
between an endpoint's IP addresses and its MAC addresses.

Because a random MAC address is used for the default bridge,
it no longer makes sense to derive container IPv6 addresses
from the MAC address. This "postIPv6" behaviour was needed
before IPv6 addresses could be configured, but not now. So,
IPv6 addresses will now be IPAM-allocated on the default
bridge network, just as they are for user-defined bridges.

Signed-off-by: Rob Murray <rob.murray@docker.com>
2025-01-22 16:59:27 +00:00
Sebastiaan van Stijn
3955c021ff libnetwork: remove redundant type conversion
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-22 12:51:39 +01:00
Sebastiaan van Stijn
6e6cf999ed libnetwork: remove isNotfound test-utility
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-22 12:45:22 +01:00
Sebastiaan van Stijn
4f5ec9994c pkg/reexec: deprecate and migrate to github.com/moby/sys/reexec
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-12-19 10:12:58 +01:00
Albin Kerouanton
f053beb34c libnet/osl: drop netns path GC
Commit 3ec19ff62b introduced a GC goroutine to delete files where netns
were mounted. It was primarly added to work around a race in kernel
3.18-4.0.1. Since no distros we support are using such old kernels,
there's no need to keep this code around.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-12-16 11:40:18 +01:00
Rob Murray
b656cffe4c Disallow "network generic data" with type options.Generic
Field 'generic' in 'libnetwork.Network' is used to store driver options,
it has type 'options.Generic', which is 'map[string]any'.

In that map, there may be a key 'netlabel.GenericData' holding options
known as "network generic options", used for options like:
  -o com.docker.network.bridge.name=br-foo

The value type for key 'netlabel.GenericData' is always 'map[string]string'
when created via an API request. But, some unit tests use type
'options.Generic'.

That works because the bridge, ipvlan and macvlan drivers look for type
'options.Generic' as well as 'map[string]string'. If they find
'options.Generic', Go reflection is used to map keys to fields of the
config struct with the expectation that the value has the same type as
that field. But, that's only used in unit tests (so the tests aren't
testing the same code path as the API would use). The 'options.Generic'
form of the bridge name option is:
  "BridgeName": "br-foo"
(Because "BridgeName" is the name of the field in the bridge driver's
network config struct.)

The libnetwork code expects "network generic options" to have type
'map[string]string', and makes no provision for 'options.Generic'. So,
for example, function Network.DriverOptions will panic if called when
'Network.generic[netlabel.GenericData]' has type 'options.Generic'.

The type of 'Network.generic[netlabel.GenericData]' can't be statically
checked, because it's just a field in a 'map[string]any'.

So - remove the driver code that converts "network generic options"
from type 'options.Generic', as it's only used in tests and just makes
things more confusing.

This should reduce the chances of things appearing to work when the
type is wrong, and converting unit tests to use 'map[string]string'
means they're testing the right thing.

Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-11-19 20:13:39 +00:00
Sebastiaan van Stijn
53e48e8623 libnetwork: remove redundant capturing of loop vars (copyloopvar)
libnetwork/endpoint_info.go:467:3: The copy of the 'for' variable "r" can be deleted (Go 1.22+) (copyloopvar)
            r := r
            ^
    libnetwork/firewall_linux_test.go:52:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    libnetwork/libnetwork_internal_test.go:403:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    libnetwork/libnetwork_linux_test.go:2045:3: The copy of the 'for' variable "i" can be deleted (Go 1.22+) (copyloopvar)
            i := i
            ^
    libnetwork/libnetwork_linux_test.go:2165:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-12 14:02:09 +01:00
Rob Murray
4ac7f14e99 Add EnableIPv4 to the bridge driver
Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-11-07 17:17:21 +00:00
Rob Murray
fbb595cb66 Remove libnetwork_test.TestMain
It only removed the test host's libnet Bolt db file, and didn't
need to do that because the tests use a TempDir for it.

Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-10-30 09:31:13 +00:00
Albin Kerouanton
ed08486ec7 libnet/ds: simplify datastore.New()
That function was needlessly complex. Instead of relying on a struct and
a sub-struct, it now just takes two string params: a path and a bucket
name.

Libnetwork config is now initialized with default values. A new struct
is introduced in libnetwork/config to let tests customize the path and
bucket name.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-09-20 08:48:16 +02:00
Albin Kerouanton
3ca91a6a12 libnetwork: unit tests: drop OptionBoltdbWithRandomDBFile
This option fn was defining a custom directory, file name and bucket
name for boltdb. Users can only change data-dir through `OptionDataDir`.
Better reuse that function instead, that'll make refactorings easier.

It won't set a custom bucket name or file name as `OptionBoltdbWithRandomDBFile`
was doing, but that's not needed since every test will use a different
temp dir anyway.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-09-19 13:46:56 +02:00
Rob Murray
00bf437d84 Add nlutil functions to retry on netlink EINTR
A recent change to the vishvananda/netlink package exposes
NLM_F_DUMP_INTR in some netlink responses as an EINTR (with
no data).

Retry the requests when that happens, up to five times, before
returning the error. The limit of five is arbitrary, on most
systems a single retry will be rare but, there's no guarantee
that a retry will succeed. So, on a very busy or misbehaving
system the error may still be returned. In most cases, this
will lead to failure of the operation being attempted (which
may lead to daemon startup failure, network initialisation
failure etc).

Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-09-15 12:28:58 +01:00
Rob Murray
c0a6145f6d Enable IPv4 in the unit tests that will need it.
Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-08-01 16:14:55 +01:00
Albin Kerouanton
b7186bdfc8 libnet: Sandbox: add ctx to SetKey
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-06-14 15:25:08 +02:00
Albin Kerouanton
cec0d50361 libnet: add ctx to Sandbox.Destroy()
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-06-13 17:13:43 +02:00
Albin Kerouanton
af23a024a1 libnet: Endpoint: add ctx to Join and Leave
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-06-13 17:00:05 +02:00
Albin Kerouanton
9391052700 libnet: Add ctx to NewSandbox
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-06-13 16:52:48 +02:00
Albin Kerouanton
c5c1d133ef libnet/driverapi: Add ctx to Join
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-06-13 16:45:54 +02:00
Albin Kerouanton
8dcded102e libnet: add OTel spans to CreateEndpoint
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-06-13 16:45:31 +02:00
Albin Kerouanton
32418e9753 daemon: set the default local addr pool if none configured
Until this commit, the default local address pool was initialized by the
defaultipam driver if none was provided by libnet / the daemon.

Now, defaultipam errors out if none is passed and instead the daemon is
made responsible for initializing it with the default values if the user
don'te set the related config parameter.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-05-29 10:47:59 +02:00
Albin Kerouanton
c5376e534c libnet/ipams/null: move driver name to its pkg
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-04-26 17:28:29 +02:00
Albin Kerouanton
f2387f3632 libnet/ipams/defaultipam: move driver name to its pkg
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-04-26 17:28:29 +02:00