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

run, create, connect: add support for gw-priority #5664

Merged
merged 1 commit into from
Dec 6, 2024

Conversation

akerouanton
Copy link
Member

@akerouanton akerouanton commented Dec 2, 2024

Related to:

- What I did

This adds support for the newly introduced GwPriority API field. It can be set on both docker container run and docker network connect.

For the run command, it's made available only through the extended syntax.

- How I did it

- How to verify it

CI is green.

- Description for the changelog

- Added a new `gw-priority` option to `docker run`, `docker container create`, and `docker network connect`. This option will be used by the Engine to determine which network provides the default gateway for a container. On `docker run`, this option is only available through the extended `--network` syntax.

@akerouanton akerouanton added this to the 28.0.0 milestone Dec 2, 2024
@akerouanton akerouanton requested a review from robmry December 2, 2024 17:08
@akerouanton akerouanton self-assigned this Dec 2, 2024
@akerouanton akerouanton requested review from thaJeztah and a team as code owners December 2, 2024 17:08
@akerouanton akerouanton changed the title run, create: add support for gw-priority run, create, connect: add support for gw-priority Dec 2, 2024
@@ -23,6 +23,7 @@ type connectOptions struct {
aliases []string
linklocalips []string
driverOpts []string
gwPriority int
Copy link
Member

Choose a reason for hiding this comment

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

I'm probably a bit late realising, but should this have been a uint, or do we expect negative values to be ok? 🙈

Copy link
Member Author

Choose a reason for hiding this comment

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

It's an int on purpose as it allows to set the priority to a negative value when some existing endpoints have the default priority 0 set. @robmry wrote about that here: moby/moby#48936 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

But wasn't higher higher priority, so if I want a new network to become the default, I need to give it a higher priority?

Copy link
Member Author

Choose a reason for hiding this comment

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

Absolutely. But if one of the current network give you a default gateway, and you want to stick with it, you can connect new networks with a negative priority.

Copy link
Member

Choose a reason for hiding this comment

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

(mostly considering if it's zero, then I did not consider it a priority (and alphabetic order is used), and only if I would explicitly want a network to take priority, I'd probably set a non-zero value; any value lower (including zero) would not take priority in that case)

Copy link
Member Author

Choose a reason for hiding this comment

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

You might consider priority of existing endpoints only when you connect a new network. In that case, having the ability to set a negative priority gives you a way to not touch the existing endpoint(s), but still get the behavior you want.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I'm good with this for now; perhaps we should brainstorm next week to see if there's still things to be done here.

  • default network (say ddd) without gw_priority; this becomes the default gateway
  • connect to other network (say ccc) without gw_priority; this sorts before ddd, so now takes over default gateway
  • ☝️ this is current situation, and potentially problematic, as it's implicit / unexpected
  • 👉 so, say, I don't want that to happen, I now set gw_priority=-999 to make sure it doesn't capture the gateway
  • But now I connect to another network, aaa. I don't want that to take over the default gateway, so I set gw_priority=-999.
  • But -999 was already set for ccc, so those are now "same priority", except for name.
  • ⚠️ if I would to connect another network (say bbb), but do NOT set a gw_priority, it would takeover the default gateway from ddd (as those both have 0 priority)

So, yes, there's some more controls here, but it's also shifting the goal-posts, and we may now end up with something similar to z-index or oomscore-adjust; "let's pick a high enough value" or "low enough value" that probably no other network has picked. Or at least, these numbers will become rather complicated to use, without either

  • "pick a low or high enough value" hoping no other network was given that value
  • or knowing what values were given to other networks and pick that value "+1" or "-1"
  • or keep track of values used, and continue incrementing or decrementing

I think at least we should consider setting a limit (which could be same as oom-score-adjust, e.g. -999 - 999); not sure if we need the whole range of +/- MAXINT32.

But perhaps we need to look at some other logic as well; I think in most cases, I'd expect the gateway to not change unless I want it to (i.e., once it's set, connecting another network should not change it, unless I want that new one to take over); almost considering if some of the enumerating could be handled daemon-side ("put me first, shift others one position down"), but that would mostly be for connecting to an existing container (for "create container with multiple networks", I could see a more declarative aproach being good).

@codecov-commenter
Copy link

codecov-commenter commented Dec 2, 2024

Codecov Report

Attention: Patch coverage is 70.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 59.53%. Comparing base (5afa739) to head (d4db289).
Report is 11 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5664   +/-   ##
=======================================
  Coverage   59.53%   59.53%           
=======================================
  Files         346      346           
  Lines       29356    29365    +9     
=======================================
+ Hits        17477    17483    +6     
- Misses      10909    10911    +2     
- Partials      970      971    +1     

@@ -55,6 +56,7 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
flags.StringSliceVar(&options.aliases, "alias", []string{}, "Add network-scoped alias for the container")
flags.StringSliceVar(&options.linklocalips, "link-local-ip", []string{}, "Add a link-local address for the container")
flags.StringSliceVar(&options.driverOpts, "driver-opt", []string{}, "driver options for the network")
flags.IntVar(&options.gwPriority, "gw-priority", 0, "Used to determine which network provides the default gateway")
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be nice to say it's the highest priority number that wins (it's not obvious, "priority 1" sounds important!). But, it's tricky without writing an essay. How about ...

Suggested change
flags.IntVar(&options.gwPriority, "gw-priority", 0, "Used to determine which network provides the default gateway")
flags.IntVar(&options.gwPriority, "gw-priority", 0, "Highest gw-priority provides the default gateway")

| `mac-address` | `--mac-address` | Container MAC address (e.g., 92:d0:c6:0a:29:33) |
| `link-local-ip` | `--link-local-ip` | Container IPv4/IPv6 link-local addresses |
| `driver-opt` | `docker network connect --driver-opt` | Network driver options |
| `gw-priority` | | Used to determine which network should provide the default gateway. |
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be good to say it's the highest priority number that gets the default gw here too (and in the network_connect.md) ... but, with a bit more space to play with, these could also say the priority can be negative?

opts/network.go Outdated
case gwPriorityOpt:
netOpt.GwPriority, err = strconv.Atoi(val)
if err != nil {
return fmt.Errorf("invalid priority: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return fmt.Errorf("invalid priority: %w", err)
return fmt.Errorf("invalid gw-priority: %w", err)

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

@thaJeztah thaJeztah merged commit 66dfe6d into docker:master Dec 6, 2024
89 checks passed
@akerouanton akerouanton deleted the gw-priority branch December 9, 2024 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants