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

manager/allocator/cnmallocator: remove uses of deprecated Init() funcs #3138

Closed
wants to merge 4 commits into from

Conversation

thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Jun 30, 2023

cnmallocator: RegisterManager does not require DriverCallback interface

This function was not using the DriverCallback interface, and only
required the Registerer interface.

manager/allocator/cnmallocator: use a map for initializers

Refactor the initializers slice to be a map (as driver-names should be
unique, and they were not dynamically set here either way).

Also inlining the code from initializeDrivers(), as it was only used
in a single location, and the code lived far from where it was used.

manager/allocator/cnmallocator: remove uses of deprecated Init() funcs

These functions were deprecated in moby/moby@28edc8e (moby/moby#44875)
in favor of the Register functions.

Unfortunately, not all Register functions have the same signature, so
some (temporary) adaptor code was needed for these.

manager/allocator/cnmallocator: StubManagerInit: config arg is unused

Make it clearer that the config argument is not used.

- What I did

- How I did it

- How to test it

- Description for the changelog

@thaJeztah thaJeztah requested a review from corhere June 30, 2023 11:23
@codecov-commenter
Copy link

Codecov Report

Merging #3138 (c9a2d13) into master (ad0f3ae) will increase coverage by 0.02%.
The diff coverage is 25.92%.

@@            Coverage Diff             @@
##           master    #3138      +/-   ##
==========================================
+ Coverage   61.58%   61.60%   +0.02%     
==========================================
  Files         154      155       +1     
  Lines       31106    31116      +10     
==========================================
+ Hits        19156    19169      +13     
+ Misses      10402    10397       -5     
- Partials     1548     1550       +2     

This function was not using the DriverCallback interface, and only
required the Registerer interface.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah marked this pull request as ready for review July 3, 2023 07:54
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.

drvregistry.New is also deprecated and its use should be removed at the same time as the uses of the deprecated Init functions which depend on it.


Given that the majority of the drivers being registered are managers which are only referenced by Swarmkit, and that Swarmkit always passes nil for the config argument, I propose we change the signatures of the managers' Register functions to

func Register(driverapi.Registerer) error

and deal with host.Register by either special-casing it in Swarmkit, using an adapter, or changing its signature in libnetwork to the above (it takes no configuration) and dealing with the change on the libnetwork side.

if err := initializeDrivers(reg); err != nil {
return nil, err
for _, init := range initializers {
if err := init(reg, nil); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

The driver config is always nil?! And the majority of initializers are for "manager" drivers which are exclusively used by swarmkit... I wish I had checked before refactoring libnetwork and used a different signature for those Register functions. It's not too late yet to fix my past mistake!

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

The driver config is always nil?!

Hm... good one, I have to double check if there's a driver I missed that still consumes a config, but at least most of them were not using it, hence me opening;

I wish I had checked before refactoring libnetwork and used a different signature for those Register functions. It's not too late yet to fix my past mistake!

Don't blame yourself! There's so much indirection, and so much "oh, let's add some options, just in case, someday, maybe we'll use them".

I started to chip away more bits in moby/moby#45870, and some of those changes may become obsolete if we carve out larger bits, but it was in an attempt to get clearer what's actually used.

manager/allocator/cnmallocator/networkallocator.go Outdated Show resolved Hide resolved
Comment on lines +12 to +18
func registerRemote(r driverapi.Registerer, _ map[string]interface{}) error {
dc, ok := r.(driverapi.DriverCallback)
if !ok {
return fmt.Errorf(`failed to register "remote" driver: driver does not implement driverapi.DriverCallback`)
}
return remote.Register(dc, dc.GetPluginGetter())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please please please just special-case the registration of the remote driver so that Swarmkit can stop depending on the deprecated drvregistry.DrvRegistry. And I'd really like to get rid of the DriverCallback interface in libnetwork as well (which I overlooked annotating as deprecated, whoops)

Copy link
Member Author

Choose a reason for hiding this comment

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

so that Swarmkit can stop depending on the deprecated drvregistry.DrvRegistry.

I need to check that last one; while working on #3139 (reviews welcome on that one as well 😅), I found that the plugingetter field is not exported, but looks to be used, so the only way to set it is through the (deprecated) drvregistry.New

I may need to have a deeper look "how" it's used, and if there's other ways to pass it around though.

Copy link
Contributor

Choose a reason for hiding this comment

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

plugingetter field? cnmallocator.New takes the PluginGetter as an argument, constructs a DrvRegistry using it, and passes it to each the initializers in the slice/map. Simply cut out the middleman by passing the PluginGetter directly into a call to remote.Register from cnmallocator.New. That's what I meant by special-casing the registration.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah! Gotcha (I think) I'll have another look tomorrow

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right that the DrvRegistry.pluginGetter field is used. It serves one purpose: implementing the GetPluginGetter() method, which is only used to indirectly pass the PluginGetter into remote.Init(). As I explained in the commit message of moby/moby@5595311, I very deliberately omitted any PluginGetter stuff from the replacement types.

Comment on lines +14 to 18
func StubManagerInit(networkType string) func(dc driverapi.DriverCallback, _ map[string]interface{}) error {
return func(dc driverapi.DriverCallback, _ map[string]interface{}) error {
return RegisterManager(dc, networkType)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this now dead code?

Copy link
Member Author

Choose a reason for hiding this comment

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

DOH! I think it is now yes; originally I had to adjust the DriverCallback, but that's no longer true, so yup.

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 see that PR was closed though, but I wasn't sure if it was still needed (to be revived); do you know?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see that PR was closed though, but I wasn't sure if it was still needed (to be revived); do you know?

It is not needed.

Refactor the initializers slice to be a map (as driver-names should be
unique, and they were not dynamically set here either way).

Also inlining the code from initializeDrivers(), as it was only used
in a single location, and the code lived far from where it was used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
These functions were deprecated in moby/moby@28edc8e
in favor of the Register functions.

Unfortunately, not all Register functions have the same signature, so
some (temporary) adaptor code was needed for these.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Make it clearer that the config argument is not used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah
Copy link
Member Author

superseded by #3143

@thaJeztah thaJeztah deleted the refactor_initializers branch July 7, 2023 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants