Skip to content

libnetwork: reduce usage of shared mutable state in ipamutils#44827

Merged
corhere merged 4 commits intomoby:masterfrom
corhere:libnw/drop-electinterfaceaddresses
Jan 26, 2023
Merged

libnetwork: reduce usage of shared mutable state in ipamutils#44827
corhere merged 4 commits intomoby:masterfrom
corhere:libnw/drop-electinterfaceaddresses

Conversation

@corhere
Copy link
Copy Markdown
Contributor

@corhere corhere commented Jan 17, 2023

- What I did
Reduced the scope of the shared mutable state in package "./libnetwork/ipamutils" down to only the bits which require Swarmkit changes to modify.

- How I did it
I deleted the function "./libnetwork/netutils".ElectInterfaceAddresses which references the mutable state in ipamutils. I determined that the one non-test reference to that function did not actually need the behaviour which is implemented using the shared mutable state. When the interface doesn't exist, the function would pick an available IP network from ipamutils.PredefinedLocalScopeDefaultNetworks. This is the same list of networks which the builtin IPAM driver allocates from, and by extension, the list of networks which libnetwork allocates from when creating a new libnetwork network without the preferred-subnet option set in its IPAM config.

I eliminated the implicit configuration of ipam.Allocator from the ipamutils state by refactoring it to require the predefined address spaces be passed into the constructor. The IPAM driver initializer still has to pull the global address space from the ipamutils state to support Swarmkit, but it's still an improvement over the status quo. This refactor allows the local-scope default address space list in ipamutils to be made immutable!

- How to verify it
CI?

- Description for the changelog

N/A; refactoring without any end-user visible changes (hopefully)

- A picture of a cute animal (not mandatory but encouraged)

@corhere corhere force-pushed the libnw/drop-electinterfaceaddresses branch 2 times, most recently from 54ca5b5 to ed0ab79 Compare January 17, 2023 16:31
@corhere corhere marked this pull request as ready for review January 17, 2023 17:27
@corhere corhere requested review from cpuguy83, neersighted, samuelkarp and thaJeztah and removed request for cpuguy83 January 17, 2023 17:27
@corhere corhere added status/2-code-review area/networking Networking kind/refactor PR's that refactor, or clean-up code labels Jan 17, 2023
Comment thread daemon/daemon_linux_test.go

// NewAllocator returns an instance of libnetwork ipam
func NewAllocator(lcDs, glDs datastore.DataStore) (*Allocator, error) {
func NewAllocator(lcDs, glDs datastore.DataStore, lcAs, glAs []*net.IPNet) (*Allocator, error) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we're changing this signature; would it make sense to have a struct to pass these options? It feels a bit error-prone with multiple arguments of the same type

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be changing the signature again in a follow-up. (lcDs and glDs are always nil and therefore can go away.) I find the compile errors when changing the signature helpful to my refactoring efforts.

Comment thread daemon/daemon_unix.go
The netutils.ElectInterfaceAddresses function is only used in one place
outside of tests: in the daemon, to configure the default bridge
network. The function is also messy to reason about as it references the
shared mutable state of ipamutils.PredefinedLocalScopeDefaultNetworks.
It uses the list of predefined default networks to always return an IPv4
address even if the named interface does not exist or does not have any
IPv4 addresses. This list happens to be the same as the one used to
initialize the address pool of the 'builtin' IPAM driver, though that is
far from obvious. (Start with "./libnetwork".initIPAMDrivers and trace
the dataflow of the addressPool value. Surprise! Global state is being
mutated using the value of other global mutable state.)

The daemon does not need the fallback behaviour of
ElectInterfaceAddresses. In fact, the daemon does not have to configure
an address pool for the network at all! libnetwork will acquire one of
the available address ranges from the network's IPAM driver when the
preferred-pool configuration is unset. It will do so using the same list
of address ranges and the exact same logic
(netutils.FindAvailableNetworks) as ElectInterfaceAddresses. So unless
the daemon needs to force the network to use a specific address range
because the bridge interface already exists, it can leave the details
up to libnetwork.

Signed-off-by: Cory Snider <[email protected]>
The function references global shared, mutable state and is no longer
needed. Deleting it brings us one step closer to getting rid of that
pesky shared state.

Signed-off-by: Cory Snider <[email protected]>
ipam.Allocator is not a singleton, but it references mutable singleton
state. Address that deficiency by refactoring it to instead take the
predefined address spaces as constructor arguments. Unfortunately some
work is needed on the Swarmkit side before the mutable singleton state
can be completely eliminated from the IPAMs.

Signed-off-by: Cory Snider <[email protected]>
ConfigLocalScopeDefaultNetworks is now dead code, thank goodness! Make
sure it stays dead by deleting the function. Refactor package ipamutils
to simplify things given its newly-reduced (ahem) scope.

Signed-off-by: Cory Snider <[email protected]>
@corhere corhere force-pushed the libnw/drop-electinterfaceaddresses branch from ed0ab79 to e8011d7 Compare January 26, 2023 19:56
@corhere corhere merged commit 6c9dd8b into moby:master Jan 26, 2023
@corhere corhere deleted the libnw/drop-electinterfaceaddresses branch January 26, 2023 21:42
@akerouanton
Copy link
Copy Markdown
Member

Fixes #44220.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/networking Networking kind/refactor PR's that refactor, or clean-up code status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants