Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libnet/ipam: Various clean-ups #47727

Merged
merged 17 commits into from
Apr 26, 2024
Merged

Conversation

akerouanton
Copy link
Member

@akerouanton akerouanton commented Apr 17, 2024

- What I did

Consider reviewing per commit.

  • Un-embed mutex from addrSpace
  • Put addrSpace into a separate file
  • Remove dead functions DumpDatabase and NetworkRange
  • Split v4/v6 addrSpace
  • Introduce two new structs, PoolRequest and AllocatedPool, to represent the in/out values of RequestPool
  • Rewrote a few tests to make them more idiomatic
  • Remove the global vars used to store the local and global address pools
  • Move package libnet/ipam to libnet/ipams/defaultipam -- grouping all IPAM drivers under libnet/ipams

- How to verify it

Through tests.

@akerouanton akerouanton added this to the 27.0.0 milestone Apr 17, 2024
@akerouanton akerouanton self-assigned this Apr 17, 2024
@akerouanton akerouanton force-pushed the libnet-ipam-cleanup branch 3 times, most recently from 4d38248 to 64049ea Compare April 17, 2024 15:31
Copy link
Contributor

@robmry robmry left a comment

Choose a reason for hiding this comment

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

Much neater, nice!

libnetwork/ipamapi/contract.go Show resolved Hide resolved
libnetwork/internal/netiputil/netiputil.go Outdated Show resolved Hide resolved
libnetwork/ipam/allocator.go Outdated Show resolved Hide resolved
@akerouanton akerouanton force-pushed the libnet-ipam-cleanup branch 2 times, most recently from 11cd948 to a1f35fe Compare April 18, 2024 11:06
@akerouanton
Copy link
Member Author

Ah, just realized one commit message was wrong. That's now fixed.

Copy link
Contributor

@corhere corhere left a comment

Choose a reason for hiding this comment

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

What an improvement! It's amazing how much we were held back by cnmallocator living in the Swarmkit repo.

The meat of the changes look good. All I really have are aesthetic quibbles; nothing that couldn't be ignored, or dealt with in a follow-up PR.

// Meta represents a list of extra IP addresses automatically reserved
// during the pool allocation. These are generally keyed by well-known
// strings defined in the netlabel package.
Meta map[string]string
Copy link
Contributor

Choose a reason for hiding this comment

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

If the values are always IP addresses, why are they stringly-typed? Or is the doc comment misleadingly describing how the default IPAM driver happens to populate a generic bag of data with implementation-defined semantics?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I'll update this comment -- it's misleading.

I'm also wondering how useful it is. No builtin driver set any Meta, only remote drivers might do that. And it's used only in two places:

if gws, ok := meta[netlabel.Gateway]; ok {
if ip, gwIP, err = net.ParseCIDR(gws); err != nil {
return nil, fmt.Errorf("failed to parse gateway address (%v) returned by ipam driver: %v", gws, err)
}
gwIP.IP = ip
}

moby/libnetwork/network.go

Lines 1619 to 1623 in 16b2c22

if gws, ok := d.Meta[netlabel.Gateway]; ok {
if d.Gateway, err = types.ParseCIDR(gws); err != nil {
return types.InvalidParameterErrorf("failed to parse gateway address (%v) returned by ipam driver: %v", gws, err)
}
}

So, I think it'd even be safe to replace it with an optional Gateway.

libnetwork/ipams/defaultipam/allocator.go Outdated Show resolved Hide resolved
libnetwork/ipams/defaultipam/allocator_test.go Outdated Show resolved Hide resolved
@@ -1508,21 +1509,27 @@ func (n *Network) ipamAllocate() error {
return err
}

func (n *Network) requestPoolHelper(ipam ipamapi.Ipam, addressSpace, requestedPool, requestedSubPool string, options map[string]string, v6 bool) (poolID string, pool *net.IPNet, meta map[string]string, err error) {
func (n *Network) requestPoolHelper(ipam ipamapi.Ipam, addressSpace, requestedPool, requestedSubPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why'd you remove the names of the return values? Those are good documentation!

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it made a lot of sense when they were assigned from the call to RequestPool. Now that everything lives in alloc, I feel like it's too easy to return the wrong thing.

Anyway, I think it doesn't matter much. As discussed on Tuesday, this method will go away in the next PR.

libnetwork/ipams/windowsipam/windowsipam_other.go Outdated Show resolved Hide resolved
libnetwork/cnmallocator/drivers_ipam.go Outdated Show resolved Hide resolved
libnetwork/controller.go Outdated Show resolved Hide resolved
libnetwork/drvregistry/ipams_test.go Outdated Show resolved Hide resolved
libnetwork/ipamutils/utils.go Outdated Show resolved Hide resolved
Embedding `sync.Mutex` into a struct is considered a bad practice
as it makes the mutex methods part of the embedding struct's API.

Signed-off-by: Albin Kerouanton <[email protected]>
`addrSpace` methods are currently scattered in two different files.
As upcoming work will rewrite some of these methods, better put them
into a separate file.

Signed-off-by: Albin Kerouanton <[email protected]>
Address spaces are a continuum of addresses that can be used for a
specific purpose (ie. 'local' for unmanaged containers, 'global for
Swarm). v4 and v6 addresses aren't of the same size -- hence
combining them into a single address space doesn't form a continuum.
Better set them apart into two different address spaces.

Also, the upcoming rewrite of `addrSpace` will benefit from that
split.

Signed-off-by: Albin Kerouanton <[email protected]>
The `RequestPool` method has many args and named returns. This
makes the code hard to follow at times. This commit adds one struct,
`PoolRequest`, to replace these args, and one struct, `AllocatedPool`,
to replace these named returns.

Both structs' fields are properly documented to better define their
semantics, and their relationship with address allocation.

Signed-off-by: Albin Kerouanton <[email protected]>
Prior to this change, daemon's `default-address-pools` param would
be passed to `SetDefaultIPAddressPool()` to set a global var named
`defaultAddressPool`. This var would then be retrieved during the
`default` IPAM driver registration. Both steps were executed in
close succession during libnet's controller initialization.

This change removes the global var and just pass the user-defined
`default-address-pools` to the `default` driver's `Register` fn.

Signed-off-by: Albin Kerouanton <[email protected]>
All drivers except the default ipam driver are stored in ipams/.
Since `default` isn't a valid Go pkg name, this package is
renamed to `defaultipam`, following `windowsipam` example.

Signed-off-by: Albin Kerouanton <[email protected]>
Packages in libnet/ipams are drivers, except builtin -- it's used
to register drivers. Move files one level up and delete this pkg.

Signed-off-by: Albin Kerouanton <[email protected]>
All drivers except the default have a Register function. Before this
change, default's registration was handled by another package. Move
this logic into the driver pkg.

Signed-off-by: Albin Kerouanton <[email protected]>
This function is made a no-op on non-windows platform.

Signed-off-by: Albin Kerouanton <[email protected]>
Signed-off-by: Albin Kerouanton <[email protected]>
Prior to this change, cnmallocator would call
`ConfigGlobalScopeDefaultNetworks` right before initializing its
IPAM drivers. This function was mutating some global state used
during drivers init.

This change just remove the global state, and adds an arg to
ipams.Register and defaultipam.Register to pass the global pools
by arguments instead.

Signed-off-by: Albin Kerouanton <[email protected]>
@akerouanton
Copy link
Member Author

akerouanton commented Apr 26, 2024

Okay, let's merge this one and rebase #47768.

@akerouanton akerouanton merged commit 48d769b into moby:master Apr 26, 2024
126 checks passed
@akerouanton akerouanton deleted the libnet-ipam-cleanup branch April 26, 2024 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants