Skip to content

daemon/cluster/executor: simplify handling of Network Attachments#49343

Merged
akerouanton merged 4 commits intomoby:masterfrom
thaJeztah:cluster_cleanup_networkattachment
Jan 27, 2025
Merged

daemon/cluster/executor: simplify handling of Network Attachments#49343
akerouanton merged 4 commits intomoby:masterfrom
thaJeztah:cluster_cleanup_networkattachment

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

daemon/cluster/executor: networkCreateRequest don't shadow config

c is used as name for the containerConfig receiver; remove the intermediate
variable so that we don't shadow it. There's no bug here, because a new
variable is created; just to prevent confusion and to make linters happier.

daemon/cluster/executor: networkCreateRequest: slight DRY cleanup

All of this function only referenced the Network field in the attachment;
use an intermediate variable to make the code less repetitive.

daemon/cluster/executor: networkCreateRequest: not a method

This method was called in a loop, iterating over the container config's
network-attachments. It was defined as a method, but only to lookup
the same attachment we just iterated over existed, and to obtain a copy.
As there were no goroutines involved, dereferencing should not be an issue
and with Go 1.22+, dereferencing loop vars is no longer needed at all,
so we can change this method to a regular constructor; also removing the
redundant error-return in the process.

daemon/cluster/executor: containerConfig: store Network instead of envelope

The Network field is the only field used from the NetworkAttachment within
this code. Remove the NetworkAttachment envelope, and store the Network
field directly instead.

- Description for the changelog

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

c is used as name for the containerConfig receiver; remove the intermediate
variable so that we don't shadow it. There's no bug here, because a new
variable is created; just to prevent confusion and to make linters happier.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
All of this function only referenced the Network field in the attachment;
use an intermediate variable to make the code less repetitive.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
This method was called in a loop, iterating over the container config's
network-attachments. It was defined as a method, but only to lookup
the same attachment we just iterated over existed, and to obtain a copy.
As there were no goroutines involved, dereferencing should not be an issue
and with Go 1.22+, dereferencing loop vars is no longer needed at all,
so we can change this method to a regular constructor; also removing the
redundant error-return in the process.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
…velope

The Network field is the only field used from the NetworkAttachment within
this code. Remove the NetworkAttachment envelope, and store the Network
field directly instead.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah added status/2-code-review area/networking Networking area/swarm kind/refactor PR's that refactor, or clean-up code labels Jan 27, 2025
@thaJeztah thaJeztah self-assigned this Jan 27, 2025
@akerouanton akerouanton merged commit a42b601 into moby:master Jan 27, 2025
@thaJeztah thaJeztah deleted the cluster_cleanup_networkattachment branch January 27, 2025 17:53
Comment on lines +69 to +76
// It looks like using a map is only for convenience, but not used
// for validation, nor for looking up the network by name. The name
// is part of the Network's properties (Network.Spec.Annotations.Name),
// and effectively only used for debugging; we should consider to
// change it to a slice.
//
// TODO(thaJeztah): should this check for empty and duplicate names?
c.networks[attachment.Network.Spec.Annotations.Name] = attachment.Network
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If one of you has thoughts in this, let me know as well; I left it as a TODO here, as it was mostly something I was wondering if it would (should've been) checked on here, but I left it at that (for now)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My thought was just that it looks like the map isn't needed and a check would be right. But, changing it seems a bit risky without knowing this code well (which I don't) ... because maybe something's working by luck or judgement with a duplicate or empty name, in a case that's not expected or not tested.

@thaJeztah thaJeztah added this to the 28.0.0 milestone Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/networking Networking area/swarm 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.

3 participants